Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2013 12:56
Show Gist options
  • Save anonymous/6038921 to your computer and use it in GitHub Desktop.
Save anonymous/6038921 to your computer and use it in GitHub Desktop.
test
import time
import json
from urllib.request import urlopen
import gzip
# site stats collector
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):
# print(json_str)
wfile = json.JSONDecoder()
q1 = wfile.decode(json_str)['items'][0]
for key, val in q1.items():
print(key, val)
# 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)
"""
# tab separated <--- append this line to a TSV
with open("BSE.tsv", 'a') as wa_file:
wa_file.write("\t".join(deets) + "\n")
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment