Coverage for otaku_info/external/mangadex.py: 49%

Shortcuts 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

59 statements  

1"""LICENSE 

2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of otaku-info. 

5 

6otaku-info 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. 

10 

11otaku-info 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. 

15 

16You should have received a copy of the GNU General Public License 

17along with otaku-info. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import json 

21import time 

22import requests 

23from jerrycan.base import app 

24from typing import Optional, List, Dict, Any, Union 

25from otaku_info.external.entities.MangadexItem import MangadexItem 

26 

27 

28def fetch_all_mangadex_items() -> List[MangadexItem]: 

29 """ 

30 Fetches all available mangadex items 

31 :return: The mangadex items 

32 """ 

33 url = "https://api.mangadex.org/manga" 

34 items: List[Dict[str, Any]] = [] 

35 mangadex_items: List[MangadexItem] = [] 

36 page = 0 

37 last_date = "1970-01-01T00:00:00" 

38 

39 while True: 

40 params: Dict[str, Union[int, str]] = { 

41 "createdAtSince": last_date, 

42 "order[createdAt]": "asc", 

43 "limit": 100, 

44 "offset": 100 * page 

45 } 

46 app.logger.debug(f"Mangadex: {params}") 

47 response = requests.get(url, params=params) 

48 data = json.loads(response.text) 

49 time.sleep(1) 

50 

51 if data["result"] == "error": 

52 # TODO understand what's happening here 

53 new_date = items[-1]["attributes"]["createdAt"] 

54 new_date = new_date.split("T")[0] + "T00:00:00" 

55 

56 if new_date == last_date: 

57 break 

58 else: 

59 last_date = new_date 

60 page = 0 

61 continue 

62 elif len(data["data"]) == 0: 

63 break 

64 

65 items += data["data"] 

66 new_items = [ 

67 MangadexItem.from_json(x) 

68 for x in data["data"] 

69 ] 

70 add_covers(new_items) 

71 mangadex_items += new_items 

72 page += 1 

73 

74 mangadex_items.sort(key=lambda x: x.english_title) 

75 return mangadex_items 

76 

77 

78def fetch_mangadex_item(mangadex_id: str) -> Optional[MangadexItem]: 

79 """ 

80 Fetches information for a mangadex 

81 """ 

82 url = "https://api.mangadex.org/manga" 

83 response = requests.get(url, params={"ids[]": mangadex_id}) 

84 time.sleep(1) 

85 

86 if response.status_code >= 300: 86 ↛ 87line 86 didn't jump to line 87, because the condition on line 86 was never true

87 return None 

88 

89 data = json.loads(response.text)["data"][0] 

90 return MangadexItem.from_json(data) 

91 

92 

93def add_covers(mangadex_items: List[MangadexItem]): 

94 """ 

95 Adds cover URLs to mangadex items 

96 :param mangadex_items: The mangadex items 

97 :return: None 

98 """ 

99 url = "https://api.mangadex.org/cover" 

100 ids = [ 

101 x.cover_url 

102 for x in mangadex_items 

103 if x.cover_url is not None and len(x.cover_url) == 36 

104 ] 

105 params: Dict[str, Union[int, list]] = {"ids[]": [ids], "limit": 100} 

106 response = requests.get(url, params=params) 

107 data = json.loads(response.text) 

108 results = data["data"] 

109 time.sleep(1) 

110 

111 covers = {} 

112 for result in results: 

113 relations = {x["type"]: x["id"] for x in result["relationships"]} 

114 covers[relations["manga"]] = result["attributes"]["fileName"] 

115 

116 for mangadex_item in mangadex_items: 

117 filename = covers.get(mangadex_item.mangadex_id) 

118 if filename is None: 118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never true

119 mangadex_item.cover_url = "" 

120 else: 

121 mangadex_item.cover_url = f"https://uploads.mangadex.org/covers/" \ 

122 f"{mangadex_item.mangadex_id}/{filename}"