LDDestroier

Super Text Downloader v1.45.2 for ComputerCraft

Sep 23rd, 2015
4,922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.47 KB | None | 0 0
  1. --[[
  2. Super Text Downloader by EldidiStroyrr/LDDestroier
  3.  
  4. The purpose of this program is to have a single
  5. unified download script for ComputerCraft, as opposed
  6. to making multiple programs, each able to download from one site.
  7.  
  8. The main aspect to make this script more modular is having
  9. a table (websiteSyntaxes) to store the website that it downloads
  10. from, as well as what abbreviation it's called with and the
  11. syntax of the raw download URL.
  12. Later updates added special prefixes that act in different ways
  13. that could not work with the standard syntax.
  14.  
  15.  pastebin get 3PBKGR4k std
  16.  std ld std std
  17. --]]
  18.  
  19. if type(std) ~= "table" then std = {} end
  20.  
  21. std.channelURLs = { --special URLs for getting a list of files.
  22.     ["STD"] = "https://raw.githubusercontent.com/LDDestroier/STD-GUI/master/list.lua", --stock
  23.     ["Discover"] = "https://pastebin.com/raw/9bXfCz6M", --owned by dannysmc95
  24.     --["OnlineAPPS"] = "https://pastebin.com/raw/g2EnDYLp", --owned by Twijn, but discontinued
  25.     ["STD-Media"] = "https://pastebin.com/raw/3JZHXTGL" --non-program media files
  26. }
  27. local goodchan = false
  28. for k,v in pairs(std.channelURLs) do
  29.     if std.channel == k then
  30.         goodchan = true
  31.         break
  32.     end
  33. end
  34. if not goodchan then
  35.     std.channel = "STD"
  36. end
  37. std.prevChannel = std.channel
  38. std.std_version = 1.453 --Number, not string!
  39. std.stdList = "/."..std.channel:lower().."_list" --String, path of store listings
  40. std.websiteList = "/.std_websites" --String, path of website listings
  41. local doStore = true --If you do 'std ld', should you open up the store? Or just fuck off?
  42. std.serious = true
  43.  
  44. local logo = {[[
  45.   __________________________
  46.  /  ___________ ______ ____ \
  47. / /           | |    | |   \ \
  48. \ \______     | |    | |    | |
  49.  \______ \    | |    | |    | |
  50.         \ \   | |    | |    | |
  51.   ______/ /   | |    | |___/ /
  52.  /_______/    |_|    |______/
  53.     Super    Text    Downloader
  54.  
  55. ]],[[
  56.   LLL  LLLLL LLL
  57.  L   L   L   L  L
  58.  L       L   L   L
  59.   LLL    L   L   L
  60.      L   L   L   L
  61.  L   L   L   L  L
  62.   LLL    L   LLL
  63.  Super Text Downloader]]
  64.  
  65. }
  66.  
  67.  
  68. -- start Base64
  69. local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  70. local encode = function(data)
  71.     return ((data:gsub('.', function(x)
  72.         local r,b='',x:byte()
  73.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  74.         return r;
  75.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  76.         if (#x < 6) then return '' end
  77.         local c=0
  78.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  79.         return b:sub(c+1,c+1)
  80.     end)..({ '', '==', '=' })[#data%3+1])
  81. end
  82. local decode = function(data)
  83.     data = string.gsub(data, '[^'..b..'=]', '')
  84.     return (data:gsub('.', function(x)
  85.         if (x == '=') then return '' end
  86.         local r,f='',(b:find(x)-1)
  87.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  88.         return r;
  89.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  90.         if (#x ~= 8) then return '' end
  91.         local c=0
  92.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  93.         return string.char(c)
  94.     end))
  95. end
  96. -- finished Base64
  97.  
  98. local runFile = function(path)
  99.     if not fs.exists(path) then
  100.         return false, "No such file!"
  101.     end
  102.     local file = fs.open(path,"r")
  103.     local contents = file.readAll()
  104.     file.close()
  105.     local func = loadstring(contents)
  106.     setfenv(func, getfenv())
  107.     return func()
  108. end
  109.  
  110. local function runURL(url, ...)
  111.     local program = http.get(url)
  112.     if not program then return false end
  113.     program = program.readAll()
  114.     local func = loadstring(program)
  115.     setfenv(func, getfenv())
  116.     return func(...)
  117. end
  118.  
  119. local function seperateMethods(input)
  120.     local output={}
  121.     for key,value in pairs(input) do
  122.         table.insert(output, {key,value})
  123.     end
  124.     return output
  125. end
  126.  
  127. local function displayHelp(mode)
  128.     if mode == 1 then
  129.         print("std <abbr> <fileid> [output]")
  130.         print("Do 'std list' to see all codes")
  131.         print("Do 'std ld' for a GUI")
  132.         write("Channel '")
  133.         if term.isColor() then term.setTextColor(colors.yellow) end
  134.         write(std.channel)
  135.         term.setTextColor(colors.white)
  136.         print("' is selected.")
  137.     elseif mode == 2 then
  138.         if std.serious then
  139.             print("List of website codes:")
  140.         else
  141.             print("all ur codes:")
  142.         end
  143.         std.websiteSyntaxes["dd"] = {} --Filler
  144.         std.websiteSyntaxes["dd64"] = {}
  145.         std.websiteSyntaxes["PB"] = {}
  146.         for k,v in pairs(std.websiteSyntaxes) do
  147.             if term.getTextColor then prevColor = term.getTextColor() else prevColor = colors.white end
  148.             write(" '")
  149.             if term.isColor() then term.setTextColor(colors.orange) end
  150.             write(k)
  151.             term.setTextColor(prevColor)
  152.             write("' ")
  153.             if k == "dd" then
  154.                 print("direct download")
  155.             elseif k == "dd64" then
  156.                 print("direct download + Base64")
  157.             elseif k == "PB" then
  158.                 print("pastebin.com (safe)")
  159.             elseif string.find(v.url,"/") then
  160.                 start = string.find(v.url,"://")+3
  161.                 finish = string.find(v.url,"/",9)-1
  162.                 print(string.sub(v.url,start,finish))
  163.             end
  164.         end
  165.     elseif mode == 3 then
  166.         print(logo[pocket and 2 or 1])
  167.     end
  168. end
  169.  
  170. local function choice(input) --A useful function for input. Similar to the MS-DOS 6.0 command.
  171.     local event, key
  172.     repeat
  173.         event, key = os.pullEvent("key")
  174.         if type(key) == "number" then key = keys.getName(key) end
  175.         if key == nil then key = " " end
  176.     until string.find(string.lower(input), string.lower(key))
  177.     return string.lower(key)
  178. end
  179.  
  180. --This list of websites is used as a backup, should you not be able to connect to pastebin.
  181. std.websiteSyntaxes = {
  182.     pb = {
  183.         url = "https://pastebin.com/raw.php?i=FILECODE",
  184.         fullName = "Pastebin",
  185.         codeLength = 6,
  186.     },
  187.     hb = {
  188.         url = "https://hastebin.com/raw/FILECODE",
  189.         fullName = "Hastebin",
  190.         codeLength = 10,
  191.     },
  192.     pe = {
  193.         url = "http://pastie.org/pastes/FILECODE/download",
  194.         fullName = "Pastie",
  195.         codeLength = 0,
  196.     },
  197.     fn = {
  198.         url = "https://fnpaste.com/FILECODE/raw",
  199.         fullName = "fnPaste",
  200.         codeLength = 4,
  201.     },
  202.     gh = {
  203.         url = "https://raw.githubusercontent.com/FILECODE",
  204.         fullName = "Github",
  205.         codeLength = 0,
  206.     },
  207.     gg = {
  208.         url = "https://gist.githubusercontent.com/FILECODE/raw/",
  209.         fullName = "Github Gist",
  210.         codeLength = 0,
  211.     },
  212.     sn = {
  213.         url = "http://s.drk.sc/FILECODE",
  214.         fullName = "Snippt",
  215.         codeLength = 6,
  216.     },
  217.     cp = {
  218.         url = "http://codepad.org/FILECODE/raw.txt",
  219.         fullName = "Codepad",
  220.         codeLength = 8,
  221.     },
  222.     id = {
  223.         url = "https://ideone.com/plain/FILECODE",
  224.         fullName = "Ideone",
  225.         codeLength = 6,
  226.     },
  227.     db = {
  228.         url = "https://www.dropbox.com/s/FILECODE?raw=true",
  229.         fullName = "Dropbox",
  230.         codeLength = 0,
  231.     },
  232.     dd = {
  233.         url = "FILECODE",
  234.         fullName = "Direct Download",
  235.         codeLength = 0,
  236.     },
  237. }
  238.  
  239. local tArg = {...}
  240. if shell then
  241.     std_file = shell.getRunningProgram()
  242. else
  243.     std_file = ""
  244. end
  245.  
  246. local getTableSize = function(tbl)
  247.     local amnt = 0
  248.     for k,v in pairs(tbl) do
  249.         amnt = amnt + 1
  250.     end
  251.     return amnt
  252. end
  253.  
  254. std.getSTDList = function(prevChannel)
  255.     local weburl = "http://pastebin.com/raw/FSCzZRUk" --URL of URL list.
  256.     local storeurl = std.channelURLs[std.channel] --URL of store list.
  257.     local webcontents = http.get(weburl)
  258.     local storecontents = http.get(storeurl)
  259.     if not (webcontents and storecontents) then
  260.         if shell then
  261.             print("Couldn't update list!")
  262.         end
  263.         return false, "Couldn't update list!"
  264.     else
  265.         local uut = runFile(std.stdList)
  266.         if not uut then std.storeURLs = nil end
  267.         local webprog = webcontents.readAll()
  268.         local storeprog = storecontents.readAll()
  269.         local webfile = fs.open(std.websiteList,"w")
  270.         local storefile = fs.open(std.stdList,"w")
  271.        
  272.         webfile.writeLine(webprog)
  273.         webfile.close()
  274.         storefile.writeLine(storeprog)
  275.         storefile.close()
  276.         runFile(std.websiteList)
  277.        
  278.         local outcome = runFile(std.stdList)
  279.         if outcome == false then
  280.             std.channel = prevChannel
  281.             return std.getSTDList("STD")
  282.         end
  283.         return true, "Downloaded to "..std.stdList
  284.     end
  285. end
  286.  
  287. if tArg[1] == "update" or not fs.exists(std.stdList) then
  288.     local updateChan = tArg[2]
  289.     if (updateChan) and (not std.channelURLs[updateChan]) and tArg[1] == "update" then
  290.         printError("No such channel.")
  291.         for k,v in pairs(std.channelURLs) do
  292.             term.setTextColor(colors.white)
  293.             write(" ")
  294.             if k == std.channel then
  295.                 write("@")
  296.                 if term.isColor() then term.setTextColor(colors.yellow) end
  297.             else
  298.                 write("O")
  299.             end
  300.             print(" "..k)
  301.         end
  302.         term.setTextColor(colors.white)
  303.         return
  304.     end
  305.     if std.serious then
  306.         write("Updating list...")
  307.     else
  308.         write("just a sec, gettin your repo...")
  309.     end
  310.     if updateChan and std.channelURLs[updateChan] then
  311.         std.prevChannel = std.channel
  312.         std.channel = updateChan
  313.     end
  314.     local success = std.getSTDList(std.prevChannel)
  315.     if not success then
  316.         if std.serious then
  317.             return printError("FAIL!")
  318.         else
  319.             return printError("IT'S NO USE!")
  320.         end
  321.     else
  322.         if std.serious then
  323.             print("good!")
  324.         else
  325.             print("excellent!")
  326.         end
  327.         if tArg[1] == "update" then return true end
  328.     end
  329. end
  330.  
  331. if not shell then return end
  332.  
  333. local websiteCode = tArg[1]
  334. local fileCode = tArg[2]
  335. local retrieveName = tArg[3]
  336.  
  337. if (websiteCode == "list") and (not fileCode) then
  338.     displayHelp(2)
  339.     return false
  340. elseif (websiteCode == "you foolish fool") and (not fileCode) then
  341.     displayHelp(3)
  342.     return false
  343. elseif (websiteCode ~= "ld") and (not fileCode) then
  344.     displayHelp(1)
  345.     return false
  346. end
  347.  
  348. local getFile = function(filename,url)
  349.     if fs.isReadOnly(filename) then
  350.         return false, "access denied"
  351.     end
  352.     local prog
  353.     if type(url) == "table" then
  354.         prog = contextualGet(url[1])
  355.     else
  356.         prog = http.get(url)
  357.     end
  358.     if not prog then
  359.         return false, "could not connect"
  360.     end
  361.     prog = prog.readAll()
  362.     local fyle = fs.open(filename,"w")
  363.     fyle.write(prog)
  364.     fyle.close()
  365.     return true, fs.getSize(filename)
  366. end
  367.  
  368. runFile(std.stdList)
  369. runFile(std.websiteList)
  370. local pastebinUpload = function(sName,sText)
  371.     write( "Connecting to pastebin.com... " )
  372.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  373.     local response = http.post(
  374.         "http://pastebin.com/api/api_post.php",
  375.         "api_option=paste&"..
  376.         "api_dev_key="..key.."&"..
  377.         "api_paste_format=lua&"..
  378.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  379.         "api_paste_code="..textutils.urlEncode(sText)
  380.     )
  381.     if response then
  382.         print( "Success." )
  383.         local sResponse = response.readAll()
  384.         response.close()
  385.         return string.match( sResponse, "[^/]+$" )
  386.     end
  387.     return false
  388. end
  389.  
  390. local fileURL
  391. if websiteCode == "ld" then
  392.     if not fileCode then
  393.         if doStore then
  394.             runURL("http://pastebin.com/raw/P9dDhQ2m")
  395.             return
  396.         else
  397.             return print("GUI Store has been disabled.")
  398.         end
  399.     else
  400.         if not std.storeURLs then
  401.             if std.serious then
  402.                 write("Updating list...")
  403.             else
  404.                 write("just a sec, gettin your repo...")
  405.             end
  406.             std.getSTDList()
  407.         end
  408.         if not std.storeURLs[fileCode] then
  409.             if std.serious then
  410.                 return printError("Invalid store code '" .. fileCode .. "'")
  411.             else
  412.                 return printError("ld code "..fileCode.." not guuud!!!")
  413.             end
  414.         else
  415.             fileURL = tostring(std.storeURLs[fileCode].url)
  416.         end
  417.     end
  418. elseif websiteCode == "PB" then --Hope it's not confusing.
  419.     fileURL = "https://pastebin.com/"..fileCode:sub(1,8)
  420.     write("Conntecting to '"..fileURL.."' safely...")
  421.     local prog = http.get(fileURL)
  422.     if not prog then
  423.         return printError("FAIL!")
  424.     else
  425.         if term.isColor() then term.setTextColor(colors.green) end
  426.         print("GOOD!")
  427.         term.setTextColor(colors.white)
  428.         local rawget = prog.readAll()
  429.         local s = string.find(rawget,"<textarea id=\"paste_code\"")+103
  430.         local e = string.find(rawget,"</textarea>")-1
  431.         local contents = string.gsub(string.sub(rawget,s,e),"&quot;","\"")
  432.         contents = contents:gsub("&lt;","<")
  433.         contents = contents:gsub("&gt;",">")
  434.         if retrieveName and shell then
  435.             local dlname = fs.combine(shell.dir(),retrieveName)
  436.             if fs.exists(dlname) then
  437.                 if std.serious then
  438.                     print("'" .. dlname .. "' exists! Overwrite?")
  439.                     write("[Y,N]?")
  440.                 else
  441.                     print("yoo alreddy got a '"..dlname.."'!! redu eet?")
  442.                     write("[why,enn]??")
  443.                 end
  444.                 local key = choice("yn")
  445.                 print(string.upper(key))
  446.                 if key == "n" then
  447.                     if std.serious then
  448.                         print("Cancelled.")
  449.                     else
  450.                         print("whatevz")
  451.                     end
  452.                     sleep(0)
  453.                     return false
  454.                 end
  455.             end
  456.             local file = fs.open(dlname, "w")
  457.             file.writeLine(contents)
  458.             file.close()
  459.             if std.serious then
  460.                 print("Done! DL'd " .. fs.getSize(dlname) .. " bytes.")
  461.             else
  462.                 print("yay guud! eets ".. fs.getSize(dlname)*2 .." nibbles")
  463.             end
  464.         else
  465.             local func = loadstring(contents)
  466.             setfenv(func, getfenv())
  467.             func()
  468.         end
  469.         sleep(0)
  470.         return
  471.     end
  472. elseif websiteCode == "dd64" then
  473.     write("Conntecting to '"..fileCode.."'...")
  474.     local cont = http.get(fileCode)
  475.     local dlname = fs.combine(shell.dir(),retrieveName)
  476.     if cont then
  477.         if term.isColor() then term.setTextColor(colors.green) end
  478.         print("GOOD!")
  479.         term.setTextColor(colors.white)
  480.         cont = decode(cont.readAll())
  481.         local file = fs.open(dlname,"w")
  482.         file.write(cont)
  483.         file.close()
  484.         if std.serious then
  485.             print("Done! DL'd " .. fs.getSize(dlname) .. " bytes.")
  486.         else
  487.             print("yay guud! eets ".. fs.getSize(dlname)*2 .." nibbles")
  488.         end
  489.         return true
  490.     else
  491.         return printError("FAIL!")
  492.     end
  493. elseif websiteCode == "pbupload" then
  494.     fileCode = fs.combine("",fileCode)
  495.     if not fs.exists(fileCode) then
  496.         return printError("NO SUCH FILE!")
  497.     else
  498.         local file = fs.open(fileCode,"r")
  499.         local cont = file.readAll()
  500.         file.close()
  501.         local sCode = pastebinUpload(fileCode,cont)
  502.         if sCode then
  503.             write("Uploaded with code:")
  504.             if term.isColor() then term.setTextColor(colors.yellow) end
  505.             print(sCode)
  506.             term.setTextColor(colors.white)
  507.             print("Don't forget it!")
  508.         else
  509.             return printError("FAIL!")
  510.         end
  511.         return true
  512.     end
  513. elseif websiteCode == "pbupload64" then
  514.     fileCode = fs.combine("",fileCode)
  515.     if not fs.exists(fileCode) then
  516.         return printError("NO SUCH FILE!")
  517.     else
  518.         local file = fs.open(fileCode,"r")
  519.         local cont = encode(file.readAll())
  520.         file.close()
  521.         local sCode = pastebinUpload(fileCode,cont)
  522.         if sCode then
  523.             write("Uploaded with Base64 with code:")
  524.             if term.isColor() then term.setTextColor(colors.yellow) end
  525.             print(sCode)
  526.             term.setTextColor(colors.white)
  527.             print("Don't forget it!")
  528.         else
  529.             return printError("FAIL!")
  530.         end
  531.         return true
  532.     end
  533. else
  534.     if not std.websiteSyntaxes[websiteCode] then
  535.         if std.serious then
  536.             return printError("Invalid website code '" .. websiteCode .. "'")
  537.         else
  538.             return printError("dat '"..websiteCode.."' is NAWT GUUD!!")
  539.         end
  540.     else
  541.         if (std.websiteSyntaxes[websiteCode].codeLength == 0) or (not std.websiteSyntaxes[websiteCode].codeLength) then
  542.             fileURL = string.gsub(std.websiteSyntaxes[websiteCode].url, "FILECODE", fileCode)
  543.         else
  544.             fileURL = string.gsub(std.websiteSyntaxes[websiteCode].url, "FILECODE", string.sub(fileCode,1,std.websiteSyntaxes[websiteCode].codeLength))
  545.         end
  546.     end
  547.     sleep(0)
  548. end
  549.  
  550. if std.serious then
  551.     write("Connecting to '" .. fileURL .. "'...")
  552. else
  553.     if math.random(1,2) == 1 then
  554.         write("gettin ze '"..fileURL.."'...")
  555.     else
  556.         write("commeptin to '"..fileURL.."' naow...")
  557.     end
  558. end
  559. local contents = http.get(fileURL)
  560. if not contents then
  561.     if term.isColor() then
  562.         term.setTextColor(colors.red)
  563.     end
  564.     if std.serious then
  565.         print("NOPE!")
  566.     else
  567.         print("NI!")
  568.     end
  569.     sleep(0)
  570.     return false
  571. else
  572.     if term.getTextColor then
  573.         prevColor = term.getTextColor()
  574.     else
  575.         prevColor = colors.white
  576.     end
  577.     if term.isColor() then
  578.         term.setTextColor(colors.green)
  579.     end
  580.     if std.serious then
  581.         print("good!")
  582.     else
  583.         print("gud!")
  584.     end
  585.     term.setTextColor(prevColor)
  586.     if retrieveName and shell then
  587.         local dlname = fs.combine(shell.dir(),retrieveName)
  588.         if fs.exists(dlname) then
  589.             if std.serious then
  590.                 print("'" .. dlname .. "' exists! Overwrite?")
  591.                 write("[Y,N]?")
  592.             else
  593.                 print("yoo alreddy got a '"..dlname.."'!! redu eet?")
  594.                 write("[why,enn]??")
  595.             end
  596.             local key = choice("yn")
  597.             print(string.upper(key))
  598.             if key == "n" then
  599.                 if std.serious then
  600.                     print("Cancelled.")
  601.                 else
  602.                     print("whatevz")
  603.                 end
  604.                 sleep(0)
  605.                 return false
  606.             end
  607.         end
  608.         local file = fs.open(dlname, "w")
  609.         file.writeLine(contents.readAll())
  610.         file.close()
  611.         if std.serious then
  612.             print("Done! DL'd " .. fs.getSize(dlname) .. " bytes.")
  613.         else
  614.             print("yay guud! eets ".. fs.getSize(dlname)*2 .." nibbles")
  615.         end
  616.     else
  617.         local contents = loadstring(contents.readAll())
  618.         setfenv(contents, getfenv())
  619.         contents()
  620.     end
  621.     sleep(0)
  622.     return true
  623. end
Add Comment
Please, Sign In to add comment