-
Notifications
You must be signed in to change notification settings - Fork 3
/
ghtraffic.py
executable file
·75 lines (65 loc) · 2.5 KB
/
ghtraffic.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import os
import sys
import requests
import getpass
def ghtraffic():
pwd = getpass.getpass()
usrpwd = ('WeiqunZhang',pwd)
repos = [{'org':'AMReX-Codes', 'repo':'amrex'},
{'org':'AMReX-Astro', 'repo':'Castro'},
{'org':'AMReX-Astro', 'repo':'Maestro'},
{'org':'AMReX-Astro', 'repo':'Nyx'},
{'org':'BoxLib-Codes', 'repo':'BoxLib'},
{'org':'ECP-WarpX', 'repo':'WarpX'},
{'org':'AMReX-FHD', 'repo':'FHDeX'}]
for repo in repos:
clone_records = {}
# read previously saved record
fname = repo['repo']+"-traffic.org"
if os.path.isfile(fname):
f = open(fname, 'r')
for i in range(3): # skip first three lines
f.readline()
for line in f.readlines():
words = line[:-1].split('|')
if len(words) < 4:
break
ts = words[1].strip()
ct = int(words[2])
uq = int(words[3])
if len(ts) == 10 and ts[0:2] == '20' and ts[4] == '-':
clone_records[ts] = (ct,uq)
else:
break
f.close()
ghurl = 'https://api.github.com/repos/'+repo['org']+'/'+repo['repo']+'/traffic/clones'
response = requests.get(ghurl, auth=usrpwd)
response = response.json()
if response.get('message'):
print(ghurl+": " + response['message'])
else:
clones = response['clones']
for lst in clones:
k = lst['timestamp'][0:10]
c = lst['count']
u = lst['uniques']
if k in clone_records:
c = max(c, clone_records[k][0])
u = max(u, clone_records[k][1])
clone_records[k] = (c,u)
f = open(fname, 'w')
f.write('* '+repo['repo']+' clones\n')
f.write('| Time | Count | Uniques |\n')
f.write('|------------+---------+---------|\n')
totcnt = 0
totunq = 0
for k, v in sorted(clone_records.items()):
f.write('| {0:10s} | {1:7d} | {2:7d} |\n'.format(k,v[0],v[1]))
totcnt += v[0]
totunq += v[1]
f.write('|------------+---------+---------|\n')
f.write('| {0:10s} | {1:7d} | {2:7d} |\n'.format('Total',totcnt,totunq))
f.close()
if __name__ == "__main__":
ghtraffic()