Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1.     # Метод класса HTTPCLIENT
  2.     async def get_snapshot(self, currency_symbol, semaphore):
  3.         http_route = "https://www.binance.com/api/v1/depth?symbol={currency_symbol}&limit=20"
  4.         while True:
  5.             try:
  6.                 async with ClientSession() as session:
  7.                     async with semaphore, session.get(http_route.format(currency_symbol=currency_symbol.upper()),
  8.                                                       proxy=proxy.get_proxy()) as response:
  9.                         response = await response.json()
  10.                         rest_log.info('Depth snapshot for {} got. Response {}'.format(currency_symbol.upper(), response))
  11.                         return response
  12.             except:
  13.                 type, value, traceback_info = sys.exc_info()
  14.                 rest_log.error('Error {type} {value} in get depth snapshot for {cur}'.format(
  15.                     type=type, value=value, cur=currency_symbol.upper()))
  16.                 continue
  17.                
  18.                
  19.    
  20.    
  21.         # Метод класса DepthSnapshot
  22.     async def get(self, semaphore):
  23.         """Try to make a http request for MAX_STEP attempts to depth snapshot
  24.  
  25.        :return dictionary {'currency': response}
  26.        """
  27.         try:
  28.             currency_id = currency_iter.currency_id(self.currency_symbol)
  29.             response = await http_client.get_snapshot(self.currency_symbol, semaphore)
  30.             if 'code' in response:
  31.                 data = {currency_id: {'lastUpdateId': 0, 'bids': {}, 'asks': {}}}
  32.                 depth.init_with_snapshot(data)
  33.                 worker_log.info('Empty response from snapshot for currency {}'.
  34.                                 format(currency_iter.currency_symbol(currency_id)))
  35.                 return {currency_id: []}
  36.             else:
  37.                 data = {currency_id: response}
  38.                 depth.init_with_snapshot(data)
  39.                 return {currency_id: response}
  40.         except:
  41.             type, value, traceback_info = sys.exc_info()
  42.             worker_log.error('Error {type} {value} in depth snapshot for {cur}'.format(
  43.                 type=type, value=value, cur=self.currency_symbol.upper()))
  44.                
  45.                
  46.     # Cам воркер
  47.     async def depth_worker(self):
  48.         depth_websocket_route = "wss://stream.binance.com:9443/stream?streams="
  49.         try:
  50.             for currency in currency_iter.cur_list:
  51.                 depth_websocket_route += '{currency}@depth/'.format(currency=currency)
  52.             async with websockets.connect(depth_websocket_route.lower()) as websocket:
  53.                 depth_semaphore = asyncio.Semaphore(10)
  54.                 #try:
  55.                 depth.set_init_values()
  56.                 worker_log.info('Start getting depth snapshot')
  57.                 await asyncio.gather(
  58.                     *[DepthSnapshot(currency).get(depth_semaphore) for currency in currency_iter.cur_list])
  59.                 worker_log.info('Depth snapshot got')
  60.                 # except:
  61.                 #     traceback.print_exc()
  62.                 while True:
  63.                     payload = await websocket.recv()
  64.                     parsed_payload = json.loads(payload)
  65.                     try:
  66.                         depth.add(parsed_payload)
  67.                     except:
  68.                         traceback.print_exc()
  69.         except Exception as e:
  70.             type, value, traceback_info = sys.exc_info()
  71.             worker_log.error('Error {type} {value} in depth worker'.format(
  72.                 type=type, value=value))
  73.             raise DepthWorkerException(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement