SHOW:
|
|
- or go back to the newest paste.
1 | local tArgs, gUser, gRepo, gPath, gBranch = {...}, nil, nil, "", "master" | |
2 | local usage = [[ | |
3 | github <user> <repo> [path] [remote path] [branch] | |
4 | Remote path defaults to the root of the repo. | |
5 | Path defaults to the download folder. | |
6 | Branch defaults to master. | |
7 | If you want to leave an option empty use a dot. | |
8 | Example: github johnsmith hello-world . foo | |
9 | Everything inside the directory foo will be | |
10 | downloaded to downloads/hello-world/. | |
11 | ]] | |
12 | local blackList = [[ | |
13 | @blacklistedfile | |
14 | ]] | |
15 | ||
16 | local title = "Github Repo Downloader" | |
17 | local fileList = {dirs={},files={}} | |
18 | local x , y = term.getSize() | |
19 | ||
20 | -- GUI | |
21 | function printTitle() | |
22 | local line = 2 | |
23 | term.setCursorPos(1,line) | |
24 | for i = 2, x, 1 do write("-") end | |
25 | term.setCursorPos((x-title:len())/2,line+1) | |
26 | print(title) | |
27 | for i = 2, x, 1 do write("-") end | |
28 | end | |
29 | ||
30 | function writeCenter( str ) | |
31 | term.clear() | |
32 | printTitle() | |
33 | term.setCursorPos((x-str:len())/2-1,y/2-1) | |
34 | for i = -1, str:len(), 1 do write("-") end | |
35 | term.setCursorPos((x-str:len())/2-1,y/2) | |
36 | print("|"..str.."|") | |
37 | term.setCursorPos((x-str:len())/2-1,y/2+1) | |
38 | for i = -1, str:len(), 1 do write("-") end | |
39 | end | |
40 | ||
41 | function printUsage() | |
42 | local str = "Press space key to continue" | |
43 | term.clear() | |
44 | printTitle() | |
45 | term.setCursorPos(1,y/2-4) | |
46 | print(usage) | |
47 | term.setCursorPos((x-str:len())/2,y/2+7) | |
48 | print(str) | |
49 | while true do | |
50 | local event, param1 = os.pullEvent("key") | |
51 | if param1 == 57 then | |
52 | sleep(0) | |
53 | break | |
54 | end | |
55 | end | |
56 | term.clear() | |
57 | term.setCursorPos(1,1) | |
58 | end | |
59 | ||
60 | -- Download File | |
61 | function downloadFile( path, url, name ) | |
62 | writeCenter("Downloading File: "..name) | |
63 | dirPath = path:gmatch('([%w%_%.% %-%+%,%;%:%*%#%=%/]+)/'..name..'$')() | |
64 | if dirPath ~= nil and not fs.isDir(dirPath) then fs.makeDir(dirPath) end | |
65 | local content = http.get(url) | |
66 | local file = fs.open(path,"w") | |
67 | file.write(content.readAll()) | |
68 | file.close() | |
69 | end | |
70 | ||
71 | -- Get Directory Contents | |
72 | function getGithubContents( path ) | |
73 | local pType, pPath, pName, checkPath = {}, {}, {}, {} | |
74 | local response = http.get("https://api.github.com/repos/"..gUser.."/"..gRepo.."/contents/"..path.."/?ref="..gBranch) | |
75 | if response then | |
76 | response = response.readAll() | |
77 | if response ~= nil then | |
78 | for str in response:gmatch('"type":"(%w+)"') do table.insert(pType, str) end | |
79 | for str in response:gmatch('"path":"([^\"]+)"') do table.insert(pPath, str) end | |
80 | for str in response:gmatch('"name":"([^\"]+)"') do table.insert(pName, str) end | |
81 | end | |
82 | else | |
83 | writeCenter( "Error: Can't resolve URL" ) | |
84 | sleep(2) | |
85 | term.clear() | |
86 | term.setCursorPos(1,1) | |
87 | error() | |
88 | end | |
89 | return pType, pPath, pName | |
90 | end | |
91 | ||
92 | -- Blacklist Function | |
93 | function isBlackListed( path ) | |
94 | if blackList:gmatch("@"..path)() ~= nil then | |
95 | return true | |
96 | end | |
97 | end | |
98 | ||
99 | -- Download Manager | |
100 | function downloadManager( path ) | |
101 | local fType, fPath, fName = getGithubContents( path ) | |
102 | for i,data in pairs(fType) do | |
103 | if data == "file" then | |
104 | checkPath = http.get("https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i]) | |
105 | if checkPath == nil then | |
106 | ||
107 | fPath[i] = fPath[i].."/"..fName[i] | |
108 | end | |
109 | local path = "downloads/"..gRepo.."/"..fPath[i] | |
110 | if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end | |
111 | if not fileList.files[path] and not isBlackListed(fPath[i]) then | |
112 | fileList.files[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]} | |
113 | end | |
114 | end | |
115 | end | |
116 | for i, data in pairs(fType) do | |
117 | if data == "dir" then | |
118 | local path = "downloads/"..gRepo.."/"..fPath[i] | |
119 | if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end | |
120 | if not fileList.dirs[path] then | |
121 | writeCenter("Listing directory: "..fName[i]) | |
122 | fileList.dirs[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]} | |
123 | downloadManager( fPath[i] ) | |
124 | end | |
125 | end | |
126 | end | |
127 | end | |
128 | ||
129 | -- Main Function | |
130 | function main( path ) | |
131 | writeCenter("Connecting to Github") | |
132 | downloadManager(path) | |
133 | for i, data in pairs(fileList.files) do | |
134 | downloadFile( i, data[1], data[2] ) | |
135 | end | |
136 | writeCenter("Download completed") | |
137 | sleep(2,5) | |
138 | term.clear() | |
139 | term.setCursorPos(1,1) | |
140 | end | |
141 | ||
142 | -- Parse User Input | |
143 | function parseInput( user, repo , dldir, path, branch ) | |
144 | if path == nil then path = "" end | |
145 | if branch ~= nil then gBranch = branch end | |
146 | if repo == nil then printUsage() | |
147 | else | |
148 | gUser = user | |
149 | gRepo = repo | |
150 | if dldir ~= nil then gPath = dldir end | |
151 | main( path ) | |
152 | end | |
153 | end | |
154 | ||
155 | if not http then | |
156 | writeCenter("You need to enable the HTTP API!") | |
157 | sleep(3) | |
158 | term.clear() | |
159 | term.setCursorPos(1,1) | |
160 | else | |
161 | for i=1, 5, 1 do | |
162 | if tArgs[i] == "." then tArgs[i] = nil end | |
163 | end | |
164 | parseInput( tArgs[1], tArgs[2], tArgs[3], tArgs[4], tArgs[5] ) | |
165 | end |