-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetProfilePictures.py
62 lines (44 loc) · 1.75 KB
/
getProfilePictures.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
import os
import json
import httpx
from bs4 import BeautifulSoup
from PIL import Image, ImageDraw, ImageFont
if not os.path.exists("Data/profilePictures"):
os.makedirs("Data/profilePictures/compressed")
ses = httpx.Client(timeout=30, follow_redirects=1)
with open("Data/leaderboard.json", "rb") as f:
leaderboard = json.load(f)
usernames = set()
for x, y in leaderboard.items():
for z in y:
usernames.add(z)
def textsize(text, font):
im = Image.new(mode="P", size=(0, 0))
draw = ImageDraw.Draw(im)
_, _, width, height = draw.textbbox((0, 0), text=text, font=font)
return width, height
for i, user in enumerate(sorted(usernames), 1):
print(f"Getting @{user} | {i}/{len(usernames)}", end="\r")
url = f"https://toph.co/u/{user}"
r = ses.get(url)
soup = BeautifulSoup(r.text, "html.parser")
try:
img = soup.find("img", class_="d-block mw-100 h-auto")["src"]
except TypeError:
print(f"Error: {user}")
img = Image.new("RGB", (450, 450), color=(0, 102, 255))
d = ImageDraw.Draw(img)
fnt = ImageFont.truetype("Data/Fonts/Roboto-Regular.ttf", 200)
text = user[0].upper()
text_width, text_height = textsize(text, fnt)
# Calculate the x and y coordinates
x = (img.width - text_width) / 2
y = (img.height - text_height) / 2
# Draw the text
d.text((x, y), text, font=fnt, fill=(255, 255, 255))
img.save(f"Data/profilePictures/{user}.jpg", "JPEG", quality=100, optimize=False, progressive=False)
continue
with open(f"Data/profilePictures/{user}.jpg", "wb") as f:
f.write(ses.get(img).content)
print()
import compressProfilePictures