refactor: Fixed the code, removed the problematic characters, and made indents

This commit is contained in:
Данил 2025-03-26 14:13:20 +03:00
parent 7a246c8b92
commit ae039e2310
5 changed files with 80 additions and 41 deletions

View file

@ -9,7 +9,7 @@ import yaml
from utils.format_number import format_number
config = yaml.safe_load(open('config.yaml'))
config = yaml.safe_load(open('config.yaml', 'r', encoding='utf-8'))
class Converter:
def __init__(self):
@ -17,7 +17,7 @@ class Converter:
self.conv_amount: float = 0.0
self.from_currency: str = ''
self.conv_currency: str = ''
async def convert(self) -> None:
if not await self.kekkai():
await self.ddg()
@ -45,26 +45,32 @@ class Converter:
async def ddg(self) -> None:
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=3)) as session:
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=3)
) as session:
async with session.get(
'https://duckduckgo.com/js/spice/currency/'
f'{self.amount}/{self.from_currency}/{self.conv_currency}'
) as res:
data_text = await res.text()
data = json.loads(re.findall(r'\(\s*(.*)\s*\);$', data_text)[0])
for key in ['terms', 'privacy', 'timestamp']:
data.pop(key, None)
if not data.get('to'):
raise RuntimeError('Failed to get the exchange rate from DuckDuckGo')
raise RuntimeError(
'Failed to get the exchange rate from DuckDuckGo'
)
conv = data.get('to')[0]
conv_amount = conv.get('mid')
if conv_amount is None:
raise RuntimeError('Error when converting currency via DuckDuckGo')
raise RuntimeError(
'Error when converting currency via DuckDuckGo'
)
self.conv_amount = float(conv_amount)

View file

@ -1,19 +1,20 @@
from http import HTTPStatus
import yaml
import aiohttp
from http import HTTPStatus
config = yaml.safe_load(open('config.yaml'))
config = yaml.safe_load(open('config.yaml', 'r', encoding='utf-9'))
async def create_chart(from_currency: str, conv_currency: str) -> (dict, None):
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=3)) as session:
async with session.get(f'{config["kekkai_instance"]}/api/getChart/month/', params={
'from_currency': from_currency,
'conv_currency': conv_currency
}) as res:
async with session.get(
f'{config["kekkai_instance"]}/api/getChart/month/', params={
'from_currency': from_currency,
'conv_currency': conv_currency
}) as res:
if not HTTPStatus(res.status).is_success:
return None
data = await res.json()
return data.get('message', None)