Example #1
0
    def _request(self, method='get', params=None, data=None, files=None, **kwargs):

        params = params or {}

        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logger.log('%s: sending %s request to %s with ...' % (self.name, method.upper(), self.url), logger.DEBUG)
        lines = [('params', (str(params), '')[not params]),
                 ('data', (str(data), '')[not data]),
                 ('files', (str(files), '')[not files]),
                 ('json', (str(kwargs.get('json')), '')[not kwargs.get('json')])]
        m, c = 300, 100
        type_chunks = [(linetype, [ln[i:i + c] for i in range(0, min(len(ln), m), c)]) for linetype, ln in lines if ln]
        for (arg, chunks) in type_chunks:
            output = []
            nch = len(chunks) - 1
            for i, seg in enumerate(chunks):
                if nch == i and 'files' == arg:
                    sample = ' ..excerpt(%s/%s)' % (m, len(lines[2][1]))
                    seg = seg[0:c - (len(sample) - 2)] + sample
                output += ['%s: request %s= %s%s%s' % (self.name, arg, ('', '..')[bool(i)], seg, ('', '..')[i != nch])]
            for out in output:
                logger.log(out, logger.DEBUG)

        if not self.auth:
            logger.log('%s: Authentication Failed' % self.name, logger.ERROR)
            return False
        try:
            response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
                                                             timeout=kwargs.pop('timeout', 120), verify=False, **kwargs)
        except requests.exceptions.ConnectionError as e:
            logger.log('%s: Unable to connect %s' % (self.name, ex(e)), logger.ERROR)
            return False
        except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
            logger.log('%s: Invalid Host' % self.name, logger.ERROR)
            return False
        except requests.exceptions.HTTPError as e:
            logger.log('%s: Invalid HTTP Request %s' % (self.name, ex(e)), logger.ERROR)
            return False
        except requests.exceptions.Timeout as e:
            logger.log('%s: Connection Timeout %s' % (self.name, ex(e)), logger.ERROR)
            return False
        except Exception as e:
            logger.log('%s: Unknown exception raised when sending torrent to %s: %s' % (self.name, self.name, ex(e)),
                       logger.ERROR)
            return False

        if 401 == response.status_code:
            logger.log('%s: Invalid Username or Password, check your config' % self.name, logger.ERROR)
            return False

        if response.status_code in http_error_code.keys():
            logger.log('%s: %s' % (self.name, http_error_code[response.status_code]), logger.DEBUG)
            return False

        logger.log('%s: Response to %s request is %s' % (self.name, method.upper(), response.text), logger.DEBUG)

        return response
Example #2
0
    def _request(self, method='get', params={}, data=None, files=None):
        response = None
        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logger.log(
            self.name + u': Requested a ' + method.upper() +
            ' connection to url ' + self.url + ' with Params= ' + str(params) +
            ' Data=' + str(data if data else 'None')[0:99] +
            ('...' if len(data if data else 'None') > 200 else ''),
            logger.DEBUG
        )

        logger.log(
            self.name + u': Requested a ' + method.upper() +
            ' connection to url ' + self.url + ' with Params= ' + str(params) +
            ((' Data=' + str(data)[0:100] + ('...' if len(data) > 100 else ''))
             if data is not None else ''),
            logger.DEBUG
        )

        if not self.auth:
            logger.log(self.name + u': Authentication Failed', logger.ERROR)
            return False
        try:
            response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
                                                                  timeout=120, verify=False)
        except requests.exceptions.ConnectionError as e:
            logger.log(self.name + u': Unable to connect ' + ex(e), logger.ERROR)
            return False
        except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
            logger.log(self.name + u': Invalid Host', logger.ERROR)
            return False
        except requests.exceptions.HTTPError as e:
            logger.log(self.name + u': Invalid HTTP Request ' + ex(e), logger.ERROR)
            return False
        except requests.exceptions.Timeout as e:
            logger.log(self.name + u': Connection Timeout ' + ex(e), logger.ERROR)
            return False
        except Exception as e:
            logger.log(self.name + u': Unknown exception raised when sending torrent to ' + self.name + ': ' + ex(e),
                       logger.ERROR)
            return False

        if response.status_code == 401:
            logger.log(self.name + u': Invalid Username or Password, check your config', logger.ERROR)
            return False

        if response.status_code in http_error_code.keys():
            logger.log(self.name + u': ' + http_error_code[response.status_code], logger.DEBUG)
            return False

        logger.log(self.name + u': Response to ' + method.upper() + ' request is ' + response.text, logger.DEBUG)

        return response
