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 

21from typing import Dict, List, Any 

22from puffotter.os import listdir 

23from toktokkie.exceptions import InvalidMetadata 

24from toktokkie.metadata.base.components.JsonComponent import JsonComponent 

25from toktokkie.enums import IdType 

26from toktokkie.metadata.base.IdHelper import IdHelper 

27 

28 

29class TvSeason(JsonComponent): 

30 """ 

31 Class that models a season of a TV Series 

32 """ 

33 

34 def __init__( 

35 self, 

36 parent_path: str, 

37 parent_ids: Dict[IdType, List[str]], 

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

39 name: str 

40 ): 

41 """ 

42 Initializes the TvSeason object 

43 :param parent_path: The path to the parent metadata directory 

44 :param parent_ids: The IDs of the parent metadata 

45 :param ids: The specific IDs for this season 

46 :param name: The name of the season 

47 """ 

48 self.parent_path = parent_path 

49 self.parent_ids = parent_ids 

50 

51 self.name = name 

52 self.path = os.path.join(parent_path, self.name) 

53 

54 self.ids = IdHelper.fill_ids(ids, [], parent_ids) 

55 

56 @property 

57 def json(self) -> Dict[str, Any]: 

58 """ 

59 :return: A JSON-compatible dictionary representing this object 

60 """ 

61 return { 

62 "name": self.name, 

63 "ids": IdHelper.stringify_ids(IdHelper.minimize_ids( 

64 self.ids, self.parent_ids 

65 )) 

66 } 

67 

68 @classmethod 

69 def from_json( 

70 cls, 

71 parent_path: str, 

72 parent_ids: Dict[IdType, List[str]], 

73 json_data: Dict[str, Any] 

74 ): 

75 """ 

76 Generates a TvSeason object based on JSON data 

77 :param parent_path: The path to the parent metadata directory 

78 :param parent_ids: The IDs of the parent metadata 

79 :param json_data: The JSON data 

80 :return: The generated TvSeason object 

81 :raises InvalidMetadataException: If the provided JSON is invalid 

82 """ 

83 try: 

84 return cls( 

85 parent_path, 

86 parent_ids, 

87 IdHelper.objectify_ids(json_data["ids"]), 

88 json_data["name"] 

89 ) 

90 except KeyError as e: 

91 raise InvalidMetadata(f"Attribute missing: {e}") 

92 

93 @property 

94 def season_number(self) -> int: 

95 """ 

96 :return: The season number of the season 

97 """ 

98 if self.name.lower().startswith("season "): 

99 return int(self.name.lower().split("season")[1]) 

100 else: 

101 return 0 

102 

103 def is_spinoff(self) -> bool: 

104 """ 

105 :return: Whether or not this season is a spinoff 

106 """ 

107 return self.parent_ids.get(IdType.TVDB) != self.ids.get(IdType.TVDB) 

108 

109 @property 

110 def episode_names(self) -> List[str]: 

111 """ 

112 :return: A sorted list of episode names 

113 """ 

114 return [ 

115 x[0].rsplit(".", 1)[0] for x in listdir(self.path, no_dirs=True) 

116 ]