-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnilistSD.py
195 lines (170 loc) · 5.58 KB
/
AnilistSD.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
192
193
194
195
import requests
from PIL import Image, ImageDraw, ImageFont
from pystyle import Colorate, Colors
# Define color palette for the visual output
colors = {
"background": (28, 28, 34),
"foreground": (255, 255, 255),
"pink": (255, 105, 180),
"blue": (70, 130, 180),
}
def fetch_genre_data(username):
# Query the AniList API to retrieve the user's watched anime genres
query = """
query ($username: String) {
MediaListCollection(userName: $username, type: ANIME) {
lists {
entries {
media {
id
genres
}
}
}
}
}
"""
response = requests.post(
"https://graphql.anilist.co",
json={"query": query, "variables": {"username": username}},
)
if response.status_code != 200:
print(
Colorate.Diagonal(
Colors.red_to_white, f"★ Error fetching data: {response.status_code}"
)
)
return None
return response.json()
def calculate_simp_percentage(genre_data):
# Define the set of genres considered "simp"
simp_genres = {
"shoujo",
"romance",
"slice of life",
"harem",
"reverse harem",
"drama",
"josei",
"school",
"hentai",
}
# Flatten all anime entries across lists and count total
items = [
entry
for list_ in genre_data["data"]["MediaListCollection"]["lists"]
for entry in list_["entries"]
]
total_anime = len(items)
simp_anime_set = set() # Track unique simp anime by ID
simp_genre_counts = {genre: 0 for genre in simp_genres}
# Check each anime's genres for "simp" ones
for item in items:
for genre in item["media"]["genres"]:
genre_lower = genre.lower()
if genre_lower in simp_genres:
simp_anime_set.add(item["media"]["id"])
simp_genre_counts[genre_lower] += 1
simp_anime_count = len(simp_anime_set)
percentage = (simp_anime_count / total_anime * 100) if total_anime > 0 else 0
return percentage, simp_genre_counts, total_anime, simp_anime_count
def create_stat_image(
username, percentage, simp_genre_counts, total_anime, simp_anime_count
):
# Set image size and create the canvas
img_size = (580, 440)
img = Image.new("RGBA", img_size, colors["background"])
draw = ImageDraw.Draw(img)
# Load fonts for text (fall back to default if unavailable)
try:
font_large = ImageFont.truetype("DejaVuSans-Bold.ttf", 24)
font_medium = ImageFont.truetype("DejaVuSans-Bold.ttf", 18)
except IOError:
font_large = ImageFont.load_default()
font_medium = ImageFont.load_default()
# Draw the stats header
draw.text(
(20, 20), f"★ {username}'s simp stats ★", fill=colors["pink"], font=font_large
)
# Display simp stats
draw.text(
(20, 60),
f"• They watched {percentage:.2f}% simp anime",
fill=colors["foreground"],
font=font_medium,
)
draw.text(
(20, 100),
f"• Total anime watched: {total_anime}",
fill=colors["foreground"],
font=font_medium,
)
draw.text(
(20, 140),
f"• Simp anime count: {simp_anime_count}",
fill=colors["foreground"],
font=font_medium,
)
# List genres and their simp counts
draw.text(
(20, 180), "• Simp anime by genre:", fill=colors["blue"], font=font_medium
)
y_offset = 220
for genre, count in simp_genre_counts.items():
draw.text(
(40, y_offset),
f"- {genre.capitalize()}: {count}",
fill=colors["pink"],
font=font_medium,
)
y_offset += 20
# Add a watermark to the image
draw.text(
(20, img_size[1] - 30),
"@planetwiide - github",
fill=colors["foreground"],
font=font_medium,
)
# Save the image with the username in the filename
img.save(f"{username}_simp_stats.png")
def simp_detector(username):
# Fetch user's anime genre data and calculate simp-related stats
genre_data = fetch_genre_data(username)
if genre_data:
(
percentage,
simp_genre_counts,
total_anime,
simp_anime_count,
) = calculate_simp_percentage(genre_data)
print(
Colorate.Diagonal(
Colors.red_to_white,
f"★ {username} has {percentage:.2f}% 'simp' anime in their watch list.",
)
)
print(
Colorate.Diagonal(Colors.red_to_white, "★ Number of 'simp' anime by genre:")
)
for genre, count in simp_genre_counts.items():
print(
Colorate.Diagonal(
Colors.red_to_white, f" - {genre.capitalize()}: {count}"
)
)
print(
Colorate.Diagonal(
Colors.red_to_white,
f"★ Out of {total_anime} animes, {simp_anime_count} are categorized as 'simp'.",
)
)
# Generate an image with the simp stats
create_stat_image(
username, percentage, simp_genre_counts, total_anime, simp_anime_count
)
if __name__ == "__main__":
username = input(
Colorate.Diagonal(Colors.red_to_white, "★ Enter AniList username: ")
)
simp_detector(username)
input()