Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/stats2.py
Last active December 19, 2015 23:59
Show Gist options
  • Save zeffii/6038924 to your computer and use it in GitHub Desktop.
Save zeffii/6038924 to your computer and use it in GitHub Desktop.
reading stats from api.stackexchange uncompressing gzipped response
import time
import json
from urllib.request import urlopen
import gzip
# site stats collector possible add filter &filter=!*LIKa78145bHnc-F
url = "http://api.stackexchange.com/2.1/info?site=blender.stackexchange.com"
b_time = time.time()
def get_html(remote_location):
fetch = urlopen(remote_location)
data = gzip.decompress(fetch.read())
return str(data,'utf-8')
def disect_json(json_str):
wfile = json.JSONDecoder()
q1 = wfile.decode(json_str)['items'][0]
deets = [str(b_time)]
append = deets.append
for key, val in sorted(q1.items()):
append(str(val))
# print(key, end='\t')
with open("BSE2.tsv", 'a') as wa_file:
wa_file.write("\t".join(deets) + "\n")
json_str = get_html(url)
#json_str = """\
#{"items":[{"total_questions":523,"total_unanswered":12,"total_accepted":336,"total_answers":935,"questions_per_minute":0.01,"answers_per_minute":0.01,"total_comments":2821,"total_votes":6622,"total_badges":1849,"badges_per_minute":0.02,"total_users":975,"new_active_users":0,"api_revision":"2013.7.17.7108"}],"quota_remaining":261,"quota_max":300,"has_more":false}"""
disect_json(json_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment