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 2015 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of toktokkie.
6toktokkie 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.
11toktokkie 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 toktokkie. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import List, Dict, Optional
21from toktokkie.enums import IdType, MediaType
24class IdHelper:
25 """
26 Class that contains static methoids that help with ID operations
27 """
29 @staticmethod
30 def stringify_ids(ids: Dict[IdType, List[str]]) -> Dict[str, List[str]]:
31 """
32 Converts the keys in an ID dictionary to strings
33 :param ids: The ID dictionary to convert
34 :return: The converted ID dictionary
35 """
36 new_ids = {}
37 for id_type, values in ids.items():
38 new_ids[id_type.value] = values
39 return new_ids
41 @staticmethod
42 def objectify_ids(ids: Dict[str, List[str]]) -> Dict[IdType, List[str]]:
43 """
44 Converts the keys in an ID dictionary using string keys to IdType
45 objects
46 :param ids: The ID dictionary to convert
47 :return: The converted ID dictionary
48 """
49 new_ids = {}
50 for id_type, values in ids.items():
51 new_ids[IdType(id_type)] = values
52 return new_ids
54 @staticmethod
55 def fill_ids(
56 ids: Dict[IdType, List[str]],
57 valid_ids: List[IdType],
58 _parent_ids: Optional[Dict[IdType, List[str]]] = None
59 ) -> Dict[IdType, List[str]]:
60 """
61 Fills in any missing id type key for an ID dictionary
62 :param ids: The ID dictionary to fill
63 :param valid_ids: Any valid ID types
64 :param _parent_ids: Optionally provided dictionary of parent IDs.
65 The values of this dictionary will be entered into
66 the filled dictionary if they aren't defined
67 already.
68 :return: The filled dictionary
69 """
70 parent_ids = {} if _parent_ids is None else _parent_ids
71 for id_type, values in parent_ids.items():
72 if id_type not in ids:
73 ids[id_type] = values
75 for id_type in valid_ids:
76 if id_type not in ids:
77 ids[id_type] = []
78 return ids
80 @staticmethod
81 def minimize_ids(
82 ids: Dict[IdType, List[str]],
83 _parent_ids: Optional[Dict[IdType, List[str]]] = None
84 ) -> Dict[IdType, List[str]]:
85 """
86 Removes redundant and empty IDs from a dictionary of IDs
87 :param ids: The dictionary to minimize
88 :param _parent_ids: Optionally provide a parent's IDs. These IDs will
89 be removed from the child dictionary if the values
90 are the same
91 :return: The minimized dictionary
92 """
93 parent_ids = {} if _parent_ids is None else _parent_ids
94 minimized = {}
96 for id_type in ids.keys():
97 values = ids[id_type]
99 if values == parent_ids.get(id_type):
100 continue
102 if len(ids[id_type]) > 0:
103 minimized[id_type] = values
105 return minimized
107 @staticmethod
108 def generate_url_for_id(
109 id_type: IdType,
110 media_type: MediaType,
111 _id: str
112 ) -> str:
113 """
114 Generates a URL for an ID
115 :param id_type: The ID Type
116 :param media_type: The media type
117 :param _id: The ID
118 :return: The template pattern
119 """
120 urlmap = {
121 IdType.ANILIST: "https://anilist.co/@{ANIME_MANGA}/{}",
122 IdType.MYANIMELIST: "https://myanimelist.net/@{ANIME_MANGA}/{}",
123 IdType.KITSU: "https://kitsu.io/@{ANIME_MANGA}/{}",
124 IdType.TVDB: "https://www.thetvdb.com/?id={}&tab=series",
125 IdType.VNDB: "https://vndb.org/{}",
126 IdType.IMDB: "https://www.imdb.com/title/{}",
127 IdType.ISBN: "https://isbnsearch.org/isbn/{}",
128 IdType.MANGADEX: "https://mangadex.org/title/{}",
129 IdType.MUSICBRAINZ_ARTIST: "https://musicbrainz.org/artist/{}",
130 IdType.MUSICBRAINZ_RECORDING:
131 "https://musicbrainz.org/recording/{}",
132 IdType.MUSICBRAINZ_RELEASE: "https://musicbrainz.org/release/{}"
133 }
134 pattern = urlmap[id_type]
136 anime_manga = "anime"
137 if media_type in IdHelper.literature_media_types():
138 anime_manga = "manga"
140 url = pattern \
141 .replace("@{ANIME_MANGA}", anime_manga) \
142 .format(_id)
143 return url
145 @staticmethod
146 def literature_media_types() -> List[MediaType]:
147 """
148 :return: A list of literature media types
149 """
150 return [
151 MediaType.BOOK,
152 MediaType.BOOK_SERIES,
153 MediaType.COMIC
154 ]
156 @staticmethod
157 def int_id_types() -> List[IdType]:
158 """
159 :return: A list of ID types that are required to be integers
160 """
161 return [
162 IdType.MYANIMELIST,
163 IdType.ANILIST,
164 IdType.KITSU,
165 IdType.TVDB
166 ]
168 @staticmethod
169 def theme_song_id_types() -> List[IdType]:
170 """
171 :return: A list of ID types that can apply to theme songs
172 """
173 return [
174 IdType.TVDB,
175 IdType.MYANIMELIST,
176 IdType.ANILIST,
177 IdType.KITSU,
178 IdType.VNDB,
179 IdType.MUSICBRAINZ_RECORDING
180 ]