chore: Divided the functionality into folders

This commit is contained in:
Данил 2024-11-28 21:50:03 +03:00
parent f00cac9ae9
commit a89362205a
4 changed files with 59 additions and 1 deletions

View file

@ -1,85 +0,0 @@
import yaml
import requests
import json
import re
from decimal import Decimal, ROUND_DOWN
from function.format_number import format_number
config = yaml.safe_load(open('config.yaml'))
coinapi_len = len(config['coinapi_keys'])
coinapi_active = [0]
class Converter:
def __init__(self):
self.amount: float = 1.0
self.conv_amount: float = 0.0
self.from_currency: str = ''
self.conv_currency: str = ''
def convert(self) -> None:
if not self.ddg():
self.coinapi()
number = Decimal(str(self.conv_amount))
self.conv_amount = format_number(number.quantize(Decimal('1.00'), rounding=ROUND_DOWN))
def ddg(self) -> bool:
res = requests.get('https://duckduckgo.com/js/spice/currency'
f'/{self.amount}'
f'/{self.from_currency}'
f'/{self.conv_currency}')
data = json.loads(re.findall(r'\(\s*(.*)\s*\);$', res.text)[0])
del data['terms']
del data['privacy']
del data['timestamp']
if len(data.get('to')) == 0:
return False
conv = data.get('to')[0]
conv_amount = conv.get('mid')
if conv_amount is None:
raise RuntimeError('Error when converting currency via DuckDuckGo')
self.conv_amount = float(conv_amount)
return True
def coinapi(self, depth: int = config['coinapi_keys']) -> None:
if depth <= 0:
raise RecursionError('Rate limit on all tokens')
resp = requests.get(
(
'https://rest.coinapi.io/v1/exchangerate'
f'/{self.from_currency}'
f'/{self.conv_currency}'
),
headers={
'X-CoinAPI-Key': config['coinapi_keys'][coinapi_active[0]],
},
timeout=config['timeout'],
)
if resp.status_code == 429:
rotate_token(config['coinapi_keys'], coinapi_active)
self.coinapi(depth - 1)
data = resp.json()
rate = data.get('rate')
if rate is None:
raise RuntimeError('Failed to get the exchange rate from CoinAPI')
self.conv_amount = float(rate * self.amount)
def rotate_token(lst, active) -> None:
active[0] = (active[0] + 1) % len(lst)

View file

@ -1,14 +0,0 @@
def format_number(number):
number_str = str(number)
if '.' in number_str:
integer_part, fractional_part = number_str.split('.')
else:
integer_part, fractional_part = number_str, ''
formatted_integer_part = '{:,}'.format(int(integer_part)).replace(',', ' ')
if fractional_part:
return formatted_integer_part + '.' + fractional_part
else:
return formatted_integer_part

22
function/get_chart.py Normal file
View file

@ -0,0 +1,22 @@
from http import HTTPStatus
import yaml
import requests
from urllib3 import HTTPSConnectionPool
config = yaml.safe_load(open('config.yaml'))
def get_chart(from_currency: str, conv_currency: str) -> (dict, None):
try:
res = requests.get(f'{config['kekkai_instance']}/api/getChart/week/', {
'from_currency': from_currency,
'conv_currency': conv_currency
}, timeout=3)
except requests.exceptions.ConnectionError:
return None
if not HTTPStatus(res.status_code).is_success:
return None
return res.json().get('message', None)

36
function/inline_query.py Normal file
View file

@ -0,0 +1,36 @@
import re
from aiogram import types
async def reply(result_id: str, args: list, query: types.InlineQuery) -> None:
if not args:
return
articles = []
for idx, arg in enumerate(args):
title = arg[0]
description = arg[1] if arg[1] else None
img = arg[2] if arg[2] else None
article = types.InlineQueryResultArticle(
id=f"{result_id}_{idx}",
title=re.sub(r'\[([^\[]+)\]\([^\)]+\)', '', title).replace('График', ''),
thumbnail_url=img,
description=description,
input_message_content=types.InputTextMessageContent(
message_text=title,
parse_mode='markdown'
)
)
articles.append(article)
await query.answer(
results=articles,
parse_mode='markdown',
cache_time=0,
is_personal=True
)