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 

20from typing import Dict, Optional 

21from imdb import IMDb, IMDbDataAccessError 

22from imdb.Movie import Movie 

23 

24 

25class ImdbCache: 

26 """ 

27 Class that caches IMDB results 

28 """ 

29 

30 episode_cache: Dict[str, Dict[int, Dict[int, Movie]]] = {} 

31 """ 

32 The cache for episodes 

33 Format: {imdb_id: {season: {episode: episode_info}}} 

34 """ 

35 

36 imdb_api = IMDb() 

37 """ 

38 The IMDb API object 

39 """ 

40 

41 @staticmethod 

42 def load_episode( 

43 imdb_id: str, 

44 season: int, 

45 episode: int 

46 ) -> Optional[Movie]: 

47 """ 

48 Loads information about a particular episode for an IMDB ID 

49 :param imdb_id: The IMDB ID 

50 :param season: The season 

51 :param episode: The episode 

52 """ 

53 if season == 0: 

54 season = -1 

55 

56 imdb_id_int = imdb_id.replace("tt", "") 

57 existing = ImdbCache.episode_cache\ 

58 .get(imdb_id_int, {})\ 

59 .get(season, {})\ 

60 .get(episode) 

61 

62 if existing is None and imdb_id_int not in ImdbCache.episode_cache: 

63 ImdbCache.episode_cache[imdb_id_int] = {} 

64 

65 try: 

66 data = ImdbCache.imdb_api\ 

67 .get_movie_episodes(imdb_id_int)["data"]["episodes"] 

68 except IMDbDataAccessError: 

69 data = {} 

70 for _season, episodes in data.items(): 

71 ImdbCache.episode_cache[imdb_id_int][_season] = {} 

72 for number, _episode in episodes.items(): 

73 ImdbCache.episode_cache[imdb_id_int][_season][number] \ 

74 = _episode 

75 return ImdbCache.load_episode(imdb_id, season, episode) 

76 else: 

77 return existing 

78 

79 @staticmethod 

80 def load_episode_name( 

81 imdb_id: str, 

82 season: int, 

83 episode: int 

84 ) -> str: 

85 """ 

86 Loads a name for an IMDB ID and season/episode 

87 :param imdb_id: The IMDB ID 

88 :param season: The season 

89 :param episode: The episode 

90 """ 

91 episode_info = ImdbCache.load_episode(imdb_id, season, episode) 

92 if episode_info is None: 

93 return f"Episode {episode}" 

94 else: 

95 return episode_info["title"]