2023-05-30 13:47:06 +03:00
|
|
|
#!/usr/bin/env python3
|
2023-12-31 12:43:45 +03:00
|
|
|
import asyncio
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import string
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
import aiohttp
|
2023-11-22 22:18:23 +03:00
|
|
|
import requests
|
2024-06-14 14:02:10 +03:00
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import yaml
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
from aiogram import Dispatcher, types, Bot
|
2024-06-14 14:35:06 +03:00
|
|
|
from aiogram.filters import CommandStart
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
# Constants
|
|
|
|
DDG_URL = 'https://duckduckgo.com/js/spice/currency'
|
|
|
|
COINAPI_URL = 'https://rest.coinapi.io/v1/exchangerate'
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
config = yaml.safe_load(open("config.yaml"))
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-05-30 13:33:41 +03:00
|
|
|
log = logging.getLogger('shirino')
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
fmt = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-05-30 13:33:41 +03:00
|
|
|
handler.setFormatter(fmt)
|
|
|
|
log.addHandler(handler)
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
if config['debug']:
|
2023-05-30 13:33:41 +03:00
|
|
|
handler.setLevel(logging.DEBUG)
|
|
|
|
log.setLevel(logging.DEBUG)
|
2023-05-30 13:47:06 +03:00
|
|
|
|
2023-11-22 22:18:23 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
coinapi_len = len(config['coinapi_keys'])
|
|
|
|
coinapi_active = [0]
|
2023-09-29 21:12:44 +03:00
|
|
|
dp = Dispatcher()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
class CurrencyConverter:
|
2024-06-14 14:02:10 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.amount = 1.0
|
|
|
|
self.conv_amount = 0.0
|
|
|
|
self.from_currency = 'RUB'
|
|
|
|
self.conv_currency = 'USD'
|
2023-05-30 13:47:06 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
def convert(self):
|
2023-12-31 12:43:45 +03:00
|
|
|
if not self.ddgapi():
|
2023-05-30 13:33:41 +03:00
|
|
|
self.coinapi()
|
2023-05-30 13:47:06 +03:00
|
|
|
|
2023-06-01 15:58:33 +03:00
|
|
|
str_amount = f'{self.conv_amount}'
|
2023-12-31 12:43:45 +03:00
|
|
|
point = str_amount.find(".")
|
2023-06-01 15:58:33 +03:00
|
|
|
after_point = str_amount[point + 1:]
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
fnz = min(
|
2023-06-01 15:58:33 +03:00
|
|
|
(
|
|
|
|
after_point.index(i)
|
|
|
|
for i in string.digits[1:]
|
|
|
|
if i in after_point
|
|
|
|
),
|
|
|
|
default=-1,
|
|
|
|
)
|
2023-06-01 15:16:37 +03:00
|
|
|
|
|
|
|
if fnz == -1:
|
2023-06-01 15:58:33 +03:00
|
|
|
return
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
ndigits = fnz + config['ndigits']
|
2023-06-01 15:16:37 +03:00
|
|
|
|
2023-06-01 16:07:20 +03:00
|
|
|
self.conv_amount = round(self.conv_amount, ndigits)
|
2023-06-01 15:16:37 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
def ddgapi(self):
|
2023-11-22 22:18:23 +03:00
|
|
|
res = requests.get(f'{DDG_URL}/{self.amount}/{self.from_currency}/{self.conv_currency}')
|
2024-06-14 14:02:10 +03:00
|
|
|
data = json.loads(re.findall(r'\(\s*(.*)\s*\);$', res.text)[0])
|
|
|
|
|
|
|
|
del data['terms']
|
|
|
|
del data['privacy']
|
|
|
|
del data['timestamp']
|
2023-05-30 13:33:41 +03:00
|
|
|
|
|
|
|
log.debug(data)
|
|
|
|
|
2023-11-22 22:18:23 +03:00
|
|
|
if len(data.get('to')) == 0:
|
2023-05-30 13:33:41 +03:00
|
|
|
return False
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
conv = data.get('to')[0]
|
2023-11-22 22:18:23 +03:00
|
|
|
conv_amount = conv.get("mid")
|
2023-09-29 21:12:44 +03:00
|
|
|
|
2023-11-22 22:18:23 +03:00
|
|
|
if conv_amount is None:
|
|
|
|
raise RuntimeError('Ошибка при конвертации валюты через DuckDuckGo')
|
2023-05-30 13:33:41 +03:00
|
|
|
|
|
|
|
log.debug(conv)
|
2023-11-22 22:18:23 +03:00
|
|
|
log.debug(conv_amount)
|
|
|
|
|
|
|
|
self.conv_amount = float(conv_amount)
|
2023-05-30 13:33:41 +03:00
|
|
|
|
|
|
|
return True
|
2023-05-30 13:47:06 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
def coinapi(self, depth: int = config['coinapi_keys']):
|
2023-06-01 16:39:46 +03:00
|
|
|
if depth <= 0:
|
|
|
|
raise RecursionError('Рейтлимит на всех токенах')
|
2023-05-30 13:33:41 +03:00
|
|
|
|
|
|
|
resp = requests.get(
|
|
|
|
(
|
|
|
|
f'{COINAPI_URL}/{self.from_currency}'
|
|
|
|
f'/{self.conv_currency}'
|
|
|
|
),
|
|
|
|
headers={
|
2024-06-14 14:02:10 +03:00
|
|
|
'X-CoinAPI-Key': config['coinapi_keys'][coinapi_active[0]],
|
2023-05-30 13:33:41 +03:00
|
|
|
},
|
2024-06-14 14:02:10 +03:00
|
|
|
timeout=config['timeout'],
|
2023-05-30 13:33:41 +03:00
|
|
|
)
|
|
|
|
|
2023-06-01 16:39:46 +03:00
|
|
|
if resp.status_code == 429:
|
|
|
|
log.warning('CoinAPI ratelimited, rotating token')
|
2024-06-14 14:02:10 +03:00
|
|
|
rotate_token(config['coinapi_keys'], coinapi_active)
|
2023-06-01 16:39:46 +03:00
|
|
|
self.coinapi(depth - 1)
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
data = resp.json()
|
2023-06-01 15:58:33 +03:00
|
|
|
rate = data.get('rate')
|
|
|
|
if rate is None:
|
|
|
|
raise RuntimeError('Не удалось получить курс валюты от CoinAPI')
|
|
|
|
self.conv_amount = float(rate * self.amount)
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
def rotate_token(lst, active):
|
2023-06-01 16:39:46 +03:00
|
|
|
active[0] = (active[0] + 1) % len(lst)
|
|
|
|
|
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
async def inline_reply(result_id: str, title: str, description: str or None, inline_query: types.InlineQuery):
|
|
|
|
article = [None]
|
2023-12-31 14:12:13 +03:00
|
|
|
article[0] = types.InlineQueryResultArticle(
|
|
|
|
id=result_id,
|
|
|
|
title=title,
|
|
|
|
description=description,
|
|
|
|
input_message_content=types.InputTextMessageContent(
|
|
|
|
message_text=title
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await inline_query.answer(
|
|
|
|
article,
|
|
|
|
cache_time=1,
|
|
|
|
is_personal=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
@dp.inline_query()
|
2024-06-14 14:02:10 +03:00
|
|
|
async def currency(inline_query: types.InlineQuery):
|
2023-05-30 13:33:41 +03:00
|
|
|
query = inline_query.query
|
2023-11-22 22:18:23 +03:00
|
|
|
args = query.split()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-05-30 13:33:41 +03:00
|
|
|
result_id = hashlib.md5(query.encode()).hexdigest()
|
2023-12-31 12:43:45 +03:00
|
|
|
conv = CurrencyConverter()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
try:
|
2023-12-31 13:26:38 +03:00
|
|
|
log.debug(len(args))
|
|
|
|
if len(args) <= 1:
|
2023-12-31 14:12:13 +03:00
|
|
|
await inline_reply(result_id,
|
|
|
|
"Требуется 2, либо 3 аргумента",
|
|
|
|
f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", inline_query)
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
if len(args) == 3:
|
|
|
|
conv.amount = float(args[0])
|
2023-11-22 22:18:23 +03:00
|
|
|
conv.from_currency = args[1].upper()
|
|
|
|
conv.conv_currency = args[2].upper()
|
2023-05-30 13:33:41 +03:00
|
|
|
conv.convert()
|
2023-12-31 12:43:45 +03:00
|
|
|
elif len(args) == 2:
|
2023-11-22 22:18:23 +03:00
|
|
|
conv.from_currency = args[0].upper()
|
|
|
|
conv.conv_currency = args[1].upper()
|
2023-05-30 13:33:41 +03:00
|
|
|
conv.convert()
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
result = (
|
|
|
|
f'{conv.amount} {conv.from_currency} = '
|
|
|
|
f'{conv.conv_amount} {conv.conv_currency}'
|
|
|
|
)
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
except aiohttp.client_exceptions.ClientError as ex:
|
2023-12-31 14:19:28 +03:00
|
|
|
await inline_reply(result_id,
|
2023-12-31 14:12:13 +03:00
|
|
|
"Rate-limit от API Telegram, повторите запрос позже",
|
|
|
|
None,
|
|
|
|
inline_query)
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
log.debug(ex)
|
2023-11-22 22:18:23 +03:00
|
|
|
await asyncio.sleep(1)
|
2023-10-28 22:02:29 +03:00
|
|
|
|
2023-05-30 13:33:41 +03:00
|
|
|
except Exception as ex:
|
2023-11-22 22:18:23 +03:00
|
|
|
log.debug(ex)
|
2023-12-31 14:19:28 +03:00
|
|
|
await inline_reply(result_id, "Неверный формат данных",
|
|
|
|
"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB",
|
|
|
|
inline_query)
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2023-12-31 14:19:28 +03:00
|
|
|
await inline_reply(result_id, result, None, inline_query)
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
|
2024-06-14 14:35:06 +03:00
|
|
|
@dp.message(CommandStart())
|
|
|
|
async def start(message: types.Message):
|
|
|
|
await message.answer("Привет! Бот может показывать курс обмена криптовалюты и фиатной валюты. "
|
|
|
|
"Бот используется с помощью инлайн-команд: "
|
|
|
|
"`@shirino_bot 12 usd rub` или `@shirino_bot usd rub`"
|
|
|
|
"\n\nИсходный код опубликован на [Github](https://github.com/redume/shirino)",
|
|
|
|
parse_mode="markdown")
|
|
|
|
|
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
async def main() -> None:
|
2024-06-14 14:10:46 +03:00
|
|
|
log.debug(config)
|
2024-06-14 14:02:10 +03:00
|
|
|
bot = Bot(config['telegram_token'])
|
2023-09-29 21:12:44 +03:00
|
|
|
await dp.start_polling(bot)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
asyncio.run(main())
|