-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.py
40 lines (26 loc) · 1.24 KB
/
extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import telebot
import requests
from config import keys, TOKEN
import json
class ConversionException(Exception):
pass
class Convertor:
@staticmethod
def convert(from_currency:str, to_currency:str, amount: str):
if from_currency == to_currency:
raise ConversionException(f'Невозможно перевести одинаковые валюты {from_currency}')
try:
from_currency_ticker = keys[from_currency]
except KeyError:
raise ConversionException(f'Не удалось обработать валюту {from_currency}')
try:
to_currency_ticker = keys[to_currency]
except KeyError:
raise ConversionException(f'Не удалось обработать валюту {to_currency}')
try:
amount_ticker=float(amount)
except ValueError:
raise ConversionException(f'Не удалось обработать количество {amount}')
r = requests.get(f'https://api.frankfurter.app/latest?amount={amount_ticker}=10&from={from_currency_ticker}&to={to_currency_ticker}')
output_base = json.loads(r.content)['rates'][keys[to_currency]]
return output_base