diff --git a/couchpotato/core/media/show/_base/main.py b/couchpotato/core/media/show/_base/main.py index 7d7e637..f96ff61 100644 --- a/couchpotato/core/media/show/_base/main.py +++ b/couchpotato/core/media/show/_base/main.py @@ -17,7 +17,6 @@ log = CPLog(__name__) class ShowBase(MediaBase): _type = 'show' - query_condenser = QueryCondenser() def __init__(self): super(ShowBase, self).__init__() @@ -35,8 +34,6 @@ class ShowBase(MediaBase): addEvent('show.add', self.add) addEvent('show.update_info', self.updateInfo) - addEvent('media.search_query', self.query) - def addView(self, **kwargs): add_dict = self.add(params = kwargs) @@ -255,25 +252,3 @@ class ShowBase(MediaBase): log.error('Failed update media: %s', traceback.format_exc()) return {} - - def query(self, media, first = True, condense = True, **kwargs): - if media.get('type') != 'show': - return - - titles = media['info']['titles'] - - if condense: - # Use QueryCondenser to build a list of optimal search titles - condensed_titles = self.query_condenser.distinct(titles) - - if condensed_titles: - # Use condensed titles if we got a valid result - titles = condensed_titles - else: - # Fallback to simplifying titles - titles = [simplifyString(title) for title in titles] - - if first: - return titles[0] if titles else None - - return titles diff --git a/couchpotato/core/media/show/episode.py b/couchpotato/core/media/show/episode.py index c4a0a1a..0f05f28 100644 --- a/couchpotato/core/media/show/episode.py +++ b/couchpotato/core/media/show/episode.py @@ -13,9 +13,6 @@ autoload = 'Episode' class Episode(MediaBase): def __init__(self): - addEvent('media.search_query', self.query) - addEvent('media.identifier', self.identifier) - addEvent('show.episode.add', self.add) addEvent('show.episode.update_info', self.updateInfo) @@ -85,63 +82,3 @@ class Episode(MediaBase): self.getPoster(image_urls, existing_files) return episode - - def query(self, library, first = True, condense = True, include_identifier = True, **kwargs): - if library is list or library.get('type') != 'episode': - return - - # Get the titles of the season - if not library.get('related_libraries', {}).get('season', []): - log.warning('Invalid library, unable to determine title.') - return - - titles = fireEvent( - 'media.search_query', - library['related_libraries']['season'][0], - first=False, - include_identifier=include_identifier, - condense=condense, - - single=True - ) - - identifier = fireEvent('media.identifier', library, single = True) - - # Add episode identifier to titles - if include_identifier and identifier.get('episode'): - titles = [title + ('E%02d' % identifier['episode']) for title in titles] - - - if first: - return titles[0] if titles else None - - return titles - - - def identifier(self, media): - if media.get('type') != 'episode': - return - - identifier = { - 'season': None, - 'episode': None - } - - scene_map = media['info'].get('map_episode', {}).get('scene') - - if scene_map: - # Use scene mappings if they are available - identifier['season'] = scene_map.get('season_nr') - identifier['episode'] = scene_map.get('episode_nr') - else: - # Fallback to normal season/episode numbers - identifier['season'] = media['info'].get('season_number') - identifier['episode'] = media['info'].get('number') - - - # 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 diff --git a/couchpotato/core/media/show/library/__init__.py b/couchpotato/core/media/show/library/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/couchpotato/core/media/show/library/episode.py b/couchpotato/core/media/show/library/episode.py new file mode 100644 index 0000000..7161f2d --- /dev/null +++ b/couchpotato/core/media/show/library/episode.py @@ -0,0 +1,71 @@ +from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.variable import tryInt +from couchpotato.core.logger import CPLog +from couchpotato.core.media._base.library.base import LibraryBase + +log = CPLog(__name__) + +autoload = 'EpisodeLibraryPlugin' + + +class EpisodeLibraryPlugin(LibraryBase): + def __init__(self): + addEvent('library.query', self.query) + addEvent('library.identifier', self.identifier) + + def query(self, media, first = True, condense = True, include_identifier = True, **kwargs): + if media.get('type') != 'episode': + return + + related = fireEvent('library.related', media, single = True) + + # Get season titles + titles = fireEvent( + 'library.query', related['season'], + + first = False, + include_identifier = include_identifier, + condense = condense, + + single = True + ) + + # Add episode identifier to titles + if include_identifier: + identifier = fireEvent('library.identifier', media, single = True) + + if identifier and identifier.get('episode'): + titles = [title + ('E%02d' % identifier['episode']) for title in titles] + + if first: + return titles[0] if titles else None + + return titles + + def identifier(self, media): + if media.get('type') != 'episode': + return + + identifier = { + 'season': None, + 'episode': None + } + + # TODO identifier mapping + # scene_map = media['info'].get('map_episode', {}).get('scene') + + # if scene_map: + # # Use scene mappings if they are available + # identifier['season'] = scene_map.get('season_nr') + # identifier['episode'] = scene_map.get('episode_nr') + # else: + # Fallback to normal season/episode numbers + identifier['season'] = media['info'].get('season_number') + identifier['episode'] = media['info'].get('number') + + # 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 diff --git a/couchpotato/core/media/show/library/season.py b/couchpotato/core/media/show/library/season.py new file mode 100644 index 0000000..228d143 --- /dev/null +++ b/couchpotato/core/media/show/library/season.py @@ -0,0 +1,52 @@ +from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.variable import tryInt +from couchpotato.core.logger import CPLog +from couchpotato.core.media._base.library.base import LibraryBase + +log = CPLog(__name__) + +autoload = 'SeasonLibraryPlugin' + + +class SeasonLibraryPlugin(LibraryBase): + def __init__(self): + addEvent('library.query', self.query) + addEvent('library.identifier', self.identifier) + + def query(self, media, first = True, condense = True, include_identifier = True, **kwargs): + if media.get('type') != 'season': + return + + related = fireEvent('library.related', media, single = True) + + # Get show titles + titles = fireEvent( + 'library.query', related['show'], + + first = False, + condense = condense, + + single = True + ) + + # TODO map_names + + # Add season identifier to titles + if include_identifier: + identifier = fireEvent('library.identifier', media, single = True) + + if identifier and identifier.get('season') is not None: + titles = [title + (' S%02d' % identifier['season']) for title in titles] + + if first: + return titles[0] if titles else None + + return titles + + def identifier(self, media): + if media.get('type') != 'season': + return + + return { + 'season': tryInt(media['info']['number'], None) + } diff --git a/couchpotato/core/media/show/library/show.py b/couchpotato/core/media/show/library/show.py new file mode 100644 index 0000000..168804c --- /dev/null +++ b/couchpotato/core/media/show/library/show.py @@ -0,0 +1,38 @@ +from couchpotato.core.event import addEvent +from couchpotato.core.helpers.encoding import simplifyString +from couchpotato.core.logger import CPLog +from couchpotato.core.media._base.library.base import LibraryBase +from qcond import QueryCondenser + +log = CPLog(__name__) + +autoload = 'ShowLibraryPlugin' + + +class ShowLibraryPlugin(LibraryBase): + query_condenser = QueryCondenser() + + def __init__(self): + addEvent('library.query', self.query) + + def query(self, media, first = True, condense = True, include_identifier = True, **kwargs): + if media.get('type') != 'show': + return + + titles = media['info']['titles'] + + if condense: + # Use QueryCondenser to build a list of optimal search titles + condensed_titles = self.query_condenser.distinct(titles) + + if condensed_titles: + # Use condensed titles if we got a valid result + titles = condensed_titles + else: + # Fallback to simplifying titles + titles = [simplifyString(title) for title in titles] + + if first: + return titles[0] if titles else None + + return titles diff --git a/couchpotato/core/media/show/season.py b/couchpotato/core/media/show/season.py index 9c6f8f0..a2dde78 100644 --- a/couchpotato/core/media/show/season.py +++ b/couchpotato/core/media/show/season.py @@ -13,9 +13,6 @@ autoload = 'Season' class Season(MediaBase): def __init__(self): - addEvent('media.search_query', self.query) - addEvent('media.identifier', self.identifier) - addEvent('show.season.add', self.add) addEvent('show.season.update_info', self.updateInfo) @@ -87,51 +84,3 @@ class Season(MediaBase): self.getPoster(image_urls, existing_files) return season - - def query(self, library, first = True, condense = True, include_identifier = True, **kwargs): - if library is list or library.get('type') != 'season': - return - - # Get the titles of the show - if not library.get('related_libraries', {}).get('show', []): - log.warning('Invalid library, unable to determine title.') - return - - titles = fireEvent( - 'media._search_query', - library['related_libraries']['show'][0], - first=False, - condense=condense, - - single=True - ) - - # Add season map_names if they exist - if 'map_names' in library['info']: - season_names = library['info']['map_names'].get(str(library['season_number']), {}) - - # Add titles from all locations - # TODO only add name maps from a specific location - for location, names in season_names.items(): - titles += [name for name in names if name and name not in titles] - - - identifier = fireEvent('media.identifier', library, single = True) - - # Add season identifier to titles - if include_identifier and identifier.get('season') is not None: - titles = [title + (' S%02d' % identifier['season']) for title in titles] - - - if first: - return titles[0] if titles else None - - return titles - - def identifier(self, library): - if library.get('type') != 'season': - return - - return { - 'season': tryInt(library['season_number'], None) - }