def import_cp_movies(self, wanted, finished): ''' Imports movies from CP list to library wanted (list): dicts of wanted movies finished (list): dicts of finished movies Yields dict ajax-style response ''' wanted = json.loads(wanted) finished = json.loads(finished) fake_results = [] success = [] length = len(wanted) + len(finished) progress = 1 logging.info('Adding {} Wanted CouchPotato movies to library.'.format(len(wanted))) for movie in wanted: response = core.manage.add_movie(movie, full_metadata=True) if response['response'] is True: yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': response['error']}) progress += 1 continue logging.info('Adding {} Wanted CouchPotato movies to library.'.format(len(finished))) for movie in finished: movie['predb'] = 'found' movie['status'] = 'Disabled' movie['origin'] = 'CouchPotato Import' response = core.manage.add_movie(movie, full_metadata=True) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': response['error']}) progress += 1 continue fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: core.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) core.sql.write_search_results(fake_results)
def searchresults(self, guid, status, movie_info=None): ''' Marks searchresults status guid (str): download link guid status (str): status to set movie_info (dict): of movie metadata <optional - default None> If guid is in SEARCHRESULTS table, marks it as status. If guid not in SEARCHRESULTS, uses movie_info to create a result. Returns bool ''' TABLE = 'SEARCHRESULTS' logging.info('Marking guid {} as {}.'.format(guid.split('&')[0], status)) if core.sql.row_exists(TABLE, guid=guid): # Mark bad in SEARCHRESULTS logging.info('Marking {} as {} in SEARCHRESULTS.'.format(guid.split('&')[0], status)) if not core.sql.update(TABLE, 'status', status, 'guid', guid): logging.error('Setting SEARCHRESULTS status of {} to {} failed.'.format(guid.split('&')[0], status)) return False else: logging.info('Successfully marked {} as {} in SEARCHRESULTS.'.format(guid.split('&')[0], status)) return True else: logging.info('Guid {} not found in SEARCHRESULTS, attempting to create entry.'.format(guid.split('&')[0])) if movie_info is None: logging.warning('Movie metadata not supplied, unable to create SEARCHRESULTS entry.') return False search_result = searchresults.generate_simulacrum(movie_info) search_result['indexer'] = 'Post-Processing Import' if not search_result.get('title'): search_result['title'] = movie_info['title'] search_result['size'] = os.path.getsize(movie_info.get('orig_filename') or '.') if not search_result['resolution']: search_result['resolution'] = 'Unknown' search_result = self.score.score([search_result], imported=True)[0] required_keys = ('score', 'size', 'status', 'pubdate', 'title', 'imdbid', 'indexer', 'date_found', 'info_link', 'guid', 'torrentfile', 'resoluion', 'type', 'downloadid', 'freeleech') search_result = {k: v for k, v in search_result.items() if k in required_keys} if core.sql.write('SEARCHRESULTS', search_result): return True else: return False
def import_cp_movies(self, wanted, finished): wanted = json.loads(wanted) finished = json.loads(finished) fake_results = [] success = [] length = len(wanted) + len(finished) progress = 1 for movie in wanted: response = json.loads(self.add_wanted_movie(json.dumps(movie), full_metadata=True)) if response['response'] is True: yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': response['error']}) progress += 1 continue for movie in finished: response = json.loads(self.add_wanted_movie(json.dumps(movie), full_metadata=True)) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': response['error']}) progress += 1 continue fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: self.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) self.sql.write_search_results(fake_results)
def import_plex_csv(self, movie_data, corrected_movies): ''' Imports list of movies genrated by csv import movie_data: list of dicts of movie info ready to import corrected_movies: list of dicts of user-corrected movie info Iterates through corrected_movies and attmpts to get metadata again if required. If imported, generates and stores fake search result. Creates dict {'success': [], 'failed': []} and appends movie data to the appropriate list. Yeilds generator object of json objects ''' movie_data = json.loads(movie_data) corrected_movies = json.loads(corrected_movies) fake_results = [] success = [] length = len(movie_data) + len(corrected_movies) progress = 1 if corrected_movies: for data in corrected_movies: tmdbdata = self.tmdb._search_imdbid(data['imdbid'])[0] if tmdbdata: data['year'] = tmdbdata['release_date'][:4] data.update(tmdbdata) movie_data.append(data) else: logging.error('Unable to find {} on TMDB.'.format(data['imdbid'])) yield json.dumps({'response': False, 'movie': data, 'progress': [progress, length], 'reason': 'Unable to find {} on TMDB.'.format(data['imdbid'])}) progress += 1 for movie in movie_data: if movie['imdbid']: movie['status'] = 'Disabled' tmdb_data = self.tmdb._search_imdbid(movie['imdbid'])[0] movie.update(tmdb_data) response = json.loads(self.add_wanted_movie(json.dumps(movie))) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': response['error']}) progress += 1 continue else: logging.error('Unable to find {} on TMDB.'.format(movie['imdbid'])) yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': 'IMDB ID invalid or missing.'}) progress += 1 fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: self.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) self.sql.write_search_results(fake_results)
def import_kodi(self, movies): ''' Imports list of movies in movies from Kodi library movie_data: JSON list of dicts of movies Iterates through movies and gathers all required metadata. If imported, generates and stores fake search result. Creates dict {'success': [], 'failed': []} and appends movie data to the appropriate list. Yeilds generator object of json objects ''' movies = json.loads(movies) fake_results = [] success = [] length = len(movies) progress = 1 print(movies[0]) for movie in movies: tmdb_data = self.tmdb._search_imdbid(movie['imdbid'])[0] if not tmdb_data.get('id'): yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': 'Unable to find {} on TMDB.'.format(movie['imdbid'])}) progress += 1 continue else: movie['id'] = tmdb_data['id'] movie['size'] = 0 movie['status'] = 'Disabled' response = json.loads(self.add_wanted_movie(json.dumps(movie))) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'reason': response['error']}) progress += 1 continue fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: self.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) self.sql.write_search_results(fake_results)
def import_plex_csv(self, movies, corrected_movies): ''' Imports list of movies genrated by csv import movie_data (list): dicts of movie info ready to import corrected_movies (list): dicts of user-corrected movie info Iterates through corrected_movies and attmpts to get metadata again if required. If imported, generates and stores fake search result. Creates dict {'success': [], 'failed': []} and appends movie data to the appropriate list. Yeilds dict ajax-style response ''' movie_data = json.loads(movies) corrected_movies = json.loads(corrected_movies) fake_results = [] success = [] length = len(movie_data) + len(corrected_movies) progress = 1 if corrected_movies: logging.info('Adding {} Plex movies to library.'.format(len(corrected_movies))) for movie in corrected_movies: tmdbdata = self.tmdb._search_imdbid(movie['imdbid']) if tmdbdata: tmdbdata = tmdbdata[0] movie['year'] = tmdbdata['release_date'][:4] movie.update(tmdbdata) movie_data.append(movie) else: logging.error(Errors.tmdb_not_found.format(movie['imdbid'])) yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': Errors.tmdb_not_found.format(movie['imdbid'])}) progress += 1 logging.info('Adding {} Plex movies to library.'.format(length)) for movie in movie_data: logging.info('Importing Plex movie {} {}'.format(movie.get('title', ''), movie.get('year', ''))) fm = False if not movie.get('imdbid') and movie.get('tmdbid'): tmdb_data = self.tmdb._search_tmdbid(movie['tmdbid']) if tmdb_data: movie.update(tmdb_data[0]) fm = True else: yield json.dumps({'response': False, 'progress': [progress, length], 'title': movie['title'], 'error': Errors.tmdb_not_found.format(movie['tmdbid'])}) progress += 1 continue if movie.get('imdbid'): movie['status'] = 'Disabled' movie['predb'] = 'found' movie['origin'] = 'Plex Import' if not movie.get('id'): tmdb_data = self.tmdb._search_imdbid(movie['imdbid']) if tmdb_data: movie.update(tmdb_data[0]) else: yield json.dumps({'response': False, 'progress': [progress, length], 'title': movie['title'], 'error': Errors.tmdb_not_found.format(movie['imdbid'])}) progress += 1 continue response = core.manage.add_movie(movie, full_metadata=fm) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'title': movie['title'], 'imdbid': movie['imdbid']}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'progress': [progress, length], 'error': response['error'], 'title': movie['title']}) progress += 1 continue else: logging.error(Errors.tmdb_not_found.format(movie['title'])) yield json.dumps({'response': False, 'progress': [progress, length], 'error': _('Unable to find IMDB ID for {} on TheMovieDB.').format(movie['title']), 'title': movie['title']}) progress += 1 continue if fake_results: fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: core.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) if fake_results: core.sql.write_search_results(fake_results)
def import_kodi_movies(self, movies): ''' Imports list of movies in movies from Kodi library movie_data (str): json-formatted list of dicts of movies Iterates through movies and gathers all required metadata. If imported, generates and stores fake search result. Creates dict {'success': [], 'failed': []} and appends movie data to the appropriate list. Yeilds dict ajax-style response ''' movies = json.loads(movies) fake_results = [] success = [] length = len(movies) progress = 1 logging.info('Adding {} Kodi movies to library.'.format(length)) for movie in movies: tmdb_data = self.tmdb._search_imdbid(movie['imdbid']) if not tmdb_data or not tmdb_data.get('id'): yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': Errors.tmdb_not_found.format(movie['imdbid'])}) progress += 1 continue movie['id'] = tmdb_data['id'] movie['size'] = 0 movie['status'] = 'Disabled' movie['predb'] = 'found' movie['finished_file'] = movie.get('finished_file', '').strip() movie['origin'] = 'Kodi Import' response = core.manage.add_movie(movie) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': response['error']}) progress += 1 continue fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: core.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) core.sql.write_search_results(fake_results)
def import_dir(self, movies, corrected_movies): ''' Imports list of movies in data movie_data (list): dicts of movie info ready to import corrected_movies (list): dicts of user-corrected movie info corrected_movies must be [{'/path/to/file': {'known': 'metadata'}}] Iterates through corrected_movies and attmpts to get metadata again if required. If imported, generates and stores fake search result. Creates dict {'success': [], 'failed': []} and appends movie data to the appropriate list. Yeilds dict ajax-style response ''' logging.info('Adding directory scan movies to library.') today = str(datetime.date.today()) movie_data = json.loads(movies) corrected_movies = json.loads(corrected_movies) fake_results = [] success = [] length = len(movie_data) + len(corrected_movies) progress = 1 if corrected_movies: logging.info('{} corrected movies, gathering metadata.'.format(len(corrected_movies))) for data in corrected_movies: tmdbdata = self.tmdb._search_tmdbid(data['tmdbid']) if tmdbdata: tmdbdata = tmdbdata[0] data['year'] = tmdbdata['release_date'][:4] data.update(tmdbdata) movie_data.append(data) else: logging.error('Unable to find {} on TMDB.'.format(data['tmdbid'])) yield json.dumps({'response': False, 'movie': data, 'progress': [progress, length], 'error': Errors.tmdb_not_found.format(data['tmdbid'])}) progress += 1 logging.info('Adding {} directory scan movies to library.'.format(len(movie_data))) for movie in movie_data: if movie.get('imdbid'): movie['status'] = 'Disabled' movie['predb'] = 'found' movie['origin'] = 'Directory Import' movie['finished_date'] = today movie['id'] = movie['tmdbid'] response = core.manage.add_movie(movie, full_metadata=True) if response['response'] is True: fake_results.append(searchresults.generate_simulacrum(movie)) yield json.dumps({'response': True, 'progress': [progress, length], 'movie': movie}) progress += 1 success.append(movie) continue else: yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': response['error']}) progress += 1 continue else: logging.error('Unable to find {} on TMDB.'.format(movie['title'])) logging.debug(movie) yield json.dumps({'response': False, 'movie': movie, 'progress': [progress, length], 'error': Errors.tmdb_not_found.format(data['title'])}) progress += 1 fake_results = self.score.score(fake_results, imported=True) for i in success: score = None for r in fake_results: if r['imdbid'] == i['imdbid']: score = r['score'] break if score: core.sql.update('MOVIES', 'finished_score', score, 'imdbid', i['imdbid']) core.sql.write_search_results(fake_results)