-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_git.py
75 lines (63 loc) · 2.1 KB
/
get_git.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
import urllib
from urllib.request import Request, urlopen
import json
import os
import sys
__all__ = ["get_all_stars"]
def get_all_repos(minstars=3, maxpages=10):
params = []
for user in ["rodluger", "showyourwork"]:
for page in range(1, maxpages + 1):
req = Request(
"https://api.github.com/users/%s/repos?page=%d&per_page=100"
% (user, page)
)
req.add_header("Accept", "application/vnd.github.v3.star+json")
API_KEY = os.getenv("GH_API_KEY", None)
if API_KEY is not None:
req.add_header("Authorization", "token %s" % API_KEY)
content = urlopen(req).read()
par = json.loads(content)
if len(par) == 0:
break
else:
params += par
repos = []
for param in params:
if int(param["stargazers_count"]) > minstars:
repos.append(param["name"])
return repos
def get_repo_stars(repo, maxpages=10):
params = []
for page in range(1, maxpages + 1):
req = Request(
"https://api.github.com/repos/rodluger/%s/stargazers?page=%d&per_page=100"
% (repo, page)
)
req.add_header("Accept", "application/vnd.github.v3.star+json")
API_KEY = os.getenv("GH_API_KEY", None)
if API_KEY is not None:
req.add_header("Authorization", "token %s" % API_KEY)
content = urlopen(req).read()
par = json.loads(content)
if len(par) == 0:
break
else:
params += par
return params
def get_all_stars(clobber=False):
if clobber or not os.path.exists("stars.json"):
stars = []
repos = get_all_repos()
for repo in repos:
stars += get_repo_stars(repo)
with open("stars.json", "w") as json_file:
json.dump(stars, json_file)
else:
print("Using cached stars.")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--clobber":
clobber = True
else:
clobber = False
get_all_stars(clobber=clobber)