-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_projects.py
75 lines (63 loc) · 2.41 KB
/
fetch_projects.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
from github import BadCredentialsException, RateLimitExceededException, UnknownObjectException
from joblib import Parallel, delayed
from pandas import DataFrame
from common import TOKENS, cleanup, github, initialize, logger, paths, refresh
initialize()
log = logger(__file__, modules={"urllib3": "ERROR"})
def fetch_projects():
token, client = github()
while True:
try:
log.info("Fetching list of projects")
projects = [
project.full_name.lower() for project in client.search_repositories("stars:>15000", sort="stars")
]
except (BadCredentialsException, RateLimitExceededException):
token, client = github(token)
except Exception as exception:
log.error(f"Failed fetching list of projects due to {exception}")
else:
break
github(token, done=True)
return projects
def fetch_metadata(project):
metadata = {"project": project, "pulls": None, "stars": None}
token, client = github()
while True:
try:
log.info(f"{project}: Fetching metadata")
repository = client.get_repo(project)
metadata.update(
{
"project": repository.full_name.lower(),
"pulls": repository.get_pulls(state="all").totalCount,
"stars": repository.watchers,
}
)
except (BadCredentialsException, RateLimitExceededException):
token, client = github(token)
except UnknownObjectException:
log.warning(f"{project}: Project does not exist")
break
except Exception as exception:
log.error(f"{project}: Failed fetching metadata due to {exception}")
else:
break
github(token, done=True)
return metadata
def export_projects(metadata):
DataFrame(metadata).sort_values(["pulls", "stars"], ascending=False).drop_duplicates("project").to_csv(
paths("projects"), index=False
)
def main():
if cleanup("projects", refresh()):
with Parallel(n_jobs=len(TOKENS), prefer="threads") as parallel:
export_projects(parallel(delayed(fetch_metadata)(project) for project in fetch_projects()))
else:
print("Skip fetching projects")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stop fetching projects")
exit(1)