Hide keyboard shortcuts

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> 

3 

4This file is part of toktokkie. 

5 

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. 

10 

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. 

15 

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

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

18LICENSE""" 

19 

20import argparse 

21from typing import List, Dict 

22from toktokkie.enums import MediaType, IdType 

23from toktokkie.commands.Command import Command 

24from toktokkie.Directory import Directory 

25from toktokkie.utils.IdFetcher import IdFetcher 

26from toktokkie.metadata.tv.Tv import Tv 

27 

28 

29class IdFetchCommand(Command): 

30 """ 

31 Class that encapsulates behaviour of the id-fetch command 

32 """ 

33 

34 @classmethod 

35 def name(cls) -> str: 

36 """ 

37 :return: The command name 

38 """ 

39 return "id-fetch" 

40 

41 @classmethod 

42 def help(cls) -> str: 

43 """ 

44 :return: The help message for the command 

45 """ 

46 return "Fills out IDs based on existing IDs" 

47 

48 @classmethod 

49 def prepare_parser(cls, parser: argparse.ArgumentParser): 

50 """ 

51 Prepares an argumentparser for this command 

52 :param parser: The parser to prepare 

53 :return: None 

54 """ 

55 cls.add_directories_arg(parser) 

56 

57 def execute(self): 

58 """ 

59 Executes the commands 

60 :return: None 

61 """ 

62 for directory in Directory.load_directories(self.args.directories): 

63 metadata = directory.metadata 

64 fetcher = IdFetcher(metadata.name, metadata.media_type()) 

65 ids = metadata.ids 

66 

67 self.logger.info(f"Fetching IDs for {metadata.name}") 

68 

69 if metadata.media_type() == MediaType.TV_SERIES: 

70 self.fetch_season_ids(metadata, fetcher) 

71 # TODO implement ID fetching for all components 

72 # elif metadata.media_type() == MediaType.MUSIC_ARTIST: 

73 # self.fetch_theme_song_ids() 

74 # self.fetch_album_ids() 

75 # elif metadata.media_type() == MediaType.BOOK_SERIES: 

76 # self.fetch_volume_ids() 

77 

78 self.fill_ids(ids, metadata.valid_id_types(), fetcher) 

79 metadata.ids = ids 

80 metadata.write() 

81 

82 def fetch_season_ids(self, metadata: Tv, fetcher: IdFetcher): 

83 """ 

84 Fetches season IDs for a Tv metadata 

85 :param metadata: The Tv metadata 

86 :param fetcher: The ID fetcher to use 

87 :return: None 

88 """ 

89 new_seasons = [] 

90 for season in metadata.seasons: 

91 season_ids = season.ids 

92 self.fill_ids(season_ids, metadata.valid_id_types(), fetcher) 

93 season.ids = season_ids 

94 new_seasons.append(season) 

95 metadata.seasons = new_seasons 

96 metadata.write() 

97 

98 def fill_ids( 

99 self, 

100 ids: Dict[IdType, List[str]], 

101 valid_id_types: List[IdType], 

102 fetcher: IdFetcher 

103 ): 

104 """ 

105 Fills IDs using an ID Fetcher 

106 :param ids: The IDs to fill 

107 :param valid_id_types: List of valid ID types 

108 :param fetcher: The IdFetcher object to use 

109 :return: None 

110 """ 

111 for id_type in valid_id_types: 

112 if len(ids[id_type]) == 0: 

113 results = fetcher.fetch_ids(id_type, ids) 

114 if results is not None: 

115 ids[id_type] = results 

116 self.logger.info(f"Found {id_type.value} ids: " 

117 f"{results}")