Пример #1
0
def update_torrents(hashes, remove_outdated=True):
    """Performs torrent updates.

    :param hashes: list - torrent identifying hashes
    :param remove_outdated: bool - flag to remove outdated torrents from torrent clients
    :return: hash-indexed dictionary with information on updated torrents
    :rtype: dict
    """
    updated_by_hashes = {}
    download_cache = {}

    for _, rpc_object in iter_rpc():
        LOGGER.info('Getting torrents using `%s` RPC ...', rpc_object.alias)
        torrents = rpc_object.method_get_torrents(hashes)

        if not torrents:
            LOGGER.info('No significant torrents found with `%s` RPC',
                        rpc_object.alias)

        for existing_torrent in torrents:
            LOGGER.info('Processing `%s` torrent with `%s` RPC ...',
                        existing_torrent['name'], rpc_object.alias)

            page_url = get_url_from_string(existing_torrent['comment'])

            if page_url in download_cache:
                new_torrent = download_cache[page_url]
            else:
                new_torrent = get_torrent_from_url(page_url)
                download_cache[page_url] = new_torrent

            if new_torrent is None:
                LOGGER.error('Unable to get torrent from `%s`', page_url)
                continue

            if existing_torrent['hash'] == new_torrent['hash']:
                LOGGER.info('Torrent `%s` is up-to-date',
                            existing_torrent['name'])
                continue

            LOGGER.info('Torrent `%s` update is available',
                        existing_torrent['name'])
            try:
                rpc_object.method_add_torrent(new_torrent['torrent'],
                                              existing_torrent['download_to'])
                LOGGER.info('Torrent `%s` is updated',
                            existing_torrent['name'])
                structure_torrent_data(updated_by_hashes,
                                       existing_torrent['hash'], new_torrent)
            except TorrtRPCException as e:
                LOGGER.error('Unable to replace `%s` torrent: %s',
                             existing_torrent['name'], e.message)
            else:
                unregister_torrent(existing_torrent['hash'])
                if remove_outdated:
                    rpc_object.method_remove_torrent(existing_torrent['hash'])

    return updated_by_hashes
Пример #2
0
def update_torrents(hashes, remove_outdated=True):
    """Performs torrent updates.

    :param hashes: list - torrent identifying hashes
    :param remove_outdated: bool - flag to remove outdated torrents from torrent clients
    :return: hash-indexed dictionary with information on updated torrents
    :rtype: dict
    """
    updated_by_hashes = {}
    download_cache = {}

    for _, rpc_object in iter_rpc():
        LOGGER.info('Getting torrents using `%s` RPC ...', rpc_object.alias)
        torrents = rpc_object.method_get_torrents(hashes)

        if not torrents:
            LOGGER.info('No significant torrents found with `%s` RPC', rpc_object.alias)

        for existing_torrent in torrents:
            LOGGER.info('Processing `%s` torrent with `%s` RPC ...', existing_torrent['name'], rpc_object.alias)

            page_url = get_url_from_string(existing_torrent['comment'])

            if page_url in download_cache:
                new_torrent = download_cache[page_url]
            else:
                new_torrent = get_torrent_from_url(page_url)
                download_cache[page_url] = new_torrent

            if new_torrent is None:
                LOGGER.error('Unable to get torrent from `%s`', page_url)
                continue

            if existing_torrent['hash'] == new_torrent['hash']:
                LOGGER.info('Torrent `%s` is up-to-date', existing_torrent['name'])
                continue

            LOGGER.info('Torrent `%s` update is available', existing_torrent['name'])
            try:
                rpc_object.method_add_torrent(new_torrent['torrent'], existing_torrent['download_to'])
                LOGGER.info('Torrent `%s` is updated', existing_torrent['name'])
                structure_torrent_data(updated_by_hashes, existing_torrent['hash'], new_torrent)
            except TorrtRPCException as e:
                LOGGER.error('Unable to replace `%s` torrent: %s', existing_torrent['name'], e.message)
            else:
                unregister_torrent(existing_torrent['hash'])
                if remove_outdated:
                    rpc_object.method_remove_torrent(existing_torrent['hash'])

    return updated_by_hashes
Пример #3
0
def register_torrent(hash_str, torrent_data=None):
    """Registers torrent within torrt. Used to register torrents that already exists
    in torrent clients.

    :param hash_str: str - torrent identifying hash
    :param torrent_data: dict
    :return:
    """
    LOGGER.info('Registering `%s` torrent ...', hash_str)
    if torrent_data is None:
        torrent_data = {}
    cfg = {'torrents': {}}
    structure_torrent_data(cfg['torrents'], hash_str, torrent_data)
    TorrtConfig.update(cfg)
Пример #4
0
def register_torrent(hash_str, torrent_data=None):
    """Registers torrent within torrt. Used to register torrents that already exists
    in torrent clients.

    :param hash_str: str - torrent identifying hash
    :param torrent_data: dict
    :return:
    """
    LOGGER.info('Registering `%s` torrent ...', hash_str)
    if torrent_data is None:
        torrent_data = {}
    cfg = {'torrents': {}}
    structure_torrent_data(cfg['torrents'], hash_str, torrent_data)
    TorrtConfig.update(cfg)
Пример #5
0
def register_torrent(hash_str, torrent_data=None, url=None):
    """Registers torrent within torrt. Used to register torrents that already exists
    in torrent clients.

    :param hash_str: str - torrent identifying hash
    :param torrent_data: dict
    :param url: fallback url that will be used in case torrent comment doesn't contain url
    :return:
    """
    LOGGER.debug('Registering `%s` torrent ...', hash_str)
    if torrent_data is None:
        torrent_data = {}
    if url:
        torrent_data['url'] = url
    cfg = {'torrents': {}}
    structure_torrent_data(cfg['torrents'], hash_str, torrent_data)
    TorrtConfig.update(cfg)
