AgentE382

Pastebin API

Oct 5th, 2013
2,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.51 KB | None | 0 0
  1. API_version = 0.8
  2.  
  3. -- Internal function that parses the pastebin XML response.
  4. local function parse(response)
  5.     local objs = {}
  6.     for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
  7.         local obj = {}
  8.         for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
  9.             obj[key] = value
  10.         end
  11.         objs[#objs+1] = obj
  12.     end
  13.     return objs
  14. end
  15.  
  16. -- Used to be a custom function, but I realized CC-Lua had one already.
  17. local url_encode = textutils.urlEncode
  18.  
  19. -- My Pastebin API key.
  20. local api_dev_key = "c09548865c8dd0c2bf57d0d060b5e96a"
  21.  
  22. -- Allowable inputs for paste creation options.
  23. local paste_formats = {["4cs"]=true,["6502acme"]=true,["6502kickass"]=true,["6502tasm"]=true,abap=true,actionscript=true,actionscript3=true,ada=true,algol68=true,apache=true,applescript=true,apt_sources=true,arm=true,asm=true,asp=true,asymptote=true,autoconf=true,autohotkey=true,autoit=true,avisynth=true,awk=true,bascomavr=true,bash=true,basic4gl=true,bibtex=true,blitzbasic=true,bnf=true,boo=true,bf=true,c=true,c_mac=true,cil=true,csharp=true,cpp=true,["cpp-qt"]=true,c_loadrunner=true,caddcl=true,cadlisp=true,cfdg=true,chaiscript=true,clojure=true,klonec=true,klonecpp=true,cmake=true,cobol=true,coffeescript=true,cfm=true,css=true,cuesheet=true,d=true,dcl=true,dcpu16=true,dcs=true,delphi=true,oxygene=true,diff=true,div=true,dos=true,dot=true,e=true,ecmascript=true,eiffel=true,email=true,epc=true,erlang=true,fsharp=true,falcon=true,fo=true,f1=true,fortran=true,freebasic=true,freeswitch=true,gambas=true,gml=true,gdb=true,genero=true,genie=true,gettext=true,go=true,groovy=true,gwbasic=true,haskell=true,haxe=true,hicest=true,hq9plus=true,html4strict=true,html5=true,icon=true,idl=true,ini=true,inno=true,intercal=true,io=true,j=true,java=true,java5=true,javascript=true,jquery=true,kixtart=true,latex=true,ldif=true,lb=true,lsl2=true,lisp=true,llvm=true,locobasic=true,logtalk=true,lolcode=true,lotusformulas=true,lotusscript=true,lscript=true,lua=true,m68k=true,magiksf=true,make=true,mapbasic=true,matlab=true,mirc=true,mmix=true,modula2=true,modula3=true,["68000devpac"]=true,mpasm=true,mxml=true,mysql=true,nagios=true,newlisp=true,text=true,nsis=true,oberon2=true,objeck=true,objc=true,["ocaml-brief"]=true,ocaml=true,octave=true,pf=true,glsl=true,oobas=true,oracle11=true,oracle8=true,oz=true,parasail=true,parigp=true,pascal=true,pawn=true,pcre=true,per=true,perl=true,perl6=true,php=true,["php-brief"]=true,pic16=true,pike=true,pixelbender=true,plsql=true,postgresql=true,povray=true,powershell=true,powerbuilder=true,proftpd=true,progress=true,prolog=true,properties=true,providex=true,purebasic=true,pycon=true,python=true,pys60=true,q=true,qbasic=true,rsplus=true,rails=true,rebol=true,reg=true,rexx=true,robots=true,rpmspec=true,ruby=true,gnuplot=true,sas=true,scala=true,scheme=true,scilab=true,sdlbasic=true,smalltalk=true,smarty=true,spark=true,sparql=true,sql=true,stonescript=true,systemverilog=true,tsql=true,tcl=true,teraterm=true,thinbasic=true,typoscript=true,unicon=true,uscript=true,ups=true,urbi=true,vala=true,vbnet=true,vedit=true,verilog=true,vhdl=true,vim=true,visualprolog=true,vb=true,visualfoxpro=true,whitespace=true,whois=true,winbatch=true,xbasic=true,xml=true,xorg_conf=true,xpp=true,yaml=true,z80=true,zxbasic=true}
  24. local paste_private_options = {[0]=true,[1]=true,[2]="Private"}
  25. local paste_expire_date_options = {["N"]=true,["10M"]=true,["1H"]=true,["1D"]=true,["1W"]=true,["2W"]=true,["1M"]=true}
  26.  
  27. -- Creates a new paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
  28. -- Example: local paste = pastebin.create("This is my awesome paste text!", nil, "Super cool name", nil, nil, "N")
  29. --
  30. -- Returns paste URL or error code.
  31. function create(api_paste_code, api_user_key, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  32.   assert(type(api_paste_code) == "string", "Only strings can be uploaded as pastes! Use `tostring()` if necessary!")
  33.   local urlstr = {"api_dev_key=", url_encode(api_dev_key), "&api_option=paste", "&api_paste_code=", url_encode(api_paste_code)}
  34.   if api_user_key then if type(api_user_key) == "string" then local len = #urlstr urlstr[len+1] = "&api_user_key=" urlstr[len+2] = url_encode(api_user_key) else io.stderr:write("Invalid user key!\n") end end
  35.   if api_paste_name then if type(api_paste_name) == "string" then local len = #urlstr urlstr[len+1] = "&api_paste_name=" urlstr[len+2] = url_encode(api_paste_name) else io.stderr:write("Only strings can be used to name pastes! Use `tostring()` if necessary!") end end
  36.   if api_paste_format then if paste_formats[api_paste_format] then local len = #urlstr urlstr[len+1] = "&api_paste_format=" urlstr[len+2] = url_encode(api_paste_format) else io.stderr:write("Invalid paste format!") end end
  37.   if api_paste_private then local paste_private_long = paste_private_options[api_paste_private] if paste_private_long then if paste_private_long == "Private" and not api_user_key then io.stderr:write("User key necessary for private pastes!") else local len = #urlstr urlstr[len+1] = "&api_paste_private=" urlstr[len+2] = url_encode(api_paste_private) end else io.stderr:write("Invalid privacy level") end end
  38.   if api_paste_expire_date then if paste_expire_date_options[api_paste_expire_date] then local len = #urlstr urlstr[len+1] = "&api_paste_expire_date=" urlstr[len+2] = api_paste_expire_date else io.stderr:write("Invalid paste expiration date!") end end
  39.  
  40.   return http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  41. end
  42.  
  43. -- Authenticates with Pastebin.
  44. -- Example: local agente382 = pastebin.login("AgentE382", "thisisnotmypassword")
  45. --
  46. -- Returns user key for use with other API functions.
  47. function login(api_user_name, api_user_password)
  48.     assert(type(api_user_name) == "string" and type(api_user_password) == "string", "Both arguments are required and must be strings!")
  49.     return http.post("http://pastebin.com/api/api_login.php", "api_dev_key="..url_encode(api_dev_key).."&api_user_name="..url_encode(api_user_name).."&api_user_password="..url_encode(api_user_password)).readAll()
  50. end
  51.  
  52. -- Deletes a paste. Uses user key from login(). api_paste_key must be a string containing the end of the paste's URL.
  53. -- Example: local response = pastebin.delete(agente382, "Rxe673BJ")
  54. --
  55. -- Returns "Paste Removed" or error message.
  56. function delete(api_user_key, api_paste_key)
  57.     assert(type(api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  58.     return http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..url_encode(api_dev_key).."&api_user_key="..url_encode(api_user_key).."&api_paste_key="..url_encode(api_paste_key).."&api_option=delete").readAll()
  59. end
  60.  
  61. -- Fetches a paste from Pastebin.
  62. -- Example: local rc4 = pastebin.get("Rxe673BJ")
  63. --
  64. -- Returns requested paste as a string or an error message.
  65. function get(api_paste_key)
  66.     assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
  67.     local response = http.get("http://pastebin.com/raw.php?i="..url_encode(api_paste_key))
  68.     return response and response.readAll()
  69. end
  70.  
  71. -- Fetches a private paste from Pastebin. Paste must belong to user logged in with api_user_key
  72. -- Example: local test = pastebin.getprivate(agente382, "fhLGZm3i")
  73. --
  74. -- Returns requested paste as a string or an error message.
  75. function getprivate(api_user_key, api_paste_key)
  76.     assert(type(api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  77.     return http.post("http://pastebin.com/api/api_raw.php", "api_dev_key="..url_encode(api_dev_key).."&api_user_key="..url_encode(api_user_key).."&api_paste_key="..url_encode(api_paste_key).."&api_option=show_paste").readAll()
  78. end
  79.  
  80. -- Lists data about all of a user's pastes. Uses user key from login(). Note that this makes it impossible to get data about random people's pastes.
  81. -- Example: local allpastes = pastebin.list(agente382, 1000)
  82. --
  83. -- Returns a list of data about all pastes associated with a user's account, using the table format described at the end of the document.
  84. function list(api_user_key, api_results_limit)
  85.     assert(type(api_user_key) == "string", "Enter a valid user key as a string!")
  86.     local urlstr = {"api_dev_key=", url_encode(api_dev_key), "&api_user_key=", url_encode(api_user_key), "&api_option=list"}
  87.     if api_results_limit then if type(api_results_limit) == "number" then if api_results_limit > 1000 then api_results_limit = 1000 elseif api_results_limit < 1 then api_results_limit = 1 end local len = #urlstr urlstr[len+1] = "&api_results_limit=" urlstr[len+2] = url_encode(api_results_limit) else io.stderr:write("Results limit must be a number!\n") end end
  88.     local response = http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  89.     if string.find(response,"<") then
  90.         return parse(response)
  91.     else
  92.         return response
  93.     end
  94. end
  95.  
  96. -- Lists data about the 18 currently trending pastes.
  97. -- Example: local trendingpastes = pastebin.trending()
  98. --
  99. -- Returns a list of data about the 18 currently trending pastes, using the table format described at the end of this document.
  100. function trending()
  101.     local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..url_encode(api_dev_key).."&api_option=trends").readAll()
  102.     if string.find(response,"<") then
  103.         return parse(response)
  104.     else
  105.         return response
  106.     end
  107. end
  108.  
  109. -- Lists all of a user's settings. Uses user key from login().
  110. -- Example: local settings = pastebin.settings(agente382)
  111. --
  112. -- Returns a list of the user's settings, using the table format described at the end of this document.
  113. function settings(api_user_key)
  114.     assert(type(api_user_key) == "string", "Enter a valid user key as a string!")
  115.     local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..url_encode(api_dev_key).."&api_user_key="..url_encode(api_user_key).."&api_option=userdetails").readAll()
  116.     if string.find(response,"<") then
  117.         return unpack(parse(response))
  118.     else
  119.         return response
  120.     end
  121. end
  122.  
  123. -- Table Format:
  124. --      Pastes:
  125. --      {
  126. --          [1] = {
  127. --              date = "1297953260",
  128. --              format_short = "javascript",
  129. --              key = "0b42rwhf",
  130. --              size = "15",
  131. --              hits = "15",
  132. --              format_long = "JavaScript",
  133. --              url = "http://pastebin.com/0b42rwhf",
  134. --              title = "javascript test",
  135. --              private = "0",        -- 0:Public - 1:Unlisted - 2:Private
  136. --              expire_date = "1297956860"
  137. --          },
  138. --          [2] = {
  139. --              date = "1297694343",
  140. --              format_short = "text",
  141. --              key = "0C343n0d",
  142. --              size = "490",
  143. --              hits = "65",
  144. --              format_long = "None",
  145. --              url = "http://pastebin.com/0C343n0d",
  146. --              title = "Welcome To Pastebin V3",
  147. --              private = "0",        -- 0:Public - 1:Unlisted - 2:Private
  148. --              expire_date = "0"
  149. --          }
  150. --      }
  151. --      Settings:
  152. --      {
  153. --          format_short = "text",
  154. --          email = "[email protected]",
  155. --          website = "http://myawesomesite.com",
  156. --          location = "New York",
  157. --          expiration = "N",
  158. --          avatar_url = "http://pastebin.com/cache/a/1.jpg",
  159. --          account_type = "1",        -- 0:Free - 1:Pro
  160. --          private = "1",        -- 0:Public - 1:Unlisted - 2:Private
  161. --          name = "wiz_kitty"
  162. --      }
Advertisement
Add Comment
Please, Sign In to add comment