Skip to content

Instantly share code, notes, and snippets.

@TurplePurtle
Created August 24, 2012 00:52
Show Gist options
  • Save TurplePurtle/3444135 to your computer and use it in GitHub Desktop.
Save TurplePurtle/3444135 to your computer and use it in GitHub Desktop.
Basic reddit API functions
--[[
Currently implemented functions are:
login(user, passwd)
me(auth)
submit(title, isSelf, content, subreddit, auth)
comment(thing_id, text, auth)
]]
local json = require "json"
local util = require "reddit.util"
local reddit = {util=util}
function reddit.login(user, passwd)
local url = "https://ssl.reddit.com/api/login/" .. user
local postData = {
api_type = "json",
user = user,
passwd = passwd,
}
local res = util.postRequest(url, postData)
local data = json.decode(res).json.data
if data and data.modhash and data.cookie then
return {
user=user,
modhash=data.modhash,
session=data.cookie,
cookie="reddit_session="..util.escape(data.cookie)}
end
end
function reddit.getJson(url, auth)
local res, code = util.getRequest(url, auth and auth.cookie)
res = json.decode(res)
util.updateAuth(auth, res)
return res, code
end
function reddit.submit(title, isSelf, content, subreddit, auth)
util.validateAuth(auth)
local url = "http://www.reddit.com/api/submit"
local postData = {
title = title,
sr = subreddit,
kind = isSelf and "self" or "link",
uh = auth.modhash,
[isSelf and "text" or "url"] = content,
api_type = "json"
}
local res, code = util.postRequest(url, postData, auth.cookie)
res = json.decode(res)
util.updateAuth(auth, res)
return res, code
end
function reddit.comment(thing_id, text, auth)
util.validateAuth(auth)
local url = "http://www.reddit.com/api/comment"
local postData = {
thing_id = thing_id,
text = text,
uh = auth.modhash,
api_type = "json"
}
local res, code = util.postRequest(url, postData, auth.cookie)
res = json.decode(res)
util.updateAuth(auth, res)
return res, code
end
return reddit
local ltn12 = require "ltn12"
local sock = require "socket"
local url = require "socket.url"
local http = require "socket.http"
local sockselect = socket.select
local util = {}
util.escape = url.escape
function util.setUseragent(s)
http.USERAGENT = s
end
function util.sleep(sec)
sockselect(nil, nil, sec)
end
function util.urlParamEncode(t)
local escape = util.escape
local _t = {}
table.foreach(t, function(k,v)
_t[#_t+1] = escape(k).."="..escape(v)
end)
return table.concat(_t, "&")
end
function util.validateAuth(auth)
if not type(auth) == "table" then
error "Auth must be a table."
elseif not type(auth.modhash) == "string" then
error "Auth modhash must be a string."
elseif not type(auth.cookie) == "string" then
error "Auth cookie must be a string."
end
end
function util.updateAuth(auth, res)
if auth and res.data and res.data.modhash then
auth.modhash = res.data.modhash
end
end
function util.getRequest(url, cookie)
local res = {}
local reqt = {
url = url,
method = "GET",
sink = ltn12.sink.table(res),
}
if cookie then reqt.headers = {cookie = cookie} end
local _, code, h, s = http.request(reqt)
return table.concat(res), code, h, s
end
function util.postRequest(url, body, cookie)
local res = {}
if type(body) == "table" then
body = util.urlParamEncode(body)
end
local headers = {
["content-length"] = body:len(),
["content-type"] = "application/x-www-form-urlencoded"
}
if cookie then headers.cookie = cookie end
local _, code, h, s = http.request {
url = url,
method = "POST",
headers = headers,
source = ltn12.source.string(body),
sink = ltn12.sink.table(res),
}
return table.concat(res), code, h, s
end
util.setUseragent "StreamLogger-by-TurplePurtle"
return util
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment