gknova61

Download API

Nov 26th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. --API Made for downloading various things via the HTTP API
  2. function dropboxGet(fileName,path,userId)
  3.     if userId == nil then
  4.         local iDropboxUser = 2813860
  5.     else
  6.         iDropboxUser = userId
  7.     end
  8.  
  9.     local sVersion = string.sub(os.version(), -3)
  10.     local fileGet = {}
  11.  
  12.     if sVersion == "1.4" then
  13.             fileGet = http.get("https://dl.dropbox.com/u/" .. iDropboxUser .. "/" .. fileName)
  14.     else
  15.             fileGet = http.get("http://dl.dropbox.com/u/" .. iDropboxUser .. "/" ..fileName)
  16.     end
  17.  
  18.     if path ~= nil then
  19.         if fs.exists(path) == true then
  20.                 fs.delete(path)
  21.         end
  22.         local tFile = fs.open(path, "w")
  23.         tFile.write(fileGet.readAll())
  24.         tFile.close()
  25.         fileGet.close()
  26.         return true
  27.     elseif path == nil then
  28.         return fileGet.readAll()
  29.     end
  30. end
  31.  
  32. function githubGet(user, repo, branch, file, saveas)
  33.         local response = http.get("raw.github.com/" ..user.. "/" ..repo.. "/" ..branch.. "/" ..file)
  34.         if response then
  35.                 local sResponse = response.readAll()
  36.                 response.close()
  37.                 local file = fs.open( saveas, "w" )
  38.                 file.write( sResponse )
  39.                 file.close()
  40.                 return true
  41.         else
  42.                 print("Something went wrong!")
  43.                 return false
  44.         end
  45. end
  46.  
  47. function hastebinGet(code,path)
  48.     local sCode = code
  49.     local sFile = path
  50.     if sFile ~= nil then
  51.         local sPath = shell.resolve( sFile )
  52.         if fs.exists( sPath ) then
  53.             fs.delete(sPath)
  54.         end
  55.  
  56.         local response = http.get(
  57.             "http://www.hastebin.com/raw/"..textutils.urlEncode(sCode)
  58.             )
  59.        
  60.         if response then
  61.             local sResponse = response.readAll()
  62.             response.close()
  63.        
  64.             local file = fs.open( sPath, "w" )
  65.             file.write( sResponse )
  66.             file.close()
  67.                     return true
  68.        
  69.         else
  70.             return false
  71.         end
  72.     elseif sFile == nil then
  73.         local response = http.get(
  74.             "http://www.hastebin.com/raw/"..textutils.urlEncode(sCode)
  75.             )
  76.        
  77.         if response then
  78.             local sResponse = response.readAll()
  79.             response.close()
  80.             return sResponse
  81.         end
  82.     end
  83. end
  84.  
  85. function pastebinGet(code,path)
  86.     local sCode = code
  87.     local sFile = path
  88.     if sFile ~= nil then
  89.         local sPath = shell.resolve( sFile )
  90.         if fs.exists( sPath ) then
  91.             fs.delete(sPath)
  92.         end
  93.  
  94.         local response = http.get(
  95.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  96.             )
  97.        
  98.         if response then
  99.             local sResponse = response.readAll()
  100.             response.close()
  101.        
  102.             local file = fs.open( sPath, "w" )
  103.             file.write( sResponse )
  104.             file.close()
  105.                     return true
  106.        
  107.         else
  108.             return false
  109.         end
  110.     elseif sFile == nil then
  111.         local response = http.get(
  112.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  113.             )
  114.        
  115.         if response then
  116.             local sResponse = response.readAll()
  117.             response.close()
  118.             return sResponse
  119.         end
  120.     end
  121. end
Add Comment
Please, Sign In to add comment