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']) done_status, snatched_status = fireEvent('status.get', ['done', 'snatched'], single=True) # Add movie movie = db.query(Media).filter_by( library_id=group['library'].get('id')).first() if not movie: movie = Media(library_id=group['library'].get('id'), profile_id=0, status_id=done_status.get('id')) db.add(movie) db.commit() # Add Release rel = db.query(Relea).filter( or_( Relea.identifier == identifier, and_( Relea.identifier.startswith( group['library']['identifier']), Relea.status_id == snatched_status.get('id')))).first() if not rel: rel = Relea(identifier=identifier, movie=movie, quality_id=group['meta_data']['quality'].get('id'), status_id=done_status.get('id')) db.add(rel) db.commit() # Add each file type added_files = [] for type in group['files']: for cur_file in group['files'][type]: added_file = self.saveFile(cur_file, type=type, include_media_info=type is 'movie') added_files.append(added_file.get('id')) # Add the release files in batch try: added_files = db.query(File).filter( or_(*[File.id == x for x in added_files])).all() rel.files.extend(added_files) db.commit() except: log.debug('Failed to attach "%s" to release: %s', (added_files, traceback.format_exc())) fireEvent('movie.restatus', movie.id) return True
def createFromSearch(self, search_results, media, quality_type): available_status = fireEvent('status.get', ['available'], single=True) try: db = get_session() found_releases = [] for rel in search_results: rel_identifier = md5(rel['url']) found_releases.append(rel_identifier) rls = db.query(Relea).filter_by( identifier=rel_identifier).first() if not rls: rls = Relea( identifier=rel_identifier, movie_id=media.get('id'), #media_id = media.get('id'), quality_id=quality_type.get('quality_id'), status_id=available_status.get('id')) db.add(rls) else: [db.delete(old_info) for old_info in rls.info] rls.last_edit = int(time.time()) db.commit() for info in rel: try: if not isinstance(rel[info], (str, unicode, int, long, float)): continue rls_info = ReleaseInfo(identifier=info, value=toUnicode(rel[info])) rls.info.append(rls_info) except InterfaceError: log.debug('Couldn\'t add %s to ReleaseInfo: %s', (info, traceback.format_exc())) db.commit() rel['status_id'] = rls.status_id return found_releases except: log.error('Failed: %s', traceback.format_exc()) db.rollback() finally: db.close() return []
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) rel = db.query(Relea).filter( or_( Relea.identifier == identifier, and_( Relea.identifier.startswith( group['library']['identifier']), Relea.status_id == snatched_status.get('id')))).first() if not rel: rel = Relea(identifier=identifier, movie=movie, quality_id=group['meta_data']['quality'].get('id'), status_id=done_status.get('id')) db.add(rel) db.commit() # Add each file type for type in group['files']: for cur_file in group['files'][type]: added_file = self.saveFile(cur_file, type=type, include_media_info=type is 'movie') try: added_file = db.query(File).filter_by( id=added_file.get('id')).one() rel.files.append(added_file) db.commit() except Exception, e: log.debug('Failed to attach "%s" to release: %s', (cur_file, e))