3
3
import json
4
4
import argparse
5
5
import sys
6
+ import decouple
6
7
7
8
8
- def collect (user , repo , token , org ):
9
+ def collect (user , repo , org ):
9
10
"""
10
11
Function for Collection of Data.
11
12
Counts the total number of views (Unique and total) on a Repo.
12
13
13
14
Parameters:
14
15
user (str): The Github Username
15
16
repo (str): The Name of Repo
16
- token (str): The Personal Access Token
17
17
org (str): The Name of Organization [Optional]
18
18
"""
19
+
20
+ token = decouple .config ("API_KEY" )
19
21
if org is None :
20
22
org = user
21
23
22
24
db = __load_db (repo = repo )
23
-
24
- # Connect API using PAT
25
25
gh = github .GitHub (access_token = token )
26
26
try :
27
27
gh .repos (org , repo ).get ()
28
28
except Exception :
29
- sys .exit (' No Data' )
29
+ sys .exit (" No Data" )
30
30
31
31
if user is not None and org != user :
32
32
try :
33
33
gh .repos (org , repo ).collaborators (user ).get ()
34
34
except Exception :
35
- sys .exit ('No Data' )
36
-
35
+ sys .exit ("No Data" )
37
36
views_data = gh .repos (org , repo ).traffic .views .get ()
38
37
found_new_data = False
39
- for view_per_day in views_data [' views' ]:
40
- timestamp = view_per_day [' timestamp' ]
41
- data = {' uniques' : view_per_day [' uniques' ], ' count' : view_per_day [' count' ]}
38
+ for view_per_day in views_data [" views" ]:
39
+ timestamp = view_per_day [" timestamp" ]
40
+ data = {" uniques" : view_per_day [" uniques" ], " count" : view_per_day [" count" ]}
42
41
if db .get (timestamp ) is None or db .get (timestamp ) is False :
43
42
db .set (timestamp , json .dumps (data ))
44
43
print (timestamp , data )
45
44
found_new_data = True
46
45
else :
47
46
db_data = json .loads (db .get (timestamp ))
48
- if db_data [' uniques' ] < data [' uniques' ]:
47
+ if db_data [" uniques" ] < data [" uniques" ]:
49
48
db .set (timestamp , json .dumps (data ))
50
49
print (timestamp , data )
51
50
found_new_data = True
52
51
if not found_new_data :
53
- print (' No Data' )
52
+ print (" No new traffic data was found for " + org + "/" + repo )
54
53
db .dump ()
55
54
56
55
@@ -61,11 +60,12 @@ def view(repo):
61
60
Parameters:
62
61
repo (str): Name of the Repo
63
62
"""
63
+
64
64
db = __load_db (repo = repo )
65
65
timestamps = db .getall ()
66
66
for ts in sorted (timestamps ):
67
67
print (ts , db .get (ts ))
68
- print (len (timestamps ), ' elements' )
68
+ print (len (timestamps ), " elements" )
69
69
70
70
71
71
def __load_db (repo ):
@@ -76,30 +76,41 @@ def __load_db(repo):
76
76
repo (str): The name of the Repo
77
77
"""
78
78
79
- return pickledb .load (' {repo}_views.db' .format (repo = repo ), False )
79
+ return pickledb .load (" {repo}_views.db" .format (repo = repo ), False )
80
80
81
81
82
82
def main ():
83
+ """
84
+ Function for Argument Parsing
85
+ :return:
86
+ user : Github username
87
+ repo : Name of Repository
88
+ org : Organization
89
+ """
83
90
parser = argparse .ArgumentParser ()
84
- parser .add_argument ('action' , choices = ['collect' , 'view' ])
85
- parser .add_argument ('-u' , '--github_user' , action = 'store' )
86
- parser .add_argument ('-t' , '--github_access_token' , action = 'store' )
87
- parser .add_argument ('-o' , '--github_org' , action = 'store' )
88
- parser .add_argument ('-r' , '--github_repo' , action = 'store' )
89
- parser .add_argument ('-v' , '--view' , help = 'view DB content' , action = 'store_true' )
91
+ parser .add_argument ("action" , choices = ["collect" , "view" ])
92
+ parser .add_argument ("-u" , "--github_user" , action = "store" )
93
+ parser .add_argument ("-o" , "--github_org" , action = "store" )
94
+ parser .add_argument ("-r" , "--github_repo" , action = "store" )
95
+ parser .add_argument ("-v" , "--view" , help = "view DB content" , action = "store_true" )
90
96
91
97
args = parser .parse_args ()
92
98
93
- if args .action == ' view' :
99
+ if args .action == " view" :
94
100
if args .github_repo is None :
95
- sys .exit (' You need to provide GitHub repo: -r|--github_repo' )
101
+ sys .exit (" You need to provide GitHub repo: -r|--github_repo" )
96
102
view (repo = args .github_repo )
97
103
else :
98
- if (args .github_repo is None or
99
- args .github_access_token is None or
100
- (args .github_user is None and args .github_org is None )):
101
- sys .exit ('Recheck all the arguments, view README\n ' )
102
- collect (user = args .github_user , repo = args .github_repo , token = args .github_access_token , org = args .github_org )
104
+ if args .github_repo is None or (
105
+ args .github_user is None and args .github_org is None
106
+ ):
107
+ sys .exit (
108
+ "Recheck all the arguments, view README\n "
109
+ "Please provide all of the following:\n "
110
+ " GitHub user/org: -u|--github_user AND/OR -o|--github_org\n "
111
+ " GitHub repo: -r|--github_repo\n "
112
+ )
113
+ collect (user = args .github_user , repo = args .github_repo , org = args .github_org )
103
114
104
115
105
116
if __name__ == "__main__" :
0 commit comments