mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-22 00:06:22 +03:00
Пытаюсь сделать вывод криптовалюты
This commit is contained in:
parent
9e7a85d6ab
commit
084826f8de
1 changed files with 54 additions and 22 deletions
76
main.py
76
main.py
|
@ -7,29 +7,50 @@ from aiogram.dispatcher import Dispatcher
|
||||||
from aiogram.utils import executor
|
from aiogram.utils import executor
|
||||||
|
|
||||||
|
|
||||||
bot = Bot(token="")
|
bot = Bot(token="5193950006:AAGU8elNfNB9FocVSIb4DnqoEvQk70Mqh5E")
|
||||||
dp = Dispatcher(bot)
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
def get_currency(amount, from_currency, to_currency):
|
class TypeDict:
|
||||||
if amount is None:
|
def __init__(self):
|
||||||
amount = "1"
|
self.amount = None
|
||||||
|
self.from_amount = None
|
||||||
|
self.from_currency = None
|
||||||
|
self.to_currency = None
|
||||||
|
|
||||||
try:
|
@staticmethod
|
||||||
page = requests.get(f"https://duckduckgo.com/js/spice/currency/{amount}/{from_currency}/{to_currency}")
|
def get_currency(self, amount, from_currency, to_currency):
|
||||||
page = page.text.replace('ddg_spice_currency(', "").replace(');', "")
|
if amount is None:
|
||||||
page = json.loads(page)
|
amount = "1"
|
||||||
if page["headers"]["description"].find("ERROR") != -1:
|
|
||||||
return
|
|
||||||
|
|
||||||
except KeyError:
|
try:
|
||||||
return print("Чет сломалось")
|
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):
|
type_dict = TypeDict()
|
||||||
return value.isdecimal() or value.replace('.', '', 1).isdecimal()
|
|
||||||
|
|
||||||
|
|
||||||
@dp.inline_handler()
|
@dp.inline_handler()
|
||||||
|
@ -41,22 +62,34 @@ async def currency(query: types.InlineQuery):
|
||||||
return
|
return
|
||||||
|
|
||||||
for i in range(len(text)):
|
for i in range(len(text)):
|
||||||
if isNum(text[0]):
|
if type_dict.is_num(text[i]):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if text[i].find(",") != -1:
|
if text[i].find(",") != -1:
|
||||||
text[i] = text[i].replace(",", ".")
|
text[i] = text[i].replace(",", ".")
|
||||||
|
|
||||||
result: str
|
|
||||||
try:
|
try:
|
||||||
if isNum(text[0]):
|
if type_dict.is_num(text[0]):
|
||||||
res = get_currency(text[0], text[1], text[2])
|
res, crypto_rate = type_dict.get_currency(text[0], text[1], text[2])
|
||||||
else:
|
else:
|
||||||
res = get_currency(None, text[0], text[1])
|
res, crypto_rate = type_dict.get_currency(None, text[0], text[1])
|
||||||
except Exception:
|
except Exception:
|
||||||
return
|
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(
|
article = [types.InlineQueryResultArticle(
|
||||||
id=result_id,
|
id=result_id,
|
||||||
|
@ -67,5 +100,4 @@ async def currency(query: types.InlineQuery):
|
||||||
|
|
||||||
await query.answer(article, cache_time=1, is_personal=True)
|
await query.answer(article, cache_time=1, is_personal=True)
|
||||||
|
|
||||||
|
|
||||||
executor.start_polling(dp, skip_updates=True)
|
executor.start_polling(dp, skip_updates=True)
|
||||||
|
|
Loading…
Add table
Reference in a new issue