Creeper9207

wgetAPI

Feb 17th, 2015
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. --Creeper9207 Code: wget substitute (API)--
  2. --functions:
  3. --openGUI
  4. --downloadFile
  5. --silentDownload
  6. --example:
  7. --os.loadAPI("wgetAPI")
  8. --wget.silentDownload("http://creeper9207.com/file.html", "file.html")
  9.  
  10. function openGUI()
  11.   term.clear()
  12.   gui1 = paintutils.loadImage(".wget/gui1")
  13.   paintutils.drawImage(gui1, 1, 1)
  14.   term.setCursorPos(3, 3)
  15.   term.setBackgroundColor( colors.lightGray )
  16.   term.setTextColor( colors.white )
  17.   io.write("url:")
  18.   guiUrl = io.read()
  19.   term.setCursorPos(3, 5)
  20.   io.write("file:")
  21.   guiFile = io.read()
  22.   downloadFile(guiUrl, guiFile)
  23. end
  24. function downloadFile(url, file)
  25.   term.setBackgroundColor( colors.black )
  26.   term.setCursorPos(1, 1)
  27.   background = paintutils.loadImage(".wget/background")
  28.   paintutils.drawImage(background, 1, 1)
  29.   print("Getting file...")
  30.   http.request(url)
  31.   event, eUrl, content = os.pullEvent()
  32.   if event == "http_success" then
  33.     print("SUCCESS!")
  34.     body = content.readAll()
  35.     file = fs.open(file, "w")
  36.     file.write(body)
  37.     file.flush()
  38.     file.close()
  39.     print("")
  40.     print("downloaded!")
  41.   elseif event == "http_failure" then
  42.     print("download error")
  43.   else
  44.     print("unknown error")
  45.   end
  46. end
  47. function silentDownload(url, file)
  48.   http.request(url)
  49.   event, eUrl, content = os.pullEvent()
  50.   if event == "http_success" then
  51.     body = content.readAll()
  52.     file = fs.open(file, "w")
  53.     file.write(body)
  54.     file.flush()
  55.     file.close()
  56.   elseif event == "http_failure" then
  57.   else
  58.   end
  59. end
  60. function screenClear()
  61.   term.setBackgroundColor( colors.black )
  62.   term.setCursorPos(1, 1)
  63.   background = paintutils.loadImage(".wget/background")
  64.   paintutils.drawImage(background, 1, 1)
  65. end
  66. --os.loadAPI("wget")
  67. --wget.silentDownload("http://creeper9207.com/file.html", "file.html")
Advertisement
Add Comment
Please, Sign In to add comment