Skip to content

Instantly share code, notes, and snippets.

@randrews
Created October 25, 2019 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrews/fee5df474eafd6d4abfd382ea6c62239 to your computer and use it in GitHub Desktop.
Save randrews/fee5df474eafd6d4abfd382ea6c62239 to your computer and use it in GitHub Desktop.
ARGV = { ... }
json = require('json')
version = 0
user_agent = string.format("'lua:save-scraper:v%s (by /u/randrews)'", version)
username = ARGV[1]
password = ARGV[2]
twofactor_code = ARGV[3]
app_id = 'blah'
secret = 'seeecret'
function get_token()
local pwd_with_auth = password
if twofactor_code then
pwd_with_auth = string.format('%s:%s', password, twofactor_code)
end
local args = {
'curl',
'-s',
string.format('-A %s', user_agent),
"-d 'grant_type=password'",
string.format("-d 'username=%s'", username),
string.format("-d 'password=%s'", pwd_with_auth),
string.format("--user '%s:%s'", app_id, secret),
'https://www.reddit.com/api/v1/access_token',
}
local handle = io.popen(table.concat(args, ' '))
local response = handle:read('*a')
handle:close()
return json.decode(response).access_token
end
auth_header = string.format("'Authorization: bearer %s'", get_token())
function request(path)
local endpoint = 'https://oauth.reddit.com'
local args = {
'curl',
string.format('-H %s', auth_header),
string.format('-A %s', user_agent),
'-s',
string.format("'%s%s'", endpoint, path)
}
local handle = io.popen(table.concat(args, ' '))
local response = handle:read('*a')
handle:close()
return json.decode(response)
end
function get_all(path)
local all = {}
local page = request(string.format('%s?limit=100', path))
while true do
for _, item in ipairs(page.data.children) do
table.insert(all, item)
end
if not page.data.after then break end
page = request(string.format('%s?limit=100&after=%s', path, page.data.after))
end
return all
end
all_saved = get_all(string.format('/user/%s/saved', username))
print(json.encode(all_saved))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment