1+ import csv
2+ from time import sleep
3+ from typing import Generator
4+ from github import Github , Repository , GithubException
5+
6+ EMPTY_FIELD = 'Empty field'
7+ TIMEDELTA = 0.05
8+ TIMEZONE = 'Europe/Moscow'
9+ FIELDNAMES = ('repository name' , 'login' , 'name' , 'email' , 'url' , 'permissions' , 'total_commits' , 'id' , 'node_id' , 'type' , 'bio' , 'site_admin' )
10+
11+
12+ def log_contributors_to_csv (info : dict , csv_name : str ):
13+ with open (csv_name , 'a' , newline = '' ) as file :
14+ writer = csv .DictWriter (file , fieldnames = FIELDNAMES )
15+ writer .writerow (info )
16+
17+ def log_contributors_to_stdout (info : dict ):
18+ print (info )
19+
20+ def log_repository_contributors (repository : Repository , csv_name : str ):
21+ contributors_stats = get_contributors_stats (repository )
22+
23+ nvl = lambda val : val or EMPTY_FIELD
24+
25+ for contributor_stat in contributors_stats .values ():
26+ contributor = contributor_stat ["contributor_object" ]
27+ contributor_permissons = repository .get_collaborator_permission (contributor )
28+
29+ info_tmp = {
30+ 'repository name' : repository .full_name ,
31+ 'login' : contributor .login ,
32+ 'name' : nvl (contributor .name ),
33+ 'email' : nvl (contributor_stat ['email' ]),
34+ 'url' : contributor .html_url ,
35+ 'permissions' : nvl (contributor_permissons ),
36+ 'total_commits' : contributor_stat ['total_commits' ],
37+ 'id' : contributor .id ,
38+ 'node_id' : contributor .node_id ,
39+ 'type' : contributor .type ,
40+ 'bio' : nvl (contributor .bio ),
41+ 'site_admin' : contributor .site_admin ,
42+ }
43+
44+ log_contributors_to_csv (info_tmp , csv_name )
45+ log_contributors_to_stdout (info_tmp )
46+
47+ sleep (TIMEDELTA )
48+
49+ def get_contributors_stats (repository : Repository ) -> dict :
50+ contributors_stats = dict ()
51+
52+ for commit in repository .get_commits ():
53+ contributor = commit .author
54+
55+ if not contributor .login in contributors_stats :
56+ contributors_stats [contributor .login ] = {
57+ 'total_commits' : 0 ,
58+ 'email' : commit .commit .author .email ,
59+ 'contributor_object' : contributor
60+ }
61+
62+ contributors_stats [contributor .login ]['total_commits' ] += 1
63+
64+ sleep (TIMEDELTA )
65+
66+ return contributors_stats
67+
68+ def log_contributors (client : Github , working_repos : Generator , csv_name : str , fork_flag : bool ):
69+ with open (csv_name , 'w' , newline = '' ) as file :
70+ writer = csv .writer (file )
71+ writer .writerow (FIELDNAMES )
72+
73+ for repo in working_repos :
74+ try :
75+ print ('=' * 20 , repo .full_name , '=' * 20 )
76+ log_repository_contributors (repo , csv_name )
77+
78+ if fork_flag :
79+ for forked_repo in repo .get_forks ():
80+ print ('=' * 20 , "FORKED:" , forked_repo .full_name , '=' * 20 )
81+ log_repository_contributors (forked_repo , csv_name )
82+ sleep (TIMEDELTA )
83+
84+ except GithubException as e :
85+ print (e )
86+ exit (1 )
0 commit comments