This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Download Folder from GitHub
Started by Wilma456, 04 September 2017 - 11:48 AMPosted 04 September 2017 - 01:48 PM
What's the best way, to download a full folder from GitHub without writing all raw URLs in the code?
Edited on 04 September 2017 - 11:49 AM
Posted 04 September 2017 - 02:05 PM
You can get the full tree of a repository by using the trees API endpoint. This returns a JSON object with a property "tree", which can be iterated over. You can then fetch from https://raw.githubusercontent.com/ to download the actual contents.
Here's an example to download all files in the SquidDev-CC/Howl repository. I'm presuming you have a JSON API loaded already:
Here's an example to download all files in the SquidDev-CC/Howl repository. I'm presuming you have a JSON API loaded already:
-- Fetch the file listing
local request = http.get("https://api.github.com/repos/SquidDev-CC/Howl/git/trees/master?recursive=1")
local contents = json.parse(request.readAll())
request.close()
for _, file in ipairs(contents.tree) do
-- Fetch the actual contents. You can save these files somewhere, put them in an array, etc...
local request = http.get("https://raw.githubusercontent.com/SquidDev-CC/Howl/master/" .. file.path)
local handle = fs.open(file.path, "w")
handle.write(request.readAll()
handle.close()
request.close()
end
Posted 04 September 2017 - 02:19 PM
What json API do you use in the example?
Posted 04 September 2017 - 02:29 PM
In that case, a purely hypothetical one. You'll probably want ElvishJerricco's API, which uses json.decode instead.What json API do you use in the example?
Posted 04 September 2017 - 04:40 PM
This API has not the function "parse"
Posted 04 September 2017 - 04:56 PM
which uses json.decode instead.
Posted 05 September 2017 - 06:23 PM
I had tried this code with the API that you linked:
os.loadAPI("/json")
local request = http.get("https://api.github.com/repos/SquidDev-CC/Howl/git/trees/master?recursive=1")
local contents = json.decode(request.readAll())
request.close()
for _, file in ipairs(contents.tree) do
local request = http.get("https://raw.githubusercontent.com/SquidDev-CC/Howl/master/" .. file.path)
local handle = fs.open(file.path, "w")
handle.write(request.readAll())
handle.close()
request.close()
end
But I got the error 9: attempt to index ? (a nil value)Posted 05 September 2017 - 06:33 PM
So the recursive file listing also includes directories, so you'll need to check that file.type == "blob" before downloading each file.But I got the error 9: attempt to index ? (a nil value)
Edited on 05 September 2017 - 04:33 PM
Posted 05 September 2017 - 06:46 PM
Same error, now with line 10
os.loadAPI("/json")
local request = http.get("https://api.github.com/repos/SquidDev-CC/Howl/git/trees/master?recursive=1")
local contents = json.decode(request.readAll())
request.close()
for _, file in ipairs(contents.tree) do
if file.type == "blob" then
local request = http.get("https://raw.githubusercontent.com/SquidDev-CC/Howl/master/" .. file.path)
local handle = fs.open(file.path, "w")
handle.write(request.readAll())
handle.close()
request.close()
end
end
Posted 05 September 2017 - 07:03 PM
I'd presume a HTTP request is failing. Maybe try adding some print statements to determine which file is failing, print the error message from the HTTP response, etc… C'mon, I've seen your programs - I know you're capable of basic debugging skills.Same error, now with line 10
Posted 05 September 2017 - 10:38 PM
I know you're capable of basic debugging skills.
[OffTopic] Do we have a tutorial on debugging on the forums yet?
Posted 06 September 2017 - 05:10 PM
Posted 06 September 2017 - 09:13 PM
You could modify gitget.
Posted 06 September 2017 - 10:22 PM
Modify line 108 to add an additional check whether the file starts with a given path.
Posted 07 September 2017 - 02:03 PM
With modifyed gitget, I would be able to download the rom folder from the offical Repo. But what's with older Versions. In the Repo of alekso56 they are sorted with Tags, but gitget only suports branches and not tags.
Posted 07 September 2017 - 02:19 PM
Both should work fine. You can use branches, tags or specific commit hashes with git, and they'll all "just work".With modifyed gitget, I would be able to download the rom folder from the offical Repo. But what's with older Versions. In the Repo of alekso56 they are sorted with Tags, but gitget only suports branches and not tags.
Posted 07 September 2017 - 02:42 PM
I had try "gitget alekso56 ComputercraftLua branch=1.79" but I get a "gitget:74 attempt to index ? (a nil value) "error
Posted 08 September 2017 - 12:53 AM
The usage shown in the gitget script doesn't seem to match the usage shown in the gitget thread: try "1.79" instead of "branch=1.79". Failing that, rig the code to print the URL it's attempting to connect to so you can report it back here.