Advertisement
LDDestroier

Super Text Downloader BETA

Apr 16th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. This is a beta. You fool!
  19.  Working on:
  20.     +multi-file download handling
  21.     +fixing bugs (namely, use of Discover and OnlineAPPS)
  22. --]]
  23.  
  24. if type(std) ~= "table" then std = {} end
  25.  
  26. std.channelURLs = { --special URLs for getting a list of files.
  27.     ["STD"] = "http://pastebin.com/raw/zVws7eLq", --stock
  28.     ["Discover"] = "http://pastebin.com/raw/9bXfCz6M", --owned by dannysmc95
  29.     ["OnlineAPPS"] = "http://pastebin.com/raw/g2EnDYLp", --owned by Twijn
  30. }
  31. local goodchan = false
  32. for k,v in pairs(std.channelURLs) do
  33.     if std.channel == k then
  34.         goodchan = true
  35.         break
  36.     end
  37. end
  38. if not goodchan then
  39.     std.channel = "STD"
  40. end
  41. std.prevChannel = std.channel
  42. std.std_version = 1.451 --Number, not string!
  43. std.stdList = "/."..std.channel:lower().."_list" --String, path of store listings
  44. std.websiteList = "/.std_websites" --String, path of website listings
  45. local doStore = true --If you do 'std ld', should you open up the store? Or just fuck off?
  46. std.serious = true
  47.  
  48. local logo = {[[
  49.   __________________________
  50.  /  ___________ ______ ____ \
  51. / /           | |    | |   \ \
  52. \ \______     | |    | |    | |
  53.  \______ \    | |    | |    | |
  54.         \ \   | |    | |    | |
  55.   ______/ /   | |    | |___/ /
  56.  /_______/    |_|    |______/
  57.     Super    Text    Downloader
  58.  
  59. ]],[[
  60.   LLL  LLLLL LLL
  61.  L   L   L   L  L
  62.  L       L   L   L
  63.   LLL    L   L   L
  64.      L   L   L   L
  65.  L   L   L   L  L
  66.   LLL    L   LLL
  67.  Super Text Downloader]]
  68.  
  69. }
  70.  
  71. local runFile = function(path)
  72.     if not fs.exists(path) then
  73.         return false, "No such file!"
  74.     end
  75.     local file = fs.open(path,"r")
  76.     local contents = file.readAll()
  77.     file.close()
  78.     local func = loadstring(contents)
  79.     setfenv(func, getfenv())
  80.     return func()
  81. end
  82.  
  83. local function runURL(url, ...)
  84.     local program = http.get(url)
  85.     if not program then return false end
  86.     program = program.readAll()
  87.     local func = loadstring(program)
  88.     setfenv(func, getfenv())
  89.     return func(...)
  90. end
  91.  
  92. local function seperateMethods(input)
  93.     local output={}
  94.     for key,value in pairs(input) do
  95.         table.insert(output, {key,value})
  96.     end
  97.     return output
  98. end
  99.  
  100. local function displayHelp(mode)
  101.     if mode == 1 then
  102.         print("std <abbr> <fileid> [output]")
  103.         print("Do 'std list' to see all codes")
  104.         print("Do 'std ld' for a GUI")
  105.         write("Channel '")
  106.         if term.isColor() then term.setTextColor(colors.yellow) end
  107.         write(std.channel)
  108.         term.setTextColor(colors.white)
  109.         print("' is selected.")
  110.     elseif mode == 2 then
  111.         if std.serious then
  112.             print("List of website codes:")
  113.         else
  114.             print("all ur codes:")
  115.         end
  116.         std.websiteSyntaxes["dd"] = {} --Filler
  117.         std.websiteSyntaxes["PB"] = {} --Filler
  118.         for k,v in pairs(std.websiteSyntaxes) do
  119.             if term.getTextColor then prevColor = term.getTextColor() else prevColor = colors.white end
  120.             write(" '")
  121.             if term.isColor() then term.setTextColor(colors.orange) end
  122.             write(k)
  123.             term.setTextColor(prevColor)
  124.             write("' ")
  125.             if k == "dd" then
  126.                 print("direct download")
  127.             elseif k == "PB" then
  128.                 print("pastebin.com (safe)")
  129.             elseif string.find(v.url,"/") then
  130.                 start = string.find(v.url,"://")+3
  131.                 finish = string.find(v.url,"/",9)-1
  132.                 print(string.sub(v.url,start,finish))
  133.             end
  134.         end
  135.     elseif mode == 3 then
  136.         print(logo[pocket and 2 or 1])
  137.     end
  138. end
  139.  
  140. local function choice(input) --A useful function for input. Similar to the MS-DOS 6.0 command.
  141.     local event, key
  142.     repeat
  143.         event, key = os.pullEvent("key")
  144.         if type(key) == "number" then key = keys.getName(key) end
  145.         if key == nil then key = " " end
  146.     until string.find(string.lower(input), string.lower(key))
  147.     return string.lower(key)
  148. end
  149.  
  150. --This list of websites is used as a backup, should you not be able to connect to pastebin.
  151. std.websiteSyntaxes = {
  152.     pb = {
  153.         url = "http://pastebin.com/raw.php?i=FILECODE",
  154.         fullName = "Pastebin",
  155.         codeLength = 6,
  156.     },
  157.     hb = {
  158.         url = "http://hastebin.com/raw/FILECODE",
  159.         fullName = "Hastebin",
  160.         codeLength = 10,
  161.     },
  162.     pe = {
  163.         url = "http://pastie.org/pastes/FILECODE/download",
  164.         fullName = "Pastie",
  165.         codeLength = 0,
  166.     },
  167.     fn = {
  168.         url = "https://fnpaste.com/FILECODE/raw",
  169.         fullName = "fnPaste",
  170.         codeLength = 4,
  171.     },
  172.     gh = {
  173.         url = "https://raw.githubusercontent.com/FILECODE",
  174.         fullName = "Github",
  175.         codeLength = 0,
  176.     },
  177.     gg = {
  178.         url = "https://gist.githubusercontent.com/FILECODE/raw/",
  179.         fullName = "Github Gist",
  180.         codeLength = 0,
  181.     },
  182.     sn = {
  183.         url = "http://s.drk.sc/FILECODE",
  184.         fullName = "Snippt",
  185.         codeLength = 6,
  186.     },
  187.     cp = {
  188.         url = "http://codepad.org/FILECODE/raw.txt",
  189.         fullName = "Codepad",
  190.         codeLength = 8,
  191.     },
  192.     id = {
  193.         url = "http://ideone.com/plain/FILECODE",
  194.         fullName = "Ideone",
  195.         codeLength = 6,
  196.     },
  197.     db = {
  198.         url = "https://www.dropbox.com/s/FILECODE?raw=true",
  199.         fullName = "Dropbox",
  200.         codeLength = 0,
  201.     },
  202.     dd = {
  203.         url = "FILECODE",
  204.         fullName = "Direct Download",
  205.         codeLength = 0,
  206.     },
  207. }
  208.  
  209. local tArg = {...}
  210. if shell then
  211.     std_file = shell.getRunningProgram()
  212. else
  213.     std_file = ""
  214. end
  215.  
  216. local getTableSize = function(tbl)
  217.     local amnt = 0
  218.     for k,v in pairs(tbl) do
  219.         amnt = amnt + 1
  220.     end
  221.     return amnt
  222. end
  223.  
  224. std.getSTDList = function(prevChannel)
  225.     local weburl = "http://pastebin.com/raw/FSCzZRUk" --URL of URL list.
  226.     local storeurl = std.channelURLs[std.channel] --URL of store list.
  227.     local webcontents = http.get(weburl)
  228.     local storecontents = http.get(storeurl)
  229.     if not (webcontents and storecontents) then
  230.         if shell then
  231.             print("Couldn't update list!")
  232.         end
  233.         return false, "Couldn't update list!"
  234.     else
  235.         local uut = runFile(std.stdList)
  236.         if not uut then std.storeURLs = nil end
  237.         local beforeSize = getTableSize(std.storeURLs or {})
  238.         local webprog = webcontents.readAll()
  239.         local storeprog = storecontents.readAll()
  240.         local webfile = fs.open(std.websiteList,"w")
  241.         local storefile = fs.open(std.stdList,"w")
  242.         webfile.writeLine(webprog)
  243.         webfile.close()
  244.         storefile.writeLine(storeprog)
  245.         storefile.close()
  246.         runFile(std.websiteList)
  247.         local outcome = runFile(std.stdList)
  248.         if outcome == false then
  249.             std.channel = prevChannel
  250.             return std.getSTDList("STD")
  251.         end
  252.         local afterSize = getTableSize(std.storeURLs or {})
  253.         return true, "Downloaded to "..std.stdList, afterSize-beforeSize
  254.     end
  255. end
  256.  
  257. if tArg[1] == "update" or not fs.exists(std.stdList) then
  258.     local updateChan = tArg[2]
  259.     if (updateChan) and (not std.channelURLs[updateChan]) and tArg[1] == "update" then
  260.         printError("No such channel.")
  261.         for k,v in pairs(std.channelURLs) do
  262.             term.setTextColor(colors.white)
  263.             write(" ")
  264.             if k == std.channel then
  265.                 write("@")
  266.                 if term.isColor() then term.setTextColor(colors.yellow) end
  267.             else
  268.                 write("O")
  269.             end
  270.             print(" "..k)
  271.         end
  272.         term.setTextColor(colors.white)
  273.         return
  274.     end
  275.     if std.serious then
  276.         write("Updating list...")
  277.     else
  278.         write("just a sec, gettin your repo...")
  279.     end
  280.     if updateChan and std.channelURLs[updateChan] then
  281.         std.prevChannel = std.channel
  282.         std.channel = updateChan
  283.     end
  284.     local success,_,diff = std.getSTDList(std.prevChannel)
  285.     if not success then
  286.         if std.serious then
  287.             return printError("FAIL!")
  288.         else
  289.             return printError("IT'S NO USE!")
  290.         end
  291.     else
  292.         if std.serious then
  293.             write("good!")
  294.             if diff > 0 then
  295.                 print(" (got "..diff.." new store entries)")
  296.             else
  297.                 write("\n")
  298.             end
  299.         else
  300.             write("yay!!")
  301.             if diff > 0 then
  302.                 print(" (nao got "..diff.." moar shop things!)")
  303.             else
  304.                 write("\n")
  305.             end
  306.         end
  307.         if tArg[1] == "update" then return true end
  308.     end
  309. end
  310.  
  311. if not shell then return end
  312.  
  313. local websiteCode = tArg[1]
  314. local fileCode = tArg[2]
  315. local retrieveName = tArg[3]
  316.  
  317. if (websiteCode == "list") and (not fileCode) then
  318.     displayHelp(2)
  319.     return false
  320. elseif (websiteCode == "you foolish fool") and (not fileCode) then
  321.     displayHelp(3)
  322.     return false
  323. elseif (websiteCode ~= "ld") and (not fileCode) then
  324.     displayHelp(1)
  325.     return false
  326. end
  327.  
  328. local getFile = function(filename,url)
  329.     if fs.isReadOnly(filename) then
  330.         return false, "access denied"
  331.     end
  332.     local prog
  333.     if type(url) == "table" then
  334.         prog = contextualGet(url[1])
  335.     else
  336.         prog = http.get(url)
  337.     end
  338.     if not prog then
  339.         return false, "could not connect"
  340.     end
  341.     prog = prog.readAll()
  342.     local fyle = fs.open(filename,"w")
  343.     fyle.write(prog)
  344.     fyle.close()
  345.     return true, fs.getSize(filename)
  346. end
  347.  
  348. runFile(std.stdList)
  349. runFile(std.websiteList)
  350.  
  351. local fileURL
  352. if websiteCode == "ld" then
  353.     if not fileCode then
  354.         if doStore then
  355.             runURL("http://pastebin.com/raw/P9dDhQ2m")
  356.             return
  357.         else
  358.             return print("GUI Store has been disabled.")
  359.         end
  360.     else
  361.         if not std.storeURLs then
  362.             if std.serious then
  363.                 write("Updating list...")
  364.             else
  365.                 write("just a sec, gettin your repo...")
  366.             end
  367.             std.getSTDList()
  368.         end
  369.         if not std.storeURLs[fileCode] then
  370.             if std.serious then
  371.                 return printError("Invalid store code '" .. fileCode .. "'")
  372.             else
  373.                 return printError("ld code "..fileCode.." not guuud!!!")
  374.             end
  375.         else
  376.             fileURL = tostring(std.storeURLs[fileCode].url)
  377.         end
  378.     end
  379. elseif websiteCode == "PB" then --Hope it's not confusing.
  380.     fileURL = "http://pastebin.com/"..fileCode
  381.     write("Conntecting to '"..fileURL.."' safely...")
  382.     local prog = http.get(fileURL)
  383.     if not prog then
  384.         return printError("FAIL!\n")
  385.     else
  386.         if term.isColor() then term.setTextColor(colors.green) end
  387.         print("GOOD!")
  388.         term.setTextColor(colors.white)
  389.         local rawget = prog.readAll()
  390.         local s = string.find(rawget,"<textarea id=\"paste_code\"")+103
  391.         local e = string.find(rawget,"</textarea>")-1
  392.         local contents = string.gsub(string.sub(rawget,s,e),"&quot;","\"")
  393.         contents = contents:gsub("&lt;","<")
  394.         contents = contents:gsub("&gt;",">")
  395.         if retrieveName and shell then
  396.             local dlname = fs.combine(shell.dir(),retrieveName)
  397.             if fs.exists(dlname) then
  398.                 if std.serious then
  399.                     print("'" .. dlname .. "' exists! Overwrite?")
  400.                     write("[Y,N]?")
  401.                 else
  402.                     print("yoo alreddy got a '"..dlname.."'!! redu eet?")
  403.                     write("[why,enn]??")
  404.                 end
  405.                 local key = choice("yn")
  406.                 print(string.upper(key))
  407.                 if key == "n" then
  408.                     if std.serious then
  409.                         print("Cancelled.")
  410.                     else
  411.                         print("whatevz")
  412.                     end
  413.                     sleep(0)
  414.                     return false
  415.                 end
  416.             end
  417.             local file = fs.open(dlname, "w")
  418.             file.writeLine(contents)
  419.             file.close()
  420.             if std.serious then
  421.                 print("Done! DL'd " .. fs.getSize(dlname) .. " bytes.")
  422.             else
  423.                 print("yay guud! eets ".. fs.getSize(dlname)*2 .." nibbles")
  424.             end
  425.         else
  426.             local func = loadstring(contents)
  427.             setfenv(func, getfenv())
  428.             func()
  429.         end
  430.         sleep(0)
  431.         return
  432.     end
  433. else
  434.     if not std.websiteSyntaxes[websiteCode] then
  435.         if std.serious then
  436.             return printError("Invalid website code '" .. websiteCode .. "'")
  437.         else
  438.             return printError("dat '"..websiteCode.."' is NAWT GUUD!!")
  439.         end
  440.     else
  441.         if (std.websiteSyntaxes[websiteCode].codeLength == 0) or (not std.websiteSyntaxes[websiteCode].codeLength) then
  442.             fileURL = string.gsub(std.websiteSyntaxes[websiteCode].url, "FILECODE", fileCode)
  443.         else
  444.             fileURL = string.gsub(std.websiteSyntaxes[websiteCode].url, "FILECODE", string.sub(fileCode,1,std.websiteSyntaxes[websiteCode].codeLength))
  445.         end
  446.     end
  447.     sleep(0)
  448. end
  449.  
  450. if std.serious then
  451.     write("Connecting to '" .. fileURL .. "'...")
  452. else
  453.     if math.random(1,2) == 1 then
  454.         write("gettin ze '"..fileURL.."'...")
  455.     else
  456.         write("commeptin to '"..fileURL.."' naow...")
  457.     end
  458. end
  459. local contents = http.get(fileURL)
  460. if not contents then
  461.     if term.isColor() then
  462.         term.setTextColor(colors.red)
  463.     end
  464.     if std.serious then
  465.         print("NOPE!")
  466.     else
  467.         print("NI!")
  468.     end
  469.     sleep(0)
  470.     return false
  471. else
  472.     if term.getTextColor then
  473.         prevColor = term.getTextColor()
  474.     else
  475.         prevColor = colors.white
  476.     end
  477.     if term.isColor() then
  478.         term.setTextColor(colors.green)
  479.     end
  480.     if std.serious then
  481.         print("good!")
  482.     else
  483.         print("gud!")
  484.     end
  485.     term.setTextColor(prevColor)
  486.     if retrieveName and shell then
  487.         local dlname = fs.combine(shell.dir(),retrieveName)
  488.         if fs.exists(dlname) then
  489.             if std.serious then
  490.                 print("'" .. dlname .. "' exists! Overwrite?")
  491.                 write("[Y,N]?")
  492.             else
  493.                 print("yoo alreddy got a '"..dlname.."'!! redu eet?")
  494.                 write("[why,enn]??")
  495.             end
  496.             local key = choice("yn")
  497.             print(string.upper(key))
  498.             if key == "n" then
  499.                 if std.serious then
  500.                     print("Cancelled.")
  501.                 else
  502.                     print("whatevz")
  503.                 end
  504.                 sleep(0)
  505.                 return false
  506.             end
  507.         end
  508.         local file = fs.open(dlname, "w")
  509.         file.writeLine(contents.readAll())
  510.         file.close()
  511.         if std.serious then
  512.             print("Done! DL'd " .. fs.getSize(dlname) .. " bytes.")
  513.         else
  514.             print("yay guud! eets ".. fs.getSize(dlname)*2 .." nibbles")
  515.         end
  516.     else
  517.         local contents = loadstring(contents.readAll())
  518.         setfenv(contents, getfenv())
  519.         contents()
  520.     end
  521.     sleep(0)
  522.     return true
  523. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement