-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
executable file
·159 lines (126 loc) · 4.54 KB
/
start.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import subprocess
import os
import signal
import gi
import time
import locale
from api.api import Api
from api.websocket import Websocket
from utils.image import ImageUtil
from utils.notification import Alert
locale.setlocale(locale.LC_ALL, '')
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Notify', '0.7')
from gi.repository import Gtk, AppIndicator3
from gi.repository import Notify as notify
from threading import Thread
currpath = os.path.dirname(os.path.realpath(__file__))
coins = ['btc', 'eth', 'ae', 'link', 'eos', 'ht', 'dock', 'egt', 'ksh']
sources = ['Binance Futures', 'Binance', 'Huobi', 'Blockchain', 'Coinsbit', 'P2PB2B']
class Indicator():
def __init__(self):
self.api = Api()
self.websocket = Websocket()
self.image_util = ImageUtil("btcf")
self.last_source = None
self.update = None
self.source = sources[0]
self.symbol = 'btcusdt'
self.app = 'update_setting'
self.alert = Alert()
self.path = currpath
self.indicator = AppIndicator3.Indicator.new(
self.app, currpath + "/icons/btc.png",
AppIndicator3.IndicatorCategory.SYSTEM_SERVICES)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.create_menu()
self.start_source()
notify.init(self.app)
def start_source(self):
if self.source == self.last_source:
return
self.last_source = self.source
self.websocket.close_ws()
if self.source == 'Binance Futures':
self.indicator.set_icon(currpath + "/icons/btcf.png")
self.update = Thread(target=self.websocket.start_ws, args=[self])
self.update.start()
else:
self.update = Thread(target=self.background_monitor)
self.update.setDaemon(True)
self.update.start()
"""Menu methods
These methods handle menu interaction
"""
def create_menu(self):
self.menu = Gtk.Menu()
group = []
for source in sources:
item = Gtk.RadioMenuItem.new_with_label(group, source)
group = item.get_group()
item.connect('activate', self.select_source)
self.menu.append(item)
self.menu.append(Gtk.SeparatorMenuItem())
group = []
for coin in coins:
if coin == 'ksh':
symbol = coin.upper() + '/BTC'
else:
symbol = coin.upper() + '/USDT'
item = Gtk.RadioMenuItem.new_with_label(group, symbol)
group = item.get_group()
item.connect('activate', self.select_coin)
self.menu.append(item)
# quit
item_quit = Gtk.MenuItem('Quit')
sep = Gtk.SeparatorMenuItem()
self.menu.append(sep)
item_quit.connect('activate', self.stop)
self.menu.append(item_quit)
self.menu.show_all()
self.indicator.set_menu(self.menu)
return self.menu
def select_source(self, item):
source = item.get_label()
self.source = source
self.start_source()
def select_coin(self, item):
coin = item.get_label().split('/')[0].lower()
if coin == 'ksh':
self.symbol = 'kshbtc'
else:
self.symbol = coin + 'usdt'
self.indicator.set_icon(currpath + "/icons/" + coin + ".png")
"""Core methods
These methods do a lot of stuff
"""
def get_monitor(self):
if self.source == 'Binance':
return self.api.binance_symbol_avg_price
elif self.source == 'Huobi':
return self.api.huobi_symbol_price
elif self.source == 'Blockchain':
return self.api.blockchain_btc_price
elif self.source == 'Coinsbit':
return self.api.coinsbit_price
elif self.source == 'P2PB2B':
return self.api.p2pb2b_price
else:
return self.api.blockchain_btc_price
def background_monitor(self):
print(f"### start {self.source} monitor ###")
monitor = self.get_monitor()
source = self.source
while source == self.source and monitor is not None:
symbol_price = monitor(self.symbol)
self.alert.check_alert(self.source, self.symbol, symbol_price)
self.indicator.set_label(f' ${symbol_price:n}', '')
time.sleep(1)
def stop(self, _arg):
self.websocket.close_ws()
Gtk.main_quit()
Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()