-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
424 lines (404 loc) · 22.1 KB
/
main.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import os, questionary, requests, re
from bs4 import BeautifulSoup, NavigableString
from plumbum import cli
def clear_screen():
"""
Clears the screen
"""
os.system('cls' if os.name == 'nt' else 'clear')
class MALHelper(cli.Application):
VERSION = '1.0'
search = cli.SwitchAttr(['s', 'search'], help='Searches MyAnimeList for the provided query and displays a list of top 50 search results')
trending = cli.Flag(['t', 'trending'], help='Displays a list of top 50 trending anime from MyAnimeList')
airing = cli.Flag(['a', 'airing'], help='Displays a list of top 50 currently airing anime from MyAnimeList')
upcoming = cli.Flag(['u', 'upcoming'], help='Displays a list of top 50 upcoming anime from MyAnimeList')
def show_anime_info(self, anime_list, anime_index):
"""
Shows info about an anime scraped from MyAnimeList
"""
selected_anime = anime_list[int(anime_index)]
anime_page_url = selected_anime.select('td:nth-of-type(2) a')[0]['href']
response = requests.get(anime_page_url)
soup = BeautifulSoup(response.content, 'html.parser')
name = soup.select('h1.title-name strong')[0].text
info = '\n'.join(
info_piece.span.text + ' ' + ' '.join(
tag.text.strip()
for tag in info_piece.contents[2:]
if isinstance(tag, NavigableString) or (not tag.has_attr('style') or not 'display: none' in tag['style'])
)
for info_piece in soup.find('h2', text='Information').find_next_siblings('div', class_='spaceit_pad')[:14]
).replace(' ', ' ').replace(' , ', ', ')
stats = '\n'.join(
(
stat_piece.span.text + ' '.join(
tag.text.strip()
for tag in stat_piece.contents
if (tag.name == 'span' and tag.has_attr('class') and 'score-label' in tag['class']) or isinstance(tag, NavigableString)
)
).replace(' ', ' ')
for stat_piece in soup.find('h2', text='Statistics').find_next_siblings('div', class_='spaceit_pad')[:5]
)
synopsis = soup.find('p', attrs={ 'itemprop': 'description' }).text
while True:
clear_screen()
questionary.print(f'Page URL: {anime_page_url}\nName: {name}\n{info}\n{stats}\nSynopsis:\n{synopsis}\n', style='fg:blue')
choice = questionary.select(
'Select an option:',
choices=[
'See recommedations based on this anime',
'Go back to the previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go back to the previous page':
clear_screen()
return False
else:
show_main_menu = self.recommend_anime(anime_list, anime_index)
if show_main_menu:
return True
def recommend_anime(self, anime_list, anime_index):
"""
Shows reccomendations based on an anime scraped from MyAnimeList
"""
selected_anime = anime_list[int(anime_index)]
anime_page_url = selected_anime.select('td:nth-of-type(2) a')[0]['href']
response = requests.get(anime_page_url+r'/userrecs')
soup = BeautifulSoup(response.content, 'html.parser')
name = soup.select('h1.title-name strong')[0].text
recommendations = soup.select('div.borderClass td:nth-of-type(2)')
if len(recommendations) == 0:
questionary.print(f'No recommendations could be found based on {name} ☹.', style='bold fg:red')
choice = questionary.select(
'Select an option:',
choices=[
'Go back to previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go back to previous page':
clear_screen()
return False
else:
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
while True:
clear_screen()
questionary.print(f'Anime recommendations based on {name}:\n\n', style='bold fg:darkgreen')
for index, recommendation in enumerate(recommendations):
recommendation_name = recommendation.select('div:nth-of-type(2) a')[0].text.strip()
recommendation_text = ' '.join(
content_piece.text.strip()
for content_piece in recommendation.find(class_='spaceit_pad detail-user-recs-text').contents
if content_piece.name != 'a'
)
recommender_name = ' '.join(
content_piece.text.strip()
for content_piece in recommendation.find(class_='spaceit_pad detail-user-recs-text').next_sibling.next_sibling.contents[2:]
)
questionary.print(f'Index: {index+1}\nName: {recommendation_name}\n{recommendation_text}\n{recommender_name}\n', style='fg:blue')
if index == len(recommendations)-1:
questionary.print(f'\nThose were the top {index+1} recommendations based on {name}!', style='bold fg:darkgreen')
choice = questionary.select(
'Select an option:',
choices=[
'Show more info about an anime',
'Go to previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go to previous page':
clear_screen()
return False
elif choice == 'Show more info about an anime':
recommendation_index = int(
questionary.text(
'Enter the index of the anime recommendation: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= int(index)+1 else 'Please enter a valid index.'
).ask()
)
clear_screen()
show_main_menu = self.show_anime_info(recommendations, recommendation_index-1) # We've put recommendation_index-1 instead of recommendation_index because this function accepts the index of the anime which is one less than the anime's rank
if show_main_menu:
return True
else:
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
if iteration_options['constant_iteration'] and iteration_options['constant_iteration_till_index'] == index:
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
if not iteration_options['constant_iteration'] and index > 0 and (index+1)%5 == 0:
choice = questionary.select(
'Select an option:',
choices=[
'Display more anime recommendations',
'Show more info about an anime',
'Go back to previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go back to previous page':
clear_screen()
return False
elif choice == 'Show more info about an anime':
recommendation_index = int(
questionary.text(
'Enter the index of the anime recommendation: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= int(index)+1 else 'Please enter a valid index.'
).ask()
)
clear_screen()
self.show_anime_info(recommendations, recommendation_index-1) # We've put recommendation_index-1 instead of recommendation_index because this function accepts the index of the anime which is one less than the anime's rank
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
def search_anime(self, query):
"""
Shows a list of anime scraped from MyAnimeList based on a search query
"""
url = f'https://myanimelist.net/anime.php?q={requests.utils.quote(query)}&cat=anime'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
all_anime_container = soup.find(class_='js-categories-seasonal js-block-list list')
all_anime = all_anime_container.find_all('tr')
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
while True:
for index, anime in enumerate(all_anime):
if index > 0:
name = anime.select('div.title strong')[0].text
score = anime.select('td:nth-of-type(5)')[0].text.strip()
info = f"{anime.select('td:nth-of-type(3)')[0].text.strip()}, {anime.select('td:nth-of-type(4)')[0].text.strip()} episode(s)"
questionary.print(f'Index: {index}\nName: {name}\nScore: {score}\nMore information: {info}\n', style='fg:blue')
if index == len(all_anime)-1:
questionary.print(f'\nThose were the top {index} search results for your query!', style='bold fg:darkgreen')
choice = questionary.select(
'Select an option:',
choices=[
'Show more info about an anime',
'Go to previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go to previous page':
clear_screen()
return False
elif choice == 'Show more info about an anime':
anime_index = int(
questionary.text(
'Enter the index of the anime: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= index else 'Please enter a valid index.'
).ask()
)
clear_screen()
self.show_anime_info(all_anime, anime_index)
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
if iteration_options['constant_iteration'] and iteration_options['constant_iteration_till_index'] == index:
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
if not iteration_options['constant_iteration'] and (index)%5 == 0:
choice = questionary.select(
'Select an option:',
choices=[
'Display more anime',
'Show more info about an anime',
'Go to previous page',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Go to previous page':
clear_screen()
return False
elif choice == 'Show more info about an anime':
anime_index = int(
questionary.text(
'Enter the index of the anime: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= index else 'Please enter a valid index.'
).ask()
)
clear_screen()
show_main_menu = self.show_anime_info(all_anime, anime_index)
if show_main_menu:
clear_screen()
return True
else:
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
def top_anime(self, page_url, top_anime_type):
"""
Shows a list of top anime depending on the type specified (trending, airing, or upcoming) scraped from MyAnimeList.
"""
response = requests.get(page_url)
content = response.content
soup = BeautifulSoup(content, 'html.parser')
all_anime = soup.find_all(class_='ranking-list')
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
while True:
clear_screen()
questionary.print(f'Top 50 {top_anime_type} anime on MyAnimeList:\n\n', style='bold fg:darkgreen')
for index, anime in enumerate(all_anime):
ranking = anime.find(class_='rank').find('span').text
name = anime.find(class_='clearfix').text.strip()
score = anime.find('span', class_='score-label').text
info = re.sub(' +', ' ', ', '.join(anime.find(class_='information').text.strip().split('\n')[:2]))
questionary.print(f'Rank: {ranking}\nName: {name}\nScore: {score}\nMore info: {info}\n', style='fg:blue')
if int(ranking) == 50:
questionary.print(f'\nThose were the top 50 {top_anime_type} anime on MyAnimeList!', style='bold fg:darkgreen')
choice = questionary.select(
'Select an option:',
choices=[
'Show more info about an anime',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Show more info about an anime':
anime_rank = int(
questionary.text(
'Enter the rank of the anime: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= int(ranking) else 'Please enter a valid index.'
).ask()
)
clear_screen()
show_main_menu = self.show_anime_info(all_anime, anime_rank-1) # We've put anime_rank-1 instead of anime_rank because this function accepts the index of the anime which is one less than the anime's rank
if show_main_menu:
clear_screen()
return True
else:
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
if iteration_options['constant_iteration'] and iteration_options['constant_iteration_till_index'] == index:
iteration_options = { 'constant_iteration': False, 'constant_iteration_till_index': 0 }
if not iteration_options['constant_iteration'] and index > 0 and (index+1)%5 == 0:
choice = questionary.select(
'Select an option:',
choices=[
'Display more anime',
'Show more info about an anime',
'Show main menu',
'Quit'
]
).ask()
if choice == 'Quit':
clear_screen()
exit()
elif choice == 'Show main menu':
clear_screen()
return True
elif choice == 'Show more info about an anime':
anime_rank = int(
questionary.text(
'Enter the rank of the anime: ',
validate=lambda user_input: True if user_input.isnumeric() and 0 < int(user_input) <= int(ranking) else 'Please enter a valid index.'
).ask()
)
clear_screen()
show_main_menu = self.show_anime_info(all_anime, anime_rank-1) # We've put anime_rank-1 instead of anime_rank because this function accepts the index of the anime which is one less than the anime's rank
if show_main_menu:
clear_screen()
return True
else:
clear_screen()
iteration_options = { 'constant_iteration': True, 'constant_iteration_till_index': index }
break
def top_trending_anime(self):
"""
Shows a list of top trending anime scraped from MyAnimeList.
"""
self.top_anime('https://myanimelist.net/topanime.php', 'trending')
def top_upcoming_anime(self):
"""
Shows a list of top upcoming anime scraped from MyAnimeList.
"""
self.top_anime('https://myanimelist.net/topanime.php?type=upcoming', 'upcoming')
def top_airing_anime(self):
"""
Shows a list of top airing anime scraped from MyAnimeList.
"""
self.top_anime('https://myanimelist.net/topanime.php?type=airing', 'airing')
def main(self):
if self.search:
self.search_anime(self.search)
elif self.trending:
self.top_trending_anime()
elif self.airing:
self.top_airing_anime()
elif self.upcoming:
self.top_upcoming_anime()
else:
while True:
choice = questionary.select(
'Select an option:',
choices=[
'Search for anime',
'Show top 50 trending anime',
'Show top 50 airing anime',
'Show top 50 upcoming anime',
'Quit'
]
).ask()
if choice == 'Search for anime':
query = questionary.text('Enter a search query: ').ask()
self.search_anime(query)
elif choice == 'Show top 50 trending anime':
self.top_trending_anime()
elif choice == 'Show top 50 airing anime':
self.top_airing_anime()
elif choice == 'Show top 50 upcoming anime':
self.top_upcoming_anime()
else:
return
if __name__ == '__main__':
MALHelper()