gknova61

Pastebin Downloader

Nov 26th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. --Set these
  2. local pastebinIds = {"tjzbPDf2","8R0XnyNq"}
  3. local pastebinFileNames = {"gps","fsa"}
  4.  
  5. local dropboxUserId = 0
  6. local dropboxIds = {}
  7. local dropboxFileNames = {}
  8.  
  9. if not http then
  10.     error( "Pastebin requires the HTTP API\nSet enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  11. end
  12.  
  13. local function dropboxGet(fileName,path,userId)
  14.         if userId == nil then
  15.                 local iDropboxUser = 2813860
  16.         else
  17.                 iDropboxUser = userId
  18.         end
  19.  
  20.         local sVersion = string.sub(os.version(), -3)
  21.         local fileGet = {}
  22.  
  23.         if sVersion == "1.4" then
  24.                 fileGet = http.get("https://dl.dropbox.com/u/" .. iDropboxUser .. "/" .. fileName)
  25.         else
  26.                 fileGet = http.get("http://dl.dropbox.com/u/" .. iDropboxUser .. "/" ..fileName)
  27.         end
  28.  
  29.         if path ~= nil then
  30.                 if fs.exists(path) == true then
  31.                         fs.delete(path)
  32.                 end
  33.                 local tFile = fs.open(path, "w")
  34.                 tFile.write(fileGet.readAll())
  35.                 tFile.close()
  36.                 fileGet.close()
  37.                 return true
  38.         elseif path == nil then
  39.                 return fileGet.readAll()
  40.         end
  41. end
  42.  
  43. local function pastebinGet(code,path)
  44.     local sCode = code
  45.     local sFile = path
  46.     local sPath = shell.resolve( sFile )
  47.     if fs.exists( sPath ) then
  48.         fs.delete(sPath)
  49.     end
  50.  
  51.     local response = http.get(
  52.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  53.         )
  54.        
  55.     if response then
  56.         local sResponse = response.readAll()
  57.         response.close()
  58.        
  59.         local file = fs.open( sPath, "w" )
  60.         file.write( sResponse )
  61.         file.close()
  62.                 return true
  63.        
  64.     else
  65.         return false
  66.     end
  67. end
  68.  
  69. local function errorCheck()
  70.     if pastebinIds == nil and dropboxIds == nil then
  71.         return "Nothing to download!"
  72.     end
  73.  
  74.     if #pastebinIds ~= pastebinFileNames then
  75.         if #dropboxIds < #dropboxFileNames then
  76.             return "Need more filenames for Pastebin!"
  77.         elseif #pastebinFileNames < #pastebinIds then
  78.             return "Need more ids for Pastebin!"
  79.         end
  80.     end
  81.  
  82.     if #dropboxIds ~= dropboxFileNames then
  83.         if #dropboxIds < #dropboxFileNames then
  84.             return "Need more filenames for Dropbox!"
  85.         elseif #dropboxFileNames < #dropboxIds then
  86.             return "Need more ids for Dropbox!"
  87.         end
  88.     end
  89. end
  90.  
  91. if not errorCheck() then
  92.     error(errorCheck())
  93. end
  94.  
  95. print("Downloading the following files:")
  96. for i=1,#pastebinFileNames do
  97.     print(pastebinFileNames[i])
  98. end
  99.  
  100. for i=1,#dropboxFileNames do
  101.     print(dropboxFileNames[i])
  102. end
  103.  
  104. sleep(1)
  105. print()
  106. if pastebinIds ~= nil then
  107.     for i=1,#pastebinIds do
  108.         sleep(0.1) 
  109.         x,y = term.getCursorPos()
  110.             term.clearLine()
  111.             term.setCursorPos(1,y)
  112.             write("Downloading from pastebin "..i.."/"..#pastebinIds)    
  113.             if pastebinGet(pastebinIds[i],pastebinFileNames[i]) then
  114.             else
  115.                     error("Something went wrong with pastebin!")
  116.             end
  117.     end
  118. end
  119.  
  120. if dropboxIds ~= nil then
  121.     for i=1,#dropboxIds do
  122.         sleep(0.1) 
  123.         x,y = term.getCursorPos()
  124.             term.clearLine()
  125.             term.setCursorPos(1,y)
  126.             write("Downloading from dropbox "..i.."/"..#dropboxIds)    
  127.             if dropboxGet(dropboxFileNames[i],dropboxIds[i],dropboxUserId) then
  128.             else
  129.                     error("Something went wrong with dropbox!")
  130.             end
  131.     end
  132. end
  133.  
  134. print()
  135. term.setCursorPos(1,y)
  136. print("Downloaded Program Pack")
  137. return true
Add Comment
Please, Sign In to add comment