-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_projects.py
62 lines (52 loc) · 2.16 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
import github
import joblib
import pandas as pd
from common import cleanup_files, connect_github, force_refresh, get_logger, get_path, initialize, tofetch, tokens
initialize()
def fetch_metadata(project):
logger = get_logger(__file__, modules={"urllib3": "ERROR"})
metadata = {"id": project, "project": None, "pulls": None, "stars": None, "archived": None, "fork": None}
token, client = connect_github()
while True:
try:
logger.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,
"archived": repository.archived,
"fork": repository.fork,
}
)
except (github.BadCredentialsException, github.RateLimitExceededException):
token, client = connect_github(token)
except github.UnknownObjectException:
logger.warning(f"{project}: Project does not exist")
break
except Exception as exception:
if isinstance(exception, github.GithubException) and exception.status in [403, 451]:
logger.warning(f"{project}: Project is blocked")
break
logger.error(f"{project}: Failed fetching metadata due to {exception}")
else:
break
connect_github(token, done=True)
return metadata
def export_projects(metadata):
pd.DataFrame(metadata).sort_values(["pulls", "stars"], ascending=False).to_csv(
get_path("projects_fetched"), index=False
)
def main():
if cleanup_files("projects_fetched", force_refresh()):
with joblib.Parallel(n_jobs=len(tokens), prefer="threads", verbose=1) as parallel:
export_projects(parallel(joblib.delayed(fetch_metadata)(project) for project in tofetch()))
else:
print("Skip fetching projects")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Stop fetching projects")
exit(1)