Example #1
0
 def _http_query(self, query, timeout=None):
     """
     Query Transmission through HTTP.
     """
     request_count = 0
     if timeout is None:
         timeout = self._query_timeout
     while True:
         if request_count >= 10:
             raise TransmissionError(
                 'too much request, try enable logger to see what happened')
         self.logger.debug({
             'url': self.url,
             'headers': self._http_header,
             'data': json.loads(query),
             'timeout': timeout,
         })
         request_count += 1
         r = requests.post(self.url,
                           headers=self._http_header,
                           json=json.loads(query),
                           timeout=timeout)
         self.session_id = r.headers.get('X-Transmission-Session-Id', 0)
         self.logger.debug(r.text)
         if r.status_code == 401:
             print(r.request.headers)
             raise TransmissionError('transmission daemon require auth',
                                     original=r)
         if r.status_code != 409:
             return r.text
Example #2
0
 def _http_query(self, query: Any, timeout: _Timeout = None) -> str:
     """
     Query Transmission through HTTP.
     """
     request_count = 0
     if timeout is None:
         timeout = self.timeout
     while True:
         if request_count >= 10:
             raise TransmissionError(
                 "too much request, try enable logger to see what happened")
         self.logger.debug({
             "url": self.url,
             "headers": self._http_header,
             "data": json.loads(query),
             "timeout": timeout,
         })
         request_count += 1
         r = self._http_session.post(
             self.url,
             headers=self._http_header,
             json=json.loads(query),
             timeout=timeout,
         )
         self.session_id = r.headers.get("X-Transmission-Session-Id", "0")
         self.logger.debug(r.text)
         if r.status_code == 401:
             self.logger.debug(r.request.headers)
             raise TransmissionError("transmission daemon require auth",
                                     original=r)
         if r.status_code != 409:
             return r.text
Example #3
0
 def _http_query(self, query, timeout=None):
     """
     Query Transmission through HTTP.
     """
     request_count = 0
     if timeout is None:
         timeout = self._query_timeout
     while True:
         if request_count >= 10:
             raise TransmissionError(
                 'too much request, try enable logger to see what happened')
         self.logger.debug({'url': self.url,
                            'headers': self._http_header,
                            'data': json.loads(query),
                            'timeout': timeout})
         request_count += 1
         r = requests.post(self.url, headers=self._http_header,
                           auth=self.auth,
                           json=json.loads(query), timeout=timeout)
         self.session_id = r.headers.get('X-Transmission-Session-Id', 0)
         self.logger.debug(r.text)
         if r.status_code == 401:
             if not self.auth:
                 raise TransmissionError(
                     'Transmission daemon need a username and password')
             else:
                 raise TransmissionError(
                     'username and password are incorrect')
         if r.status_code != 409:
             return r.text
Example #4
0
    def _http_query(self, query: dict, timeout: _Timeout = None) -> str:
        """
        Query Transmission through HTTP.
        """
        request_count = 0
        if timeout is None:
            timeout = self.timeout
        while True:
            if request_count >= 10:
                raise TransmissionError(
                    "too much request, try enable logger to see what happened")
            self.logger.debug({
                "url": self.url,
                "headers": self._http_header,
                "data": query,
                "timeout": timeout,
            })
            request_count += 1
            try:
                r = self._http_session.post(
                    self.url,
                    headers=self._http_header,
                    json=query,
                    timeout=timeout,
                )
            except requests.exceptions.Timeout as e:
                raise TransmissionTimeoutError(
                    "timeout when connection to transmission daemon") from e
            except requests.exceptions.ConnectionError as e:
                raise TransmissionConnectError(
                    f"can't connect to transmission daemon: {str(e)}") from e

            self.session_id = r.headers.get("X-Transmission-Session-Id", "0")
            self.logger.debug(r.text)
            if r.status_code in {401, 403}:
                self.logger.debug(r.request.headers)
                raise TransmissionAuthError("transmission daemon require auth",
                                            original=r)
            if r.status_code != 409:
                return r.text
