2023-03-08 13:31:45 +03:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from aiogram import Bot, types
|
|
|
|
from aiogram.dispatcher import Dispatcher
|
|
|
|
from aiogram.utils import executor
|
|
|
|
|
|
|
|
|
|
|
|
bot = Bot(token="")
|
|
|
|
dp = Dispatcher(bot)
|
|
|
|
|
|
|
|
|
|
|
|
def get_currency(amount, from_currency, to_currency):
|
|
|
|
if amount is None:
|
|
|
|
amount = "1"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
return print("Чет сломалось")
|
|
|
|
|
|
|
|
return page["conversion"]
|
|
|
|
|
|
|
|
|
|
|
|
def isNum(value):
|
|
|
|
return value.isdecimal() or value.replace('.', '', 1).isdecimal()
|
|
|
|
|
|
|
|
|
|
|
|
@dp.inline_handler()
|
|
|
|
async def currency(query: types.InlineQuery):
|
|
|
|
text = query.query.split(" ")
|
|
|
|
result_id: str = hashlib.md5(query.query.encode()).hexdigest()
|
|
|
|
|
|
|
|
if text == ['']:
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in range(len(text)):
|
|
|
|
if isNum(text[0]):
|
|
|
|
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])
|
|
|
|
else:
|
|
|
|
res = get_currency(None, text[0], text[1])
|
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
|
2023-03-10 12:49:35 +03:00
|
|
|
result = str(f"{res['from-amount']} {res['from-currency-symbol']} = {res['converted-amount']} {res['to-currency-symbol']}")
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
article = [types.InlineQueryResultArticle(
|
|
|
|
id=result_id,
|
2023-03-10 12:49:35 +03:00
|
|
|
title=result,
|
2023-03-08 13:31:45 +03:00
|
|
|
input_message_content=types.InputTextMessageContent(
|
2023-03-10 12:49:35 +03:00
|
|
|
message_text=result
|
2023-03-08 13:31:45 +03:00
|
|
|
))]
|
|
|
|
|
|
|
|
await query.answer(article, cache_time=1, is_personal=True)
|
|
|
|
|
|
|
|
|
|
|
|
executor.start_polling(dp, skip_updates=True)
|