From 5ea7dc592065d8fe8256224db58ef4f8a4b73c01 Mon Sep 17 00:00:00 2001 From: Dean Gardiner Date: Tue, 3 Dec 2013 18:54:57 +1300 Subject: [PATCH] Moved 'searcher.get_media_identifier' into season and episode libraries as 'library.identifier' --- .../core/media/show/library/episode/main.py | 29 +++++++++++++++ .../core/media/show/library/season/main.py | 12 ++++++ couchpotato/core/media/show/searcher/main.py | 37 ++----------------- couchpotato/core/plugins/matcher/main.py | 2 +- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/couchpotato/core/media/show/library/episode/main.py b/couchpotato/core/media/show/library/episode/main.py index 07344a02..e24338e2 100644 --- a/couchpotato/core/media/show/library/episode/main.py +++ b/couchpotato/core/media/show/library/episode/main.py @@ -17,10 +17,39 @@ class EpisodeLibraryPlugin(LibraryBase): default_dict = {'titles': {}, 'files':{}} def __init__(self): + addEvent('library.identifier', self.identifier) addEvent('library.add.episode', self.add) addEvent('library.update.episode', self.update) addEvent('library.update.episode_release_date', self.updateReleaseDate) + def identifier(self, library): + if library.get('type') != 'episode': + return + + identifier = { + 'season': None, + 'episode': None + } + + scene_map = library['info'].get('map_episode', {}).get('scene') + + if scene_map: + # Use scene mappings if they are available + identifier['season'] = scene_map.get('season') + identifier['episode'] = scene_map.get('episode') + else: + # Fallback to normal season/episode numbers + identifier['season'] = library.get('season') + identifier['episode'] = library.get('episode') + + + # Cast identifiers to integers + # TODO this will need changing to support identifiers with trailing 'a', 'b' characters + identifier['season'] = tryInt(identifier['season'], None) + identifier['episode'] = tryInt(identifier['episode'], None) + + return identifier + def add(self, attrs = {}, update_after = True): type = attrs.get('type', 'episode') primary_provider = attrs.get('primary_provider', 'thetvdb') diff --git a/couchpotato/core/media/show/library/season/main.py b/couchpotato/core/media/show/library/season/main.py index 75f19d5a..48d201ed 100644 --- a/couchpotato/core/media/show/library/season/main.py +++ b/couchpotato/core/media/show/library/season/main.py @@ -17,10 +17,22 @@ class SeasonLibraryPlugin(LibraryBase): default_dict = {'titles': {}, 'files':{}} def __init__(self): + addEvent('library.identifier', self.identifier) addEvent('library.add.season', self.add) addEvent('library.update.season', self.update) addEvent('library.update.season_release_date', self.updateReleaseDate) + def identifier(self, library): + if library.get('type') != 'season': + return + + season_num = tryInt(library['season_number'], None) + + return { + 'season': season_num, + 'episode': None + } + def add(self, attrs = {}, update_after = True): type = attrs.get('type', 'season') primary_provider = attrs.get('primary_provider', 'thetvdb') diff --git a/couchpotato/core/media/show/searcher/main.py b/couchpotato/core/media/show/searcher/main.py index 586462af..c4aca7b7 100644 --- a/couchpotato/core/media/show/searcher/main.py +++ b/couchpotato/core/media/show/searcher/main.py @@ -39,8 +39,6 @@ class ShowSearcher(Plugin): addEvent('searcher.correct_match', self.correctMatch) addEvent('searcher.correct_release', self.correctRelease) - addEvent('searcher.get_media_identifier', self.getMediaIdentifier) - def single(self, media, search_protocols = None, manual = False): show, season, episode = self.getLibraries(media['library']) @@ -175,9 +173,9 @@ class ShowSearcher(Plugin): return None # Add the identifier to search title - # TODO supporting other identifier formats - identifier = fireEvent('searcher.get_media_identifier', media['library'], single = True) + identifier = fireEvent('library.identifier', media['library'], single = True) + # TODO this needs to support other identifier formats if identifier['season']: title += ' S%02d' % identifier['season'] @@ -200,6 +198,7 @@ class ShowSearcher(Plugin): if not fireEvent('searcher.correct_words', release['name'], media, single = True): return False + # TODO Matching is quite costly, maybe we should be caching release matches somehow? (also look at caper optimizations) match = fireEvent('matcher.best', release, media, quality, single = True) if match: return match.weight @@ -224,36 +223,6 @@ class ShowSearcher(Plugin): return True - # TODO move this somewhere else - def getMediaIdentifier(self, media_library): - if media_library['type'] not in ['show', 'season', 'episode']: - return None - - identifier = { - 'season': None, - 'episode': None - } - - if media_library['type'] == 'episode': - map_episode = media_library['info'].get('map_episode') - - if map_episode and 'scene' in map_episode: - identifier['season'] = map_episode['scene'].get('season') - identifier['episode'] = map_episode['scene'].get('episode') - else: - # TODO xem mapping? - identifier['season'] = media_library.get('season_number') - identifier['episode'] = media_library.get('episode_number') - - if media_library['type'] == 'season': - identifier['season'] = media_library.get('season_number') - - # Try cast identifier values to integers - identifier['season'] = tryInt(identifier['season'], None) - identifier['episode'] = tryInt(identifier['episode'], None) - - return identifier - def getLibraries(self, library): if 'related_libraries' not in library: log.warning("'related_libraries' missing from media library, unable to continue searching") diff --git a/couchpotato/core/plugins/matcher/main.py b/couchpotato/core/plugins/matcher/main.py index 3f06628b..0baba03c 100644 --- a/couchpotato/core/plugins/matcher/main.py +++ b/couchpotato/core/plugins/matcher/main.py @@ -50,7 +50,7 @@ class Matcher(Plugin): return set([key for key, value in tags.items() if None not in value]) == set(found_tags) def correctIdentifier(self, chain, media): - required_id = fireEvent('searcher.get_media_identifier', media['library'], single = True) + required_id = fireEvent('library.identifier', media['library'], single = True) if 'identifier' not in chain.info: return False