Example #5
0
    def _request(self,
                 method,
                 arguments=None,
                 ids=None,
                 require_ids=False,
                 timeout=None):
        """
        Send json-rpc request to Transmission using http POST
        :type arguments: object
        :type method: str
        """
        if not isinstance(method, str):
            raise ValueError('request takes method as string')
        if arguments is None:
            arguments = {}
        if not isinstance(arguments, dict):
            raise ValueError('request takes arguments as dict')
        arguments = {
            key.replace('_', '-'): value
            for key, value in arguments.items()
        }
        ids = parse_torrent_ids(ids)
        if len(ids) > 0:
            arguments['ids'] = ids
        elif require_ids:
            raise ValueError('request require ids')

        query = json.dumps({
            'tag': self._sequence,
            'method': method,
            'arguments': arguments
        })
        self._sequence += 1
        start = time.time()
        http_data = self._http_query(query, timeout)
        elapsed = time.time() - start
        self.logger.info('http request took %.3f s' % elapsed)

        try:
            data = json.loads(http_data)
        except ValueError as error:
            self.logger.error('Error: ' + str(error))
            self.logger.error('Request: \"%s\"' % query)
            self.logger.error('HTTP data: \"%s\"' % http_data)
            raise ValueError from error

        self.logger.debug(json.dumps(data, indent=2))
        if 'result' in data:
            if data['result'] != 'success':
                raise TransmissionError('Query failed with result \"%s\".' %
                                        (data['result']))
        else:
            raise TransmissionError('Query failed without result.')

        results = {}
        if method == 'torrent-get':
            for item in data['arguments']['torrents']:
                results[item['id']] = Torrent(self, item)
                if self.protocol_version == 2 and 'peers' not in item:
                    self.protocol_version = 1
        elif method == 'torrent-add':
            item = None
            if 'torrent-added' in data['arguments']:
                item = data['arguments']['torrent-added']
            elif 'torrent-duplicate' in data['arguments']:
                item = data['arguments']['torrent-duplicate']
            if item:
                results[item['id']] = Torrent(self, item)
            else:
                raise TransmissionError('Invalid torrent-add response.')
        elif method == 'session-get':
            self._update_session(data['arguments'])
        elif method == 'session-stats':
            # older versions of T has the return data in "session-stats"
            if 'session-stats' in data['arguments']:
                self._update_session(data['arguments']['session-stats'])
            else:
                self._update_session(data['arguments'])
        elif method in ('port-test', 'blocklist-update', 'free-space',
                        'torrent-rename-path'):
            results = data['arguments']
        else:
            return None

        return results
Example #6
0
    def _request(
        self,
        method: str,
        arguments: Dict[str, Any] = None,
        ids: _TorrentIDs = None,
        require_ids: bool = False,
        timeout: _Timeout = None,
    ) -> dict:
        """
        Send json-rpc request to Transmission using http POST
        """
        if not isinstance(method, str):
            raise ValueError("request takes method as string")
        if arguments is None:
            arguments = {}
        if not isinstance(arguments, dict):
            raise ValueError("request takes arguments as dict")
        arguments = {
            key.replace("_", "-"): value
            for key, value in arguments.items()
        }
        ids = _parse_torrent_ids(ids)
        if len(ids) > 0:
            arguments["ids"] = ids
        elif require_ids:
            raise ValueError("request require ids")

        query = json.dumps({
            "tag": self._sequence,
            "method": method,
            "arguments": arguments
        })
        self._sequence += 1
        start = time.time()
        http_data = self._http_query(query, timeout)
        elapsed = time.time() - start
        self.logger.info("http request took %.3f s" % elapsed)

        try:
            data: dict = json.loads(http_data)
        except ValueError as error:
            self.logger.error("Error: " + str(error))
            self.logger.error('Request: "%s"' % query)
            self.logger.error('HTTP data: "%s"' % http_data)
            raise ValueError from error

        self.logger.debug(json.dumps(data, indent=2))
        if "result" in data:
            if data["result"] != "success":
                raise TransmissionError('Query failed with result "%s".' %
                                        (data["result"]))
        else:
            raise TransmissionError("Query failed without result.")

        results = {}
        if method == "torrent-get":
            for item in data["arguments"]["torrents"]:
                results[item["id"]] = Torrent(self, item)
                if self.protocol_version == 2 and "peers" not in item:
                    self.protocol_version = 1
        elif method == "torrent-add":
            item = None
            if "torrent-added" in data["arguments"]:
                item = data["arguments"]["torrent-added"]
            elif "torrent-duplicate" in data["arguments"]:
                item = data["arguments"]["torrent-duplicate"]
            if item:
                results[item["id"]] = Torrent(self, item)
            else:
                raise TransmissionError("Invalid torrent-add response.")
        elif method == "session-get":
            self._update_session(data["arguments"])
        elif method == "session-stats":
            # older versions of T has the return data in "session-stats"
            if "session-stats" in data["arguments"]:
                self._update_session(data["arguments"]["session-stats"])
            else:
                self._update_session(data["arguments"])
        elif method in (
                "port-test",
                "blocklist-update",
                "free-space",
                "torrent-rename-path",
        ):
            results = data["arguments"]
        else:
            return data

        return results