Coverage for otaku_info/external/entities/AnilistItem.py: 70%

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

42 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 

20from typing import Dict, Any, Optional 

21from otaku_info.enums import MediaType, MediaSubType, \ 

22 ReleasingState, MediaRelationType, ListService 

23from otaku_info.external.entities.AnimeListItem import AnimeListItem 

24 

25 

26class AnilistItem(AnimeListItem): 

27 """ 

28 Class that models a general anilist list item 

29 Represents the information fetched using anilist's API 

30 """ 

31 

32 @property 

33 def myanimelist_id(self) -> Optional[int]: 

34 """ 

35 :return: The myanimelist ID 

36 """ 

37 mal_id = self.extra_ids.get(ListService.MYANIMELIST) 

38 if mal_id is not None and mal_id.isdigit(): 

39 return int(mal_id) 

40 else: 

41 return None 

42 

43 @classmethod 

44 def from_query(cls, media_type: MediaType, data: Dict[str, Any]) \ 

45 -> "AnilistItem": 

46 """ 

47 Generates an AnilistItem from a dictionary generated by an APi query 

48 :param media_type: The media type of the item 

49 :param data: The data to use 

50 :return: The generated AnilistItem 

51 """ 

52 _media_subtype = data["format"] 

53 if _media_subtype is None: 53 ↛ 54line 53 didn't jump to line 54, because the condition on line 53 was never true

54 _media_subtype = "unknown" 

55 media_subtype = MediaSubType(_media_subtype.lower()) 

56 

57 _releasing_state = data["status"] 

58 if _releasing_state is None: 58 ↛ 59line 58 didn't jump to line 59, because the condition on line 58 was never true

59 _releasing_state = "unknown" 

60 releasing_state = ReleasingState(_releasing_state.lower()) 

61 

62 relations = {} 

63 for edge in data["relations"]["edges"]: 

64 node_id = edge["node"]["id"] 

65 node_type = MediaType(edge["node"]["type"].lower()) 

66 relation_type = MediaRelationType(edge["relationType"].lower()) 

67 relations[(node_type, node_id)] = relation_type 

68 

69 extra_ids = {} 

70 mal_id = data["idMal"] 

71 if mal_id is not None: 71 ↛ 76line 71 didn't jump to line 76, because the condition on line 71 was never false

72 extra_ids[ListService.MYANIMELIST] = str(mal_id) 

73 

74 # Special cases 

75 # TODO figure out how to get this info from the API 

76 doujins = [113181, 108352, 115939, 106890, 114527, 103608, 101236] 

77 if data["id"] in doujins: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true

78 data["title"]["romaji"] += " (Doujin)" 

79 new = [109302] 

80 if data["id"] in new: 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true

81 data["title"]["romaji"] += " (New)" 

82 

83 next_episode_data = data["nextAiringEpisode"] 

84 if next_episode_data is None: 84 ↛ 87line 84 didn't jump to line 87, because the condition on line 84 was never false

85 next_episode, next_episode_airing_time = None, None 

86 else: 

87 next_episode = next_episode_data["episode"] 

88 next_episode_airing_time = next_episode_data["airingAt"] 

89 

90 return AnilistItem( 

91 data["id"], 

92 ListService.ANILIST, 

93 extra_ids, 

94 media_type, 

95 media_subtype, 

96 data["title"]["english"], 

97 data["title"]["romaji"], 

98 data["coverImage"]["large"], 

99 data["chapters"], 

100 data["volumes"], 

101 data["episodes"], 

102 next_episode, 

103 next_episode_airing_time, 

104 releasing_state, 

105 relations 

106 )