コード例 #1
0
def rename_files(host_filter, torrent_filter, btn_api_key, orpheus_api_key,
                 redacted_api_key):
    hosts = DELUGE_API()
    apis = []

    if (btn_api_key):
        logger.trace('Testing BTN API configuration')
        btn_api = BTN_API(btn_api_key)
        btn_index = btn_api.get_index()
        logger.success(f'Connected to BTN as {btn_index.username}')
        apis.append(btn_api)

    if (orpheus_api_key):
        logger.trace('Testing Orpheus API configuration')
        orp_api = ORP_API(orpheus_api_key)
        orp_index = orp_api.get_index()
        logger.success(f'Connected to Orpheus as {orp_index.username}')
        apis.append(orp_api)

    if (redacted_api_key):
        logger.trace('Testing Redacted API configuration')
        red_api = RED_API(redacted_api_key)
        red_index = red_api.get_index()
        logger.success(f'Connected to Redacted as {red_index.username}')
        apis.append(red_api)

    host_filter = compile(host_filter) if not isinstance(
        host_filter, Pattern) else host_filter
    torrent_filter = compile(torrent_filter) if not isinstance(
        torrent_filter, Pattern) else torrent_filter

    filtered_hosts = [host for host in hosts if host_filter.search(str(host))]
    logger.info(f'{hosts} ({len(filtered_hosts)} filtered):')
    total_torrents = 0
    total_files = 0
    renamed_torrents = 0
    renamed_files = 0
    failed_torrents = 0
    failed_files = 0
    for host in filtered_hosts:
        logger.info(f'  {host}:')
        filtered_torrents = [
            torrent for torrent in host.torrents
            if torrent_filter.search(str(torrent))
        ]
        logger.info(
            f'    {host.torrents} ({len(filtered_torrents)} filtered):')
        for torrent in filtered_torrents:
            total_torrents += 1
            logger.info(f'      {torrent}:')
            logger.info(f'        {torrent.trackers}:')
            for tracker in torrent.trackers:
                logger.info(f'          {tracker}')
            logger.info(f'        {torrent.files}:')
            changed = False
            failed = False
            prompt = False
            for api in apis:
                if torrent.trackers.tracker in api.supported_trackers:
                    logger.success(f'Renaming files with {type(api)}')
                    for file in torrent.files:
                        total_files += 1
                        logger.info(f'          {file}')
                        result = api.rename_torrent_file(
                            torrent.hash, file.path)
                        if result is None:
                            failed_files += 1
                            failed = True
                            continue
                        path = sanitize_path(
                            asciify_path(unescape(result), '_')).replace(
                                '_', '').replace(' (VBR)]', ']')
                        if file.path != path and (not prompt or confirm(
                                f'Rename {file.path} to {path}?',
                                default=True)):
                            renamed_files += 1
                            file.path = path
                            changed = True
            if changed:
                renamed_torrents += 1
                torrent.force_recheck()
            if failed:
                failed_torrents += 1

    echo(f'Checked {total_files} files in {total_torrents} torrents', nl=False)
    if renamed_files > 0:
        echo(', ' + style(
            f'renamed {renamed_files} files in {renamed_torrents} torrents',
            fg='green'),
             nl=False)
    if failed_files > 0:
        echo(
            ', ' +
            style(f'failed {failed_files} files in {failed_torrents} torrents',
                  fg='red'),
            nl=False)
    echo('.')