-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.py
95 lines (78 loc) · 3.79 KB
/
alert.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
93
94
95
import os
import json
import time
import requests
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
from prettytable import PrettyTable
from colorama import Fore, init
from datetime import datetime, timedelta
from playsound import playsound
# PrettyTable Colors
R = "\033[0;31;40m" # RED
G = "\033[0;32;40m" # GREEN
N = "\033[0m" # RESET
DEFAULT_TICKER = 'BTC'
DEFAULT_CONVERT = 'USDT'
DEFAULT_DELTA_TRIGGER = 0.01
DEFAULT_SOUNDFILE = 'alert.wav'
DEFAULT_DELTA_REFRESH_SECONDS = 30
MAX_ROWS_DISPLAYED = 10
class BitcoinAlert():
endpoint = 'https://api.binance.com/api/v3/avgPrice'
@staticmethod
def __getPercent(price, prev):
return ((price - prev) * 100 / price)
@staticmethod
def __getLogo():
os.system('cls' if os.name == 'nt' else 'clear')
return Fore.YELLOW + """\
______ _ _ _ _ __ _
|_ _ \ (_) / |_ (_) / \ [ | / |_
| |_) | __ `| |-'.---. .--. __ _ .--. / _ \ | | .---. _ .--.`| |-'
| __'.[ | | | / /'`\]/ .'`\ \[ | [ `.-. | / ___ \ | |/ /__\ [ `/'`\]| |
_| |__) || | | |,| \__. | \__. | | | | | | | _/ / \ \_ | || \__., | | | |,
|_______/[___]\__/'.___.' '.__.' [___][___||__] |____| |____|[___]'.__.'[___] \__/
______ _____ _________ _______ __ _ _________ ___
|_ _ \ |_ _|| _ _ ||_ __ \ [ | (_)| _ _ | .' `.
| |_) | | | |_/ | | \_| | |__) | .--. | | __ |_/ | | \_|/ .-. \
| __'. | | | | | ___// .'`\ \| | [ | | | | | | |
_| |__) |_| |_ _| |_ _| |_ | \__. || | | | _| |_ \ `-' /
|_______/|_____| |_____| |_____| '.__.'[___][___] |_____| `.___.'
""" + Fore.RESET
def __init__(self, ticker, convert, delta, sound_file, delta_refresh_seconds):
self.ticker = ticker
self.convert = convert
self.delta = delta
self.sound_file = sound_file
self.delta_refresh_seconds = delta_refresh_seconds
def start(self):
try:
init() # colorama init
table = PrettyTable(['Asset', 'Previous Value (' + self.convert + ')',
'New Value (' + self.convert + ')', 'Last Updated', 'Percentage (%)'], )
previous = 0.0
n_prev = 0
offset = 0
while True:
response = requests.get(
f'{self.endpoint}?symbol={self.ticker}{self.convert}').json()
price = eval(response['price'])
percent = self.__getPercent(price, previous)
table.add_row([f'{self.ticker}', f'{round(previous, 2):,}',
f'{round(price, 2):,}', datetime.now().strftime("%H:%M:%S"),
G+f'+{round(percent, 3)}'+N if percent > 0 else R+f'{round(percent, 3)}'+N])
n_prev += 1
if n_prev >= MAX_ROWS_DISPLAYED:
offset += 1
next_update = (datetime.now() + timedelta(seconds=self.delta_refresh_seconds)).strftime("%H:%M:%S")
print(f'{self.__getLogo()}\n{table.get_string(start=offset, end=MAX_ROWS_DISPLAYED + offset)}\
\n\nAPI refreshes every {self.delta_refresh_seconds} seconds (next update at {next_update})')
if (abs(percent) > self.delta):
playsound(self.sound_file)
previous = price
time.sleep(self.delta_refresh_seconds)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
if __name__ == "__main__":
BitcoinAlert(DEFAULT_TICKER, DEFAULT_CONVERT, DEFAULT_DELTA_TRIGGER,
DEFAULT_SOUNDFILE, DEFAULT_DELTA_REFRESH_SECONDS).start()