Coverage for otaku_info/background/mangadex.py: 18%

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

58 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 time 

21from typing import Optional, List, Tuple 

22from jerrycan.base import app, db 

23from otaku_info.db.MediaItem import MediaItem 

24from otaku_info.db.MediaIdMapping import MediaIdMapping 

25from otaku_info.enums import ListService, MediaType 

26from otaku_info.external.entities.AnimeListItem import AnimeListItem 

27from otaku_info.external.entities.MangadexItem import MangadexItem 

28from otaku_info.external.mangadex import fetch_all_mangadex_items 

29from otaku_info.external.anilist import load_anilist_info 

30from otaku_info.external.myanimelist import load_myanimelist_item 

31from otaku_info.utils.object_conversion import anime_list_item_to_media_item, \ 

32 mangadex_item_to_media_item 

33 

34 

35def update_mangadex_data(): 

36 """ 

37 Loads the newest mangadex information and updates the mangadex entries in 

38 the database. 

39 :return: None 

40 """ 

41 start_time = time.time() 

42 app.logger.info("Starting Mangadex Update") 

43 

44 _existing_items = MediaItem.query.all() 

45 existing_items = { 

46 service: { 

47 x.service_id: x 

48 for x in _existing_items 

49 if x.service == service 

50 } 

51 for service in ListService 

52 } 

53 fetched_items = fetch_all_mangadex_items() 

54 

55 mangadex_items: List[Tuple[MediaItem, MangadexItem]] = [] 

56 for mangadex_item in fetched_items: 

57 media_item = mangadex_item_to_media_item(mangadex_item) 

58 app.logger.debug(f"Upserting mangadex item {media_item.english_title}") 

59 media_item = db.session.merge(media_item) 

60 __add_id_mappings(media_item, mangadex_item) 

61 mangadex_items.append((media_item, mangadex_item)) 

62 db.session.commit() 

63 

64 for media_item, mangadex_item in mangadex_items: 

65 

66 for service in [ListService.ANILIST, ListService.MYANIMELIST]: 

67 

68 service_id = mangadex_item.external_ids.get(service) 

69 existing = existing_items[service].get(service_id) 

70 

71 if service_id is None: 

72 continue 

73 if existing is not None: 

74 __add_id_mappings(existing, mangadex_item) 

75 continue 

76 

77 data: Optional[AnimeListItem] = None 

78 if service == ListService.ANILIST: 

79 data = load_anilist_info( 

80 int(service_id), MediaType.MANGA 

81 ) 

82 elif service == ListService.MYANIMELIST: 

83 data = load_myanimelist_item( 

84 int(service_id), MediaType.MANGA 

85 ) 

86 if data is not None: 

87 anime_item = anime_list_item_to_media_item(data) 

88 title = anime_item.title 

89 app.logger.debug(f"Upserting {service.value} item {title}") 

90 anime_item = db.session.merge(anime_item) 

91 existing_items[service][service_id] = anime_item 

92 __add_id_mappings(anime_item, mangadex_item) 

93 db.session.commit() 

94 

95 app.logger.info(f"Finished Mangadex Update in " 

96 f"{time.time() - start_time}s.") 

97 

98 

99def __add_id_mappings(media_item: MediaItem, mangadex_item: MangadexItem): 

100 """ 

101 Adds ID mappings to a media item 

102 :param media_item: The media item for which to add the mapping 

103 :param mangadex_item: The mangadex ID containing the mapping information 

104 :return: None 

105 """ 

106 ids = mangadex_item.external_ids 

107 ids[ListService.MANGADEX] = mangadex_item.mangadex_id 

108 

109 for service, _id in ids.items(): 

110 if service == media_item.service: 

111 continue 

112 

113 mapping = MediaIdMapping( 

114 parent_service=media_item.service, 

115 parent_service_id=media_item.service_id, 

116 media_type=media_item.media_type, 

117 service=service, 

118 service_id=_id 

119 ) 

120 app.logger.debug(f"Upserting ID mapping " 

121 f"{media_item.service.value}:{media_item.service_id} " 

122 f"-> {service.value}:{_id}") 

123 db.session.merge(mapping)