-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
192 lines (163 loc) · 8.13 KB
/
API.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import requests #type: ignore
from typing import Dict, Any, Union
import pandas as pd #type: ignore
import plotly.express as px #type: ignore
import plotly.io as pio #type: ignore
from datetime import datetime
import pytz #type: ignore
class API :
def __init__(self, username : str, token : str = None) -> None:
self.username = username
self.token = token
self.headers = { "Accept" : "application/vnd.github.v3+json"}
if token and len(token) == 40 :
self.headers["Authorization"] = f"token {self.token}"
print(self.headers)
def fetchUserDict(self) -> Union[Dict[str, Any], List[Union[int, str]], None]:
url = f"https://api.github.com/users/{self.username}"
response = requests.get(url, self.headers)
if response.status_code == 200:
user_dict = response.json()
return user_dict
elif response.status_code == 403:
if 'X-RateLimit-Remaining' in response.headers and response.headers['X-RateLimit-Remaining'] == '0':
reset_time = int(response.headers['X-RateLimit-Reset'])
reset_time_human = datetime.fromtimestamp(reset_time, tz=pytz.timezone('Europe/Bucharest')).strftime('%Y-%m-%d %H:%M:%S')
return [-1, reset_time_human]
else :
return None
def fetchReposDict(self) -> Union[List[Dict[str, Any]], List[Union[int, str]], None]:
url = f"https://api.github.com/users/{self.username}/repos"
response = requests.get(url, self.headers)
if response.status_code == 200 :
repos_dict = response.json()
return sorted(repos_dict, key=lambda repo : repo['created_at'])
elif response.status_code == 403:
if 'X-RateLimit-Remaining' in response.headers and response.headers['X-RateLimit-Remaining'] == '0':
reset_time = int(response.headers['X-RateLimit-Reset'])
reset_time_human = datetime.fromtimestamp(reset_time, tz=pytz.timezone('Europe/Bucharest')).strftime('%Y-%m-%d %H:%M:%S')
return [-1, reset_time_human]
else :
return None
def fetchEvents(self) -> Union[List[Dict[str, Any]], List[Union[int, str]], None]:
url = f"https://api.github.com/users/{self.username}/events"
events = []
while url:
response = requests.get(url, headers=self.headers)
if response.status_code == 200:
events.extend(response.json())
url = response.links.get('next', {}).get('url')
elif response.status_code == 403:
if 'X-RateLimit-Remaining' in response.headers and response.headers['X-RateLimit-Remaining'] == '0':
reset_time = int(response.headers['X-RateLimit-Reset'])
reset_time_human = datetime.fromtimestamp(reset_time, tz=pytz.timezone('Europe/Bucharest')).strftime('%Y-%m-%d %H:%M:%S')
return [-1, reset_time_human]
else:
return None
return events
def fetchCommits(self, owner: str, repo: str) -> Union[List[Dict[str, Any]], List[Union[int, str]], None]:
commits = []
url = f"https://api.github.com/repos/{owner}/{repo}/commits?author={self.username}"
response = requests.get(url, headers=self.headers)
if response.status_code == 200 :
commits.extend(response.json())
return commits
elif response.status_code == 403:
if 'X-RateLimit-Remaining' in response.headers and response.headers['X-RateLimit-Remaining'] == '0':
reset_time = int(response.headers['X-RateLimit-Reset'])
reset_time_human = datetime.fromtimestamp(reset_time, tz=pytz.timezone('Europe/Bucharest')).strftime('%Y-%m-%d %H:%M:%S')
return [-1, reset_time_human]
else :
return None
def makeStarPlot(self, repos_dict: List[Dict[str, Any]]) -> str:
repo_links, stars, hover_texts = [], [], []
for rd in repos_dict :
repo_name = rd['name']
repo_url = rd['html_url']
repo_link = f"<a href = '{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)
stars.append(rd['stargazers_count'])
owner = rd['owner']['login']
description = rd['description']
hover_text = f"{owner} <br />{description}"
hover_texts.append(hover_text)
title = f"{self.username}'s Repositories Star Count"
labels = { 'x' : 'Repository Name' , 'y' : 'Stars'}
fig = px.bar(x = repo_links, y = stars, title = title, labels = labels, hover_name = hover_texts)
fig.update_layout(title_font_size = 20, xaxis_title_font_size = 18, yaxis_title_font_size = 18, )
fig.update_traces(marker_color = 'SteelBlue', marker_opacity = 0.6)
graph_html = pio.to_html(fig, full_html=False)
return graph_html
def getTotalStars(self, repos_dict: List[Dict[str, Any]]) -> int:
stars = 0
for rd in repos_dict :
stars += rd['stargazers_count']
return stars
def makeLanguageDistributionPlot(self, repos_dict: List[Dict[str, Any]]) -> str:
df = pd.DataFrame(repos_dict)
df['language'] = df['language'].fillna('Unknown')
title = "Repository Language Distribution"
fig = px.pie(df, names='language', values='size', title=title)
return pio.to_html(fig, full_html=False)
def extractContributions(self, events: List[Dict[str, Any]]) -> List[Dict[str, str]]:
contributions = []
for event in events:
event_date = event['created_at'].split('T')[0]
contributions.append(dict(Task=f"{event['type']}", Date=f"{event_date}"))
return contributions
def makeActivityMap(self, contributions: List[Dict[str, str]]) -> str:
df = pd.DataFrame(contributions)
df['Date'] = pd.to_datetime(df['Date'])
fig = px.scatter(
df,
x="Date",
y="Task",
color="Task",
title="Scatter Plot of Activities Over Time",
labels={"Task": "Activity Type", "Date": "Date"}
)
fig.update_layout(
xaxis_title='Date',
yaxis_title='Activity Type',
xaxis=dict(
tickformat="%Y-%m-%d",
tickangle=45
),
yaxis=dict(
categoryorder="total ascending"
),
showlegend=False
)
return pio.to_html(fig, full_html=False)
def getFirst100StarRepo(self, repos_dict: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
for rd in repos_dict :
if rd['stargazers_count'] >= 100 :
return rd
return None
def getFirstForkRepo(self, repos_dict: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
for rd in repos_dict :
if rd.get('fork') is True :
return rd
return None
def getFirstCollaboration(self, repos_dict: List[Dict[str, Any]]) -> Optional[List[Dict[str, Any]]]:
for rd in repos_dict:
pr_url = f"https://api.github.com/repos/{self.username}/{rd['name']}/pulls?state=all&sort=created&direction=asc"
pr_response = requests.get(pr_url, headers=self.headers)
if pr_response.status_code == 200:
pull_requests = pr_response.json()
for pr in pull_requests:
if pr['user']['login'] != self.username:
return [rd, pr]
return None
def getEmployment(self, user_dict: Dict[str, Any]) -> Optional[str]:
if user_dict['company'] != 'Not specified' :
return user_dict['company']
return None
def getCommitNumber(self, repos_dict: List[Dict[str, Any]]) -> int:
total_commits = 0
for rd in repos_dict :
owner = rd['owner']['login']
repo_name = rd['name']
commits = self.fetchCommits(owner, repo_name)
total_commits += len(commits)
return total_commits