def _get_torrents(self, filter=None, category=None, sort=None): """Get all torrents from qbittorrent api.""" params = {} if not self.api: raise DownloadClientConnectionException( 'Error while fetching torrent. Not authenticated.') if self.api >= (2, 0, 0): self.url = urljoin(self.host, 'api/v2/torrents/info') if filter: params['filter'] = filter if category: params['category'] = category if sort: params['sort'] = sort else: self.url = urljoin(self.host, 'json/torrents') try: if not self._request( method='get', params=params, cookies=self.session.cookies): log.warning('Error while fetching torrents.') return [] except RequestException as error: raise DownloadClientConnectionException( f'Error while fetching torrent. Error: {error}') return self.response.json()
def _torrent_properties(self, info_hash): """Get torrent properties.""" post_data = json.dumps({ 'method': 'core.get_torrent_status', 'params': [ info_hash, [ 'name', 'hash', 'progress', 'state', 'ratio', 'stop_ratio', 'is_seed', 'is_finished', 'paused', 'files', 'download_location' ], ], 'id': 72, }) log.debug('Checking {client} torrent {hash} status.', { 'client': self.name, 'hash': info_hash }) try: if not self._request( method='post', data=post_data) or self.response.json()['error']: log.warning('Error while fetching torrent {hash} status.', {'hash': info_hash}) return except RequestException as error: raise DownloadClientConnectionException( f'Error while fetching torrent info_hash {info_hash}. Error: {error}' ) return self.response.json()['result']
def _get_nzb_history(host=None): """Return a list of all groups (nzbs) currently in history.""" url = urljoin(app.SAB_HOST, 'api') params = { 'mode': 'history', 'apikey': app.SAB_APIKEY, 'output': 'json', 'failed_only': 0, 'limit': 100 } try: data = session.get_json(url, params=params, verify=False) except ConnectionError as error: raise DownloadClientConnectionException( f'Error while fetching sabnzbd history. Error: {error}') if not data: log.info('Error connecting to sab, no data returned') else: # check the result and determine if it's good or not result, _ = _check_sab_response(data) if result: return data return False
def _get_auth(self): if self.auth is not None: return self.auth if not self.host: return tp_kwargs = {} if app.TORRENT_AUTH_TYPE != 'none': tp_kwargs['authtype'] = app.TORRENT_AUTH_TYPE if not app.TORRENT_VERIFY_CERT: tp_kwargs['check_ssl_cert'] = False else: if app.SSL_CA_BUNDLE: tp_kwargs['check_ssl_cert'] = app.SSL_CA_BUNDLE try: if self.username and self.password: self.auth = RTorrent(self.host, self.username, self.password, True, tp_kwargs=tp_kwargs) else: self.auth = RTorrent(self.host, None, None, True) except Exception as error: # No request/connection specific exception thrown. raise DownloadClientConnectionException( f'Unable to authenticate with rtorrent client: {error}') return self.auth
def _get_nzb_history(): """Return a list of all groups (nzbs) from history.""" url = 'http{}://{}:{}@{}/xmlrpc'.format( 's' if app.NZBGET_USE_HTTPS else '', app.NZBGET_USERNAME, app.NZBGET_PASSWORD, app.NZBGET_HOST) if not nzb_connection(url): raise DownloadClientConnectionException( 'Error while fetching nzbget history') nzb_get_rpc = ServerProxy(url) try: nzb_groups = nzb_get_rpc.history() except ConnectionRefusedError as error: raise DownloadClientConnectionException( f'Error while fetching nzbget history. Error: {error}') return nzb_groups
def get_status(self, info_hash): """ Return torrent status. Example result: ``` 'hash': '35b814f1438054158b0bd07d305dc0edeb20b704' 'is_finished': False 'ratio': 0.0 'paused': False 'name': '[FFA] Haikyuu!!: To the Top 2nd Season - 11 [1080p][HEVC][Multiple Subtitle].mkv' 'stop_ratio': 2.0 'state': 'Downloading' 'progress': 23.362499237060547 'files': ({'index': 0, 'offset': 0, 'path': '[FFA] Haikyuu!!: To ...title].mkv', 'size': 362955692},) 'is_seed': False ``` """ if not self.connect(): raise DownloadClientConnectionException(f'Error while fetching torrent info_hash {info_hash}') torrent = self.drpc._torrent_properties(info_hash) if not torrent: return False client_status = ClientStatus() if torrent['state'] == 'Downloading': client_status.add_status_string('Downloading') if torrent['paused']: client_status.add_status_string('Paused') # TODO: Find out which state the torrent get's when it fails. # if torrent[1] & 16: # client_status.add_status_string('Failed') if torrent['is_finished']: client_status.add_status_string('Completed') if torrent['ratio'] >= torrent['stop_ratio']: client_status.add_status_string('Seeded') # Store ratio client_status.ratio = torrent['ratio'] # Store progress client_status.progress = int(torrent['progress']) # Store destination client_status.destination = torrent['download_location'] # Store resource client_status.resource = torrent['name'] return client_status
def _torrent_properties(self, info_hash): """Get torrent properties.""" try: self.connect() log.debug('Checking DelugeD torrent {hash} status.', {'hash': info_hash}) torrent_data = self.client.core.get_torrent_status( info_hash, ('name', 'hash', 'progress', 'state', 'ratio', 'stop_ratio', 'is_seed', 'is_finished', 'paused', 'files', 'download_location')) except RequestException as error: raise DownloadClientConnectionException(f'Error while fetching torrent info_hash {info_hash}. Error: {error}') except Exception: log.warning('Error while fetching torrent {hash} status.', {'hash': info_hash}) return else: return torrent_data finally: if self.client: self.disconnect()
def _get_torrents(self): """ Get all torrents from utorrent api. Note! This is an expensive method. As when your looping through the history table to get a specific info_hash, it will get all torrents for each info_hash. We might want to cache this one. """ params = {'list': 1} try: self._request(params=params) json_response = self.response.json() except RequestException as error: raise DownloadClientConnectionException( f'Error while fetching torrent. Error: {error}') if json_response.get('torrents'): return json_response['torrents'] return []
def _torrent_properties(self, info_hash): """Get torrent properties.""" log.debug('Checking {client} torrent {hash} status.', {'client': self.name, 'hash': info_hash}) return_params = { 'ids': info_hash, 'fields': ['name', 'hashString', 'percentDone', 'status', 'percentDone', 'downloadDir', 'isStalled', 'errorString', 'seedRatioLimit', 'torrent-file', 'name', 'isFinished', 'uploadRatio', 'seedIdleLimit', 'activityDate'] } post_data = json.dumps({'arguments': return_params, 'method': 'torrent-get'}) if not self._request(method='post', data=post_data) or not self.check_response(): raise DownloadClientConnectionException(f'Error while fetching torrent {info_hash} status.') torrent = self.response.json()['arguments']['torrents'] if not torrent: log.debug('Could not locate torrent with {hash} status.', {'hash': info_hash}) return return torrent[0]