Advertisement
minizbot

cc gist

Aug 4th, 2015
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. local logins = {}
  2. --be sure to get a Personal Access token from https://github.com/settings/tokens and save it somewhere in order to use this API (requires gist permission node)
  3. local function installAPI(code, name)
  4.     local webHandle = http.get("http://pastebin.com/raw.php?i="..code)
  5.     if webHandle then
  6.         local apiData = webHandle.readAll()
  7.         webHandle.close()
  8.         local tEnv = {}
  9.         setmetatable( tEnv, { __index = _ENV } )
  10.         local fnAPI, err = load( apiData, name, "t", tEnv )
  11.         if fnAPI then
  12.             fnAPI()
  13.         end
  14.         local tAPI = {}
  15.         for k,v in pairs( tEnv ) do
  16.             if k ~= "_ENV" then
  17.                 tAPI[k] =  v
  18.             end
  19.         end
  20.         _G[name] = tAPI
  21.     else
  22.         print("Could not retrieve API: "..name.."!")
  23.         print("Ensure that the HTTP API is enabled.")
  24.         return false
  25.     end
  26. end
  27. if not json then
  28.     installAPI("4nRg9CHU", "json")
  29. end
  30. local function genToken(token)
  31.     local userp = user..":"..pass
  32.     return "Token "..enc(userp)
  33. end
  34. local function createFS(fileTbl)
  35.     local ret = {}
  36.     for k,v in pairs(fileTbl) do
  37.         ret[k] = {content=v}
  38.     end
  39.     return ret
  40. end
  41. function login(token, nick)
  42.     logins[nick] = genToken(token)
  43. end
  44. function getNicks()
  45.     local ret = {}
  46.     for k,v in pairs(logins) do
  47.         ret[#ret+1] = k
  48.     end
  49.     return ret
  50. end
  51. function createGist(nick, fileTbl, desc, pub)
  52.     local send = json.encode({files = createFS(fileTbl), description = desc, public = pub})
  53.     local s = http.post("https://api.github.com/gists", send, {Authorization = logins[nick]})
  54.     local inf = s.readAll()
  55.     return json.decode(inf).id
  56. end
  57. function updateGist(nick, ID, files)
  58.     local send = json.encode({files = createFS(files)})
  59.     local s = http.post("https://api.github.com/gists/"..ID, send, {Authorization = logins[nick]})
  60.     return json.decode(s.readAll()).id
  61. end
  62. function getGistFiles(ID)
  63.     local s = http.get("https://api.github.com/gists/"..ID)
  64.     return json.decode(s.readAll()).files
  65. end
  66. function getUsersGists(nick)
  67.     local s = http.get("https://api.github.com/gists", {Authorization=logins[nick]})
  68.     return json.decode(s.readAll())
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement