def _sync_new_movies(movies): ''' Adds new movies from rss feed movies (list): dicts of movies Checks last sync time and pulls new imdbids from feed. Checks if movies are already in library and ignores. Executes ajax.add_wanted_movie() for each new imdbid Does not return ''' existing_movies = [i['imdbid'] for i in core.sql.get_user_movies()] movies_to_add = [i for i in movies if i['imdb_id'] not in existing_movies] # do quick-add procedure for movie in movies_to_add: imdbid = movie['imdb_id'] movie = TheMovieDatabase._search_imdbid(imdbid) if not movie: logging.warning('{} not found on TMDB. Cannot add.'.format(imdbid)) continue else: movie = movie[0] logging.info('Adding movie {} {} from PopularMovies list.'.format( movie['title'], movie['imdbid'])) movie['quality'] = 'Default' movie['origin'] = 'PopularMovies' added = Manage.add_movie(movie) if added['response'] and core.CONFIG['Search'][ 'searchafteradd'] and movie['year'] != 'N/A': searcher.search(movie)
def addmovie(self, params): ''' Add movie with default quality settings params (dict): params passed in request url params must contain either 'imdbid' or 'tmdbid' key and value Returns dict {'status': 'success', 'message': 'X added to wanted list.'} ''' if not (params.get('imdbid') or params.get('tmdbid')): return {'response': False, 'error': 'no movie id supplied'} elif (params.get('imdbid') and params.get('tmdbid')): return {'response': False, 'error': 'multiple movie ids supplied'} origin = cherrypy.request.headers.get('User-Agent', 'API') origin = 'API' if origin.startswith('Mozilla/') else origin quality = params.get('quality') or core.config.default_profile() category = params.get('category', 'Default') if params.get('imdbid'): imdbid = params['imdbid'] logging.info('API request add movie imdb {}'.format(imdbid)) movie = TheMovieDatabase._search_imdbid(imdbid) if not movie: return { 'response': False, 'error': 'Cannot find {} on TMDB'.format(imdbid) } else: movie = movie[0] movie['imdbid'] = imdbid elif params.get('tmdbid'): tmdbid = params['tmdbid'] logging.info('API request add movie tmdb {}'.format(tmdbid)) movie = TheMovieDatabase._search_tmdbid(tmdbid) if not movie: return { 'response': False, 'error': 'Cannot find {} on TMDB'.format(tmdbid) } else: movie = movie[0] movie['quality'] = quality movie['category'] = category movie['status'] = 'Waiting' movie['origin'] = origin response = Manage.add_movie(movie, full_metadata=True) if response['response'] and core.CONFIG['Search'][ 'searchafteradd'] and movie['year'] != 'N/A': threading.Thread(target=searcher._t_search_grab, args=(movie, )).start() return response
def update(imdbid, tmdbid=None, force_poster=True): ''' Updates metadata from TMDB imdbid (str): imdb id # tmdbid (str): or int tmdb id # <optional - default None> force_poster (bool): whether or not to always redownload poster <optional - default True> If tmdbid is None, looks in database for tmdbid using imdbid. If that fails, looks on tmdb api for imdbid If that fails returns error message If force_poster is True, the poster will be re-downloaded. If force_poster is False, the poster will only be redownloaded if the local database does not have a 'poster' filepath stored. In other words, this will only grab missing posters. Returns dict ajax-style response ''' logging.info('Updating metadata for {}'.format(imdbid)) movie = core.sql.get_movie_details('imdbid', imdbid) if force_poster: get_poster = True elif not movie.get('poster'): get_poster = True elif not os.path.isfile(os.path.join(core.PROG_PATH, movie['poster'])): get_poster = True else: logging.debug('Poster will not be redownloaded.') get_poster = False if tmdbid is None: tmdbid = movie.get('tmdbid') if not tmdbid: logging.debug( 'TMDB id not found in local database, searching TMDB for {}' .format(imdbid)) tmdb_data = TheMovieDatabase._search_imdbid(imdbid) tmdbid = tmdb_data[0].get('id') if tmdb_data else None if not tmdbid: logging.debug('Unable to find {} on TMDB.'.format(imdbid)) return { 'response': False, 'error': 'Unable to find {} on TMDB.'.format(imdbid) } new_data = TheMovieDatabase._search_tmdbid(tmdbid) if not new_data: logging.warning('Empty response from TMDB.') return else: new_data = new_data[0] new_data.pop('status') target_poster = os.path.join(Poster.folder, '{}.jpg'.format(imdbid)) if new_data.get('poster_path'): poster_path = 'http://image.tmdb.org/t/p/w300{}'.format( new_data['poster_path']) movie['poster'] = '{}.jpg'.format(movie['imdbid']) else: poster_path = None movie.update(new_data) movie = Metadata.convert_to_db(movie) core.sql.update_multiple_values('MOVIES', movie, 'imdbid', imdbid) if poster_path and get_poster: if os.path.isfile(target_poster): try: os.remove(target_poster) except FileNotFoundError: pass except Exception as e: logging.warning('Unable to remove existing poster.', exc_info=True) return { 'response': False, 'error': 'Unable to remove existing poster.' } Poster.save(imdbid, poster_path) return {'response': True, 'message': 'Metadata updated.'}
def sync(): ''' Syncs CSV lists from IMDB Does not return ''' movies_to_add = [] library = [i[2] for i in core.sql.quick_titles()] try: record = json.loads(core.sql.system('imdb_sync_record')) except Exception as e: record = {} for url in core.CONFIG['Search']['Watchlists']['imdbcsv']: if url[-6:] not in ('export', 'export/'): logging.warning('{} does not look like a valid imdb list'.format(url)) continue list_id = 'ls' + ''.join(filter(str.isdigit, url)) logging.info('Syncing rss IMDB watchlist {}'.format(list_id)) last_sync = datetime.strptime((record.get(list_id) or '2000-01-01'), date_format) try: csv_text = Url.open(url).text watchlist = [dict(i) for i in csv.DictReader(csv_text.splitlines())][::-1] record[list_id] = watchlist[0]['Created'] for movie in watchlist: pub_date = datetime.strptime(movie['Created'], date_format) if last_sync > pub_date: break imdbid = movie['Const'] if imdbid not in library and imdbid not in movies_to_add: logging.info('Found new watchlist movie {}'.format(movie['Title'])) movies_to_add.append(imdbid) except Exception as e: logging.warning('Unable to sync list {}'.format(list_id)) m = [] for imdbid in movies_to_add: movie = TheMovieDatabase._search_imdbid(imdbid) if not movie: logging.warning('{} not found on TheMovieDB. Cannot add.'.format(imdbid)) continue else: movie = movie[0] logging.info('Adding movie {} {} from IMDB watchlist.'.format(movie['title'], movie['imdbid'])) movie['year'] = movie['release_date'][:4] if movie.get('release_date') else 'N/A' movie['origin'] = 'IMDB' added = Manage.add_movie(movie) if added['response']: m.append((imdbid, movie['title'], movie['year'])) if core.CONFIG['Search']['searchafteradd']: for i in m: if i[2] != 'N/A': searcher.search(i[0], i[1], i[2], core.config.default_profile()) logging.info('Storing last synced date.') if core.sql.row_exists('SYSTEM', name='imdb_sync_record'): core.sql.update('SYSTEM', 'data', json.dumps(record), 'name', 'imdb_sync_record') else: core.sql.write('SYSTEM', {'data': json.dumps(record), 'name': 'imdb_sync_record'}) logging.info('IMDB sync complete.')