Esempio n. 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
Esempio n. 2
0
def test_trackers():
    bootstrap()

    tracker_objects = TrackerObjectsRegistry.get()

    assert tracker_objects

    for tracker_alias, tracker_obj in tracker_objects.items():

        urls = tracker_obj.test_urls

        for url in urls:
            torrent_data = get_torrent_from_url(url)
            assert torrent_data, '%s: Unable to parse test URL %s' % (tracker_alias, url)
Esempio n. 3
0
def add_torrent_from_url(url, download_to=None):
    """Adds torrent from a given URL to torrt and torrent clients,

    :param url: str - torrent URL
    :param download_to: str or None - path to download files from torrent into (in terms of torrent client filesystem)
    :return:
    """
    LOGGER.info('Adding torrent from `%s` ...', url)

    torrent_data = get_torrent_from_url(url)
    if torrent_data is None:
        LOGGER.error('Unable to add torrent from `%s`', url)
    else:
        for rpc_alias, rpc_object in iter_rpc():
            rpc_object.method_add_torrent(torrent_data['torrent'], download_to=download_to)
            register_torrent(torrent_data['hash'], torrent_data)
            LOGGER.info('Torrent from `%s` is added within `%s`', url, rpc_alias)
Esempio n. 4
0
def add_torrent_from_url(url, download_to=None):
    """Adds torrent from a given URL to torrt and torrent clients,

    :param url: str - torrent URL
    :param download_to: str or None - path to download files from torrent into (in terms of torrent client filesystem)
    :return:
    """
    LOGGER.debug('Adding torrent from `%s` ...', url)

    torrent_data = get_torrent_from_url(url)
    if torrent_data is None:
        LOGGER.error('Unable to add torrent from `%s`', url)
    else:
        for rpc_alias, rpc_object in iter_rpc():
            rpc_object.method_add_torrent(torrent_data['torrent'],
                                          download_to=download_to)
            register_torrent(torrent_data['hash'], torrent_data)
            LOGGER.info('Torrent from `%s` is added within `%s`', url,
                        rpc_alias)
Esempio n. 5
0
 def handle_process_url(self, bot, update, user_data):
     torrent_url = update.message.text
     torrent_data = get_torrent_from_url(torrent_url)
     if torrent_data is None:
         update.message.reply_text('Unable to add torrent from `%s`' % torrent_url,
                                   reply_markup=ReplyKeyboardRemove())
         return ConversationHandler.END
     else:
         user_data['url'] = torrent_url
         download_dirs = set()
         for rpc_alias, rpc in RPCObjectsRegistry.get().items():
             if rpc.enabled:
                 torrents = rpc.method_get_torrents()
                 for torrent in torrents:
                     download_dirs.add(torrent['download_to'])
         choices = [[directory] for directory in download_dirs]
         update.message.reply_text('Where to download data? Send absolute path or "."',
                                   reply_markup=ReplyKeyboardMarkup(choices, one_time_keyboard=True))
         return self.PATH
Esempio n. 6
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
Esempio n. 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