Watchful11

Dropbox

Nov 12th, 2012
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. --name dropbox
  2. --version 2
  3.  
  4. local tArgs = {...} --read in arguments
  5.  
  6. function out(str) -- function to print only if the argument true is not passed in
  7.     if tArgs[1] ~= "true" then
  8.         print(str)
  9.     end
  10. end
  11.  
  12. function download(address)
  13.     site = http.get(address) -- sets site to the file found at address
  14.     if site == nil then -- if there is no file, print "No file" and skip the rest of the program
  15.         print("No file")
  16.     else
  17.         code = site.readAll() -- read the whole file and put it in code
  18.         bool = true -- temp variable for loop
  19.         i = 1 -- temp variable for loop
  20.         start = 0 -- temp variable for loop
  21.         while bool do -- find the rightmost "/" in the address
  22.             start = string.find(address, "/", string.len(address)-i, true)
  23.             i = i + 1
  24.             if start ~= nil then
  25.                 name = string.sub(address, start+1) -- sets name to the part of the address to the right of the last "/"
  26.                 bool = false
  27.             end
  28.             if i > string.len(address) then -- if i gets bigger than the address and no "/" is found, name the file "noname"
  29.                 name = "noname"
  30.                 bool = false
  31.             end
  32.         end
  33.  
  34.         firstline = string.find(code, "\n") -- finds the end of the first line
  35.         if firstline == nil then -- if the code does not have an end of line, set firstline to the length of code
  36.             firstline = string.len(code)
  37.         end
  38.         firstname = string.find(code, "--name") -- tries to find "--name"
  39.         name2 = nil -- name2 defaults to nil
  40.         if firstname ~= nil then -- if "--name" was found
  41.             if firstname < firstline then -- and if it was on the first line
  42.                 name2 = string.sub(code, 8, firstline-1) -- set name2 to the new name
  43.             end
  44.         end
  45.        
  46.         out("Downloaded: \""..name.."\"") -- use the out function to print "Downloaded: name"
  47.         if name2 ~= nil then -- if there was a name2, print "renamed to: name2"
  48.             out("renamed to: \""..name2.."\"")
  49.         end
  50.        
  51.         versionline = string.find(code, "--version") -- find the line "--version" is on
  52.         versionend = string.find(code, "\n", versionline) -- find the end of that line
  53.         secondline = string.find(code, "\n", firstline+1) -- find the end of the second line
  54.         if secondline == nil then  -- if the code does not have a second end of line, set secondline to the length of code
  55.             secondline = string.len(code)
  56.         end
  57.         if versionline ~= nil then -- checks that "--version" exists
  58.             if versionline < secondline then -- checks that it exists on one of the first 2 lines
  59.                 out("Version: "..string.sub(code, versionline+10, versionend-1)) -- prints "Version: " and then the version number
  60.             end
  61.         end
  62.  
  63.         if name2 ~= nil then -- if name2 exists, open a file with that name
  64.             file = fs.open(name2,"w")
  65.         else -- else open a file with name
  66.             file = fs.open(name,"w")
  67.         end
  68.         file.write(code) -- write code to that file
  69.         file.close() -- and close it
  70.     end
  71. end
  72.  
  73. function files() -- reads in the file "dropboxfiles" and returns an array with all the urls
  74.     file = fs.open("dropboxfiles","r") -- opens the file
  75.     hasline = true -- temp variable
  76.     files = {} -- initialize array
  77.     i = 1 -- temp variable
  78.     while hasline do -- while we have another line
  79.         line = file.readLine() -- read the line
  80.         if line == nil then -- if it is nil, there are no more lines
  81.             hasline = false
  82.         else -- else write the url to the array
  83.             files[i] = line
  84.         end
  85.         i = i + 1
  86.     end
  87.     return files -- return the array
  88. end
  89.  
  90. urls = files() -- read the file
  91. for i=1,#urls do -- download from the urls
  92.     download(urls[i])
  93. end
Advertisement
Add Comment
Please, Sign In to add comment