def handle_manual(torrent): auto_processed = False def handle_media(path, move): nonlocal auto_processed guess = guessit.guessit(path) if guess['type'] == 'episode': move_episode(path, guess, move) auto_processed = True elif guess['type'] == 'movie': move_movie(path, guess, move) auto_processed = True part_regex = re.compile('.*part(\d+).rar', re.IGNORECASE) for index, file in torrent.files().items(): file_path = os.path.join(torrent.downloadDir, file['name']) if check_extension(file_path) and 'sample' not in file_path.lower(): # Log and ignore mkv files of less than ~92MiB try: if os.path.getsize(file_path) >= 96811278: handle_media(file_path, False) else: syslog.syslog( syslog.LOG_ERR, 'Detected false media file, skipping' ) except FileNotFoundError: syslog.syslog(syslog.LOG_ERR, 'Torrent file missing, skipping') elif file_path.endswith('rar'): # Ignore parts beyond the first in a rar series match = part_regex.match(file_path) if match and int(match.group(1)) > 1: continue with tempfile.TemporaryDirectory() as temp_dir: paths = extract(file_path, temp_dir) if paths: for path in paths: shutil.chown(path, group=plex_group) os.chmod(path, 0o664) handle_media(path, True) if auto_processed: pb_notify( textwrap.dedent( ''' Manually added torrent {0} finished downloading and was auto-processed '''.format(torrent.name) ).strip() ) else: pb_notify( 'Manually added torrent {0} finished downloading'.format( torrent.name ) )
def handle_manual(torrent): auto_processed = False def handle_media(path, move): nonlocal auto_processed guess = guessit.guessit(path) if guess['type'] == 'episode': move_episode(path, guess, move) auto_processed = True elif guess['type'] == 'movie': move_movie(path, guess, move) auto_processed = True part_regex = re.compile('.*part(\d+).rar', re.IGNORECASE) for index, file in torrent.files().items(): file_path = os.path.join(torrent.downloadDir, file['name']) if check_extension(file_path) and 'sample' not in file_path.lower(): # Log and ignore mkv files of less than ~92MiB try: if os.path.getsize(file_path) >= 96811278: handle_media(file_path, False) else: syslog.syslog(syslog.LOG_ERR, 'Detected false media file, skipping') except FileNotFoundError: syslog.syslog(syslog.LOG_ERR, 'Torrent file missing, skipping') elif file_path.endswith('rar'): # Ignore parts beyond the first in a rar series match = part_regex.match(file_path) if match and int(match.group(1)) > 1: continue with tempfile.TemporaryDirectory() as temp_dir: paths = extract(file_path, temp_dir) if paths: for path in paths: shutil.chown(path, group=plex_group) os.chmod(path, 0o664) handle_media(path, True) if auto_processed: pb_notify( textwrap.dedent(''' Manually added torrent {0} finished downloading and was auto-processed '''.format(torrent.name)).strip()) else: pb_notify('Manually added torrent {0} finished downloading'.format( torrent.name))
def extract(path, destination): if unrar.rarfile.is_rarfile(path): try: rar = unrar.rarfile.RarFile(path) if not rar.testrar(): # Use a set to prevent duplicates from infolist() paths = set() for member in rar.infolist(): if check_extension(member.filename): paths.add(rar.extract(member, path=destination)) pb_notify( 'Successfully unpacked rar archive: {0}'.format(file_path)) return paths else: pb_notify('{0} failed the rar integrity check'.format(path)) except unrar.unrarlib.UnrarException: pb_notify('Error while opening {0}'.format(path)) pb_notify('Failed to unpacked rar archive (not a rar): {0}'.format(path)) return False
def extract(path, destination): if unrar.rarfile.is_rarfile(path): try: rar = unrar.rarfile.RarFile(path) if not rar.testrar(): # Use a set to prevent duplicates from infolist() paths = set() for member in rar.infolist(): if check_extension(member.filename): paths.add(rar.extract(member, path=destination)) pb_notify( 'Successfully unpacked rar archive: {0}'.format(file_path) ) return paths else: pb_notify('{0} failed the rar integrity check'.format(path)) except unrar.unrarlib.UnrarException: pb_notify('Error while opening {0}'.format(path)) pb_notify('Failed to unpacked rar archive (not a rar): {0}'.format(path)) return False
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.')