mirror of
https://github.com/Redume/Shirino.git
synced 2025-02-04 09:58:57 +03:00
refactor
This commit is contained in:
parent
f103970be2
commit
24507c2619
1 changed files with 37 additions and 36 deletions
|
@ -12,6 +12,7 @@ from utils.format_number import format_number
|
||||||
|
|
||||||
config = yaml.safe_load(open('config.yaml'))
|
config = yaml.safe_load(open('config.yaml'))
|
||||||
|
|
||||||
|
|
||||||
class Converter:
|
class Converter:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.amount: float = 1.0
|
self.amount: float = 1.0
|
||||||
|
@ -19,59 +20,59 @@ class Converter:
|
||||||
self.from_currency: str = ''
|
self.from_currency: str = ''
|
||||||
self.conv_currency: str = ''
|
self.conv_currency: str = ''
|
||||||
|
|
||||||
|
|
||||||
def convert(self) -> None:
|
def convert(self) -> None:
|
||||||
if not self.kekkai():
|
if not self.kekkai() and not self.ddg():
|
||||||
self.ddg()
|
raise RuntimeError('Failed to convert via Kekkai or DDG')
|
||||||
|
|
||||||
number = Decimal(str(self.conv_amount))
|
number = Decimal(str(self.conv_amount))
|
||||||
|
|
||||||
self.conv_amount = format_number(number.quantize(Decimal('1.00'), rounding=ROUND_DOWN))
|
self.conv_amount = format_number(number.quantize(Decimal('1.00'), rounding=ROUND_DOWN))
|
||||||
|
|
||||||
def ddg(self) -> None:
|
|
||||||
res = requests.get('https://duckduckgo.com/js/spice/currency'
|
def ddg(self) -> bool:
|
||||||
f'/{self.amount}'
|
try:
|
||||||
f'/{self.from_currency}'
|
res = requests.get(
|
||||||
f'/{self.conv_currency}')
|
f'https://duckduckgo.com/js/spice/currency/'
|
||||||
|
f'{self.amount}/{self.from_currency}/{self.conv_currency}',
|
||||||
|
timeout=3
|
||||||
|
)
|
||||||
|
|
||||||
data = json.loads(re.findall(r'\(\s*(.*)\s*\);$', res.text)[0])
|
data = json.loads(re.findall(r'\(\s*(.*)\s*\);$', res.text)[0])
|
||||||
|
|
||||||
del data['terms']
|
for key in ['terms', 'privacy', 'timestamp']:
|
||||||
del data['privacy']
|
data.pop(key, None)
|
||||||
del data['timestamp']
|
|
||||||
|
|
||||||
if len(data.get('to')) == 0:
|
if not data.get('to'):
|
||||||
raise RuntimeError('Failed to get the exchange rate from DDG')
|
return False
|
||||||
|
|
||||||
conv = data.get('to')[0]
|
self.conv_amount = float(data['to'][0].get('mid', 0.0))
|
||||||
conv_amount = conv.get('mid')
|
return True
|
||||||
|
|
||||||
if conv_amount is None:
|
|
||||||
raise RuntimeError('Error when converting currency via DuckDuckGo')
|
|
||||||
|
|
||||||
self.conv_amount = float(conv_amount)
|
|
||||||
|
|
||||||
|
except (requests.exceptions.RequestException, json.JSONDecodeError, IndexError) as e:
|
||||||
|
print(f"Error when requesting DDG: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
def kekkai(self) -> bool:
|
def kekkai(self) -> bool:
|
||||||
date = datetime.today().strftime('%Y-%m-%d')
|
date = datetime.today().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests.get(f'{config['kekkai_instance']}/api/getRate/', {
|
res = requests.get(
|
||||||
|
f"{config['kekkai_instance']}/api/getRate/",
|
||||||
|
params={
|
||||||
'from_currency': self.from_currency,
|
'from_currency': self.from_currency,
|
||||||
'conv_currency': self.conv_currency,
|
'conv_currency': self.conv_currency,
|
||||||
'date': date,
|
'date': date,
|
||||||
'conv_amount': self.amount
|
'conv_amount': self.amount
|
||||||
}, timeout=3)
|
},
|
||||||
|
timeout=3
|
||||||
|
)
|
||||||
|
|
||||||
|
if res.status_code != HTTPStatus.OK:
|
||||||
|
return False
|
||||||
|
|
||||||
data = res.json()
|
data = res.json()
|
||||||
|
self.conv_amount = data.get('conv_amount', 0.0)
|
||||||
if not HTTPStatus(res.status_code).is_success:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
self.conv_amount = data.get('conv_amount')
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
|
except (requests.exceptions.ConnectionError, json.JSONDecodeError) as e:
|
||||||
|
print(f"Error when querying Kekkai: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue