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 sync(): ''' Syncs all enabled Trakt lists and rss lists Gets list of movies from each enabled Trakt lists Adds missing movies to library as Waiting/Default Returns bool for success/failure ''' logging.info('Syncing Trakt lists.') success = True min_score = core.CONFIG['Search']['Watchlists']['traktscore'] length = core.CONFIG['Search']['Watchlists']['traktlength'] movies = [] if core.CONFIG['Search']['Watchlists']['traktrss']: sync_rss() for k, v in core.CONFIG['Search']['Watchlists']['Traktlists'].items(): if v is False: continue movies += [ i for i in get_list(k, min_score=min_score, length=length) if i not in movies ] library = [i['imdbid'] for i in core.sql.get_user_movies()] movies = [ i for i in movies if ((i['ids']['imdb'] not in library) and (i['ids']['imdb'] != 'N/A')) ] logging.info('Found {} new movies from Trakt lists.'.format(len(movies))) for i in movies: imdbid = i['ids']['imdb'] logging.info('Adding movie {} {} from Trakt'.format( i['title'], imdbid)) added = Manage.add_movie({ 'id': i['ids']['tmdb'], 'imdbid': i['ids']['imdb'], 'title': i['title'], 'origin': 'Trakt' }) try: if added['response'] and core.CONFIG['Search'][ 'searchafteradd'] and i['year'] != 'N/A': searcher.search(imdbid, i['title'], i['year'], core.config.default_profile()) except Exception as e: logging.error('Movie {} did not get added.'.format(i['title']), exc_info=False) return success
def sync_rss(): ''' Gets list of new movies in user's rss feed(s) Returns list of movie dicts ''' try: record = json.loads(core.sql.system('trakt_sync_record')) except Exception as e: record = {} for url in core.CONFIG['Search']['Watchlists']['traktrss'].split(','): list_id = url.split('.atom')[0].split('/')[-1] last_sync = record.get(list_id) or 'Sat, 01 Jan 2000 00:00:00' last_sync = datetime.datetime.strptime(last_sync, date_format) logging.info('Syncing Trakt RSS watchlist {}. Last sync: {}'.format(list_id, last_sync)) try: feed = Url.open(url).text feed = re.sub(r'xmlns=".*?"', r'', feed) root = ET.fromstring(feed) except Exception as e: logging.error('Trakt rss request:\n{}'.format(feed), exc_info=True) continue d = root.find('updated').text[:19] do = datetime.datetime.strptime(d, trakt_date_format) record[list_id] = datetime.datetime.strftime(do, date_format) for entry in root.iter('entry'): try: pub = datetime.datetime.strptime(entry.find('published').text[:19], trakt_date_format) if last_sync >= pub: break else: t = entry.find('title').text title = ' ('.join(t.split(' (')[:-1]) year = '' for i in t.split(' (')[-1]: if i.isdigit(): year += i year = int(year) logging.info('Searching TheMovieDatabase for {} {}'.format(title, year)) movie = Manage.tmdb._search_title('{} {}'.format(title, year))[0] if movie: movie['origin'] = 'Trakt' logging.info('Found new watchlist movie {} {}'.format(title, year)) r = Manage.add_movie(movie) if r['response'] and core.CONFIG['Search']['searchafteradd'] and movie['year'] != 'N/A': searcher.search(movie) else: logging.warning('Unable to find {} {} on TheMovieDatabase'.format(title, year)) except Exception as e: logging.error('Unable to parse Trakt RSS list entry.', exc_info=True) logging.info('Storing last synced date.') if core.sql.row_exists('SYSTEM', name='trakt_sync_record'): core.sql.update('SYSTEM', 'data', json.dumps(record), 'name', 'trakt_sync_record') else: core.sql.write('SYSTEM', {'data': json.dumps(record), 'name': 'trakt_sync_record'}) logging.info('Trakt RSS sync complete.')
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.')