-
Notifications
You must be signed in to change notification settings - Fork 1
/
mastodon_bot.py
58 lines (45 loc) · 1.44 KB
/
mastodon_bot.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
import os
import time
import requests
import schedule
token = os.getenv('TOKEN')
url = "https://botsin.space/api/v1/statuses"
coins = ['btc', 'eth', 'ltc',
'xmr', 'uni', 'dot',
'ksm', 'ada', 'xrp']
last_price = {'btc': 0, 'eth': 0, 'ltc': 0,
'xmr': 0, 'uni': 0, 'dot': 0,
'ksm': 0, 'ada': 0, 'xrp': 0}
def persent_price(last, new):
return round((new - last) / last * 100, 2)
def price_coin(arr):
string = ''
for i in range(len(arr)):
r = requests.get('https://api.bitfinex.com/v1/pubticker/'+arr[i]+'usd')
data = r.json()
price = data['ask']
coin = arr[i]
if last_price[coin] == 0:
string = string + coin.upper() + ': ' + '$' + price + '\n'
else:
per = persent_price(float(last_price[coin]), float(price))
string += f"{coin.upper()}: $ {price} {per}%\n"
last_price[coin] = price
return string
def request(price_coin):
headers = {"Authorization": "Bearer " + token}
body = {"status": price_coin(coins)}
r = requests.post(url, headers=headers, json=body, timeout=60)
def main():
schedule.every().hours.do(request, price_coin)
while True:
try:
schedule.run_pending()
except Exception:
print("--Request Error!--")
else:
print("--Request Ok!--")
finally:
time.sleep(15)
if __name__ == "__main__":
main()