def search_tmdb(query): search = tmdb.Search() response = search.movie(query=query) selection = None if search.results: for index, result in enumerate(search.results): print( str(index).zfill(2) + ') ', result['title'], result['id'], result['release_date'], result['popularity'] ) while True: try: choice = int(input('> ')) # Need to compare to None explicitly since choice can be 0 if choice is not None: selection = search.results[choice] else: print('Skipping...') except IndexError: print('Invalid selection') except ValueError: print('Not a number') else: break else: log('No matches found (searched for "{0}")'.format(query)) return selection
def search_tmdb(query): search = tmdb.Search() response = search.movie(query=query) selection = None if search.results: for index, result in enumerate(search.results): print( str(index).zfill(2) + ') ', result['title'], result['id'], result['release_date'], result['popularity']) while True: try: choice = int(input('> ')) # Need to compare to None explicitly since choice can be 0 if choice is not None: selection = search.results[choice] else: print('Skipping...') except IndexError: print('Invalid selection') except ValueError: print('Not a number') else: break else: log('No matches found (searched for "{0}")'.format(query)) return selection
def audit_movies(): if config['tmdb_api_key']: tmdb.API_KEY = config['tmdb_api_key'] for movie in os.listdir(config['movie_dir']): source_path = os.path.join(config['movie_dir'], movie) # Check the conformance of the movie dir naming itself if not movie_dir_format.match(movie): log('"{0}" does not match, searching...'.format(movie)) selection = search_tmdb(movie) if selection: new_name = '{name} ({year})'.format( name=selection['title'], year=str(selection['release_date'][:4]) ) dest_path = os.path.join(config['movie_dir'], new_name) print('Changing from "{old}" to "{new}"'.format( old=movie, new=new_name )) os.rename(source_path, dest_path) # Set the source path to the renamed directory source_path = dest_path movie_dir = source_path # Now we inspect the contents of each movie dir for entry in os.listdir(movie_dir): source_path = os.path.join(movie_dir, entry) # Don't try to process subtitles, etc if not check_extension(entry): continue match = movie_format.match(entry) if match: quality = determine_quality(source_path) if match.group('quality') != quality: log( 'actual quality does not match filename: ' + '{0}'.format(entry), level='warn' ) if match.group('quality') != '1080p': log( 'non-1080p detected: {0}'.format(entry), level='warn' ) else: log('{0} does not match, searching...'.format(entry)) guess = guessit.guessit(entry) selection = search_tmdb(guess['title']) if selection: quality = determine_quality(source_path) if quality: name_format = '{name} ({year}) - {quality}.{ext}' new_name = name_format.format( name=selection['title'], year=str(selection['release_date'][:4]), quality=quality, ext=guess['container'] ) dest_path = os.path.join(movie_dir, new_name) print('Changing from "{old}" to "{new}"'.format( old=entry, new=new_name )) os.rename(source_path, dest_path) else: log('Failed to determine quality', level='error') print('Finished!') else: print('Register for an API key from The Movie DB first.')
def audit_movies(): if config['tmdb_api_key']: tmdb.API_KEY = config['tmdb_api_key'] for movie in os.listdir(config['movie_dir']): source_path = os.path.join(config['movie_dir'], movie) # Check the conformance of the movie dir naming itself if not movie_dir_format.match(movie): log('"{0}" does not match, searching...'.format(movie)) selection = search_tmdb(movie) if selection: new_name = '{name} ({year})'.format( name=selection['title'], year=str(selection['release_date'][:4])) dest_path = os.path.join(config['movie_dir'], new_name) print('Changing from "{old}" to "{new}"'.format( old=movie, new=new_name)) os.rename(source_path, dest_path) # Set the source path to the renamed directory source_path = dest_path movie_dir = source_path # Now we inspect the contents of each movie dir for entry in os.listdir(movie_dir): source_path = os.path.join(movie_dir, entry) # Don't try to process subtitles, etc if not entry.endswith('mkv'): continue match = movie_format.match(entry) if match: quality = determine_quality(source_path) if match.group('quality') != quality: log('actual quality does not match filename: ' + '{0}'.format(entry), level='warn') if match.group('quality') != '1080p': log('non-1080p detected: {0}'.format(entry), level='warn') else: log('{0} does not match, searching...'.format(entry)) guess = guessit.guessit(entry) selection = search_tmdb(guess['title']) if selection: quality = determine_quality(source_path) if quality: name_format = '{name} ({year}) - {quality}.{ext}' new_name = name_format.format( name=selection['title'], year=str(selection['release_date'][:4]), quality=quality, ext=guess['container']) dest_path = os.path.join(movie_dir, new_name) print('Changing from "{old}" to "{new}"'.format( old=entry, new=new_name)) os.rename(source_path, dest_path) else: log('Failed to determine quality', level='error') print('Finished!') else: print('Register for an API key from The Movie DB first.')