-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinance.py
55 lines (50 loc) · 2.37 KB
/
binance.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
import requests
from lists import binancelist
def handleBinance(message, resultMessage):
upperMessage = message.text.upper()
speciallist = ["USDT", "ETHBTC"]
if (upperMessage in binancelist) or (upperMessage in speciallist):
url = "https://api.binance.com/api/v3/ticker/24hr?symbol=" + upperMessage + "USDT"
if upperMessage == "USDT":
url = "https://api.binance.com/api/v3/ticker/24hr?symbol=" + "USDT" + "TRY"
if upperMessage == "ETHBTC":
url = "https://api.binance.com/api/v3/ticker/24hr?symbol=" + "ETH" + "BTC"
response = requests.get(url)
output = response.json()
symbol = ""
price = ""
if upperMessage == "USDT":
symbol = "₺"
price = format(float(output['lastPrice']))
elif upperMessage == "ETHBTC":
symbol = "₿"
price = format(float(output['lastPrice']))
else:
symbol = "$"
price = format(float(output['lastPrice']))
percent = " %{:.2f}".format(float(output['priceChangePercent']))
if resultMessage != "":
resultMessage += "\n"
resultMessage += "Binance -> " + upperMessage + ': ' + symbol + price + percent
if message.text.upper()=="GAINERS" or message.text.upper()=="LOSERS":
changePercentList = []
url = "https://api.binance.com/api/v3/ticker/24hr"
response = requests.get(url)
output = response.json()
for i in binancelist:
upperMessage = i + "USDT"
index = [j for j,_ in enumerate(output) if _['symbol'] == upperMessage][0]
changePercentList.append([i,float(output[index]['lastPrice']),float(output[index]["priceChangePercent"])])
if resultMessage != "":
resultMessage += "\n"
if message.text.upper()=="GAINERS":
changePercentList.sort(key= lambda coin: coin[2], reverse=True)
resultMessage += "Binance Top Gainers:"
if message.text.upper()=="LOSERS":
changePercentList.sort(key= lambda coin: coin[2])
resultMessage += "Binance Top Losers:"
for i in range(5):
if resultMessage != "":
resultMessage += "\n"
resultMessage += changePercentList[i][0] + ': ' + format(changePercentList[i][1]) + " %{:.2f}".format(float(changePercentList[i][2]))
return resultMessage