mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-22 00:06:22 +03:00
Миграция aiogram с v2 на v3, исправление парсинга API от DuckDuckGo
This commit is contained in:
parent
b98c16f642
commit
212d30d867
1 changed files with 22 additions and 20 deletions
42
main.py
42
main.py
|
@ -9,15 +9,11 @@ from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from pydantic import BaseSettings
|
from pydantic.v1 import BaseSettings
|
||||||
|
|
||||||
from aiogram import Bot # type: ignore
|
from aiogram import Bot, Dispatcher, types # type: ignore
|
||||||
from aiogram.dispatcher import Dispatcher # type: ignore
|
|
||||||
from aiogram.utils import executor # type: ignore
|
|
||||||
|
|
||||||
from aiogram.types import InlineQuery # type: ignore
|
import asyncio
|
||||||
from aiogram.types import InlineQueryResultArticle
|
|
||||||
from aiogram.types import InputTextMessageContent
|
|
||||||
|
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
|
@ -59,8 +55,7 @@ if settings.debug:
|
||||||
|
|
||||||
coinapi_len = len(settings.coinapi_keys)
|
coinapi_len = len(settings.coinapi_keys)
|
||||||
coinapi_active = [0] # API key index
|
coinapi_active = [0] # API key index
|
||||||
bot = Bot(token=settings.telegram_token)
|
dp = Dispatcher()
|
||||||
dp = Dispatcher(bot)
|
|
||||||
|
|
||||||
|
|
||||||
class CurrencyConverter:
|
class CurrencyConverter:
|
||||||
|
@ -79,7 +74,7 @@ class CurrencyConverter:
|
||||||
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( # index of first non-zero digit
|
fnz = min( # index of first non-zero digit
|
||||||
|
@ -130,15 +125,16 @@ class CurrencyConverter:
|
||||||
log.debug(data)
|
log.debug(data)
|
||||||
|
|
||||||
# If the currency does not exist
|
# If the currency does not exist
|
||||||
descr = data.get('headers', {}).get('description', '')
|
error = data.get('to')[0].get('quotecurrency')
|
||||||
if descr.find('ERROR') != -1:
|
if error is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Otherwise
|
# Otherwise
|
||||||
conv: Dict[str, str] = data.get('conversion', {})
|
conv: Dict[str, str] = data.get('to')[0]
|
||||||
conv_amount = conv.get('converted-amount')
|
conv_amount = conv.get('mid')
|
||||||
if conv_amount is None:
|
if conv_amount is None:
|
||||||
raise RuntimeError('Ошибка при конвертации через DDG')
|
raise RuntimeError('Ошибка при конвертации через DDG')
|
||||||
|
|
||||||
self.conv_amount = float(conv_amount)
|
self.conv_amount = float(conv_amount)
|
||||||
|
|
||||||
log.debug(conv)
|
log.debug(conv)
|
||||||
|
@ -189,11 +185,11 @@ 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_handler()
|
@dp.inline_query()
|
||||||
async def currency(inline_query: InlineQuery) -> None:
|
async def currency(inline_query: types.InlineQuery) -> None:
|
||||||
|
|
||||||
query = inline_query.query
|
query = inline_query.query
|
||||||
article: List[Optional[InlineQueryResultArticle]] = [None]
|
article: List[Optional[types.InlineQueryResultArticle]] = [None]
|
||||||
|
|
||||||
text = query.split()
|
text = query.split()
|
||||||
len_ = len(text)
|
len_ = len(text)
|
||||||
|
@ -222,10 +218,10 @@ async def currency(inline_query: InlineQuery) -> None:
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
result = f'{type(ex).__name__}: {ex}'
|
result = f'{type(ex).__name__}: {ex}'
|
||||||
|
|
||||||
article[0] = InlineQueryResultArticle(
|
article[0] = types.InlineQueryResultArticle(
|
||||||
id=result_id,
|
id=result_id,
|
||||||
title=result,
|
title=result,
|
||||||
input_message_content=InputTextMessageContent(
|
input_message_content=types.InputTextMessageContent(
|
||||||
message_text=result,
|
message_text=result,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -237,4 +233,10 @@ async def currency(inline_query: InlineQuery) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
executor.start_polling(dp, skip_updates=True)
|
async def main() -> None:
|
||||||
|
bot = Bot(settings.telegram_token)
|
||||||
|
await dp.start_polling(bot)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
asyncio.run(main())
|
||||||
|
|
Loading…
Add table
Reference in a new issue