-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-watchers.py
executable file
·106 lines (83 loc) · 2.11 KB
/
get-watchers.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
#!/usr/bin/env python3
import sys
import urllib.request
import json
import csv
import time
PAUSE = 0.1
def main(argv):
owner = argv[0]
repo = argv[1]
token = argv[2]
print('Getting watchers...')
users = get_users(owner, repo, token)
info = []
print('Looking up user info...')
info = get_info(users, token)
if len(info) == 0:
print('No users to lookup...exiting...')
return
csvf = open('watchers.csv', 'w')
csvw = csv.writer(csvf)
header = info[0].keys()
csvw.writerow(header)
for i in info:
csvw.writerow(i.values())
csvf.close()
print("File writen to 'watchers.csv'")
return
def get_users(owner, repo, token):
users = []
reading = True
page = 0
while reading:
url = 'https://api.github.com/repos/'+str(owner)+'/'+str(repo) \
+'/subscribers?page='+str(page)+'&access_token='+str(token)
try:
time.sleep(PAUSE)
resp = urllib.request.urlopen(url)
data = json.loads(resp.read())
if data and len(data) > 0:
for u in data:
if u['login'] not in users:
users.append(u['login'])
print('Finished page '+str(page))
page += 1
else:
reading = False
except:
print('Expception with URL... over rate limit?')
reading = False
print('Users found: '+str(len(users)))
return users
def get_info(users, token):
cnt = 1
info = []
for u in users:
time.sleep(PAUSE)
lookup = get_user_info(u, token)
if lookup:
info.append(lookup)
if cnt % 10 == 0:
print('Looked up '+str(cnt)+'/'+str(len(users))+' users')
cnt += 1
return info
def get_user_info(user, token):
info = {}
url = 'https://api.github.com/users/'+str(user)+'?access_token='+str(token)
try:
resp = urllib.request.urlopen(url)
data = json.loads(resp.read())
if data and len(data) > 0:
return data
else:
return None
except:
print('Expception with URL... over rate limit?')
return None
if __name__ == "__main__":
if len(sys.argv) > 3:
main(sys.argv[1:])
else:
print('Usage:')
print('\t'+sys.argv[0]+' <owner> <repo> <token>')