Пример #6
0
def register_torrent(hash_str, torrent_data=None, url=None):
    """Registers torrent within torrt. Used to register torrents that already exists
    in torrent clients.

    :param hash_str: str - torrent identifying hash
    :param torrent_data: dict
    :param url: fallback url that will be used in case torrent comment doesn't contain url
    :return:
    """
    LOGGER.debug('Registering `%s` torrent ...', hash_str)

    if torrent_data is None:
        torrent_data = {}

    if url:
        torrent_data['url'] = url

    cfg = {'torrents': {}}
    structure_torrent_data(cfg['torrents'], hash_str, torrent_data)
    config.update(cfg)
Пример #7
0
def update_torrents(hashes, remove_outdated=True):
    """Performs torrent updates.

    :param hashes: list - torrent identifying hashes
    :param remove_outdated: bool - flag to remove outdated torrents from torrent clients
    :return: hash-indexed dictionary with information on updated torrents
    :rtype: dict
    """
    updated_by_hashes = {}
    download_cache = {}

    to_check = None

    if isinstance(hashes, list):
        warn('`hashes` argument of list type is deprecated and will be removed in 1.0. '
             'Please pass the argument of Dict[hash, torrent] type instead.', DeprecationWarning, stacklevel=2)

    else:
        to_check = hashes
        hashes = list(to_check.keys())

    for _, rpc_object in iter_rpc():

        LOGGER.info('Getting torrents from `%s` ...', rpc_object.alias)
        torrents = rpc_object.method_get_torrents(hashes)

        if not torrents:
            LOGGER.info('  No significant torrents found')

        for existing_torrent in torrents:
            LOGGER.info('  Processing `%s`...', existing_torrent['name'])

            page_url = get_url_from_string(existing_torrent['comment'])
            if not page_url:
                page_url = to_check[existing_torrent['hash']].get('url', None) if to_check else None

            if not page_url:
                LOGGER.warning('    Torrent `%s` has no link in comment. Skipped', existing_torrent['name'])
                continue

            if page_url in download_cache:
                new_torrent = download_cache[page_url]

            else:
                new_torrent = get_torrent_from_url(page_url)
                download_cache[page_url] = new_torrent

            if new_torrent is None:
                LOGGER.error('    Unable to get torrent from `%s`', page_url)
                continue

            if existing_torrent['hash'] == new_torrent['hash']:
                LOGGER.info('    No updates')
                continue

            LOGGER.debug('    Update is available')

            try:
                rpc_object.method_add_torrent(new_torrent['torrent'], existing_torrent['download_to'])
                new_torrent['url'] = page_url
                LOGGER.info('    Torrent is updated')
                structure_torrent_data(updated_by_hashes, existing_torrent['hash'], new_torrent)

            except TorrtRPCException as e:
                LOGGER.error('    Unable to replace torrent: %s', e.message)

            else:
                unregister_torrent(existing_torrent['hash'])

                if remove_outdated:
                    rpc_object.method_remove_torrent(existing_torrent['hash'])

    return updated_by_hashes
Пример #8
0
def update_torrents(hashes, remove_outdated=True):
    """Performs torrent updates.

    :param hashes: list - torrent identifying hashes
    :param remove_outdated: bool - flag to remove outdated torrents from torrent clients
    :return: hash-indexed dictionary with information on updated torrents
    :rtype: dict
    """
    updated_by_hashes = {}
    download_cache = {}

    to_check = None
    if isinstance(hashes, list):
        warn(
            '`hashes` argument of list type is deprecated and will be removed in 1.0. '
            'Please pass the argument of Dict[hash, torrent] type instead.',
            DeprecationWarning,
            stacklevel=2)
    else:
        to_check = hashes
        hashes = list(to_check.keys())

    for _, rpc_object in iter_rpc():
        LOGGER.info('Getting torrents from `%s` ...', rpc_object.alias)
        torrents = rpc_object.method_get_torrents(hashes)

        if not torrents:
            LOGGER.info('  No significant torrents found')

        for existing_torrent in torrents:
            LOGGER.info('  Processing `%s`...', existing_torrent['name'])

            page_url = get_url_from_string(existing_torrent['comment'])
            if not page_url:
                page_url = to_check[existing_torrent['hash']].get(
                    'url', None) if to_check else None

            if not page_url:
                LOGGER.warning(
                    '    Torrent `%s` has no link in comment. Skipped',
                    existing_torrent['name'])
                continue

            if page_url in download_cache:
                new_torrent = download_cache[page_url]
            else:
                new_torrent = get_torrent_from_url(page_url)
                download_cache[page_url] = new_torrent

            if new_torrent is None:
                LOGGER.error('    Unable to get torrent from `%s`', page_url)
                continue

            if existing_torrent['hash'] == new_torrent['hash']:
                LOGGER.info('    No updates')
                continue

            LOGGER.debug('    Update is available')
            try:
                rpc_object.method_add_torrent(new_torrent['torrent'],
                                              existing_torrent['download_to'])
                new_torrent['url'] = page_url
                LOGGER.info('    Torrent is updated')
                structure_torrent_data(updated_by_hashes,
                                       existing_torrent['hash'], new_torrent)

            except TorrtRPCException as e:
                LOGGER.error('    Unable to replace torrent: %s', e.message)

            else:
                unregister_torrent(existing_torrent['hash'])
                if remove_outdated:
                    rpc_object.method_remove_torrent(existing_torrent['hash'])

    return updated_by_hashes