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 os 

21import argparse 

22from puffotter.os import makedirs 

23from toktokkie.commands.Command import Command 

24from toktokkie.enums import IdType 

25from toktokkie.enums import MediaType 

26from toktokkie.Directory import Directory 

27from toktokkie.exceptions import InvalidMetadata, MissingMetadata 

28from toktokkie.metadata.tv.Tv import Tv 

29from toktokkie.metadata.tv.components.TvSeason import TvSeason 

30 

31 

32class MetadataAddCommand(Command): 

33 """ 

34 Class that encapsulates behaviour of the metadata-add command 

35 """ 

36 

37 @classmethod 

38 def name(cls) -> str: 

39 """ 

40 :return: The command name 

41 """ 

42 return "metadata-add" 

43 

44 @classmethod 

45 def help(cls) -> str: 

46 """ 

47 :return: The help message for the command 

48 """ 

49 return "Adds data to existing metadata" 

50 

51 @classmethod 

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

53 """ 

54 Prepares an argumentparser for this command 

55 :param parser: The parser to prepare 

56 :return: None 

57 """ 

58 parser.add_argument("directory", 

59 help="The directory to which to add metadata") 

60 

61 subparser = parser.add_subparsers(dest="mode") 

62 

63 tv_series_ids = [x.value for x in Tv.valid_id_types()] 

64 

65 multi_episode_parser = subparser.add_parser( 

66 "multi-episode", help="Add a multi-episode to a TV Series" 

67 ) 

68 multi_episode_parser.add_argument("id_type", choices=tv_series_ids, 

69 help="The ID type") 

70 multi_episode_parser.add_argument("season", type=int, 

71 help="The season of the episode") 

72 multi_episode_parser.add_argument("start_episode", type=int, 

73 help="The start episode") 

74 multi_episode_parser.add_argument("end_episode", type=int, 

75 help="The end episode") 

76 

77 exclude_parser = subparser.add_parser( 

78 "exclude", help="Add an excluded episode to a TV Series" 

79 ) 

80 exclude_parser.add_argument("id_type", choices=tv_series_ids, 

81 help="The ID type") 

82 exclude_parser.add_argument("season", type=int, 

83 help="The season of the episode") 

84 exclude_parser.add_argument("episode", type=int, 

85 help="The episode to exclude") 

86 

87 season_parser = subparser.add_parser( 

88 "season", help="Adds a season to a TV Series" 

89 ) 

90 season_parser.add_argument("season_name", 

91 help="The name of the season") 

92 

93 def execute(self): 

94 """ 

95 Executes the commands 

96 :return: None 

97 """ 

98 try: 

99 directory = Directory(self.args.directory) 

100 except (MissingMetadata, InvalidMetadata): 

101 self.logger.warning(f"Invalid directory {self.args.directory}") 

102 return 

103 

104 if directory.metadata.media_type() == MediaType.TV_SERIES: 

105 if self.args.mode == "multi-episode": 

106 self.add_multi_episode() 

107 elif self.args.mode == "exclude": 

108 self.add_exclude() 

109 elif self.args.mode == "season": 

110 self.add_season() 

111 else: # pragma: no cover 

112 self.logger.error("Invalid command") 

113 else: 

114 self.logger.warning(f"Invalid directory for action " 

115 f"{self.args.mode}") 

116 

117 def add_multi_episode(self): 

118 """ 

119 Adds a multi-episode to a tv series metadata 

120 :return: None 

121 """ 

122 meta = Tv(self.args.directory) 

123 meta.add_multi_episode( 

124 IdType(self.args.id_type), 

125 self.args.season, 

126 self.args.start_episode, 

127 self.args.end_episode 

128 ) 

129 meta.write() 

130 

131 def add_exclude(self): 

132 """ 

133 Adds an excluded episode to a tv series metadata 

134 :return: None 

135 """ 

136 meta = Tv(self.args.directory) 

137 meta.add_exclude( 

138 IdType(self.args.id_type), 

139 self.args.season, 

140 self.args.episode 

141 ) 

142 meta.write() 

143 

144 def add_season(self): 

145 """ 

146 Adds a season to a TV series metadata 

147 :return: None 

148 """ 

149 meta = Tv(self.args.directory) 

150 season_name = self.args.season_name 

151 

152 seasons = meta.seasons 

153 season_names = {x.name for x in meta.seasons} 

154 if season_name in season_names: 

155 self.logger.warning("Season already exists") 

156 return 

157 

158 season_path = os.path.join(meta.directory_path, season_name) 

159 

160 print(f"Enter IDs for season {season_name}") 

161 season_ids = meta.objectify_ids(meta.prompt_component_ids( 

162 meta.valid_id_types(), 

163 meta.stringify_ids(meta.ids), 

164 meta.create_id_fetcher(meta.directory_path) 

165 )) 

166 season = TvSeason( 

167 meta.directory_path, 

168 meta.ids, 

169 season_ids, 

170 season_name 

171 ) 

172 seasons.append(season) 

173 meta.seasons = seasons 

174 makedirs(season_path) 

175 meta.write()