def _connection_handler(self):
        def _get_err_msg(exception):
            error_message = 'Exception = {}, Message = <{}>'.format(
                type(exception), exception)
            return error_message

        if str(type(Session())) == OtherConstants.CF_SESSION_TYPE:
            logger.info(
                'Establishing connection to Bittrex through {}.'.format(
                    self.url))
            logger.info(
                'cfscrape detected, will try to bypass Cloudflare if enabled.')
        else:
            logger.info(
                'Establishing connection to Bittrex through {}.'.format(
                    self.url))
        try:
            self.connection.conn.start()
        except TimeoutError as e:
            self.control_queue.put(ReconnectEvent(_get_err_msg(e)))
        except WebSocketConnectionClosedByUser:
            logger.info(InfoMessages.SUCCESSFUL_DISCONNECT)
        except WebSocketConnectionClosedException as e:
            self.control_queue.put(ReconnectEvent(_get_err_msg(e)))
        except TimeoutErrorUrlLib as e:
            self.control_queue.put(ReconnectEvent(_get_err_msg(e)))
        except ConnectionError:
            pass
            # Commenting it for the time being. It should be handled in _handle_subscribe.
            # event = ReconnectEvent(None)
            # self.control_queue.put(event)
        except Exception as e:
            logger.error(
                ErrorMessages.UNHANDLED_EXCEPTION.format(_get_err_msg(e)))
            self.disconnect()
Пример #2
0
 def _handle_connect(self):
     connection = Connection(self.url, Session())
     hub = connection.register_hub(BittrexParameters.HUB)
     connection.received += self._on_debug
     connection.error += self.on_error
     hub.client.on(BittrexParameters.MARKET_DELTA, self._on_public)
     hub.client.on(BittrexParameters.SUMMARY_DELTA, self._on_public)
     hub.client.on(BittrexParameters.SUMMARY_DELTA_LITE, self._on_public)
     hub.client.on(BittrexParameters.BALANCE_DELTA, self._on_private)
     hub.client.on(BittrexParameters.ORDER_DELTA, self._on_private)
     self.connection = BittrexConnection(connection, hub)
     thread = Thread(target=self._connection_handler, daemon=True, name='SocketConnectionThread')
     self.threads.append(thread)
     thread.start()
Пример #3
0
 def _handle_connect(self):
     if self.last_retry is not None and time() - self.last_retry >= 60:
         logger.debug('Last reconnection was more than 60 seconds ago. Resetting retry counter.')
         self.retry_fail = 0
     else:
         self.last_retry = time()
     connection = Connection(self.url, Session())
     hub = connection.register_hub(BittrexParameters.HUB)
     connection.received += self._on_debug
     connection.error += self.on_error
     hub.client.on(BittrexParameters.MARKET_DELTA, self._on_public)
     hub.client.on(BittrexParameters.SUMMARY_DELTA, self._on_public)
     hub.client.on(BittrexParameters.SUMMARY_DELTA_LITE, self._on_public)
     hub.client.on(BittrexParameters.BALANCE_DELTA, self._on_private)
     hub.client.on(BittrexParameters.ORDER_DELTA, self._on_private)
     self.connection = BittrexConnection(connection, hub)
     thread = Thread(target=self._connection_handler, name=OtherConstants.SOCKET_CONNECTION_THREAD)
     thread.daemon = True
     self.threads.append(thread)
     thread.start()
Пример #4
0
    def __init__(self,
                 uri='',
                 markup='',
                 session=None,
                 headers={'User-Agent': 'Mozilla/5.0'},
                 debug=False):
        logger.setLevel(logging.DEBUG if debug else logging.ERROR)

        self.debug = debug

        if not uri or uri in self.uris:
            super().__init__(markup, 'lxml')
            return

        self.uris.add(uri)

        from urllib.parse import ParseResult, urlparse
        from cfscrape import CloudflareScraper as Session
        from requests.exceptions import RequestException

        pu: ParseResult = urlparse(uri)

        self.start_uri = uri
        self.scheme = pu.scheme
        self.host = pu.hostname
        self.start_path = '?'.join((pu.path, pu.query))
        self.base = '%s://%s' % (pu.scheme, pu.netloc)
        self._session = session or Session()

        try:
            logger.debug('GET %s', uri)
            r = self._session.get(uri, timeout=10, headers=headers)
            if r.status_code >= 400:
                logger.error('%s %s\n', r.status_code, uri)
            markup = r.text
            self.elapsed = r.elapsed.total_seconds()
        except RequestException as e:
            logger.error('%s %s\n', type(e), uri)

        super().__init__(markup, 'lxml')