Example #3
0
    def _request(self, method='get', params=None, data=None, files=None):

        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logging.debug(self.name + ': Requested a ' + method.upper() +
                      ' connection to url ' + self.url + ' with Params: ' +
                      str(params) + ' Data: ' + str(data)[0:99] +
                      ('...' if len(str(data)) > 200 else ''))

        if not self.auth:
            logging.warning(self.name + ': Authentication Failed')
            return False
        try:
            self.response = self.session.__getattribute__(method)(
                self.url,
                params=params,
                data=data,
                files=files,
                timeout=120,
                verify=False)
        except requests.exceptions.ConnectionError as e:
            logging.error(self.name + ': Unable to connect ' + str(e))
            return False
        except (requests.exceptions.MissingSchema,
                requests.exceptions.InvalidURL):
            logging.error(self.name + ': Invalid Host')
            return False
        except requests.exceptions.HTTPError as e:
            logging.error(self.name + ': Invalid HTTP Request ' + str(e))
            return False
        except requests.exceptions.Timeout as e:
            logging.warning(self.name + ': Connection Timeout ' + str(e))
            return False
        except Exception as e:
            logging.error(self.name +
                          ': Unknown exception raised when send torrent to ' +
                          self.name + ': ' + str(e))
            return False

        if self.response.status_code == 401:
            logging.error(self.name +
                          u': Invalid Username or Password, check your config')
            return False

        if self.response.status_code in http_error_code.keys():
            logging.debug(self.name + ': ' +
                          http_error_code[self.response.status_code])
            return False

        logging.debug(self.name + ': Response to ' + method.upper() +
                      ' request is ' + self.response.text)

        return True
Example #4
0
    def _request(self, method='get', params=None, data=None, files=None):

        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logging.debug(
                self.name + ': Requested a ' + method.upper() + ' connection to url ' + self.url +
                ' with Params: ' + str(params) + ' Data: ' + str(data)[0:99] + ('...' if len(str(data)) > 200 else ''))

        if not self.auth:
            logging.warning(self.name + ': Authentication Failed')
            return False
        try:
            self.response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
                                                                  timeout=120, verify=False)
        except requests.exceptions.ConnectionError as e:
            logging.error(self.name + ': Unable to connect ' + str(e))
            return False
        except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
            logging.error(self.name + ': Invalid Host')
            return False
        except requests.exceptions.HTTPError as e:
            logging.error(self.name + ': Invalid HTTP Request ' + str(e))
            return False
        except requests.exceptions.Timeout as e:
            logging.warning(self.name + ': Connection Timeout ' + str(e))
            return False
        except Exception as e:
            logging.error(self.name + ': Unknown exception raised when send torrent to ' + self.name + ': ' + str(e))
            return False

        if self.response.status_code == 401:
            logging.error(self.name + u': Invalid Username or Password, check your config')
            return False

        if self.response.status_code in http_error_code.keys():
            logging.debug(self.name + ': ' + http_error_code[self.response.status_code])
            return False

        logging.debug(self.name + ': Response to ' + method.upper() + ' request is ' + self.response.text)

        return True
