This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggester.py
302 lines (243 loc) · 9.53 KB
/
suggester.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""MALSuggest/suggester
=================================================
suggester.py contains all functions used to
parse, search and suggest a new animanga
"""
import settings as s
from time import sleep
import math
import os
"""
loadMAL function
=================================================
Accesses user's animanga list and saves to file
"""
def loadMAL():
pass
"""
load function
=================================================
Accesses user's animanga information and saves to list
"""
def load():
print('Started Loading...')
os.listdir()
animangalist = []
page = 1
"""Loops through all pages of completed entries in decreasing order by score and stores them"""
while True:
# Returns and stores completed animanga list as python dictionary
amlist = s.jikan.user(s.user, s.am + 'list', 'completed', page=page,
parameters={'sort': 'desc', 'order_by': 'score'})
# Checks for last page
if len(amlist[s.am]) == 0:
break
# Appends each entry to list
for am in amlist[s.am]:
animangalist.append(am)
page += 1
sleep(2)
"""Saves all completed entries to file"""
with open('data/completedList', 'w', encoding='utf-8') as file:
for amlist in animangalist:
file.write('{},{}\n'.format(amlist['mal_id'], amlist['score']))
animangalist = []
page = 1
"""Loops through all pages of planned entries and stores them"""
while True:
# Returns and stores planned animanga list as python dictionary
amlist = s.jikan.user(s.user, s.am + 'list', 'ptw', page=page)
# Checks for last page
if len(amlist[s.am]) == 0:
break
# Appends each entry to list
for am in amlist[s.am]:
animangalist.append(am)
page += 1
sleep(2)
"""Saves all planned entries to file"""
with open('data/plannedList', 'w', encoding='utf-8') as file:
for amlist in animangalist:
file.write('{}\n'.format(amlist['mal_id']))
print('Finished Loading')
"""
score function
=================================================
Scores all genres based on user's ratings
"""
def score():
print('Started Calculating...')
animangalist = []
genreTotal = [0] * 43
genreCount = [0] * 43
genreAvg = [0] * 43
"""Reads all completed entries from file to list"""
with open('data/completedList', 'r', encoding='utf-8') as file:
amlist = file.readlines()
for aml in amlist:
animangalist.append(list(aml.strip().split(',')))
total = len(animangalist)
if total == 0:
print('Empty List')
s.exit()
print('Length: {}'.format(total))
print('ET: {} min {} sec'.format((total * 2) // 60, (total * 2) % 60))
"""Loops through completed entries and searches + scores genres"""
for amlist in animangalist:
# Returns and stores animanga's genre list as python list
genrelist = (s.jikan.anime(amlist[0])['genres'] if s.am == 'anime' else s.jikan.manga(amlist[0])['genres'])
# Stores genre information
for glist in genrelist:
id = glist['mal_id']
genreTotal[id-1] += int(amlist[1])**1.25 # Sum of each score^(5/4)
genreCount[id-1] += 1 # Counts genre frequency
sleep(2)
"""Applies a 'weighted average' to the genre's score"""
for i in range(43):
genreAvg[i] = (0 if genreCount[i] == 0 else genreTotal[i]/genreCount[i]*(1+math.log10(genreCount[i])))
# List of genre names
genres = ['Action', 'Adventure', 'Cars', 'Comedy', 'Dementia', 'Demons', 'Mystery', 'Drama', 'Ecchi', 'Fantasy',
'Game', 'Hentai', 'Historical', 'Horror', 'Kids', 'Magic', 'Martial Arts', 'Mecha', 'Music', 'Parody',
'Samurai', 'Romance', 'School', 'Sci Fi', 'Shoujo', 'Shoujo Ai', 'Shounen', 'Shounen Ai', 'Space',
'Sports', 'Super Power', 'Vampire', 'Yaoi', 'Yuri', 'Harem', 'Slice of Life', 'Supernatural', 'Military',
'Police', 'Psychological', 'Thriller', 'Seinen', 'Josei']
"""Saves genre scores to file"""
with open('data/genreScores', 'w', encoding='utf-8') as file:
for i in range(43):
file.write('{},{},{}\n'.format(genres[i], i+1, genreAvg[i]))
print('Finished Calculating.')
"""Object used to handle genre sorting"""
class Genre:
def __init__(self, name, id, score):
self.name = name
self.id = id
self.score = score
"""Object used to handle animanga sorting"""
class Animanga:
def __init__(self, name, type, id, score, genres):
self.name = name
self.type = type # Anime / Manga
self.id = id
self.score = score
self.genres = genres
"""
search function
=================================================
Searches planned animangas and sorts by rating
"""
def search():
print('Started Searching...')
animangas = []
animangalist = []
genrelist = []
"""Reads all planned entries from file to list"""
with open('data/plannedList', 'r', encoding='utf-8') as file:
amlist = file.readlines()
for aml in amlist:
animangas.append(int(aml.strip()))
total = len(animangas)
if total == 0:
print('Empty List')
s.exit()
print('Length: {}'.format(total))
print('ET: {} min {} sec'.format((total * 2) // 60, (total * 2) % 60))
"""Loops through planned entries and searches animangas"""
for am in animangas:
# Returns and stores animanga as python dictionary
animanga = (s.jikan.anime(am) if s.am == 'anime' else s.jikan.manga(am))
glist = []
# Stores genres
for g in animanga['genres']:
glist.append(g['mal_id'])
# Stores relevant animanga data as an object
if animanga['score'] is not None:
animangalist.append(
Animanga(animanga['title'], animanga['type'], animanga['mal_id'], animanga['score'], glist))
sleep(2)
"""Reads all genre scores from file to list"""
with open('data/genreScores', 'r', encoding='utf-8') as file:
glist = file.readlines()
for gl in glist:
name, id, score = map(str, gl.strip().split(','))
# Stores relevant genre data as an object
genrelist.append(Genre(name, int(id), float(score)))
"""Sorts both animangas and genres by score"""
animangalist.sort(key=lambda am: am.score, reverse=True)
genrelist.sort(key=lambda g: g.score, reverse=True)
"""Saves animangas and genres to file in order"""
with open('data/animangaRank', 'w', encoding='utf-8') as file:
for am in animangalist:
file.write('{}|{}|{}|{}\n'.format(am.name, am.type, am.id, ','.join(str(e) for e in am.genres)))
with open('data/genreRank', 'w') as file:
for g in genrelist:
file.write('{}\n'.format(g.id))
"""Saves genre rankings to file"""
with open('topgenres.txt', 'w', encoding='utf-8') as file:
i = 1
for g in genrelist:
file.write('#{:02d}: {} - {}\n'.format(i, g.name, g.score))
i += 1
print('Finished Searching...')
"""
suggest function
=================================================
Calculates top 5 animangas based on previous data
"""
def suggest():
print('Started Suggestion Calculation...')
animangas = []
genres = []
"""Reads all ranked animangas and genres from file to list"""
with open('data/animangaRank', 'r', encoding='utf-8') as file:
amlist = file.readlines()
for aml in amlist:
w, x, y, z = map(str, aml.strip().split('|'))
animangas.append([w, x, int(y), list(z.split(','))])
with open('data/genreRank', 'r', encoding='utf-8') as file:
glist = file.readlines()
for gl in glist:
genres.append(gl.strip())
suggestions = []
count = 0
"""Stores top 5 animangas from top genres"""
for am in animangas:
for g in genres:
if g in am[3]:
suggestions.append([am[0], am[1], am[2]])
count += 1
break
if count == 5:
break
"""Saves 5 suggestions to file as MAL links"""
with open('suggestions.txt', 'w', encoding='utf-8') as file:
for sg in suggestions:
file.write('{} ({}): https://myanimelist.net/{}/{}\n'.format(sg[0], sg[1], s.am, sg[2]))
print('Finished Suggestion Calculation...')
"""
test function
=================================================
Saves to file a complete ranking of completed
animangas
Displays title, media type, ID, score and genres
Mainly a debugging method
"""
def test():
print('Started Test Program')
animangas = []
with open('data/completedList', 'r', encoding='utf-8') as file:
alist = file.readlines()
print('Length: {}'.format(len(alist)))
print('ET: {} min {} sec'.format((len(alist) * 2) // 60, (len(alist) * 2) % 60))
for al in alist:
mal_id, score = map(int, al.strip().split(','))
animanga = (s.jikan.anime(mal_id) if s.am == 'anime' else s.jikan.manga(mal_id))
genres = []
for g in animanga['genres']:
genres.append(g['name'])
animangas.append('{} ({}) [{}] ({}/10): {}'.format(animanga['title'], animanga['type'], mal_id, score,
', '.join(genres)))
sleep(2)
with open('data/test', 'w', encoding='utf-8') as file:
for a in animangas:
file.write(a+'\n')
print('Finished Test Program')