diff --git a/backend_integration.py b/backend_integration.py index 9d2e4f6..abdafcf 100644 --- a/backend_integration.py +++ b/backend_integration.py @@ -1,6 +1,7 @@ import aiohttp, os, sys from colors import * import traceback +from steam import SteamID class BackendClient: up = False @@ -34,23 +35,28 @@ class BackendClient: self.warn = False return self.up - async def vip(self, steamid, amount: int, extra: str, unique: str = ""): + async def vip(self, steamid: SteamID, amount: int, extra: str, unique: str = ""): async with aiohttp.ClientSession(cookies={ "secretkey":self.secret_key}) as session: - async with session.post(self.vip_url + f"?steam={steamid}&amount={int(amount)}&service=qiwi&extra={extra}&unique={unique}", ssl=False) as response: + async with session.post(self.vip_url + f"?steam={steamid.id64}&amount={int(amount)}&service=steam&extra={extra}&unique={unique}", ssl=False) as response: try: result = int(await response.text()) if result == 0: - warning(f"[S64:{steamid}] VIP as not be added, maybe permition already exists") + warning(f"[S64:{steamid.id64}] VIP as not be added, maybe permition already exists") return 99 elif result > 0: - info(f"[S64:{steamid}] VIP has be added!") + info(f"[S64:{steamid.id64}] VIP has be added!") return 100 elif result < 0: - info(f"[S64:{steamid}] VIP has be extends!") + info(f"[S64:{steamid.id64}] VIP has be extends!") return 101 except: - error(f"[S64:{steamid}] Backend returned error") + error(f"[S64:{steamid.id64}] Backend returned error") traceback.print_exc() return False - return False \ No newline at end of file + return False + + async def prices(self): + async with aiohttp.ClientSession() as session: + async with session.get(self.vip_url) as response: + return await response.json() \ No newline at end of file diff --git a/paybot.facti13.py b/paybot.facti13.py index 24da7de..c03de19 100644 --- a/paybot.facti13.py +++ b/paybot.facti13.py @@ -11,24 +11,19 @@ from backend_integration import BackendClient from colors import * import os, sys, asyncio -prices = { - 20.0: 1*24*60*60, - 75.0: 7*24*60*60, - 150.0: 30*24*60*60 -} - -def price_checker(amount): - seconds2give = 0 - for rub, sec in prices.items(): - if amount >= rub: - seconds2give = sec - return seconds2give - class Client: wallet: QiwiWallet qiwi_dp: QiwiDispatcher backend: BackendClient + prices = {} + + convert_map = { + "month":30*24*60*60, + "week":7*24*60*60, + "day":1*24*60*60 + } + def __init__(self): if not os.getenv("BACKEND_URL", "") or not os.getenv("SECRET_KEY", ""): error("BACKEND_URL or SECRET_KEY is not setted!") @@ -47,6 +42,7 @@ class Client: async def on_startup(self, ctx: Context): print(await self.wallet.get_balance()) await self.backend.pulse() + await self.update_prices_from_server() #Добавить фоновую проверку живности бека def build_handler(self): @@ -62,8 +58,11 @@ class Client: warning(f"[{t.id}] currency not be rub, drop") return ######################################## + if not self.prices: + await self.update_prices_from_server() + ######################################## money = t.sum.amount - seconds = price_checker(money) + seconds = self.price_checker(money) if seconds == 0: warning(f"[{t.id}] so smol donate {money} RUB") return @@ -82,6 +81,28 @@ class Client: info(f"Add vip {steam2}, amount {seconds}") await self.backend.vip(steam2, seconds, f"rub={int(money)};", unique=f"qiwi_{t.id}") + async def update_prices_from_server(self): + info("Fetch prices from server backend") + prices = await self.backend.prices() + self.update_prices(prices) + + def update_prices(self, prices): + for price in prices: + if price["money_price"] == 0: + continue + self.prices.update({float(price["money_price"]):self.convert_map[price['period']]}) + if not self.prices: + error("cannot get prices from server") + sys.exit(228) + info(f"prices: {self.prices}") + + def price_checker(self, amount): + seconds2give = 0 + for rub, sec in self.prices.items(): + if amount >= rub: + seconds2give = sec + return seconds2give + if __name__ == "__main__": print("Build date: "+os.getenv("BUILDDATE", "not setted"))