Guest User

gh-dl 0.1

a guest
May 7th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.61 KB | None | 0 0
  1. args = {...}
  2. local ur = nil
  3. local ch = 0
  4. local ui = true
  5. local h = false
  6. local dd = ""
  7. local debug = false
  8. local logfile = "gh-dl.log"
  9. local sft = ""
  10. local af = false
  11.  
  12. function check(str)
  13.     local r = str
  14.     for a = string.len(str), 1, -1 do
  15.         if string.sub(str, a, a) ~= "/" and string.sub(str, a, a) ~="\\" then
  16.             break
  17.         else
  18.             r = string.sub(r, 1, -2)
  19.         end
  20.     end
  21.    
  22.     return r.."/"
  23. end
  24.  
  25. local function log(str)
  26.     if debug == false then return end
  27.     local mode = "a"
  28.     if not fs.exists(logfile) then
  29.         mode = "w"
  30.     end
  31.     local w = fs.open(logfile, mode)
  32.     w.writeLine(str)
  33.     w.close()
  34. end
  35.  
  36. local function niy()
  37.     print("This is not implemented yet.")
  38.     error()
  39. end
  40.  
  41. local a = 0
  42.  
  43. local function c()
  44.     if a > #args then
  45.         return true
  46.     end
  47.     return false
  48. end
  49.  
  50. local function printUsage()
  51.     print("Usage: gh-dl -ur <ur path>")
  52.     print("Possible args: -ur <ur path> -- required")
  53.     print("               -h -- help arg")
  54.     print("               -dft -- only display file tree")
  55.     print("               -sft <sft path>-- save the file tree")
  56.     print("               -dl <dl path> -- download path")
  57.     print("               -debug -- Debugs\n")
  58.     print("<ur path> = :user/:reponame for example: octocat/Hello-World")
  59.     print("<dl path> The download dir where the files will get saved in.")
  60.     print("<sft path> The file where the file tree will get saved in.")
  61.     error()
  62. end
  63.  
  64. while a <= #args do
  65.  
  66.     a = a + 1
  67.     if c() then
  68.         break
  69.     end
  70.     ui = false
  71.  
  72.     if args[a] == "-ur" then
  73.    
  74.         af = true
  75.         ur = args[a + 1]
  76.         a = a + 1
  77.        
  78.         if c() then break end
  79.        
  80.     elseif args[a] == "-ft" then
  81.    
  82.         af = true
  83.         ch = 2
  84.        
  85.     elseif args[a] == "-sft" then
  86.    
  87.         af = true
  88.         ch = 3
  89.         sft = args[a + 1]
  90.        
  91.         a = a + 1
  92.        
  93.         if c() then break end
  94.        
  95.     elseif args[a] == "-dl" then
  96.        
  97.         af = true
  98.         ch = 1
  99.         dd = args[a + 1]
  100.         dd = check(dd)
  101.         if not fs.exists(dd) then
  102.             fs.makeDir(dd)
  103.         elseif fs.isDir(dd) == false then
  104.             error("Download Dir is not a Directory")
  105.         end
  106.         a = a + 1
  107.        
  108.         if c() then break end
  109.        
  110.     elseif args[a] == "-debug" then
  111.        
  112.         debug = true
  113.        
  114.     elseif args[a] == "-h" then
  115.        
  116.         printUsage()
  117.        
  118.     end
  119.    
  120. end
  121.  
  122. if af == false then
  123.    
  124.     printUsage()
  125.    
  126. end
  127.  
  128. str = [[
  129. -----------------------------------------------------------------------------
  130. -- JSON4Lua: JSON encoding / decoding support for the Lua language.
  131. -- json Module.
  132. -- Author: Craig Mason-Jones
  133. -- Homepage: http://json.luaforge.net/
  134. -- Version: 0.9.40
  135. -- This module is released under the MIT License (MIT).
  136. -- Please see LICENCE.txt for details.
  137. --
  138. -- USAGE:
  139. -- This module exposes two functions:
  140. --   encode(o)
  141. --     Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string.
  142. --   decode(json_string)
  143. --     Returns a Lua object populated with the data encoded in the JSON string json_string.
  144. --
  145. -- REQUIREMENTS:
  146. --   compat-5.1 if using Lua 5.0
  147. --
  148. -- CHANGELOG
  149. --   0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
  150. --          Fixed Lua 5.1 compatibility issues.
  151. --          Introduced json.null to have null values in associative arrays.
  152. --          encode() performance improvement (more than 50%) through table.concat rather than ..
  153. --          Introduced decode ability to ignore /**/ comments in the JSON string.
  154. --   0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays.
  155. -----------------------------------------------------------------------------
  156.  
  157. -----------------------------------------------------------------------------
  158. -- Imports and dependencies
  159. -----------------------------------------------------------------------------
  160.  
  161. local base = _G
  162.  
  163. -----------------------------------------------------------------------------
  164. -- Module declaration
  165. -----------------------------------------------------------------------------
  166.  
  167. -- Public functions
  168.  
  169. -- Private functions
  170. local decode_scanArray
  171. local decode_scanComment
  172. local decode_scanConstant
  173. local decode_scanNumber
  174. local decode_scanObject
  175. local decode_scanString
  176. local decode_scanWhitespace
  177. local encodeString
  178. local isArray
  179. local isEncodable
  180.  
  181. -----------------------------------------------------------------------------
  182. -- PUBLIC FUNCTIONS
  183. -----------------------------------------------------------------------------
  184. --- Encodes an arbitrary Lua object / variable.
  185. -- @param v The Lua object / variable to be JSON encoded.
  186. -- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
  187. function encode (v)
  188.   -- Handle nil values
  189.   if v==nil then
  190.     return "null"
  191.   end
  192.  
  193.   local vtype = base.type(v)  
  194.  
  195.   -- Handle strings
  196.   if vtype=='string' then    
  197.     return '"' .. encodeString(v) .. '"'        -- Need to handle encoding in string
  198.   end
  199.  
  200.   -- Handle booleans
  201.   if vtype=='number' or vtype=='boolean' then
  202.     return base.tostring(v)
  203.   end
  204.  
  205.   -- Handle tables
  206.   if vtype=='table' then
  207.     local rval = {}
  208.     -- Consider arrays separately
  209.     local bArray, maxCount = isArray(v)
  210.     if bArray then
  211.       for i = 1,maxCount do
  212.         table.insert(rval, encode(v[i]))
  213.       end
  214.     else    -- An object, not an array
  215.       for i,j in base.pairs(v) do
  216.         if isEncodable(i) and isEncodable(j) then
  217.           table.insert(rval, '"' .. encodeString(i) .. '":' .. encode(j))
  218.         end
  219.       end
  220.     end
  221.     if bArray then
  222.       return '[' .. table.concat(rval,',') ..']'
  223.     else
  224.       return '{' .. table.concat(rval,',') .. '}'
  225.     end
  226.   end
  227.  
  228.   -- Handle null values
  229.   if vtype=='function' and v==null then
  230.     return 'null'
  231.   end
  232.  
  233.   base.assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. base.tostring(v))
  234. end
  235.  
  236.  
  237. --- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
  238. -- @param s The string to scan.
  239. -- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
  240. -- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
  241. -- and the position of the first character after
  242. -- the scanned JSON object.
  243. function decode(s, startPos)
  244.   startPos = startPos and startPos or 1
  245.   startPos = decode_scanWhitespace(s,startPos)
  246.   base.assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
  247.   local curChar = string.sub(s,startPos,startPos)
  248.   -- Object
  249.   if curChar=='{' then
  250.     return decode_scanObject(s,startPos)
  251.   end
  252.   -- Array
  253.   if curChar=='[' then
  254.     return decode_scanArray(s,startPos)
  255.   end
  256.   -- Number
  257.   if string.find("+-0123456789.e", curChar, 1, true) then
  258.     return decode_scanNumber(s,startPos)
  259.   end
  260.   -- String
  261.   if curChar=='"' or curChar=="'" then
  262.     return decode_scanString(s,startPos)
  263.   end
  264.   if string.sub(s,startPos,startPos+1)=='/*' then
  265.     return decode(s, decode_scanComment(s,startPos))
  266.   end
  267.   -- Otherwise, it must be a constant
  268.   return decode_scanConstant(s,startPos)
  269. end
  270.  
  271. --- The null function allows one to specify a null value in an associative array (which is otherwise
  272. -- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
  273. function null()
  274.   return null -- so json.null() will also return null ;-)
  275. end
  276. -----------------------------------------------------------------------------
  277. -- Internal, PRIVATE functions.
  278. -- Following a Python-like convention, I have prefixed all these 'PRIVATE'
  279. -- functions with an underscore.
  280. -----------------------------------------------------------------------------
  281.  
  282. --- Scans an array from JSON into a Lua object
  283. -- startPos begins at the start of the array.
  284. -- Returns the array and the next starting position
  285. -- @param s The string being scanned.
  286. -- @param startPos The starting position for the scan.
  287. -- @return table, int The scanned array as a table, and the position of the next character to scan.
  288. function decode_scanArray(s,startPos)
  289.   local array = {}  -- The return value
  290.   local stringLen = string.len(s)
  291.   base.assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
  292.   startPos = startPos + 1
  293.   -- Infinite loop for array elements
  294.   repeat
  295.     startPos = decode_scanWhitespace(s,startPos)
  296.     base.assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
  297.     local curChar = string.sub(s,startPos,startPos)
  298.     if (curChar==']') then
  299.       return array, startPos+1
  300.     end
  301.     if (curChar==',') then
  302.       startPos = decode_scanWhitespace(s,startPos+1)
  303.     end
  304.     base.assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
  305.     object, startPos = decode(s,startPos)
  306.     table.insert(array,object)
  307.   until false
  308. end
  309.  
  310. --- Scans a comment and discards the comment.
  311. -- Returns the position of the next character following the comment.
  312. -- @param string s The JSON string to scan.
  313. -- @param int startPos The starting position of the comment
  314. function decode_scanComment(s, startPos)
  315.   base.assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
  316.   local endPos = string.find(s,'*/',startPos+2)
  317.   base.assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
  318.   return endPos+2  
  319. end
  320.  
  321. --- Scans for given constants: true, false or null
  322. -- Returns the appropriate Lua type, and the position of the next character to read.
  323. -- @param s The string being scanned.
  324. -- @param startPos The position in the string at which to start scanning.
  325. -- @return object, int The object (true, false or nil) and the position at which the next character should be
  326. -- scanned.
  327. function decode_scanConstant(s, startPos)
  328.   local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
  329.   local constNames = {"true","false","null"}
  330.  
  331.   for i,k in base.pairs(constNames) do
  332.     --print ("[" .. string.sub(s,startPos, startPos + string.len(k) -1) .."]", k)
  333.     if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
  334.       return consts[k], startPos + string.len(k)
  335.     end
  336.   end
  337.   base.assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
  338. end
  339.  
  340. --- Scans a number from the JSON encoded string.
  341. -- (in fact, also is able to scan numeric +- eqns, which is not
  342. -- in the JSON spec.)
  343. -- Returns the number, and the position of the next character
  344. -- after the number.
  345. -- @param s The string being scanned.
  346. -- @param startPos The position at which to start scanning.
  347. -- @return number, int The extracted number and the position of the next character to scan.
  348. function decode_scanNumber(s,startPos)
  349.   local endPos = startPos+1
  350.   local stringLen = string.len(s)
  351.   local acceptableChars = "+-0123456789.e"
  352.   while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
  353.     and endPos<=stringLen
  354.     ) do
  355.     endPos = endPos + 1
  356.   end
  357.   local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
  358.   local stringEval = base.loadstring(stringValue)
  359.   base.assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
  360.   return stringEval(), endPos
  361. end
  362.  
  363. --- Scans a JSON object into a Lua object.
  364. -- startPos begins at the start of the object.
  365. -- Returns the object and the next starting position.
  366. -- @param s The string being scanned.
  367. -- @param startPos The starting position of the scan.
  368. -- @return table, int The scanned object as a table and the position of the next character to scan.
  369. function decode_scanObject(s,startPos)
  370.   local object = {}
  371.   local stringLen = string.len(s)
  372.   local key, value
  373.   base.assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
  374.   startPos = startPos + 1
  375.   repeat
  376.     startPos = decode_scanWhitespace(s,startPos)
  377.     base.assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
  378.     local curChar = string.sub(s,startPos,startPos)
  379.     if (curChar=='}') then
  380.       return object,startPos+1
  381.     end
  382.     if (curChar==',') then
  383.       startPos = decode_scanWhitespace(s,startPos+1)
  384.     end
  385.     base.assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
  386.     -- Scan the key
  387.     key, startPos = decode(s,startPos)
  388.     base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  389.     startPos = decode_scanWhitespace(s,startPos)
  390.     base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  391.     base.assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
  392.     startPos = decode_scanWhitespace(s,startPos+1)
  393.     base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  394.     value, startPos = decode(s,startPos)
  395.     object[key]=value
  396.   until false   -- infinite loop while key-value pairs are found
  397. end
  398.  
  399. --- Scans a JSON string from the opening inverted comma or single quote to the
  400. -- end of the string.
  401. -- Returns the string extracted as a Lua string,
  402. -- and the position of the next non-string character
  403. -- (after the closing inverted comma or single quote).
  404. -- @param s The string being scanned.
  405. -- @param startPos The starting position of the scan.
  406. -- @return string, int The extracted string as a Lua string, and the next character to parse.
  407. function decode_scanString(s,startPos)
  408.   base.assert(startPos, 'decode_scanString(..) called without start position')
  409.   local startChar = string.sub(s,startPos,startPos)
  410.   base.assert(startChar=="'" or startChar=='"','decode_scanString called for a non-string')
  411.   local escaped = false
  412.   local endPos = startPos + 1
  413.   local bEnded = false
  414.   local stringLen = string.len(s)
  415.   repeat
  416.     local curChar = string.sub(s,endPos,endPos)
  417.     -- Character escaping is only used to escape the string delimiters
  418.     if not escaped then
  419.       if curChar=="\\" then
  420.         escaped = true
  421.       else
  422.         bEnded = curChar==startChar
  423.       end
  424.     else
  425.       -- If we're escaped, we accept the current character come what may
  426.       escaped = false
  427.     end
  428.     endPos = endPos + 1
  429.     base.assert(endPos <= stringLen+1, "String decoding failed: unterminated string at position " .. endPos)
  430.   until bEnded
  431.   local stringValue = 'return ' .. string.sub(s, startPos, endPos-1)
  432.   local stringEval = base.loadstring(stringValue)
  433.   base.assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos)
  434.   return stringEval(), endPos  
  435. end
  436.  
  437. --- Scans a JSON string skipping all whitespace from the current start position.
  438. -- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
  439. -- @param s The string being scanned
  440. -- @param startPos The starting position where we should begin removing whitespace.
  441. -- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
  442. -- was reached.
  443. function decode_scanWhitespace(s,startPos)
  444.   local whitespace=" \n\r\t"
  445.   local stringLen = string.len(s)
  446.   while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true)  and startPos <= stringLen) do
  447.     startPos = startPos + 1
  448.   end
  449.   return startPos
  450. end
  451.  
  452. --- Encodes a string to be JSON-compatible.
  453. -- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
  454. -- @param s The string to return as a JSON encoded (i.e. backquoted string)
  455. -- @return The string appropriately escaped.
  456. function encodeString(s)
  457.   s = string.gsub(s,'\\','\\\\')
  458.   s = string.gsub(s,'"','\\"')
  459.   s = string.gsub(s,"'","\\'")
  460.   s = string.gsub(s,'\n','\\n')
  461.   s = string.gsub(s,'\t','\\t')
  462.   return s
  463. end
  464.  
  465. -- Determines whether the given Lua type is an array or a table / dictionary.
  466. -- We consider any table an array if it has indexes 1..n for its n items, and no
  467. -- other data in the table.
  468. -- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
  469. -- @param t The table to evaluate as an array
  470. -- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
  471. -- the second returned value is the maximum
  472. -- number of indexed elements in the array.
  473. function isArray(t)
  474.   -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
  475.   -- (with the possible exception of 'n')
  476.   local maxIndex = 0
  477.   for k,v in base.pairs(t) do
  478.     if (base.type(k)=='number' and math.floor(k)==k and 1<=k) then  -- k,v is an indexed pair
  479.       if (not isEncodable(v)) then return false end -- All array elements must be encodable
  480.       maxIndex = math.max(maxIndex,k)
  481.     else
  482.       if (k=='n') then
  483.         if v ~= table.getn(t) then return false end  -- False if n does not hold the number of elements
  484.       else -- Else of (k=='n')
  485.         if isEncodable(v) then return false end
  486.       end  -- End of (k~='n')
  487.     end -- End of k,v not an indexed pair
  488.   end  -- End of loop across all pairs
  489.   return true, maxIndex
  490. end
  491.  
  492. --- Determines whether the given Lua object / table / variable can be JSON encoded. The only
  493. -- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
  494. -- In this implementation, all other types are ignored.
  495. -- @param o The object to examine.
  496. -- @return boolean True if the object should be JSON encoded, false if it should be ignored.
  497. function isEncodable(o)
  498.   local t = base.type(o)
  499.   return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
  500. end
  501. ]]
  502.  
  503. local w = fs.open("json", "w")
  504. w.write(str)
  505. w.close()
  506.  
  507. if not json then
  508.     os.loadAPI("json")
  509. end
  510.  
  511. fs.delete("json")
  512.  
  513. function dec(data)
  514.     data = string.gsub(data, '[^'..b..'=]', '')
  515.     return (data:gsub('.', function(x)
  516.         if (x == '=') then return '' end
  517.         local r,f='',(b:find(x)-1)
  518.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  519.         return r;
  520.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  521.         if (#x ~= 8) then return '' end
  522.         local c=0
  523.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  524.         return string.char(c)
  525.     end))
  526. end
  527.  
  528. function getWebsite(url, pd)
  529.    
  530.     if string.find(url, "?") then
  531.         local p = string.find(url, "?")
  532.         url = string.sub(url, 1, p - 1)
  533.     end
  534.    
  535.     http.request(url)
  536.     while true do
  537.         local ev = {os.pullEvent()}
  538.         if ev[1] == "http_success" or ev[1] == "http_failure" then
  539.             if ev[1] == "http_failure" then
  540.                 error("An http failure error ocurred.")
  541.             end
  542.             if ev[2] == url then
  543.                 local c = ev[3].readAll()
  544.                 ev[3].close()
  545.                 return c
  546.             end
  547.         end
  548.     end
  549. end
  550.  
  551. local function parse(src)
  552.    
  553.     log("Parsing \""..src.."\"...")
  554.    
  555.     local website, err = getWebsite(src)
  556.  
  557.     if not website then
  558.         error("An error ocurred: "..err)
  559.     end
  560.  
  561.     local lt = json.decode(website)
  562.  
  563.     if not lt then
  564.         error("An error ocurred.")
  565.     end
  566.    
  567.     for a, b in pairs(lt) do
  568.        
  569.         if type(b) == "table" and b["message"] ~= nil then
  570.            
  571.             error("An API error ocurred: "..b["message"])
  572.            
  573.         end
  574.    
  575.         if type(b) == "table" and b["type"] == "file" then --Path is a file
  576.            
  577.             local w2 = getWebsite(assert(b["url"], "Error 1"))
  578.             local lt2 = json.decode(w2)
  579.            
  580.             local enContent = assert(lt2["content"], "Error 2")
  581.             local b64Contents = {}
  582.             local content = ""
  583.            
  584.             for match in string.gmatch(enContent, "^\n") do
  585.                 b64Contents[#b64Contents] = match
  586.             end
  587.            
  588.             if not string.find(enContent, "\n") then
  589.                 b64Contents[1] = enContent
  590.             end
  591.            
  592.             for a, b in pairs(b64Contents) do
  593.                 content = content..dec(b)
  594.             end
  595.            
  596.             local name = assert(b["name"], "Error 3")
  597.             local path = assert(b["path"], "Error 4")
  598.             path = string.sub(path, 1, 0 - #name - 1)
  599.             path = dd..path
  600.            
  601.             if not fs.exists(path) then
  602.                 fs.makeDir(path)
  603.             end
  604.            
  605.             local w = fs.open(path..name, "w")
  606.             w.write(content)
  607.             w.close()
  608.        
  609.         elseif type(b) == "table" and b["type"] == "dir" then
  610.            
  611.             local c = b["url"]
  612.            
  613.             parse(c)
  614.            
  615.         end
  616.        
  617.     end
  618.    
  619. end
  620.  
  621. local s = {}
  622.  
  623. function parseTree(src, snum)
  624.  
  625.     log("Parsing \""..src.."\"...")
  626.    
  627.     local website, err = getWebsite(src)
  628.  
  629.     if not website then
  630.         error("An error ocurred: "..err)
  631.     end
  632.  
  633.     local lt = json.decode(website)
  634.  
  635.     if not lt then
  636.         error("An error ocurred.")
  637.     end
  638.    
  639.     for a, b in pairs(lt) do
  640.        
  641.         if type(b) == "table" and b["message"] ~= nil then
  642.            
  643.             error("An API error ocurred: "..b["message"])
  644.            
  645.         end
  646.    
  647.         if type(b) == "table" and b["type"] == "file" then --Path is a file
  648.            
  649.             local f = loadstring("s"..snum..' = "'..b["name"]..'"')
  650.             setfenv(f, {s = s, b = b})
  651.            
  652.             f()
  653.            
  654.        
  655.         elseif type(b) == "table" and b["type"] == "dir" then
  656.            
  657.             local f = loadstring("s"..snum..' = { }')
  658.             setfenv(f, {s = s, b = b})
  659.            
  660.             f()
  661.            
  662.             parseTree(src..b["name"].."/", snum.."["..b["name"].."]")
  663.            
  664.         end
  665.        
  666.     end
  667.    
  668. end
  669.  
  670. local function saveTree()
  671.     local w = fs.open(sft, "w")
  672.    
  673.     local function sa(t, ind)
  674.         for a, b in pairs(t) do
  675.             if type(b) == "string" then
  676.                 w.writeLine(string.rep("-", ind).."> "..b)
  677.             elseif type(b) == "table" then
  678.                 w.writeLine(string.rep("-", ind)..": "..a)
  679.                 s(b, ind + 2)
  680.             end
  681.         end
  682.     end
  683.    
  684.     sa(s, 0)
  685.     w.close()
  686.    
  687. end
  688.        
  689. --[[function displayTree()
  690.    
  691.     local s2 = s
  692.     local p = {}
  693.    
  694.     local function repr()
  695.        
  696.         local function d(t, ind, posy, pn)
  697.            
  698.             for a, b in pairs(t) do
  699.                 if type(b) == "string" then
  700.                
  701.                     term.setCursorPos(1, posy)
  702.                    
  703.                     term.write((t.state == 1 and "+ " or t.state == 2 and "- ")..string.rep("-", ind)..a)
  704.                    
  705.                 elseif type(b) == "table" then
  706.                
  707.                     term.setCursorPos(1, posy)
  708.                     if t.state == nil then
  709.                         t.state = 1
  710.                         p[pn] =
  711.                     end
  712.                    
  713.                     term.write(string.rep("-", ind).." "(t.state == 1 and "+" or t.state == 2 and "-")..a)
  714.                    
  715.                     if t.state == 1 then
  716.                         s(b, ind + 2, posy + 1)
  717.                     end
  718.                    
  719.                 end
  720.             end
  721.         end
  722.        
  723.         term.setCursorPos(5,  1)
  724.         term.setBackgroundColor(red)
  725.         term.clearLine()
  726.         term.setTextColor(colors.white)
  727.         term.write("Exit")
  728.        
  729.         term.setBackgroundColor(colors.lightGray)
  730.         term.setTextColor(colors.orange)
  731.        
  732.         local w, h = term.getSize()
  733.        
  734.         for a = 2, h do
  735.            
  736.             term.setCursorPos(1, a)
  737.             term.clearLine()
  738.            
  739.         end
  740.        
  741.         term.setCursorPos(1, 2)
  742.        
  743.        
  744.         d(s2, 0, 2)
  745.        
  746.     end
  747.    
  748.     while true do
  749.    
  750.    
  751.    
  752.     end
  753. end]]
  754.  
  755. if ch == 1 then
  756.        
  757.     parse("https://api.github.com/repos/"..ur.."/contents/")
  758.    
  759. elseif ch == 2 or ch == 3 then
  760.  
  761.     parseTree("https://api.github.com/repos/"..ur.."/contents/")
  762.    
  763.     if ch == 2 then
  764.         saveTree()
  765.     elseif ch == 3 then
  766.         --displayTree()
  767.     end
  768.    
  769. end
Advertisement
Add Comment
Please, Sign In to add comment