Advertisement
Guest User

wilsonbits

a guest
Dec 12th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local function explodeString(d,p)
  2.  local t, ll
  3.  t = {}
  4.  ll = 0
  5.  if (#p == 1) then return {p} end
  6.   while true do
  7.    l = string.find(p,d,ll,true)
  8.    if l ~= nil then
  9.     table.insert(t, string.sub(p,ll,l-1))
  10.     ll = l + 1
  11.    else
  12.     table.insert(t, string.sub(p,ll))
  13.     break
  14.    end
  15.   end
  16.  return t
  17. end
  18.  
  19. local function printUsage()
  20.  local filePath = shell.getRunningProgram()
  21.  if string.find(filePath, "/") ~= nil then
  22.   local path = explodeString("/", shell.getRunningProgram())
  23.   programName = path[#path]
  24.  else
  25.   programName = filePath
  26.  end
  27.  
  28.  print("Usage:")
  29.  print(programName.." <code> <filename>")
  30. end
  31.  
  32. tArgs = { ... }
  33. if #tArgs ~= 2 then
  34.  printUsage()
  35.  return
  36. end
  37.  
  38. if not http then
  39.  print("This program requires http API")
  40.  print("Please enable in the mod config")
  41.  return
  42. end
  43.  
  44. local code = tArgs[1]
  45. local filename = tArgs[2]
  46. local path = shell.resolve(filename)
  47. if fs.exists(path) then
  48.  print("File already exists")
  49.  return
  50. end
  51.  
  52. write("Connecting to site... ")
  53. local response = http.get("http://wilsonbits.com/scripts/"..textutils.urlEncode(code))
  54.  
  55. if response then
  56.  print("Success")
  57.  
  58.  local sCode = response.readAll()
  59.  response.close()
  60.  
  61.  local file = fs.open(filename, "w")
  62.  file.write(sCode)
  63.  file.close()
  64.  
  65.  print("Downloaded as "..filename)
  66.  
  67. else
  68.  print("Failed")
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement