-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_activity.py
More file actions
103 lines (80 loc) · 2.84 KB
/
Copy pathgithub_activity.py
File metadata and controls
103 lines (80 loc) · 2.84 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import requests
import argparse
import os
# Constant values
api = "https://api.github.com/"
token = os.getenv("GITHUB_TOKEN", "error")
if token == "error":
raise Exception("Set GITHUB_TOKEN environment variable.")
def auth():
headers = {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + str(token),
"X-GitHub-Api-Version": "2022-11-28",
}
return headers
def verify_user(user):
"""Verify if the user exists"""
req = requests.get(f"{api}/users/{user}")
if req.status_code == 200:
return True
else:
return False
def fetch_activity(user):
"""Get github's user actitvity"""
req = requests.get(f"{api}users/{user}/events/public", headers=auth())
if req.status_code != 200:
print(f"Error fetching activity: {req.status_code}")
return []
return req.json()
def preprocess_events(activity):
"""
Preprocess activity data to categorize events by repository and event type.
This reduces redundant iterations during analysis.
"""
processed_data = {}
for event in activity:
repo_name = event["repo"]["name"]
event_type = event["type"]
if repo_name not in processed_data:
processed_data[repo_name] = {}
if event_type not in processed_data[repo_name]:
processed_data[repo_name][event_type] = 0
processed_data[repo_name][event_type] += 1
return processed_data
def get_event_count(preprocessed_data, repo, event_type):
"""
Get the count of a specific event type for a given repository from preprocessed data.
"""
if repo in preprocessed_data and event_type in preprocessed_data[repo]:
return preprocessed_data[repo][event_type]
return 0
def get_repo_names(events):
"""Get all repos' name and event type fetched and delete duplications."""
repos = {repo["repo"]["name"] for repo in events}
return repos
def main():
# User Parser
parser = argparse.ArgumentParser()
parser.add_argument("user", type=str, help="User to get activity from.")
parser.add_argument("--debug", action="store_true", help="Print all events")
# Parse argument
args = parser.parse_args()
# Verify User
user = verify_user(args.user)
if not user:
print(f"There's no such user {args.user}")
else:
# Get activity
activity = fetch_activity(args.user)
if args.debug:
for event in activity:
print("++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(event)
print("----------------------------------------------------")
# Preprocess the activity data
preprocessed_data = preprocess_events(activity)
for repo, events in preprocessed_data.copy().items():
print(repo, events)
if __name__ == "__main__":
main()