Skip to content

Instantly share code, notes, and snippets.

@daantjie
Last active June 26, 2016 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daantjie/0421d3ad4c0f21ee35e1fc389d1090d9 to your computer and use it in GitHub Desktop.
Save daantjie/0421d3ad4c0f21ee35e1fc389d1090d9 to your computer and use it in GitHub Desktop.
UPDATING = true
USER = 'digitalis'
USERID = 5477562
cache = {}
cache_name = function (user, site)
return ('cache/' .. user .. '_' .. site)
end
curl_user = function (user, site)
os.execute('mkdir -p cache')
print('\nGetting ' .. user .. ' from ' .. site .. '...\n')
os.execute('curl "http://api.stackexchange.com/2.2/users?inname='
.. user .. '&site=' .. site
.. '" > ' .. cache_name(user, site) .. '.gz')
os.execute('gunzip -f ' .. cache_name(user, site) .. '.gz')
end
extract_user = function (user, userid, site)
if UPDATING then
curl_user(user, site)
end
io.input(cache_name(user, site))
raw = io.read('*all')
me = string.match(raw, userid .. '.*')
io.input():close()
return me
end
get_user = function (user, userid, site)
if cache[user] == nil then
cache[user] = {}
end
if cache[user][site] == nil then
cache[user][site] = {}
cache[user][site]['_raw'] = extract_user(user, userid, site)
end
return cache[user][site]['_raw']
end
get_field = function (field, user, userid, site)
if not (cache[user]
and cache[user][site]
and cache[user][site][field]) then
me = get_user(user, userid, site)
pure = string.match(me, '"' .. field .. '":[^,}]+')
cache[user][site][field] = string.match(pure, '[^:]+$')
end
return cache[user][site][field]
end
get_rep = function (site)
return get_field('reputation', USER, USERID, site)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment