-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_tray.py
220 lines (167 loc) · 7.37 KB
/
system_tray.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import PySide2.QtWidgets as QtWidgets
from data_manager import DataStore
from watch_highlights import HighlightsWatchThread
import league
import process_highlights
import queue
import setup_thread
import upload_thread
import utils
import webbrowser
import window
import ws
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
MAX_UPLOAD_THREADS = 3
def __init__(self, icon):
super().__init__(icon)
self.notification_shown = False
self.ws_is_running = False
self.threads = []
self.init()
def init(self):
self.setToolTip('Lol-highlights-enhancer')
self.activated.connect(self.icon_activated)
self.checker_thread = league.ProcessCheckerThread()
self.checker_thread.start()
self.checker_thread.status.connect(self.process_status)
self.threads.append(self.checker_thread)
self.setup_thread_pool()
self.setup_context_menu()
self.window = window.MainWindow()
self.window.show()
self.window.closing_event.connect(self.handle_exit)
self.window.action.connect(self.handle_buttons)
self.window.minimise.connect(self.hide_window)
self.preferences = DataStore.get_preferences()
is_first_time = self.preferences['first_time']
if not is_first_time:
self.window.init_UI()
def setup_thread_pool(self):
self.queue = queue.SimpleQueue()
self.thread_pool = []
for x in range(self.MAX_UPLOAD_THREADS):
thread = upload_thread.Thread(self.queue)
thread.start()
thread.uploaded.connect(self.handle_upload_url)
self.thread_pool.append(thread)
def handle_upload_url(self, result):
highlight_name, video_url = result
self.window.status.showMessage(
f'Uploaded {highlight_name} as {video_url}')
if 'gfycat' in video_url:
service = 'gfycat'
elif 'streamable' in video_url:
service = 'streamable'
highlight = DataStore.get_highlight(highlight_name)
highlight[service] = video_url
DataStore.save_highlight(highlight_name, highlight)
self.window.refresh()
def setup_context_menu(self):
context_menu = QtWidgets.QMenu()
quit_action = context_menu.addAction('Quit')
quit_action.triggered.connect(self.handle_exit)
quit_action.triggered.connect(QtWidgets.qApp.quit)
self.minimize_action = context_menu.addAction('Minimize')
self.minimize_action.triggered.connect(self.hide_window)
self.setContextMenu(context_menu)
def icon_activated(self, reason):
print(reason)
def show_window(self):
context_menu = self.contextMenu()
context_menu.removeAction(self.show_action)
context_menu.addAction(self.minimize_action)
self.window.show()
def hide_window(self):
context_menu = self.contextMenu()
context_menu.removeAction(self.minimize_action)
self.show_action = context_menu.addAction('Show window')
self.show_action.triggered.connect(self.show_window)
self.window.hide()
def handle_exit(self):
self.show_notification('The application is closing.')
for thread in self.threads:
if thread is not None:
thread.exit()
for thread in self.thread_pool:
thread.exit()
def handle_buttons(self, service, highlight_name):
highlight = DataStore.get_highlight(highlight_name)
if service != 'gfycat_alternate':
url = highlight[service]
if url is None:
self.window.status.showMessage(
f'Added {highlight_name} to upload queue')
self.queue.put((highlight_name, service))
else:
webbrowser.open(url, new=2)
else:
url = highlight['gfycat']
gfyname = utils.get_filename(url)
webbrowser.open(f'https://giant.gfycat.com/{gfyname}.mp4', new=2)
def handle_api_events(self, message):
uri = message[2]['uri']
data = message[2]['data']
if uri == '/lol-gameflow/v1/watch':
if data == 'WatchInProgress':
higlights_path = self.preferences['current-highlights-folder']
self.highlights_thread = HighlightsWatchThread(higlights_path)
self.highlights_thread.start()
self.highlights_thread.done.connect(self.handle_new_highlights)
else:
self.highlights_thread.exit()
elif uri == '/lol-gameflow/v1/gameflow-phase':
if data == 'GameStart':
self.window.status.showMessage('Pausing Uploads')
for thread in self.thread_pool:
thread.pause()
elif data == 'WaitingForStats':
self.window.status.showMessage('Resuming Uploads')
for thread in self.thread_pool:
thread.resume()
def handle_new_highlights(self, highlights):
DataStore.refresh_highlights()
upload_preferences = DataStore.get_preference('upload_new_highlights')
new_highlights = {}
for highlight_name in highlights:
new_highlights[highlight_name] = DataStore.get_highlight(
highlight_name)
if upload_preferences['gfycat']:
self.queue.put((highlight_name, 'gfycat'))
if upload_preferences['streamable']:
self.queue.put((highlight_name, 'streamable'))
processor_thread = process_highlights.Thread(new_highlights)
processor_thread.start()
processor_thread.status.connect(self.window.status.showMessage)
processor_thread.done_status.connect(self.window.refresh)
self.threads.append(processor_thread)
def show_notification(self, message):
self.showMessage('Lol-Highlights-Enhancer', message, self.NoIcon)
def get_match_details(self):
processor_thread = process_highlights.Thread(
DataStore.get_highlights())
processor_thread.start()
processor_thread.status.connect(self.window.status.showMessage)
processor_thread.done_status.connect(self.window.refresh)
self.threads.append(processor_thread)
def process_status(self, status):
self.preferences = DataStore.get_preferences()
is_first_time = self.preferences['first_time']
if status == 'not_running':
if is_first_time and not self.notification_shown:
msg = 'The League of Legends Client is not running.'
self.show_notification(msg)
self.window.status.showMessage(msg)
self.notification_shown = True
elif status == 'running' and not self.ws_is_running:
self.websocket_thread = ws.WebSocketThread()
self.websocket_thread.start()
self.websocket_thread.api_event.connect(self.handle_api_events)
self.threads.append(self.websocket_thread)
self.ws_is_running = True
self.setup_thread = setup_thread.Thread(is_first_time)
self.setup_thread.start()
self.setup_thread.done_setup.connect(self.window.init_UI)
self.setup_thread.done_setup.connect(self.get_match_details)
self.setup_thread.login_status.connect(
self.window.status.showMessage)
self.threads.append(self.setup_thread)