Example #5
0
        except requests.exceptions.HTTPError, e:
            logger.log(self.name + u': Invalid HTTP Request ' + ex(e), logger.ERROR)
            return False
        except requests.exceptions.Timeout, e:
            logger.log(self.name + u': Connection Timeout ' + ex(e), logger.ERROR)
            return False
        except Exception, e:
            logger.log(self.name + u': Unknown exception raised when send torrent to ' + self.name + ': ' + ex(e),
                       logger.ERROR)
            return False

        if self.response.status_code == 401:
            logger.log(self.name + u': Invalid Username or Password, check your config', logger.ERROR)
            return False

        if self.response.status_code in http_error_code.keys():
            logger.log(self.name + u': ' + http_error_code[self.response.status_code], logger.DEBUG)
            return False

        logger.log(self.name + u': Response to ' + method.upper() + ' request is ' + self.response.text, logger.DEBUG)

        return True

    def _get_auth(self):
        """
        This should be overridden and should return the auth_id needed for the client
        """
        return None

    def _add_torrent_uri(self, result):
        """
Example #6
0
    def _request(self,
                 method='get',
                 params=None,
                 data=None,
                 files=None,
                 **kwargs):

        params = params or {}

        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logger.log(
            '%s: sending %s request to %s with ...' %
            (self.name, method.upper(), self.url), logger.DEBUG)
        lines = [('params', (str(params), '')[not params]),
                 ('data', (str(data), '')[not data]),
                 ('files', (str(files), '')[not files]),
                 ('json', (str(kwargs.get('json')),
                           '')[not kwargs.get('json')])]
        m, c = 300, 100
        type_chunks = [(linetype,
                        [ln[i:i + c] for i in range(0, min(len(ln), m), c)])
                       for linetype, ln in lines if ln]
        for (arg, chunks) in type_chunks:
            output = []
            nch = len(chunks) - 1
            for i, seg in enumerate(chunks):
                if nch == i and 'files' == arg:
                    sample = ' ..excerpt(%s/%s)' % (m, len(lines[2][1]))
                    seg = seg[0:c - (len(sample) - 2)] + sample
                output += [
                    '%s: request %s= %s%s%s' % (self.name, arg,
                                                ('', '..')[bool(i)], seg,
                                                ('', '..')[i != nch])
                ]
            for out in output:
                logger.log(out, logger.DEBUG)

        if not self.auth:
            logger.log('%s: Authentication Failed' % self.name, logger.ERROR)
            return False
        try:
            response = self.session.__getattribute__(method)(
                self.url,
                params=params,
                data=data,
                files=files,
                timeout=kwargs.pop('timeout', 120),
                verify=False,
                **kwargs)
        except requests.exceptions.ConnectionError as e:
            logger.log('%s: Unable to connect %s' % (self.name, ex(e)),
                       logger.ERROR)
            return False
        except (requests.exceptions.MissingSchema,
                requests.exceptions.InvalidURL):
            logger.log('%s: Invalid Host' % self.name, logger.ERROR)
            return False
        except requests.exceptions.HTTPError as e:
            logger.log('%s: Invalid HTTP Request %s' % (self.name, ex(e)),
                       logger.ERROR)
            return False
        except requests.exceptions.Timeout as e:
            logger.log('%s: Connection Timeout %s' % (self.name, ex(e)),
                       logger.ERROR)
            return False
        except Exception as e:
            logger.log(
                '%s: Unknown exception raised when sending torrent to %s: %s' %
                (self.name, self.name, ex(e)), logger.ERROR)
            return False

        if 401 == response.status_code:
            logger.log(
                '%s: Invalid Username or Password, check your config' %
                self.name, logger.ERROR)
            return False

        if response.status_code in http_error_code.keys():
            logger.log(
                '%s: %s' % (self.name, http_error_code[response.status_code]),
                logger.DEBUG)
            return False

        logger.log(
            '%s: Response to %s request is %s' %
            (self.name, method.upper(), response.text), logger.DEBUG)

        return response
Example #7
0
        except requests.exceptions.HTTPError, e:
            logger.log(self.name + u': Invalid HTTP Request ' + ex(e), logger.ERROR)
            return False
        except requests.exceptions.Timeout, e:
            logger.log(self.name + u': Connection Timeout ' + ex(e), logger.ERROR)
            return False
        except Exception, e:
            logger.log(self.name + u': Unknown exception raised when send torrent to ' + self.name + ': ' + ex(e),
                       logger.ERROR)
            return False

        if self.response.status_code == 401:
            logger.log(self.name + u': Invalid Username or Password, check your config', logger.ERROR)
            return False

        if self.response.status_code in http_error_code.keys():
            logger.log(self.name + u': ' + http_error_code[self.response.status_code], logger.DEBUG)
            return False

        logger.log(self.name + u': Response to ' + method.upper() + ' request is ' + self.response.text, logger.DEBUG)

        return True

    def _get_auth(self):
        """
        This should be overridden and should return the auth_id needed for the client
        """
        return None

    def _add_torrent_uri(self, result):
        """
Example #8
0
    def _request(self, method='get', params={}, data=None, files=None):
        response = None
        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        logger.log(
            self.name + u': Requested a ' + method.upper() +
            ' connection to url ' + self.url + ' with Params= ' + str(params) +
            ' Data=' + str(data if data else 'None')[0:99] +
            ('...' if len(data if data else 'None') > 200 else ''),
            logger.DEBUG)

        logger.log(
            self.name + u': Requested a ' + method.upper() +
            ' connection to url ' + self.url + ' with Params= ' + str(params) +
            ((' Data=' + str(data)[0:100] +
              ('...' if len(data) > 100 else '')) if data is not None else ''),
            logger.DEBUG)

        if not self.auth:
            logger.log(self.name + u': Authentication Failed', logger.ERROR)
            return False
        try:
            response = self.session.__getattribute__(method)(self.url,
                                                             params=params,
                                                             data=data,
                                                             files=files,
                                                             timeout=120,
                                                             verify=False)
        except requests.exceptions.ConnectionError as e:
            logger.log(self.name + u': Unable to connect ' + ex(e),
                       logger.ERROR)
            return False
        except (requests.exceptions.MissingSchema,
                requests.exceptions.InvalidURL):
            logger.log(self.name + u': Invalid Host', logger.ERROR)
            return False
        except requests.exceptions.HTTPError as e:
            logger.log(self.name + u': Invalid HTTP Request ' + ex(e),
                       logger.ERROR)
            return False
        except requests.exceptions.Timeout as e:
            logger.log(self.name + u': Connection Timeout ' + ex(e),
                       logger.ERROR)
            return False
        except Exception as e:
            logger.log(
                self.name +
                u': Unknown exception raised when sending torrent to ' +
                self.name + ': ' + ex(e), logger.ERROR)
            return False

        if response.status_code == 401:
            logger.log(
                self.name +
                u': Invalid Username or Password, check your config',
                logger.ERROR)
            return False

        if response.status_code in http_error_code.keys():
            logger.log(
                self.name + u': ' + http_error_code[response.status_code],
                logger.DEBUG)
            return False

        logger.log(
            self.name + u': Response to ' + method.upper() + ' request is ' +
            response.text, logger.DEBUG)

        return response