-
Notifications
You must be signed in to change notification settings - Fork 45
/
extensions.py
35 lines (27 loc) · 1.09 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
import json
import requests
from config import *
class ExchangeException(Exception):
pass
class Exchange:
@staticmethod
def get_price(quote: str, base: str, amount: str):
if quote == base:
raise ExchangeException(
f'Нельзя перевести одинаковые валюты {base}.')
try:
quote_ticker = keys[quote]
except KeyError:
raise ExchangeException(f'Не смог обработать валюту {quote}')
try:
base_ticker = keys[base]
except KeyError:
raise ExchangeException(f'Не смог обработать валюту {base}')
try:
amount = int(amount)
except ValueError:
raise ExchangeException(f'Не смог обработать количество {amount}')
r = requests.get(
f'https://min-api.cryptocompare.com/data/price?fsym={base_ticker}&tsyms={quote_ticker}')
total_base = float(json.loads(r.content)[keys[quote]])
return total_base