mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-21 15:56:22 +03:00
сделал w2n
This commit is contained in:
parent
b2aa0589a5
commit
019f844e4f
1 changed files with 44 additions and 24 deletions
68
main.py
68
main.py
|
@ -2,10 +2,14 @@ from aiogram import Bot, Dispatcher, types
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import json
|
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
from word2number import w2n
|
||||||
from function.convert import Converter
|
from function.convert import Converter
|
||||||
|
|
||||||
config = yaml.safe_load(open("config.yaml"))
|
config = yaml.safe_load(open("config.yaml"))
|
||||||
|
@ -14,15 +18,37 @@ dp = Dispatcher()
|
||||||
|
|
||||||
@dp.message()
|
@dp.message()
|
||||||
async def message_conv(message: types.Message):
|
async def message_conv(message: types.Message):
|
||||||
query = message.text.split(' ')
|
with open('currency.json', 'r', encoding='utf-8') as f:
|
||||||
conv = Converter()
|
currency_json = json.load(f)
|
||||||
|
|
||||||
amount = query[0]
|
text = message.text.lower()
|
||||||
source_currency_alias = query[1]
|
args = text.split()
|
||||||
target_currency_alias = query[3]
|
|
||||||
|
|
||||||
with open('currency.json', encoding='utf-8') as file:
|
number_match = re.match(r'\d+\.?\d*|\w+', args[0])
|
||||||
currency_json = json.loads(file.read())
|
if number_match:
|
||||||
|
number_text = number_match.group(0)
|
||||||
|
|
||||||
|
try:
|
||||||
|
amount = float(number_text)
|
||||||
|
except ValueError:
|
||||||
|
try:
|
||||||
|
amount = float(w2n.word_to_num(number_text))
|
||||||
|
except ValueError:
|
||||||
|
await message.reply("Не удалось распознать числовое значение.")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
await message.reply("Не удалось найти числовое значение.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if len(args) == 4:
|
||||||
|
source_currency_alias = args[1]
|
||||||
|
target_currency_alias = args[3]
|
||||||
|
elif len(args) == 3:
|
||||||
|
source_currency_alias = args[0]
|
||||||
|
target_currency_alias = args[2]
|
||||||
|
else:
|
||||||
|
await message.reply("Не удалось определить исходную и целевую валюту.")
|
||||||
|
return
|
||||||
|
|
||||||
source_currency_code = None
|
source_currency_code = None
|
||||||
target_currency_code = None
|
target_currency_code = None
|
||||||
|
@ -30,26 +56,20 @@ async def message_conv(message: types.Message):
|
||||||
for currency_code, aliases in currency_json.items():
|
for currency_code, aliases in currency_json.items():
|
||||||
if source_currency_alias in aliases:
|
if source_currency_alias in aliases:
|
||||||
source_currency_code = currency_code
|
source_currency_code = currency_code
|
||||||
|
if target_currency_alias in aliases:
|
||||||
elif target_currency_alias in aliases:
|
|
||||||
target_currency_code = currency_code
|
target_currency_code = currency_code
|
||||||
|
|
||||||
elif source_currency_code and target_currency_code:
|
if not source_currency_code or not target_currency_code or amount is None:
|
||||||
break
|
await message.reply("Не удалось найти сумму или валюты по указанным данным.")
|
||||||
else:
|
return
|
||||||
return
|
|
||||||
|
|
||||||
if source_currency_code and target_currency_code:
|
conv = Converter()
|
||||||
conv.amount = float(amount)
|
conv.amount = amount
|
||||||
conv.from_currency = source_currency_code.upper()
|
conv.from_currency = source_currency_code.upper()
|
||||||
conv.conv_currency = target_currency_code.upper()
|
conv.conv_currency = target_currency_code.upper()
|
||||||
conv.convert()
|
conv.convert()
|
||||||
|
|
||||||
result = (
|
|
||||||
f'{conv.amount} {conv.from_currency} = '
|
|
||||||
f'{conv.conv_amount} {conv.conv_currency}'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
result = f'{conv.amount} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
||||||
await message.reply(result)
|
await message.reply(result)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue