fatboychummy

AdventOfCodePget

Dec 1st, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local loc = "/advent/"
  2. local fileName = "Challenge"
  3. local dirName = "Day"
  4. local fileToGrab = "hdF9mauN" ---------------------------------------------------------
  5. local tArgs = {...}
  6. local day = tArgs[1]
  7. local challenge = tArgs[2]
  8.  
  9. assert(type(tonumber(day)) == "number", "Bad input: #1, expected number.")
  10. assert(type(tonumber(challenge)) == "number", "Bad input: #2, expected number.")
  11. assert(tonumber(challenge) >= 1 and tonumber(challenge) <= 25 and tonumber(challenge) % 1 == 0,"Badinput: #2, expected number (1-25)")
  12.  
  13. fileName = fileName .. challenge
  14. dirName = dirName .. day .. "/"
  15.  
  16. if not fs.isDir(loc..dirName) then
  17.   fs.makeDir(dirName)
  18. end
  19.  
  20.  
  21. fs.delete(loc .. dirName .. fileName)
  22.  
  23. do
  24.   print()
  25.   print("Attempting to download file from Pastebin: "..fileToGrab)
  26.   print("Connecting to Pastebin...")
  27.   local h1 = http.get("https://pastebin.com/raw/"..fileToGrab)
  28.   if h1 then
  29.     print("Connected!")
  30.     print("Opening file: ".. loc .. dirName .. fileName)
  31.     local h2 = fs.open(loc .. dirName .. fileName,"w")
  32.     if h2 then
  33.       print("File successfully opened!")
  34.       h2.write(h1.readAll())
  35.       h2.writeLine()
  36.       h2.close()
  37.       print("Download success!")
  38.     else
  39.       error("Failed to open local file.")
  40.     end
  41.     h1.close()
  42.   else
  43.     error("Failed to connect to pastebin.")
  44.   end
  45. end
Add Comment
Please, Sign In to add comment