Last active
June 26, 2016 08:38
-
-
Save daantjie/0421d3ad4c0f21ee35e1fc389d1090d9 to your computer and use it in GitHub Desktop.
The code for a tex.se answer. (http://tex.stackexchange.com/questions/316606/placing-your-live-reputation-in-a-document)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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