-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-activity.py
More file actions
84 lines (74 loc) · 2.8 KB
/
Copy pathgithub-activity.py
File metadata and controls
84 lines (74 loc) · 2.8 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
import sys, requests
def main():
# Handling username not provided
if len(sys.argv) < 2:
print('Error: Username not provided.')
return
user = sys.argv[1]
# URL and headers to comunicate with the API
url_template = f'https://api.github.com/users/{user}/events'
headers = {
'accept':'application/vnd.github+json',
'X-GitHub-Api-Version':'2026-03-10'
}
# GET request for getting the data
response = requests.get(url_template, headers=headers)
# Handling errors in status code
if response.status_code == 404:
print('Error: User not found')
return
elif response.status_code != 200:
print('Unexpected error requesting user activity')
return
elif not response.json():
print('No recent activity found')
return
# Dictionary for storing all the events
events_counter = {
'CommitCommentEvent': {},
'CreateEvent': {},
'DeleteEvent': {},
'DiscussionEvent': {},
'ForkEvent': {},
'GollumEvent': {},
'IssueCommentEvent': {},
'IssuesEvent': {},
'MemberEvent': {},
'PublicEvent': {},
'PullRequestEvent': {},
'PullRequestReviewEvent': {},
'PullRequestReviewCommentEvent': {},
'PushEvent': {},
'ReleaseEvent': {},
'WatchEvent': {}
}
# Storing the events in the dictionary
for event in response.json():
try:
events_counter[event.get('type')][event.get('repo', {}).get('name')] += 1
except KeyError:
events_counter[event.get('type')][event.get('repo', {}).get('name')] = 1
# Writing the activity on the terminal
print(f'\n---------- Recent activity of {user} ----------')
for event in events_counter:
if event == 'CreateEvent' and events_counter[event]:
for repo in events_counter[event]:
print(f'Created {events_counter[event][repo]} branches/tags in {repo}')
elif event == 'ForkEvent' and events_counter[event]:
for repo in events_counter[event]:
print(f'Forked {repo}')
elif event == 'PushEvent' and events_counter[event]:
for repo in events_counter[event]:
print(f'Pushed {events_counter[event][repo]} commits to {repo}')
elif event == 'ReleaseEvent' and events_counter[event]:
for repo in events_counter[event]:
print(f'Released {repo}')
elif event == 'WatchEvent' and events_counter[event]:
for repo in events_counter[event]:
print(f'Starred {repo}')
else:
for repo in events_counter[event]:
print(f'{events_counter[event][repo]} events of type {event} in {repo}')
print()
if __name__ == '__main__':
main()