-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathanime.py
More file actions
169 lines (132 loc) · 6.27 KB
/
Copy pathanime.py
File metadata and controls
169 lines (132 loc) · 6.27 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
import time
import requests
from retrieve_data import ExtractInfo
from retrieve_id import ExtractID
class Anime:
def __init__(self, access_info, activated=True):
self.extractInfo = ExtractInfo(access_info, activated)
self.extractID = ExtractID(access_info, activated)
def getAnime(self, anime_name, manual_select=False) -> dict:
'''
Retrieve anime info in the form of a json object.
Retrieve json object will be reformatted in a easily accessable json obj.
:param anime_name: The name of the anime
:return: parsed dict containing the anime's data
:rtype: dict
'''
anime_id = self.getAnimeID(anime_name, manual_select)
if anime_id == -1:
return None
return self.getAnimeWithID(anime_id)
def getAnimeWithID(self, anime_id) -> dict:
'''
Retrieve anime info in the form of a json object.
Retrieve json object will be reformatted in a easily accessable json obj.
:param anime_name: The name of the anime
:return: parsed dict containing the anime's data
:rtype: dict
'''
data = self.extractInfo.anime(anime_id)
media_lvl = data['data']['Media']
name_romaji = media_lvl['title']['romaji']
name_english = media_lvl['title']['english']
start_year = media_lvl['startDate']['year']
start_month = media_lvl['startDate']['month']
start_day = media_lvl['startDate']['day']
end_year = media_lvl['endDate']['year']
end_month = media_lvl['endDate']['month']
end_day = media_lvl['endDate']['day']
starting_time = f'{start_month}/{start_day}/{start_year}'
ending_time = f'{end_month}/{end_day}/{end_year}'
cover_image = media_lvl['coverImage']['large']
banner_image = media_lvl['bannerImage']
airing_format = media_lvl['format']
airing_status = media_lvl['status']
airing_episodes = media_lvl['episodes']
season = media_lvl['season']
desc = media_lvl['description']
average_score = media_lvl['averageScore']
genres = media_lvl['genres']
next_airing_ep = media_lvl['nextAiringEpisode']
anime_dict = {"name_romaji": name_romaji,
"name_english": name_english,
"starting_time": starting_time,
"ending_time": ending_time,
"cover_image": cover_image,
"banner_image": banner_image,
"airing_format": airing_format,
"airing_status": airing_status,
"airing_episodes": airing_episodes,
"season": season,
"desc": desc,
"average_score": average_score,
"genres": genres,
"next_airing_ep": next_airing_ep,}
return anime_dict
def getAnimeID(self, anime_name, manual_select=False):
'''
Retrieves the anime ID on Anilist.
:param anime_name: The name of the anime
:return: The anime's ID on Anilist. Returns -1 if an error is caught.
:rtype: int
'''
# if manual select is turned off ============================================================================
if not manual_select:
anime_list = []
data = self.extractID.anime(anime_name)
for i in range(len(data['data']['Page']['media'])):
curr_anime = data['data']['Page']['media'][i]['title']['romaji']
anime_list.append(curr_anime)
# returns the first anime found
try:
anime_ID = data['data']['Page']['media'][0]['id']
except IndexError:
raise IndexError('Anime Not Found')
return anime_ID
# if manual select is turned on =============================================================================
elif manual_select:
data = self.extractID.anime(anime_name)
max_result = 0
counter = 0 # number of displayed results from search
for i in range(len(data['data']['Page']['media'])):
curr_anime = data['data']['Page']['media'][i]['title']['romaji']
print(f"{counter + 1}. {curr_anime}")
max_result = i + 1
counter += 1
if counter > 1: # only one result found if counter == 1
try:
user_input = int(input("Please select the anime that you are searching for in number: "))
except TypeError:
print(f"Your input is incorrect! Please try again!")
return -1
if user_input > max_result or user_input <= 0:
print("Your input does not correspound to any of the anime displayed!")
return -1
elif counter == 0:
print(f'No search result has been found for the anime "{anime_name}"!')
return -1
else:
user_input = 1
return data['data']['Page']['media'][user_input - 1]['id']
else:
# placeholder
pass
def displayAnimeInfo(self, anime_name, manual_select=False):
'''
Displays all anime data.
Auto formats the displayed version of the data.
:param anime_name: The name of the anime
'''
ani_dict = self.getAnime(anime_name, manual_select)
if ani_dict == None:
print('Name Error')
else:
arr = ["Name(romaji)", "Name(Eng)", "Started Airing On", "Ended On", "Cover Image", "Banner Image",
"Airing Format", "Airing Status", "Total Ep Count", "Season", "Description", "Ave. Score", "Genres",
"Next Ep Airing Date"]
counter = 0
print("====================================================================")
print("============================ ANIME INFO ============================")
for key, value in ani_dict.items():
print(f"{arr[counter]}: {value}")
counter += 1