forked from self-aware-git/exchange-rates-tg-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetExchangeRates.py
92 lines (85 loc) · 3.79 KB
/
GetExchangeRates.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#Public libraries
import requests
import time
from Token import apiKey
from DBH import UpdateExchangeRatesDB, GetExchangeRates, UpdateCryptoRatesDB, GetCryptoRates
from random import randint
#Own libraries
from NewPrint import Print
cryptoList = ["ADA", "BCH", "BNB", "BTC", "DASH", "DOGE", "ETC", "ETH", "LTC", "RVN", "TRX", "XLM", "XMR", "XRP"]
exchangeRates = {}
cryptoRates = []
def SheduleUpdate():
global exchangeRates
while True:
exchangeRates = UpdateExchangeRates()
time.sleep(28800)
def SheduleCryptoUpdate():
global cryptoRates
while True:
cryptoRates = UpdateCryptoRates()
time.sleep(randint(10,30))
def UpdateExchangeRates() -> dict:
global exchangeRates
Print("Updating of exchange rates has started.", "S")
try:
url = "http://data.fixer.io/api/latest?access_key=" + apiKey
response = requests.get(url)
exchangeRates = response.json()['rates']
exchangeRates['TMT'] = exchangeRates['RUB']/2
UpdateExchangeRatesDB(exchangeRates.copy())
Print("Updating of exchange rates is successfull.", "S")
except:
Print("Updating ER failed.", "E")
Print("Using exchange rates from DB.", "S")
exchangeRates = GetExchangeRates()
return exchangeRates.copy()
def UpdateCryptoRates() -> dict:
global cryptoRates
Print("Updating of crypto rates has started.", "S")
try:
url = "https://api.binance.com/api/v3/ticker/price"
response = requests.get(url)
cryptoRates = {}
for pair in response.json():
if pair['symbol'].find("USDT") != -1 and any(pair['symbol'][:-4] == s for s in cryptoList):
cryptoRates[pair['symbol'][:-4]]=float(pair['price'])
UpdateCryptoRatesDB(cryptoRates.copy())
Print("Updating of crypto rates is successfull.", "S")
except:
Print("Updating CR failed. Trying different endpoint", "E")
try:
url = "https://api1.binance.com/api/v3/ticker/price"
response = requests.get(url)
cryptoRates = {}
for pair in response.json():
if pair['symbol'].find("USDT") != -1 and any(pair['symbol'][:-4] == s for s in cryptoList):
cryptoRates[pair['symbol'][:-4]]=float(pair['price'])
UpdateCryptoRatesDB(cryptoRates.copy())
Print("Updating of crypto rates is successfull.", "S")
except:
Print("Updating CR failed. Trying different endpoint", "E")
try:
url = "https://api2.binance.com/api/v3/ticker/price"
response = requests.get(url)
cryptoRates = {}
for pair in response.json():
if pair['symbol'].find("USDT") != -1 and any(pair['symbol'][:-4] == s for s in cryptoList):
cryptoRates[pair['symbol'][:-4]]=float(pair['price'])
UpdateCryptoRatesDB(cryptoRates.copy())
Print("Updating of crypto rates is successfull.", "S")
except:
Print("Updating CR failed. Trying different endpoint", "E")
try:
url = "https://api3.binance.com/api/v3/ticker/price"
response = requests.get(url)
cryptoRates = {}
for pair in response.json():
if pair['symbol'].find("USDT") != -1 and any(pair['symbol'][:-4] == s for s in cryptoList):
cryptoRates[pair['symbol'][:-4]]=float(pair['price'])
UpdateCryptoRatesDB(cryptoRates.copy())
Print("Updating of crypto rates is successfull.", "S")
except:
Print("Using crypto rates from DB.", "S")
cryptoRates = GetCryptoRates()
return cryptoRates.copy()