-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatoverflow.py
executable file
·43 lines (37 loc) · 1.36 KB
/
statoverflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import json
from urllib.request import Request, urlopen
from gzip import GzipFile
from io import BytesIO
from datetime import datetime
import sys
BASE = 'https://api.stackexchange.com/tags/%(tags)s/info?site=%(site)s&pagesize=100'
def fetch_tags(tags, site='stackoverflow'):
if isinstance(tags, str):
tags = [tags]
if len(tags) > 100:
raise ValueError('Give <100 tags')
res = urlopen(Request(BASE % { 'tags': ';'.join(tags), 'site': site },
headers={'Accept-Encoding': 'gzip'}))
if res.info().get('Content-Encoding') == 'gzip':
f = GzipFile(fileobj=BytesIO(res.read()))
else:
f = res
items = {}
for i in json.loads(f.read().decode())['items']:
items[i['name']] = i
return items
def write_counts(data, site='stackoverflow', out=sys.stdout):
date = datetime.today().isoformat()
for t, d in data.items():
out.write('%s,%s,%s,%s\n' % (site, t, date, d['count']))
def exec(conffile=sys.stdin):
conf = json.load(conffile)
with open(conf['outfile'], 'a') as out:
for s in conf['sites']:
try:
data = fetch_tags(conf['tags'], site=s)
write_counts(data, site=s, out=out)
except Exception as e:
print('Error fetching data %r in %s' % (e, s), file=sys.stderr)
exec()