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
def remove_torrent(hash_str, with_data=False): """Removes torrent by its hash from torrt and torrent clients, :param hash_str: str - torrent identifying hash :param with_data: bool - flag to also remove files from torrent :return: """ LOGGER.info('Removing torrent `%s` (with data = %s) ...', hash_str, with_data) for _, rpc_object in iter_rpc(): LOGGER.info('Removing torrent using `%s` RPC ...', rpc_object.alias) rpc_object.method_remove_torrent(hash_str, with_data=with_data) unregister_torrent(hash_str)
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)
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
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