diff --git a/couchpotato/core/plugins/release/__init__.py b/couchpotato/core/plugins/release/__init__.py new file mode 100644 index 0000000..b6a667c --- /dev/null +++ b/couchpotato/core/plugins/release/__init__.py @@ -0,0 +1,6 @@ +from .main import Release + +def start(): + return Release() + +config = [] diff --git a/couchpotato/core/plugins/release/main.py b/couchpotato/core/plugins/release/main.py new file mode 100644 index 0000000..96f7a3d --- /dev/null +++ b/couchpotato/core/plugins/release/main.py @@ -0,0 +1,75 @@ +from couchpotato import get_session +from couchpotato.core.event import fireEvent, addEvent +from couchpotato.core.logger import CPLog +from couchpotato.core.plugins.base import Plugin +from couchpotato.core.settings.model import File, Release, Movie +from sqlalchemy.sql.expression import and_, or_ + +log = CPLog(__name__) + + +class Release(Plugin): + + def __init__(self): + addEvent('release.add', self.add) + + def add(self, group): + db = get_session() + + identifier = '%s.%s.%s' % (group['library']['identifier'], group['meta_data'].get('audio', 'unknown'), group['meta_data']['quality']['identifier']) + + # Add movie + done_status = fireEvent('status.get', 'done', single = True) + movie = db.query(Movie).filter_by(library_id = group['library'].get('id')).first() + if not movie: + movie = Movie( + library_id = group['library'].get('id'), + profile_id = 0, + status_id = done_status.get('id') + ) + db.add(movie) + db.commit() + + # Add release + snatched_status = fireEvent('status.get', 'snatched', single = True) + release = db.query(Release).filter( + or_( + Release.identifier == identifier, + and_(Release.identifier.startswith(group['library']['identifier'], Release.status_id == snatched_status.get('id'))) + ) + ).first() + if not release: + release = Release( + identifier = identifier, + movie = movie, + quality_id = group['meta_data']['quality'].get('id'), + status_id = done_status.get('id') + ) + db.add(release) + db.commit() + + # Add each file type + for type in group['files']: + for file in group['files'][type]: + added_file = self.saveFile(file, type = type, include_media_info = type is 'movie') + try: + added_file = db.query(File).filter_by(id = added_file.get('id')).one() + release.files.append(added_file) + db.commit() + except Exception, e: + log.debug('Failed to attach "%s" to release: %s' % (file, e)) + + db.remove() + + + def saveFile(self, file, type = 'unknown', include_media_info = False): + + properties = {} + + # Get media info for files + if include_media_info: + properties = {} + + # Check database and update/insert if necessary + return fireEvent('file.add', path = file, part = self.getPartNumber(file), type = self.file_types[type], properties = properties, single = True) + diff --git a/couchpotato/core/plugins/scanner/main.py b/couchpotato/core/plugins/scanner/main.py index caae1ce..ec1fc67 100644 --- a/couchpotato/core/plugins/scanner/main.py +++ b/couchpotato/core/plugins/scanner/main.py @@ -7,7 +7,7 @@ from couchpotato.core.plugins.base import Plugin from couchpotato.core.settings.model import File, Release, Movie from couchpotato.environment import Env from flask.helpers import json -from themoviedb.tmdb import opensubtitleHashFile +from sqlalchemy.sql.expression import and_, or_ import os import re import subprocess @@ -70,6 +70,7 @@ class Scanner(Plugin): def __init__(self): #addEvent('app.load', self.scanLibrary) + addEvent('scanner.create_file_identifier', self.createStringIdentifier) addEvent('scanner.scan', self.scan) @@ -95,7 +96,7 @@ class Scanner(Plugin): #library = db.query(Library).filter_by(id = library.get('id')).one() # Add release - self.addRelease(group) + fireEvent('release.add', group = group) # Add identifier for library update update_after.append(group['library'].get('identifier')) @@ -133,7 +134,7 @@ class Scanner(Plugin): is_dvd_file = self.isDVDFile(file_path) if os.path.getsize(file_path) > self.minimal_filesize['media'] or is_dvd_file: # Minimal 300MB files or is DVD file - identifier = self.createFileIdentifier(file_path, folder, exclude_filename = is_dvd_file) + identifier = self.createStringIdentifier(file_path, folder, exclude_filename = is_dvd_file) if not movie_files.get(identifier): movie_files[identifier] = { @@ -221,51 +222,6 @@ class Scanner(Plugin): return movie_files - - def addRelease(self, group): - db = get_session() - - identifier = '%s.%s.%s' % (group['library']['identifier'], group['meta_data'].get('audio', 'unknown'), group['meta_data']['quality']['identifier']) - - # Add movie - done_status = fireEvent('status.get', 'done', single = True) - movie = db.query(Movie).filter_by(library_id = group['library'].get('id')).first() - if not movie: - movie = Movie( - library_id = group['library'].get('id'), - profile_id = 0, - status_id = done_status.get('id') - ) - db.add(movie) - db.commit() - - # Add release - release = db.query(Release).filter_by(identifier = identifier).first() - if not release: - - release = Release( - identifier = identifier, - movie = movie, - quality_id = group['meta_data']['quality'].get('id'), - status_id = done_status.get('id') - ) - db.add(release) - db.commit() - - # Add each file type - for type in group['files']: - - for file in group['files'][type]: - added_file = self.saveFile(file, type = type, include_media_info = type is 'movie') - try: - added_file = db.query(File).filter_by(id = added_file.get('id')).one() - release.files.append(added_file) - db.commit() - except Exception, e: - log.debug('Failed to attach "%s" to release: %s' % (file, e)) - - db.remove() - def getMetaData(self, group): data = {} @@ -374,17 +330,6 @@ class Scanner(Plugin): log.error('No imdb_id found for %s.' % group['identifiers']) return {} - def saveFile(self, file, type = 'unknown', include_media_info = False): - - properties = {} - - # Get media info for files - if include_media_info: - properties = {} - - # Check database and update/insert if necessary - return fireEvent('file.add', path = file, part = self.getPartNumber(file), type = self.file_types[type], properties = properties, single = True) - def getCPImdb(self, string): try: @@ -501,9 +446,9 @@ class Scanner(Plugin): return False def getGroupFiles(self, identifier, folder, file_pile): - return set(filter(lambda s:identifier in self.createFileIdentifier(s, folder), file_pile)) + return set(filter(lambda s:identifier in self.createStringIdentifier(s, folder), file_pile)) - def createFileIdentifier(self, file_path, folder, exclude_filename = False): + def createStringIdentifier(self, file_path, folder = '', exclude_filename = False): identifier = file_path.replace(folder, '') # root folder identifier = os.path.splitext(identifier)[0] # ext diff --git a/couchpotato/core/plugins/searcher/main.py b/couchpotato/core/plugins/searcher/main.py index d2d8372..0f6ae14 100644 --- a/couchpotato/core/plugins/searcher/main.py +++ b/couchpotato/core/plugins/searcher/main.py @@ -3,7 +3,7 @@ from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.encoding import simplifyString from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin -from couchpotato.core.settings.model import Movie +from couchpotato.core.settings.model import Movie, Release from couchpotato.environment import Env import re @@ -19,7 +19,7 @@ class Searcher(Plugin): # Schedule cronjob fireEvent('schedule.cron', 'searcher.all', self.all, day = self.conf('cron_day'), hour = self.conf('cron_hour'), minute = self.conf('cron_minute')) - #addEvent('app.load', self.all) + addEvent('app.load', self.all) def all(self): @@ -29,47 +29,56 @@ class Searcher(Plugin): Movie.status.has(identifier = 'active') ).all() - snatched_status = fireEvent('status.get', 'snatched', single = True) - for movie in movies: - success = self.single(movie.to_dict(deep = { + self.single(movie.to_dict(deep = { 'profile': {'types': {'quality': {}}}, 'releases': {'status': {}, 'quality': {}}, 'library': {'titles': {}, 'files':{}}, 'files': {} })) - # Mark as snatched on success - if success: - movie.status_id = snatched_status.get('id') - db.commit() - - def single(self, movie): successful = False for type in movie['profile']['types']: - has_better_quality = False + has_better_quality = 0 + default_title = movie['library']['titles'][0]['title'] # See if beter quality is available for release in movie['releases']: if release['quality']['order'] <= type['quality']['order']: - has_better_quality = True + has_better_quality += 1 # Don't search for quality lower then already available. - if not has_better_quality: + if has_better_quality is 0: - log.info('Search for %s in %s' % (movie['library']['titles'][0]['title'], type['quality']['label'])) + log.info('Search for %s in %s' % (default_title, type['quality']['label'])) results = fireEvent('provider.yarr.search', movie, type['quality'], merge = True) sorted_results = sorted(results, key = lambda k: k['score'], reverse = True) for nzb in sorted_results: - successful = fireEvent('download', data = nzb, single = True) + successful = fireEvent('download', data = nzb, movie = movie, single = True) if successful: log.info('Downloading of %s successful.' % nzb.get('name')) + + # Add release item, should be updated later when renaming + snatched_status = fireEvent('status.get', 'snatched', single = True) + db = get_session() + rls = Release( + identifier = '%s.%s' % (movie['library']['identifier'], type['quality']['identifier']), + movie_id = movie.get('id'), + quality_id = type.get('quality_id'), + status_id = snatched_status.get('id') + ) + db.add(rls) + db.commit() + return True + else: + log.info('Better quality (%s) already available or snatched for %s' % (type['quality']['label'], default_title)) + break return False