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"""
20import json
21import requests
22from typing import Dict, Optional
23from otaku_info_web.utils.enums import ListService
24from otaku_info_web.utils.mappings import mangadex_external_id_names, \
25 list_service_id_types
28def get_external_ids(mangadex_id: int) -> Optional[Dict[ListService, str]]:
29 """
30 Retrieves associated IDs for a mangadex ID
31 :param mangadex_id: The mangadex ID
32 :return: The other IDs, mapped to their list service
33 """
34 endpoint = "https://mangadex.org/api/manga/{}".format(mangadex_id)
35 response = json.loads(requests.get(endpoint).text)
37 ids = {ListService.MANGADEX: str(mangadex_id)}
39 if response["status"] != "OK":
40 return None
41 else:
42 links = response["manga"]["links"]
43 if links is None:
44 return ids
46 for service, identifier in mangadex_external_id_names.items():
47 if identifier in links:
48 _id = links[identifier]
49 id_type = list_service_id_types[service]
51 if id_type == int:
52 _id = "".join([x for x in _id if x.isdigit()])
54 try:
55 ids[service] = str(id_type(_id))
56 except ValueError:
57 pass
59 return ids