Skip to content

Commit fb462f1

Browse files
committed
Added an Environment Var for PAT
1 parent fccee61 commit fb462f1

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

Python/Github_Traffic/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
API_KEY="XXXXXXXXXXXXX"
2+
3+
// Here you have to save your own Private Access Token which will be used in Main code to fetch the data

Python/Github_Traffic/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This script can be converted in a Bot, Action or GitHub app!!!
1414

1515
- githubpy
1616
- PickleDB
17+
- python-decouple
1718

1819
These are summarised in `requirement.txt`
1920

@@ -22,8 +23,9 @@ These are summarised in `requirement.txt`
2223

2324
- A virtual environment (recommended)
2425
- `pip install -r requirements.txt`
25-
- Generate your own access token from [here](https://github.com/settings/tokens) (If you already have one with `repo` rights, it can be used as well)
26-
- It is recommended to paste this token somewhere, as one cant review it again.
26+
- Generate your own access token from [here](https://github.com/settings/tokens)
27+
- Paste the token in a `.env` file (Take [`.env.example`](.env.example) as an example)
28+
- It is recommended to paste this token somewhere, as one can't review it again.
2729
- Determine the Repository whose traffic you want to view.
2830
- Run the Script
2931

@@ -32,7 +34,7 @@ These are summarised in `requirement.txt`
3234
Sample Usage -
3335

3436
`
35-
python github_traffic.py collect -u vybhav72954 -r Music-Mood-Analysis -t *********
37+
python github_traffic.py collect -u vybhav72954 -r Music-Mood-Analysis
3638
`
3739

3840
Output -
@@ -52,13 +54,13 @@ Generalized Usage -
5254
- collect (Collect Information for first time in Database)
5355

5456
`
55-
python3 github_traffic.py collect -u [github-user] -r [github-repo] -t [github-access-token]
57+
python3 github_traffic.py collect -u [github-user] -r [github-repo]
5658
`
5759

5860
- view (View Information already stored in Database)
5961

6062
`
61-
python3 github_traffic.py view -u [github-user] -r [github-repo] -t [github-access-token]
63+
python3 github_traffic.py view -u [github-user] -r [github-repo]
6264
`
6365

6466
`

Python/Github_Traffic/github_traffic.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,53 @@
33
import json
44
import argparse
55
import sys
6+
import decouple
67

78

8-
def collect(user, repo, token, org):
9+
def collect(user, repo, org):
910
"""
1011
Function for Collection of Data.
1112
Counts the total number of views (Unique and total) on a Repo.
1213
1314
Parameters:
1415
user (str): The Github Username
1516
repo (str): The Name of Repo
16-
token (str): The Personal Access Token
1717
org (str): The Name of Organization [Optional]
1818
"""
19+
20+
token = decouple.config("API_KEY")
1921
if org is None:
2022
org = user
2123

2224
db = __load_db(repo=repo)
23-
24-
# Connect API using PAT
2525
gh = github.GitHub(access_token=token)
2626
try:
2727
gh.repos(org, repo).get()
2828
except Exception:
29-
sys.exit('No Data')
29+
sys.exit("No Data")
3030

3131
if user is not None and org != user:
3232
try:
3333
gh.repos(org, repo).collaborators(user).get()
3434
except Exception:
35-
sys.exit('No Data')
36-
35+
sys.exit("No Data")
3736
views_data = gh.repos(org, repo).traffic.views.get()
3837
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"]}
4241
if db.get(timestamp) is None or db.get(timestamp) is False:
4342
db.set(timestamp, json.dumps(data))
4443
print(timestamp, data)
4544
found_new_data = True
4645
else:
4746
db_data = json.loads(db.get(timestamp))
48-
if db_data['uniques'] < data['uniques']:
47+
if db_data["uniques"] < data["uniques"]:
4948
db.set(timestamp, json.dumps(data))
5049
print(timestamp, data)
5150
found_new_data = True
5251
if not found_new_data:
53-
print('No Data')
52+
print("No new traffic data was found for " + org + "/" + repo)
5453
db.dump()
5554

5655

@@ -61,11 +60,12 @@ def view(repo):
6160
Parameters:
6261
repo (str): Name of the Repo
6362
"""
63+
6464
db = __load_db(repo=repo)
6565
timestamps = db.getall()
6666
for ts in sorted(timestamps):
6767
print(ts, db.get(ts))
68-
print(len(timestamps), 'elements')
68+
print(len(timestamps), "elements")
6969

7070

7171
def __load_db(repo):
@@ -76,30 +76,41 @@ def __load_db(repo):
7676
repo (str): The name of the Repo
7777
"""
7878

79-
return pickledb.load('{repo}_views.db'.format(repo=repo), False)
79+
return pickledb.load("{repo}_views.db".format(repo=repo), False)
8080

8181

8282
def main():
83+
"""
84+
Function for Argument Parsing
85+
:return:
86+
user : Github username
87+
repo : Name of Repository
88+
org : Organization
89+
"""
8390
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")
9096

9197
args = parser.parse_args()
9298

93-
if args.action == 'view':
99+
if args.action == "view":
94100
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")
96102
view(repo=args.github_repo)
97103
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)
103114

104115

105116
if __name__ == "__main__":

Python/Github_Traffic/requirement.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
githubpy==1.1.0
22
pickleDB==0.9.2
3-
simplejson==3.17.0
3+
simplejson==3.17.0
4+
python-decouple==3.4

0 commit comments

Comments
 (0)