from http import client from turtle import back from donationalerts_api.asyncio_api import Alert, Event from backend_integration import BackendClient from colors import * import os, sys, asyncio import ws class CustomAlerts(Alert): backend: BackendClient prices = {} original_prices = {} convert_map = { "month":30*24*60*60, "week":7*24*60*60, "day":1*24*60*60 } def __init__(self, token, backend): self.backend = backend super().__init__(token) def run(self): info("Start pooling") @self.event() async def handler(event): print(event) await self.calculate(event) async def calculate(self, event: Event): if event.currency != "RUB": warning(f"[{event.id}] donate is not RUB") return None info("Update prices") await self.update_prices_from_server() money = event.amount uid = event.id seconds = self.price_checker(money, event.additional_data.get('is_commission_covered', 0)) if seconds == 0: warning(f"[{uid}] so smol donate {money} RUB") try: if event._is_test_alert: event.message = "https://steamcommunity.com/id/gabe/" steam2 = (await self.backend.getSteam(event.message))['steam2'] except Exception as e: error(f"Cannot get steam64 to steam2: {e}") return info(f"Add vip {steam2}, amount {money}, seconds: {seconds}") if event._is_test_alert: warning(f"Is test alert! rub={int(money * 0.9)}; da_{uid}") return await self.backend.vip(steam2, seconds, f"rub={int(money * 0.9)};", unique=f"da_{uid}") async def update_prices_from_server(self, exit_if_error = True): #info("Fetch prices from server backend") prices = await self.backend.prices(exit_if_error) self.update_prices(prices) def update_prices(self, prices): self.prices = {} self.original_prices = {} for price in prices: if price["money_price"] == 0: continue self.prices.update({float(price["money_price"]) + float(price["money_price"] * price["da_percent"] / 100):self.convert_map[price['period']]}) self.original_prices.update({float(price["money_price"]):self.convert_map[price['period']]}) if not self.prices: error("cannot get prices from server") sys.exit(228) def price_checker(self, amount, original = False): seconds2give = 0 if original: for rub, sec in self.original_prices.items(): if amount >= rub and seconds2give <= sec: seconds2give = sec else: for rub, sec in self.prices.items(): if amount >= rub and seconds2give <= sec: seconds2give = sec return seconds2give if __name__ == "__main__": if not os.getenv("BACKEND_URL", "") or not os.getenv("SECRET_KEY", ""): error("BACKEND_URL or SECRET_KEY is not setted!") sys.exit(1) if not os.getenv("DA_TOKEN", ""): error("DA_TOKEN in not setted!") sys.exit(2) print("Build date: " + os.getenv("BUILDDATE", "not setted")) def run(): client = CustomAlerts(os.getenv("DA_TOKEN"), BackendClient()) client.run() ws.SERVICE_NAME = "dapay" ws.START_AFTER_CONNECT = run wsc = ws.WS(os.getenv("BACKEND_URL"), os.getenv("SECRET_KEY")) wsc.run()