Skip to content

Commit dcef5af

Browse files
committed
GitHub Traffic
1 parent a199038 commit dcef5af

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

Python/Github_Traffic/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Human_Detector
2+
3+
This script, can help one determine all the unique viewers on his/her Repository.
4+
By using `access-token` one can determine the viewers and number of time they viewed the Repo.
5+
Currently data of only 14 days is possible (limitation of GitHib-API), but in future by using the GraphQL queries, this limitation can be answered.
6+
Te script uses PickleDB, the data can be imported in CSV as well, although the functionality hasn't been added till this point of time.
7+
8+
## Note
9+
10+
This script can be converted in a Bot, Action or GitHub app!!!
11+
12+
13+
## Dependency
14+
15+
- githubpy
16+
- PickleDB
17+
18+
These are summarised in `requirement.txt`
19+
20+
21+
## Setup
22+
23+
1. A virtual environment (recommended)
24+
1. `pip install -r requirements.txt`
25+
1. 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+
1. It is recommended to paste this token somewhere, as one cant review it again.
27+
1. Determine the Repository whose traffic you want to view.
28+
1. Run the Script
29+
30+
## Usage
31+
32+
Sample Usage -
33+
34+
`
35+
python github_traffic.py collect -u vybhav72954 -r Music-Mood-Analysis -t *********
36+
`
37+
38+
Output -
39+
40+
`
41+
2020-12-20T00:00:00Z {'uniques': 1, 'count': 1}
42+
2020-12-24T00:00:00Z {'uniques': 2, 'count': 17}
43+
2020-12-25T00:00:00Z {'uniques': 1, 'count': 4}
44+
2020-12-27T00:00:00Z {'uniques': 1, 'count': 1}
45+
2020-12-28T00:00:00Z {'uniques': 1, 'count': 23}
46+
2020-12-29T00:00:00Z {'uniques': 1, 'count': 1}
47+
2020-12-30T00:00:00Z {'uniques': 1, 'count': 3}
48+
2020-12-31T00:00:00Z {'uniques': 1, 'count': 7}
49+
`
50+
Generalized Usage -
51+
52+
- collect (Collect Information for first time in Database)
53+
54+
`
55+
python3 github_traffic.py collect -u [github-user] -r [github-repo] -t [github-access-token]
56+
`
57+
58+
- view (View Information already stored in Database)
59+
60+
`
61+
python3 github_traffic.py view -u [github-user] -r [github-repo] -t [github-access-token]
62+
`
63+
64+
`
65+
2020-12-20T00:00:00Z {"uniques": 1, "count": 1}
66+
2020-12-24T00:00:00Z {"uniques": 2, "count": 17}
67+
2020-12-25T00:00:00Z {"uniques": 1, "count": 4}
68+
2020-12-27T00:00:00Z {"uniques": 1, "count": 1}
69+
2020-12-28T00:00:00Z {"uniques": 1, "count": 23}
70+
2020-12-29T00:00:00Z {"uniques": 1, "count": 1}
71+
2020-12-30T00:00:00Z {"uniques": 1, "count": 3}
72+
2020-12-31T00:00:00Z {"uniques": 1, "count": 7}
73+
8 elements
74+
`
75+
76+
## Disclaimer
77+
78+
- `githubpy` is a 8 year old package and is no longer maintained
79+
- Inspired by [this](https://github.com/seladb/github-traffic-stats/blob/master/README.md)
80+
81+
82+
## Author(s)
83+
84+
Made by [Vybhav Chaturvedi](https://www.linkedin.com/in/vybhav-chaturvedi-0ba82614a/)
85+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import github
2+
import pickledb
3+
import json
4+
import argparse
5+
import sys
6+
7+
# Collect Information for Databse
8+
def collect(user, repo, token, org):
9+
if org is None:
10+
org = user
11+
12+
db = __load_db(repo=repo)
13+
14+
# Connect API using PAT
15+
gh = github.GitHub(access_token=token)
16+
try:
17+
gh.repos(org, repo).get()
18+
except Exception:
19+
sys.exit('No Data')
20+
21+
if user is not None and org != user:
22+
try:
23+
gh.repos(org, repo).collaborators(user).get()
24+
except Exception:
25+
sys.exit('No Data')
26+
27+
views_data = gh.repos(org, repo).traffic.views.get()
28+
found_new_data = False
29+
for view_per_day in views_data['views']:
30+
timestamp = view_per_day['timestamp']
31+
data = {'uniques': view_per_day['uniques'], 'count': view_per_day['count']}
32+
if db.get(timestamp) is None or db.get(timestamp) is False:
33+
db.set(timestamp, json.dumps(data))
34+
print(timestamp, data)
35+
found_new_data = True
36+
else:
37+
db_data = json.loads(db.get(timestamp))
38+
if db_data['uniques'] < data['uniques']:
39+
db.set(timestamp, json.dumps(data))
40+
print(timestamp, data)
41+
found_new_data = True
42+
if not found_new_data:
43+
print('No Data')
44+
db.dump()
45+
46+
47+
def view(repo):
48+
db = __load_db(repo=repo)
49+
timestamps = db.getall()
50+
for ts in sorted(timestamps):
51+
print(ts, db.get(ts))
52+
print(len(timestamps), 'elements')
53+
54+
55+
def __load_db(repo):
56+
return pickledb.load('{repo}_views.db'.format(repo=repo), False)
57+
58+
59+
def main():
60+
parser = argparse.ArgumentParser()
61+
parser.add_argument('action', choices=['collect', 'view'])
62+
parser.add_argument('-u', '--github_user', action='store')
63+
parser.add_argument('-t', '--github_access_token', action='store')
64+
parser.add_argument('-o', '--github_org', action='store')
65+
parser.add_argument('-r', '--github_repo', action='store')
66+
parser.add_argument('-v', '--view', help='view DB content', action='store_true')
67+
68+
args = parser.parse_args()
69+
70+
if args.action == 'view':
71+
if args.github_repo is None:
72+
sys.exit('You need to provide GitHub repo: -r|--github_repo')
73+
view(repo=args.github_repo)
74+
else:
75+
if (args.github_repo is None or
76+
args.github_access_token is None or
77+
(args.github_user is None and args.github_org is None)):
78+
sys.exit('Recheck all the arguments, view README\n')
79+
collect(user=args.github_user, repo=args.github_repo, token=args.github_access_token, org=args.github_org)
80+
81+
82+
if __name__ == "__main__":
83+
main()

Python/Github_Traffic/requirement.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
githubpy==1.1.0
2+
pickleDB==0.9.2
3+
simplejson==3.17.0

0 commit comments

Comments
 (0)