gsd 2 years ago
parent
commit
77caa31ac2
  1. 15
      backend_integration.py
  2. 22
      tradebot.facti13.external.py

15
backend_integration.py

@ -18,7 +18,7 @@ class BackendClient:
self.pulse_url = f"{os.getenv('BACKEND_URL')}/api/pulse"
self.vip_url = f"{os.getenv('BACKEND_URL')}/api/external/vip"
async def pulse(self):
async def pulse(self, exit_if_error = False):
async with aiohttp.ClientSession(cookies={
"secretkey":self.secret_key}) as session:
async with session.get(self.pulse_url, ssl=False) as response:
@ -31,6 +31,9 @@ class BackendClient:
except:
error("Backend not respond")
traceback.print_exc()
if exit_if_error:
error("exit if backend is down")
sys.exit(200)
self.up = False
self.warn = False
return self.up
@ -56,7 +59,13 @@ class BackendClient:
return False
return False
async def prices(self):
async def prices(self, exit_if_error = False):
async with aiohttp.ClientSession() as session:
async with session.get(self.vip_url) as response:
return await response.json()
try:
return await response.json()
except:
traceback.print_exc()
if exit_if_error:
error("exit if cannot get prices")
sys.exit(200)

22
tradebot.facti13.external.py

@ -75,11 +75,11 @@ class SteamClient(SteamPy.Client):
items = None
trade_tracker = {}
backend : BackendClient = None
backend_checker_enable = False
async def on_ready(self):
info(f"Logged in as: {self.user}")
prices = await self.backend.prices()
self.items = TradeChecker(prices)
asyncio.ensure_future(self.backend_checker)
async def on_connect(self):
await self.backend.pulse()
@ -89,12 +89,24 @@ class SteamClient(SteamPy.Client):
await self.backend.pulse()
ok("Disconnect from steam")
async def update_prices(self, exit_if_error = False):
prices = await self.backend.prices(exit_if_error)
self.items = TradeChecker(prices)
async def backend_checker(self, timeout=60):
if not self.backend_checker_enable:
info(f"Check backend stats every {timeout} seconds")
self.backend_checker_enable = True
while True:
await self.backend.pulse(True)
await self.update_prices(True)
await asyncio.sleep(timeout)
#Проверка шмота на леквид
async def on_trade_receive(self, trade: SteamPy.TradeOffer):
if not self.items:
info("prices is not setup")
prices = await self.backend.prices()
self.items = TradeChecker(prices)
await self.update_prices()
info(f"[{trade.id}] Incoming trade from [{trade.partner.id}] {trade.partner.name}")
@ -181,7 +193,7 @@ class SteamClient(SteamPy.Client):
if __name__ == "__main__":
print(f"Build date: {os.getenv('BUILDDATE', 'not set')}")
backend = BackendClient()
asyncio.get_event_loop().run_until_complete(backend.pulse())
asyncio.get_event_loop().run_until_complete(backend.pulse(True))
if not backend.up:
print("backend not working down bot")
sys.exit(200)

Loading…
Cancel
Save