Sir_Mr_Bman

Paste API - Version 2.0

May 25th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. -- Pastebin API
  2. -- Created by Sir_Mr_Bman
  3. -- Copyright (c) 2014
  4. -- Feel free to redistribute this program as part of your own programs (if you give me credit)
  5.  
  6. -- Light-Docs:
  7. --      RETURN CODES (downloader):
  8. --      11: HTTP API is not enabled
  9. --      12: Savefile exists
  10. --      13: Could not connect. Is pastebin down?
  11. --      2: Download complete!
  12. --
  13. --      RETURN CODES (uploader):
  14. --      111: HTTP API is not enabled
  15. --      112: Attempt to upload directory
  16. --      113: File does not exist.
  17. --      114: Could not connect.
  18. --      <pastebin_code>: Success! Everything worked as it should!
  19.  
  20. function downloader(paste, savefile)
  21.  
  22.     if not http then
  23.         return 11
  24.     end
  25.     if fs.exists(textutils.urlEncode(savefile)) then
  26.         return 12
  27.     end
  28.  
  29.     local pastebin = http.get("http://www.pastebin.com/raw.php?i="..textutils.urlEncode(paste))
  30.     if pastebin then
  31.         local saveTo = pastebin.readAll()
  32.         pastebin.close()
  33.         local file = fs.open(savefile, "w")
  34.         file.write(saveTo)
  35.         file.close()
  36.         return 2
  37.     else
  38.         return 13
  39.     end
  40. end
  41.  
  42. function upload(path, pasteName)
  43.  
  44.     -- My Developer key
  45.     local developerKey = "48e51ae9fe3fe690184d111a047b51b9"
  46.     -- See if there is any reason this should not work...
  47.     if fs.isDir(path) then
  48.         return 112
  49.     end
  50.     if not http then
  51.         return 111
  52.     end
  53.     if not fs.exists(path) then
  54.         return 113
  55.     end
  56.  
  57.  
  58.     local file = fs.open(path, "r")
  59.     local pasteString = file.readAll()
  60.     local pastebin = http.post(
  61.         "http://pastebin.com/api/api_post.php",
  62.         "api_option=paste&"..
  63.         "api_dev_key="..developerKey.."&"..
  64.         "api_paste_format=lua&"..
  65.         "api_paste_name="..textutils.urlEncode(pasteName).."&"..
  66.         "api_paste_code="..textutils.urlEncode(pasteString)
  67.         )
  68.     if pastebin then
  69.         local saveInfo = pastebin.readAll()
  70.         pastebin.close()
  71.         local pasteCode = string.match(saveInfo, "[^/]+$")
  72.         return pasteCode
  73.     else
  74.         return 114
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment