Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info-web.
6otaku-info-web is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11otaku-info-web is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with otaku-info-web. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import Optional, Dict, Any, Tuple
21from otaku_info_web.utils.enums import MediaType, MediaSubType, \
22 ReleasingState, ConsumingState, MediaRelationType
25class AnilistItem:
26 """
27 Class that models a general anilist list item
28 Represents the information fetched using anilist's API
29 """
30 def __init__(
31 self,
32 anilist_id: int,
33 media_type: MediaType,
34 media_subtype: MediaSubType,
35 english_title: Optional[str],
36 romaji_title: str,
37 cover_url: str,
38 chapters: Optional[int],
39 episodes: Optional[int],
40 releasing_state: ReleasingState,
41 relations: Dict[Tuple[MediaType, int], MediaRelationType]
42 ):
43 """
44 Initializes the AnilistItem object
45 :param anilist_id: The anilist ID of the series
46 :param media_type: The media type of the series
47 :param media_subtype: The media subtype of the series
48 :param english_title: The English title of the series
49 :param romaji_title: The Japanes title of the series written in romaji
50 :param cover_url: URL to a cover image for the series
51 :param chapters: The total amount of known manga chapters
52 :param episodes: The total amount of known anime episodes
53 :param releasing_state: The current releasing state of the series
54 :param relations: Related media items identified by IDs
55 """
56 self.anilist_id = anilist_id
57 self.media_type = media_type
58 self.media_subtype = media_subtype
59 self.english_title = english_title
60 self.romaji_title = romaji_title
61 self.cover_url = cover_url
62 self.chapters = chapters
63 self.episodes = episodes
64 self.releasing_state = releasing_state
65 self.relations = relations
67 @property
68 def latest_release(self) -> Optional[int]:
69 """
70 :return: The latest release. Chapters for manga, episodes for anime
71 """
72 if self.media_type == MediaType.ANIME:
73 return self.episodes
74 else:
75 return self.chapters
77 @classmethod
78 def from_query(cls, media_type: MediaType, data: Dict[str, Any]) \
79 -> "AnilistItem":
80 """
81 Generates an AnilistItem from a dictionary generated by an APi query
82 :param media_type: The media type of the item
83 :param data: The data to use
84 :return: The generated AnilistItem
85 """
86 _media_subtype = data["format"]
87 if _media_subtype is None:
88 _media_subtype = "unknown"
89 media_subtype = MediaSubType(_media_subtype.lower())
91 _releasing_state = data["status"]
92 if _releasing_state is None:
93 _releasing_state = "unknown"
94 releasing_state = ReleasingState(_releasing_state.lower())
96 relations = {}
97 for edge in data["relations"]["edges"]:
98 node_id = edge["node"]["id"]
99 node_type = MediaType(edge["node"]["type"].lower())
100 relation_type = MediaRelationType(edge["relationType"].lower())
101 relations[(node_type, node_id)] = relation_type
103 return AnilistItem(
104 data["id"],
105 media_type,
106 media_subtype,
107 data["title"]["english"],
108 data["title"]["romaji"],
109 data["coverImage"]["large"],
110 data["chapters"],
111 data["episodes"],
112 releasing_state,
113 relations
114 )
117class AnilistUserItem(AnilistItem):
118 """
119 Class that models an anilist list item for a user
120 Represents the information fetched using anilist's API
121 """
122 def __init__(
123 self,
124 anilist_id: int,
125 media_type: MediaType,
126 media_subtype: MediaSubType,
127 english_title: Optional[str],
128 romaji_title: str,
129 cover_url: str,
130 chapters: Optional[int],
131 episodes: Optional[int],
132 releasing_state: ReleasingState,
133 relations: Dict[Tuple[MediaType, int], MediaRelationType],
134 score: Optional[int],
135 progress: Optional[int],
136 consuming_state: ConsumingState,
137 list_name: str
138 ):
139 """
140 Initializes the AnilistItem object
141 :param anilist_id: The anilist ID of the series
142 :param media_type: The media type of the series
143 :param media_subtype: The media subtype of the series
144 :param english_title: The English title of the series
145 :param romaji_title: The Japanes title of the series written in romaji
146 :param cover_url: URL to a cover image for the series
147 :param chapters: The total amount of known manga chapters
148 :param episodes: The total amount of known anime episodes
149 :param releasing_state: The current releasing state of the series
150 :param relations: Related media items identified by IDs
151 :param score: The user's score for the series
152 :param progress: The user's progress for the series
153 :param consuming_state: The user's consumption state for the series
154 :param list_name: Which of the user's lists this entry belongs to
155 """
156 super().__init__(
157 anilist_id,
158 media_type,
159 media_subtype,
160 english_title,
161 romaji_title,
162 cover_url,
163 chapters,
164 episodes,
165 releasing_state,
166 relations
167 )
168 self.score = score
169 self.progress = progress
170 self.consuming_state = consuming_state
171 self.list_name = list_name
173 @classmethod
174 def from_query(
175 cls,
176 media_type: MediaType,
177 data: Dict[str, Any]
178 ) -> "AnilistUserItem":
179 """
180 Generates an AnilistUserItem from a dictionary generated
181 by an APi query
182 :param media_type: The media type of the item
183 :param data: The data to use
184 :return: The generated AnilistItem
185 """
186 base = AnilistItem.from_query(media_type, data["media"])
187 consuming_state = ConsumingState(data["status"].lower())
189 return AnilistUserItem(
190 base.anilist_id,
191 base.media_type,
192 base.media_subtype,
193 base.english_title,
194 base.romaji_title,
195 base.cover_url,
196 base.chapters,
197 base.episodes,
198 base.releasing_state,
199 base.relations,
200 data["score"],
201 data["progress"],
202 consuming_state,
203 data["list_name"]
204 )