From 084826f8dea0e9ac3c4b0cc54fd54d74fbe22f7a Mon Sep 17 00:00:00 2001 From: Redume Date: Tue, 30 May 2023 11:51:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=B0=D1=8E=D1=81=D1=8C=20?= =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B2=D1=8B=D0=B2?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BB=D1=8E=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 76 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 14dda63..dddc6bf 100644 --- a/main.py +++ b/main.py @@ -7,29 +7,50 @@ from aiogram.dispatcher import Dispatcher from aiogram.utils import executor -bot = Bot(token="") +bot = Bot(token="5193950006:AAGU8elNfNB9FocVSIb4DnqoEvQk70Mqh5E") dp = Dispatcher(bot) -def get_currency(amount, from_currency, to_currency): - if amount is None: - amount = "1" +class TypeDict: + def __init__(self): + self.amount = None + self.from_amount = None + self.from_currency = None + self.to_currency = None - try: - page = requests.get(f"https://duckduckgo.com/js/spice/currency/{amount}/{from_currency}/{to_currency}") - page = page.text.replace('ddg_spice_currency(', "").replace(');', "") - page = json.loads(page) - if page["headers"]["description"].find("ERROR") != -1: - return + @staticmethod + def get_currency(self, amount, from_currency, to_currency): + if amount is None: + amount = "1" - except KeyError: - return print("Чет сломалось") + try: + page = requests.get(f"https://duckduckgo.com/js/spice/currency/{amount}/{from_currency}/{to_currency}") + page = page.text.replace('ddg_spice_currency(', "").replace(');', "") + page = json.loads(page) - return page["conversion"] + if page["headers"]["description"].find("ERROR") != -1: + print(from_currency, to_currency) + crypto = requests.get(f"https://rest.coinapi.io/v1/exchangerate/{from_currency.upper()}/{to_currency.upper()}", headers={ + "X-CoinAPI-Key": "8A465920-C233-4EE2-860B-A0AF9EC21FFF" + }).json() + + print(crypto) + + self.from_amount = crypto.get("rate") + return crypto.get("rate") + + except KeyError: + print("blyat slomal") + return None + + return page.get("conversion") + + @staticmethod + def is_num(value): + return value.isdecimal() or value.replace('.', '', 1).isdecimal() -def isNum(value): - return value.isdecimal() or value.replace('.', '', 1).isdecimal() +type_dict = TypeDict() @dp.inline_handler() @@ -41,22 +62,34 @@ async def currency(query: types.InlineQuery): return for i in range(len(text)): - if isNum(text[0]): + if type_dict.is_num(text[i]): continue if text[i].find(",") != -1: text[i] = text[i].replace(",", ".") - result: str try: - if isNum(text[0]): - res = get_currency(text[0], text[1], text[2]) + if type_dict.is_num(text[0]): + res, crypto_rate = type_dict.get_currency(text[0], text[1], text[2]) else: - res = get_currency(None, text[0], text[1]) + res, crypto_rate = type_dict.get_currency(None, text[0], text[1]) except Exception: return - result = str(f"{res['from-amount']} {res['from-currency-symbol']} = {res['converted-amount']} {res['to-currency-symbol']}") + if res is None: + return + + print(res) + + from_amount = res.get('from_amount', res['from-amount']) + from_currency_symbol = res.get('from_currency_symbol', res['from-currency-symbol']) + converted_amount = res.get('converted_amount', res['converted-amount']) + to_currency_symbol = res.get('to_currency_symbol', res['to-currency-symbol']) + + result = f"{from_amount} {from_currency_symbol} = {converted_amount} {to_currency_symbol}" + + if crypto_rate: + result += f" | Crypto Rate: {crypto_rate}" article = [types.InlineQueryResultArticle( id=result_id, @@ -67,5 +100,4 @@ async def currency(query: types.InlineQuery): await query.answer(article, cache_time=1, is_personal=True) - executor.start_polling(dp, skip_updates=True)