forked from WeiqunZhang/ghtraffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghtraffic.py
executable file
·114 lines (99 loc) · 3.74 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
import os
import sys
import requests
import getpass
import datetime
def ghtraffic():
# f = open('mypwd.txt')
# line = f.readline()
# credentials=line.split(':')
# usr=credentials[0]
# pwd=credentials[1]
## pwd = getpass.getpass()
# usrpwd = (usr,pwd)
f = open('mytoken.txt')
line = f.readline()
tokens=line.split(':')
mytoken=tokens[0]
today=f"{datetime.datetime.now():%Y-%m-%d}"
year=int(today[0:4])
month=int(today[5:7])
if(month>=10):
FY=year+1
else:
FY=year
repos = [{'org':'liuyangzhuan', 'repo':'ButterflyPACK'},
{'org':'pghysels', 'repo':'STRUMPACK'},
{'org':'gptune', 'repo':'GPTune'},
{'org':'xiaoyeli', 'repo':'superlu_dist'},
{'org':'xiaoyeli', 'repo':'superlu'},
{'org':'xiaoyeli', 'repo':'superlu_mt'}]
for repo in repos:
clone_records = {}
# read previously saved record
fname = repo['repo']+"-traffic.org"
fname1 = "FY"+str(FY)+"_"+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 = requests.get(ghurl, headers={'Authorization':mytoken})
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()
f = open(fname1, '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()):
year=int(k[0:4])
month=int(k[5:7])
if((year==FY and month<10) or (year==FY-1 and month>=10)):
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()