From 3767a2411a98748552618001eb87b418ebbc0d97 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 8 Sep 2012 14:26:15 +0200 Subject: [PATCH] Movie check snatched to renamer plugin --- couchpotato/core/plugins/renamer/__init__.py | 16 ++++ couchpotato/core/plugins/renamer/main.py | 73 ++++++++++++++++++- couchpotato/core/plugins/searcher/__init__.py | 15 ---- couchpotato/core/plugins/searcher/main.py | 72 +----------------- 4 files changed, 88 insertions(+), 88 deletions(-) diff --git a/couchpotato/core/plugins/renamer/__init__.py b/couchpotato/core/plugins/renamer/__init__.py index 21076d68..c3afbc8b 100644 --- a/couchpotato/core/plugins/renamer/__init__.py +++ b/couchpotato/core/plugins/renamer/__init__.py @@ -73,6 +73,22 @@ config = [{ 'description': 'Cleanup leftover files after successful rename.', 'default': False, }, + { + 'advanced': True, + 'name': 'run_every', + 'label': 'Run every', + 'default': 1, + 'type': 'int', + 'unit': 'min(s)', + 'description': 'Detect movie status every X minutes. Will start the renamer if movie is completed or handle failed download if these options are enabled', + }, + { + 'advanced': True, + 'name': 'next_on_failed', + 'default': True, + 'type': 'bool', + 'description': 'Try the next best release for a movie after a download failed.', + }, { 'name': 'move_leftover', 'type': 'bool', diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py index 437374ea..8e5b66c4 100644 --- a/couchpotato/core/plugins/renamer/main.py +++ b/couchpotato/core/plugins/renamer/main.py @@ -6,13 +6,13 @@ from couchpotato.core.helpers.request import jsonified from couchpotato.core.helpers.variable import getExt, mergeDicts, getTitle from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin -from couchpotato.core.settings.model import Library, File, Profile +from couchpotato.core.settings.model import Library, File, Profile, Release from couchpotato.environment import Env +import errno import os import re import shutil import traceback -import errno log = CPLog(__name__) @@ -28,9 +28,11 @@ class Renamer(Plugin): }) addEvent('renamer.scan', self.scan) + addEvent('renamer.check_snatched', self.checkSnatched) + addEvent('app.load', self.scan) - #fireEvent('schedule.interval', 'renamer.scan', self.scan, minutes = self.conf('run_every')) + fireEvent('schedule.interval', 'renamer.check_snatched', self.checkSnatched, minutes = self.conf('run_every')) def scanView(self): @@ -491,3 +493,68 @@ class Renamer(Plugin): os.rmdir(folder) except: loge('Couldn\'t remove empty directory %s: %s', (folder, traceback.format_exc())) + + def checkSnatched(self): + snatched_status = fireEvent('status.get', 'snatched', single = True) + ignored_status = fireEvent('status.get', 'ignored', single = True) + failed_status = fireEvent('status.get', 'failed', single = True) + + done_status = fireEvent('status.get', 'done', single = True) + + db = get_session() + rels = db.query(Release).filter_by(status_id = snatched_status.get('id')) + + if rels: + log.debug('Checking status snatched releases...') + + scan_required = False + + for rel in rels: + + # Get current selected title + default_title = '' + for title in rel.movie.library.titles: + if title.default: default_title = title.title + + # Check if movie has already completed and is manage tab (legacy db correction) + if rel.movie.status_id == done_status.get('id'): + log.debug('Found a completed movie with a snatched release : %s. Setting release status to ignored...' , default_title) + rel.status_id = ignored_status.get('id') + db.commit() + continue + + item = {} + for info in rel.info: + item[info.identifier] = info.value + + movie_dict = fireEvent('movie.get', rel.movie_id, single = True) + + # check status + downloadstatus = fireEvent('download.status', data = item, movie = movie_dict, single = True) + if not downloadstatus: + log.debug('Download status functionality is not implemented for active downloaders.') + scan_required = True + else: + log.debug('Download status: %s' , downloadstatus) + + if downloadstatus == 'failed': + if self.conf('next_on_failed'): + fireEvent('searcher.try_next_release', movie_id = rel.movie_id) + else: + rel.status_id = failed_status.get('id') + db.commit() + + log.info('Download of %s failed.', item['name']) + + elif downloadstatus == 'completed': + log.info('Download of %s completed!', item['name']) + scan_required = True + + elif downloadstatus == 'not_found': + log.info('%s not found in downloaders', item['name']) + rel.status_id = ignored_status.get('id') + db.commit() + + # Note that Queued, Downloading, Paused, Repair and Unpackimg are also available as status for SabNZBd + if scan_required: + fireEvent('renamer.scan') diff --git a/couchpotato/core/plugins/searcher/__init__.py b/couchpotato/core/plugins/searcher/__init__.py index e2c2bb83..f499e2bd 100644 --- a/couchpotato/core/plugins/searcher/__init__.py +++ b/couchpotato/core/plugins/searcher/__init__.py @@ -39,21 +39,6 @@ config = [{ 'type': 'dropdown', 'values': [('usenet & torrents', 'both'), ('usenet', 'nzb'), ('torrents', 'torrent')], }, - { - 'advanced': True, - 'name': 'run_every', - 'label': 'Run every', - 'default': 1, - 'type': 'int', - 'unit': 'min(s)', - 'description': 'Detect movie status every X minutes. Will start the renamer if movie is completed or handle failed download if these options are enabled', - }, - { - 'name': 'next_on_failed', - 'default': True, - 'type': 'bool', - 'description': 'Try the next best release for a movie after a download failed.', - }, ], }, { 'tab': 'searcher', diff --git a/couchpotato/core/plugins/searcher/main.py b/couchpotato/core/plugins/searcher/main.py index c464741f..d088c66c 100644 --- a/couchpotato/core/plugins/searcher/main.py +++ b/couchpotato/core/plugins/searcher/main.py @@ -3,7 +3,7 @@ from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.encoding import simplifyString, toUnicode from couchpotato.core.helpers.request import jsonified, getParam -from couchpotato.core.helpers.variable import md5, getImdb, getTitle +from couchpotato.core.helpers.variable import md5, getTitle from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin from couchpotato.core.settings.model import Movie, Release, ReleaseInfo @@ -27,7 +27,7 @@ class Searcher(Plugin): addEvent('searcher.single', self.single) addEvent('searcher.correct_movie', self.correctMovie) addEvent('searcher.download', self.download) - addEvent('searcher.check_snatched', self.checkSnatched) + addEvent('searcher.try_next_release', self.tryNextRelease) addApiView('searcher.try_next', self.tryNextReleaseView, docs = { 'desc': 'Marks the snatched results as ignored and try the next best release', @@ -38,7 +38,6 @@ class Searcher(Plugin): # Schedule cronjob fireEvent('schedule.cron', 'searcher.all', self.allMovies, day = self.conf('cron_day'), hour = self.conf('cron_hour'), minute = self.conf('cron_minute')) - fireEvent('schedule.interval', 'searcher.check_snatched', self.checkSnatched, minutes = self.conf('run_every')) def allMovies(self): @@ -431,73 +430,6 @@ class Searcher(Plugin): return False - def checkSnatched(self): - snatched_status = fireEvent('status.get', 'snatched', single = True) - ignored_status = fireEvent('status.get', 'ignored', single = True) - failed_status = fireEvent('status.get', 'failed', single = True) - - done_status = fireEvent('status.get', 'done', single = True) - - db = get_session() - rels = db.query(Release).filter_by(status_id = snatched_status.get('id')) - - if rels: - log.debug('Checking status snatched releases...') - - scanrequired = False - - for rel in rels: - - # Get current selected title - default_title = '' - for title in rel.movie.library.titles: - if title.default: default_title = title.title - - log.debug('Checking snatched movie: %s' , default_title) - - # Check if movie has already completed and is manage tab (legacy db correction) - if rel.movie.status_id == done_status.get('id'): - log.debug('Found a completed movie with a snatched release : %s. Setting release status to ignored...' , default_title) - rel.status_id = ignored_status.get('id') - db.commit() - continue - - item = {} - for info in rel.info: - item[info.identifier] = info.value - - movie_dict = fireEvent('movie.get', rel.movie_id, single = True) - - # check status - downloadstatus = fireEvent('download.status', data = item, movie = movie_dict, single = True) - if not downloadstatus: - log.debug('Download status functionality is not implemented for active downloaders.') - scanrequired = True - else: - log.debug('Download status: %s' , downloadstatus) - - if downloadstatus == 'failed': - if self.conf('next_on_failed'): - self.tryNextRelease(rel.movie_id) - else: - rel.status_id = failed_status.get('id') - db.commit() - - log.info('Download of %s failed.', item['name']) - - elif downloadstatus == 'completed': - log.info('Download of %s completed!', item['name']) - scanrequired = True - - elif downloadstatus == 'not_found': - log.info('%s not found in downloaders', item['name']) - rel.status_id = ignored_status.get('id') - db.commit() - - # Note that Queued, Downloading, Paused, Repair and Unpackimg are also available as status for SabNZBd - if scanrequired: - fireEvent('renamer.scan') - def tryNextReleaseView(self): trynext = self.tryNextRelease(getParam('id'))