mirror of
https://github.com/Redume/Shirino.git
synced 2024-12-04 22:16:21 +03:00
Compare commits
5 commits
4b47f61840
...
6b943ad40a
Author | SHA1 | Date | |
---|---|---|---|
6b943ad40a | |||
88b7fc6063 | |||
bc9098cd14 | |||
f13ff07de7 | |||
8c3f7d36d8 |
1 changed files with 97 additions and 74 deletions
171
main.py
171
main.py
|
@ -1,24 +1,32 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import aiohttp.client_exceptions
|
|
||||||
from pydantic.v1 import BaseSettings
|
|
||||||
from typing import List, Any, Dict, Optional
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from aiogram import Dispatcher, types, Bot
|
|
||||||
|
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import string
|
import string
|
||||||
|
from typing import List, Any, Dict, Optional
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from aiogram import Dispatcher, types, Bot
|
||||||
|
import aiohttp.client_exceptions
|
||||||
|
from pydantic.v1 import BaseSettings
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
DDG_URL = 'https://duckduckgo.com/js/spice/currency'
|
||||||
|
COINAPI_URL = 'https://rest.coinapi.io/v1/exchangerate'
|
||||||
|
|
||||||
|
|
||||||
|
# ---
|
||||||
|
|
||||||
|
|
||||||
|
# Config from .env
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
debug: bool
|
debug: bool
|
||||||
|
timeout: int = 2
|
||||||
|
ndigits: int = 3
|
||||||
coinapi_keys: List[str]
|
coinapi_keys: List[str]
|
||||||
telegram_token: str
|
telegram_token: str
|
||||||
|
|
||||||
|
@ -27,8 +35,11 @@ class Settings(BaseSettings):
|
||||||
env_file_encoding = 'utf-8'
|
env_file_encoding = 'utf-8'
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings() # type: ignore
|
||||||
|
# ---
|
||||||
|
|
||||||
|
|
||||||
|
# Logging
|
||||||
log = logging.getLogger('shirino')
|
log = logging.getLogger('shirino')
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
fmt = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
fmt = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
||||||
|
@ -39,32 +50,34 @@ log.addHandler(handler)
|
||||||
if settings.debug:
|
if settings.debug:
|
||||||
handler.setLevel(logging.DEBUG)
|
handler.setLevel(logging.DEBUG)
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.DEBUG)
|
||||||
|
# ---
|
||||||
|
|
||||||
|
|
||||||
coinapi_len = len(settings.coinapi_keys)
|
coinapi_len = len(settings.coinapi_keys)
|
||||||
coinapi_active = [0]
|
coinapi_active = [0] # API key index
|
||||||
|
|
||||||
dp = Dispatcher()
|
dp = Dispatcher()
|
||||||
|
|
||||||
DDG_URL = 'https://duckduckgo.com/js/spice/currency'
|
|
||||||
COINAPI_URL = 'https://rest.coinapi.io/v1/exchangerate'
|
|
||||||
|
|
||||||
|
class CurrencyConverter:
|
||||||
|
|
||||||
class Currency:
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
|
||||||
self.amount: float = 1.0
|
self.amount: float = 1.0
|
||||||
self.conv_amount: float = 1.0
|
self.conv_amount: float = 0.0
|
||||||
self.from_currency = ''
|
self.from_currency = ''
|
||||||
self.conv_currency = ''
|
self.conv_currency = ''
|
||||||
|
|
||||||
def convert(self) -> None:
|
def convert(self) -> None:
|
||||||
if not self.ddg():
|
"""Currency conversion"""
|
||||||
|
|
||||||
|
if not self.ddgapi():
|
||||||
self.coinapi()
|
self.coinapi()
|
||||||
|
|
||||||
str_amount = f'{self.conv_amount}'
|
str_amount = f'{self.conv_amount}'
|
||||||
point = str_amount.find('.')
|
point = str_amount.find(".")
|
||||||
after_point = str_amount[point + 1:]
|
after_point = str_amount[point + 1:]
|
||||||
|
|
||||||
fnz = min(
|
fnz = min( # index of first non-zero digit
|
||||||
(
|
(
|
||||||
after_point.index(i)
|
after_point.index(i)
|
||||||
for i in string.digits[1:]
|
for i in string.digits[1:]
|
||||||
|
@ -79,16 +92,21 @@ class Currency:
|
||||||
|
|
||||||
# how many digits should be after the point:
|
# how many digits should be after the point:
|
||||||
# ndigits (3 by default) after first non-zero
|
# ndigits (3 by default) after first non-zero
|
||||||
ndigits = fnz + 3
|
ndigits = fnz + settings.ndigits
|
||||||
|
|
||||||
self.conv_amount = round(self.conv_amount, ndigits)
|
self.conv_amount = round(self.conv_amount, ndigits)
|
||||||
|
|
||||||
def ddg(self) -> float:
|
def ddgapi(self) -> bool:
|
||||||
"""Получение данных фиатной валюты через DuckDuckGo
|
"""Получение данных фиатной валюты через DuckDuckGo
|
||||||
|
|
||||||
e.g: https://duckduckgo.com/js/spice/currency/1/USD/RUB
|
e.g: https://duckduckgo.com/js/spice/currency/1/USD/RUB
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
`False` если валюты нет в API
|
||||||
|
`True` если конвертация прошла успешно
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Запрос к API
|
||||||
res = requests.get(f'{DDG_URL}/{self.amount}/{self.from_currency}/{self.conv_currency}')
|
res = requests.get(f'{DDG_URL}/{self.amount}/{self.from_currency}/{self.conv_currency}')
|
||||||
data: Dict[str, Any] = json.loads(re.findall(r'(.+)\);', res.text)[0])
|
data: Dict[str, Any] = json.loads(re.findall(r'(.+)\);', res.text)[0])
|
||||||
|
|
||||||
|
@ -101,7 +119,6 @@ class Currency:
|
||||||
conv_amount = conv.get("mid")
|
conv_amount = conv.get("mid")
|
||||||
|
|
||||||
if conv_amount is None:
|
if conv_amount is None:
|
||||||
print("FUCK")
|
|
||||||
raise RuntimeError('Ошибка при конвертации валюты через DuckDuckGo')
|
raise RuntimeError('Ошибка при конвертации валюты через DuckDuckGo')
|
||||||
|
|
||||||
log.debug(conv)
|
log.debug(conv)
|
||||||
|
@ -115,7 +132,7 @@ class Currency:
|
||||||
"""Получение данных с CoinAPI для получения курса криптовалюты
|
"""Получение данных с CoinAPI для получения курса криптовалюты
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
depth (int, optional): Счетчик, защищающий от бесконечной рекурсии
|
depth (int, optional): Счетчик, защищающий от рекурсии
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if depth <= 0:
|
if depth <= 0:
|
||||||
|
@ -129,7 +146,7 @@ class Currency:
|
||||||
headers={
|
headers={
|
||||||
'X-CoinAPI-Key': settings.coinapi_keys[coinapi_active[0]],
|
'X-CoinAPI-Key': settings.coinapi_keys[coinapi_active[0]],
|
||||||
},
|
},
|
||||||
timeout=3,
|
timeout=settings.timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
if resp.status_code == 429:
|
if resp.status_code == 429:
|
||||||
|
@ -155,57 +172,15 @@ def rotate_token(lst: List[str], active: List[int]) -> None:
|
||||||
active[0] = (active[0] + 1) % len(lst)
|
active[0] = (active[0] + 1) % len(lst)
|
||||||
|
|
||||||
|
|
||||||
@dp.inline_query()
|
async def inline_reply(result_id: str, title: str, description: str or None, inline_query: types.InlineQuery) -> None:
|
||||||
async def currency(inline_query: types.InlineQuery) -> None:
|
|
||||||
query = inline_query.query
|
|
||||||
article: List[Optional[types.InlineQueryResultArticle]] = [None]
|
article: List[Optional[types.InlineQueryResultArticle]] = [None]
|
||||||
|
|
||||||
args = query.split()
|
|
||||||
|
|
||||||
result_id = hashlib.md5(query.encode()).hexdigest()
|
|
||||||
conv = Currency()
|
|
||||||
|
|
||||||
try:
|
|
||||||
if len(args) == 0:
|
|
||||||
article[0] = types.InlineQueryResultArticle(
|
|
||||||
id=result_id,
|
|
||||||
title="Требуется 2, либо 3 аргумента",
|
|
||||||
description=f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB",
|
|
||||||
input_message_content=types.InputTextMessageContent(
|
|
||||||
message_text="Требуется 2, либо 3 аргумента"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
elif args[0].isdigit() or re.match(r'^-?\d+(?:\.\d+)$', args[0]) is not None:
|
|
||||||
conv.from_currency = args[1].upper()
|
|
||||||
conv.conv_currency = args[2].upper()
|
|
||||||
conv.convert()
|
|
||||||
elif type(args[0]) is str:
|
|
||||||
conv.from_currency = args[0].upper()
|
|
||||||
conv.conv_currency = args[1].upper()
|
|
||||||
conv.convert()
|
|
||||||
|
|
||||||
result_title = f'{conv.amount} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
|
||||||
result_desc = None
|
|
||||||
|
|
||||||
|
|
||||||
except aiohttp.client_exceptions.ClientError:
|
|
||||||
result_title = 'Произошла ошибка'
|
|
||||||
result_desc = 'Рейт-лимит от api telegram, попробуйте позже'
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
|
|
||||||
except Exception as ex:
|
|
||||||
log.debug(ex)
|
|
||||||
result_title = 'Произошла ошибка'
|
|
||||||
result_desc = f'{type(ex).__name__}: {ex}'
|
|
||||||
|
|
||||||
article[0] = types.InlineQueryResultArticle(
|
article[0] = types.InlineQueryResultArticle(
|
||||||
id=result_id,
|
id=result_id,
|
||||||
title=result_title,
|
title=title,
|
||||||
description=result_desc,
|
description=description,
|
||||||
input_message_content=types.InputTextMessageContent(
|
input_message_content=types.InputTextMessageContent(
|
||||||
message_text=f"{result_title} \n{result_desc}",
|
message_text=title
|
||||||
),
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
await inline_query.answer(
|
await inline_query.answer(
|
||||||
|
@ -215,6 +190,54 @@ async def currency(inline_query: types.InlineQuery) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.inline_query()
|
||||||
|
async def currency(inline_query: types.InlineQuery) -> None:
|
||||||
|
query = inline_query.query
|
||||||
|
args = query.split()
|
||||||
|
|
||||||
|
result_id = hashlib.md5(query.encode()).hexdigest()
|
||||||
|
conv = CurrencyConverter()
|
||||||
|
|
||||||
|
try:
|
||||||
|
log.debug(len(args))
|
||||||
|
if len(args) <= 1:
|
||||||
|
await inline_reply(result_id,
|
||||||
|
"Требуется 2, либо 3 аргумента",
|
||||||
|
f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", inline_query)
|
||||||
|
|
||||||
|
if len(args) == 3:
|
||||||
|
conv.amount = float(args[0])
|
||||||
|
conv.from_currency = args[1].upper()
|
||||||
|
conv.conv_currency = args[2].upper()
|
||||||
|
conv.convert()
|
||||||
|
elif len(args) == 2:
|
||||||
|
conv.from_currency = args[0].upper()
|
||||||
|
conv.conv_currency = args[1].upper()
|
||||||
|
conv.convert()
|
||||||
|
|
||||||
|
result = (
|
||||||
|
f'{conv.amount} {conv.from_currency} = '
|
||||||
|
f'{conv.conv_amount} {conv.conv_currency}'
|
||||||
|
)
|
||||||
|
|
||||||
|
except aiohttp.client_exceptions.ClientError as ex:
|
||||||
|
await inline_reply(result_id,
|
||||||
|
"Rate-limit от API Telegram, повторите запрос позже",
|
||||||
|
None,
|
||||||
|
inline_query)
|
||||||
|
|
||||||
|
log.debug(ex)
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
log.debug(ex)
|
||||||
|
await inline_reply(result_id, "Неверный формат данных",
|
||||||
|
"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB",
|
||||||
|
inline_query)
|
||||||
|
|
||||||
|
await inline_reply(result_id, result, None, inline_query)
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
bot = Bot(settings.telegram_token)
|
bot = Bot(settings.telegram_token)
|
||||||
await dp.start_polling(bot)
|
await dp.start_polling(bot)
|
||||||
|
|
Loading…
Add table
Reference in a new issue