diff --git a/couchpotato/core/media/show/_base/main.py b/couchpotato/core/media/show/_base/main.py index b1cbe66..ca3abfa 100644 --- a/couchpotato/core/media/show/_base/main.py +++ b/couchpotato/core/media/show/_base/main.py @@ -42,6 +42,12 @@ class ShowBase(MediaBase): 'shows': array, shows found, }"""} }) + addApiView('show.refresh', self.refresh, docs = { + 'desc': 'Refresh a show, season or episode by id', + 'params': { + 'id': {'desc': 'Show, Season or Episode ID(s) you want to refresh.', 'type': 'int (comma separated)'}, + } + }) addApiView('show.add', self.addView, docs = { 'desc': 'Add new movie to the wanted list', 'params': { @@ -53,6 +59,26 @@ class ShowBase(MediaBase): addEvent('show.add', self.add) + def refresh(self, id = '', **kwargs): + db = get_session() + + for x in splitString(id): + media = db.query(Media).filter_by(id = x).first() + + if media: + # Get current selected title + default_title = '' + for title in media.library.titles: + if title.default: default_title = title.title + + fireEvent('notify.frontend', type = '%s.busy.%s' % (media.type, x), data = True) + fireEventAsync('library.update.%s' % media.type, identifier = media.library.identifier, default_title = default_title, force = True, on_complete = self.createOnComplete(x)) + + db.expire_all() + return { + 'success': True, + } + def search(self, q = '', **kwargs): cache_key = u'%s/%s' % (__name__, simplifyString(q)) shows = Env.get('cache').get(cache_key) @@ -273,12 +299,12 @@ class ShowBase(MediaBase): db.expire_all() return show_dict - def createOnComplete(self, show_id): + def createOnComplete(self, id): def onComplete(): db = get_session() - show = db.query(Media).filter_by(id = show_id).first() - fireEventAsync('show.searcher.single', show.to_dict(self.default_dict), on_complete = self.createNotifyFront(show_id)) + media = db.query(Media).filter_by(id = id).first() + fireEventAsync('show.searcher.%s' % media.type, media.to_dict(self.default_dict), on_complete = self.createNotifyFront(id)) db.expire_all() return onComplete diff --git a/couchpotato/core/media/show/searcher/main.py b/couchpotato/core/media/show/searcher/main.py index 956490a..abae0d4 100644 --- a/couchpotato/core/media/show/searcher/main.py +++ b/couchpotato/core/media/show/searcher/main.py @@ -1,4 +1,8 @@ +from couchpotato import get_session +from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.variable import getTitle from couchpotato.core.logger import CPLog +from couchpotato.core.media._base.searcher.main import SearchSetupError from couchpotato.core.plugins.base import Plugin log = CPLog(__name__) @@ -9,4 +13,75 @@ class ShowSearcher(Plugin): in_progress = False def __init__(self): + super(ShowSearcher, self).__init__() + + addEvent('show.searcher.show', self.show) + addEvent('show.searcher.season', self.season) + addEvent('show.searcher.episode', self.episode) + + def _get_search_protocols(self): + try: + return fireEvent('searcher.protocols', single = True) + except SearchSetupError: + return None + + def show(self, show, search_protocols = None): + # TODO - handover to searching for seasons pass + + def season(self, season, search_protocols = None): + # Find out search type + search_protocols = self._get_search_protocols() if not search_protocols else None + if search_protocols is None: + return + + done_status = fireEvent('status.get', 'done', single = True) + + if not season['profile'] or season['status_id'] == done_status.get('id'): + log.debug('Season doesn\'t have a profile or already done, assuming in manage tab.') + return + + db = get_session() + + pre_releases = fireEvent('quality.pre_releases', single = True) + available_status, ignored_status, failed_status = fireEvent('status.get', ['available', 'ignored', 'failed'], single = True) + + found_releases = [] + too_early_to_search = [] + + default_title = getTitle(season['library']) + if not default_title: + log.error('No proper info found for season, removing it from library to cause it from having more issues.') + #fireEvent('season.delete', season['id'], single = True) + return + + fireEvent('notify.frontend', type = 'show.searcher.started.%s' % season['id'], data = True, message = 'Searching for "%s"' % default_title) + + + def episode(self, episode, search_protocols = None): + # Find out search type + search_protocols = self._get_search_protocols() if not search_protocols else None + if search_protocols is None: + return + + done_status = fireEvent('status.get', 'done', single = True) + + if not episode['profile'] or episode['status_id'] == done_status.get('id'): + log.debug('Episode doesn\'t have a profile or already done, assuming in manage tab.') + return + + db = get_session() + + pre_releases = fireEvent('quality.pre_releases', single = True) + available_status, ignored_status, failed_status = fireEvent('status.get', ['available', 'ignored', 'failed'], single = True) + + found_releases = [] + too_early_to_search = [] + + default_title = getTitle(episode['library']) + if not default_title: + log.error('No proper info found for episode, removing it from library to cause it from having more issues.') + #fireEvent('episode.delete', episode['id'], single = True) + return + + fireEvent('notify.frontend', type = 'show.searcher.started.%s' % episode['id'], data = True, message = 'Searching for "%s"' % default_title)