-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.py
More file actions
44 lines (33 loc) · 1.5 KB
/
extensions.py
File metadata and controls
44 lines (33 loc) · 1.5 KB
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
41
42
43
44
import requests
import json
from config import symbols, API_KEY
class APIException(Exception):
pass
class Converter:
@staticmethod
def get_price(quote: str, base: str, amount: str):
if quote == base:
raise APIException(f'Невозможно перевести одинаковые валюты {base}.')
try:
quote_tiker = symbols[quote]
except KeyError:
raise APIException(f'Не удалось обработать валюту{quote}')
try:
base_ticker = symbols[base]
except KeyError:
raise APIException(f'Не удалось обработать валюту{base}')
try:
amount = float(amount)
except ValueError:
raise APIException(f'Не удалось обработать количество {amount}')
req_part = f'http://api.exchangeratesapi.io/v1/latest?access_key={API_KEY}'
if base_ticker == 'EUR':
r = requests.get(f'{req_part}&symbols={quote_tiker}')
total_base = float(json.loads(r.content)['rates'][quote_tiker]) * amount
else:
r_q = requests.get(f'{req_part}&symbols={quote_tiker}')
r_b = requests.get(f'{req_part}&symbols={base_ticker}')
price_q = float(json.loads(r_q.content)['rates'][quote_tiker])
price_b = float(json.loads(r_b.content)['rates'][base_ticker])
total_base = (price_b / price_q) * amount
return round(total_base, 3)