Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (208 loc) · 6.35 KB
/
Copy pathmain.py
File metadata and controls
261 lines (208 loc) · 6.35 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
GitHub Profile Viewer
Python backend + Eel GUI
Install:
python -m pip install -r requirements.txt
Run:
python main.py
"""
import base64
import io
from pathlib import Path
import eel
import requests
from github import Auth, Github
from github.GithubException import (
BadCredentialsException,
GithubException,
RateLimitExceededException,
UnknownObjectException,
)
from PIL import Image, ImageDraw
# ---------------------------------------------------------
# Paths
# ---------------------------------------------------------
APP_DIR = Path(__file__).resolve().parent
WEB_DIR = APP_DIR / "web"
# ---------------------------------------------------------
# Helpers
# ---------------------------------------------------------
def display(value, default="-"):
"""Return a display-friendly string."""
if value is None or str(value).strip() == "":
return default
return str(value)
def circular_avatar_data_uri(raw: bytes, size=160):
"""
Convert avatar bytes into a circular PNG data URI.
The returned string can be assigned directly to an
HTML image element's src attribute.
"""
image = Image.open(io.BytesIO(raw)).convert("RGBA")
image = image.resize(
(size, size),
Image.Resampling.LANCZOS,
)
mask = Image.new(
"L",
(size * 4, size * 4),
0,
)
ImageDraw.Draw(mask).ellipse(
(0, 0, size * 4, size * 4),
fill=255,
)
mask = mask.resize(
(size, size),
Image.Resampling.LANCZOS,
)
image.putalpha(mask)
output = io.BytesIO()
image.save(output, format="PNG")
encoded = base64.b64encode(
output.getvalue()
).decode("ascii")
return f"data:image/png;base64,{encoded}"
# ---------------------------------------------------------
# GitHub API
# ---------------------------------------------------------
@eel.expose
def get_profile(username, token=""):
"""
Fetch a GitHub user's profile and recent repositories.
Returns a dictionary that JavaScript can consume.
"""
username = (username or "").strip()
token = (token or "").strip()
if not username:
return {
"ok": False,
"error": "Please enter a username.",
}
try:
# Authenticate only if a token was provided.
if token:
gh = Github(auth=Auth.Token(token))
else:
gh = Github()
user = gh.get_user(username)
# Profile information.
data = {
"ok": True,
"name": display(user.name, user.login),
"login": user.login,
"bio": display(
user.bio,
"No bio provided.",
),
"company": display(user.company),
"location": display(user.location),
"blog": display(user.blog),
"email": display(user.email),
"joined": user.created_at.strftime(
"%B %d, %Y"
).replace(" 0", " "),
"repos": user.public_repos,
"gists": user.public_gists,
"followers": user.followers,
"following": user.following,
"html_url": user.html_url,
"avatar": None,
"repo_list": [],
}
# Avatar is optional. Profile loading should not
# fail if the image cannot be downloaded.
try:
response = requests.get(
user.avatar_url,
params={"s": 320},
timeout=10,
)
response.raise_for_status()
data["avatar"] = circular_avatar_data_uri(
response.content
)
except Exception:
pass
# Fetch up to eight recently updated repositories.
# Repository errors should not discard profile data.
try:
for repo in user.get_repos(
sort="updated"
)[:8]:
updated = (
repo.updated_at.strftime("%Y-%m-%d")
if repo.updated_at
else "-"
)
data["repo_list"].append({
"name": repo.name,
"description": display(
repo.description,
"No description",
),
"language": display(
repo.language,
"Other",
),
"stars": repo.stargazers_count,
"forks": repo.forks_count,
"updated": updated,
"url": repo.html_url,
})
except Exception:
pass
return data
except UnknownObjectException:
return {
"ok": False,
"error": (
f"User '{username}' was not found."
),
}
except BadCredentialsException:
return {
"ok": False,
"error": (
"The token was rejected. "
"Check it or clear the field."
),
}
except RateLimitExceededException:
return {
"ok": False,
"error": (
"Rate limit exceeded. "
"Wait a while or add a token."
),
}
except GithubException as exc:
if exc.status == 403:
message = (
"Access denied or rate limited. "
"Wait or add a token."
)
else:
message = (
f"GitHub request failed: "
f"{exc.data or exc}"
)
return {
"ok": False,
"error": message,
}
except Exception as exc:
return {
"ok": False,
"error": f"Request failed: {exc}",
}
# ---------------------------------------------------------
# Application entry point
# ---------------------------------------------------------
if __name__ == "__main__":
eel.init(str(WEB_DIR))
eel.start(
"index.html",
size=(1040, 850),
port=0,
)