View difference between Paste ID: hUVd8kgH and YV1P4ykm
SHOW: | | - or go back to the newest paste.
1
--[[ /gitget
2
GitHub downloading utility for CC.
3
Developed by apemanzilla.
4
 
5
This requires ElvishJerricco's JSON parsing API.
6
Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
7
]]--
8
9
-- Edit these variables to use preset mode.
10
-- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
11
-- If false will download the files one by one and use the old output (List each file name as it's downloaded) instead of the progress bar
12
local async = true
13
14
-- Whether to write to the terminal as files are downloaded
15
-- Note that unless checked for this will not affect pre-set start/done code below
16
local silent = false
17
18
local preset = {
19
	-- The GitHub account name
20-
	user = "apemanzilla",
20+
	user = "osmarks",
21
	-- The GitHub repository name
22
	repo = "swarm-quarry-miners",
23
	
24
	-- The branch or commit tree to download (defaults to 'master')
25
	branch = nil,
26
	
27
	-- The local folder to save all the files to (defaults to '/')
28
	path = ".sq",
29
	
30
	-- Function to run before starting the download
31
	start = function()
32
		print("Downloading required files...")
33
	end,
34
	
35
	-- Function to run when the download completes
36
	done = function()
37
		term.clear() term.setCursorPos(1,1)
38
		shell.run(".sq/setup")
39
	end
40
}
41
42
-- Leave the rest of the program alone.
43
local args = {...}
44
45
args[1] = preset.user or args[1]
46
args[2] = preset.repo or args[2]
47
args[3] = preset.branch or args[3] or "master"
48
args[4] = preset.path or args[4] or ""
49
 
50
if #args < 2 then
51
		print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
52
end
53
 
54
local function save(data,file)
55
	local file = shell.resolve(file:gsub("%%20"," "))
56
	if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
57
		if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
58
		fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
59
	end
60
	local f = fs.open(file,"w")
61
	f.write(data)
62
	f.close()
63
end
64
 
65
local function download(url, file)
66
	save(http.get(url).readAll(),file)
67
end
68
69
if not json then
70
	download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
71
	os.loadAPI("json")
72
end
73
 
74
preset.start()
75
local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
76
if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
77
if data.message and data.message == "Not found" then error("Invalid repository",2) else
78
	for k,v in pairs(data.tree) do
79
		-- Make directories
80
		if v.type == "tree" then
81
			fs.makeDir(fs.combine(args[4],v.path))
82
			if not hide_progress then
83
			end
84
		end
85
	end
86
	local drawProgress
87
	if async and not silent then
88
		local _, y = term.getCursorPos()
89
		local wide, _ = term.getSize()
90
		term.setCursorPos(1, y)
91
		term.write("[")
92
		term.setCursorPos(wide - 6, y)
93
		term.write("]")
94
		drawProgress = function(done, max)
95
			local value = done / max
96
			term.setCursorPos(2,y)
97
			term.write(("="):rep(math.floor(value * (wide - 8))))
98
			local percent = math.floor(value * 100) .. "%"
99
			term.setCursorPos(wide - percent:len(),y)
100
			term.write(percent)
101
		end
102
	end
103
	local filecount = 0
104
	local downloaded = 0
105
	local paths = {}
106
	local failed = {}
107
	for k,v in pairs(data.tree) do
108
		-- Send all HTTP requests (async)
109
		if v.type == "blob" then
110
			v.path = v.path:gsub("%s","%%20")
111
			local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
112
			if async then
113
				http.request(url)
114
				paths[url] = fs.combine(args[4],v.path)
115
				filecount = filecount + 1
116
			else
117
				download(url, fs.combine(args[4], v.path))
118
				if not silent then print(fs.combine(args[4], v.path)) end
119
			end
120
		end
121
	end
122
	while downloaded < filecount do
123
		local e, a, b = os.pullEvent()
124
		if e == "http_success" then
125
			save(b.readAll(),paths[a])
126
			downloaded = downloaded + 1
127
			if not silent then drawProgress(downloaded,filecount) end
128
		elseif e == "http_failure" then
129
			-- Retry in 3 seconds
130
			failed[os.startTimer(3)] = a
131
		elseif e == "timer" and failed[a] then
132
			http.request(failed[a])
133
		end
134
	end
135
end
136
preset.done()