/
main.py
executable file
·88 lines (65 loc) · 2.18 KB
/
main.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import webbrowser
import requests
from threading import Thread
from socketIO_client import SocketIO, LoggingNamespace
LOCATION_ID = 2
API_URL = 'https://api.project-p.xyz'
TRAY_TOOLTIP = 'Project P'
TRAY_ICON_OPEN = './project-p-tray-16-open.png'
TRAY_ICON_CLOSED = './project-p-tray-16-closed.png'
class App(wx.App):
def OnInit(self):
frame = wx.Frame(None)
self.SetTopWindow(frame)
self.tbicon = TaskBarIcon(frame)
return True
def on_ws_location(self, *args):
res, = args # Unpack tuple
self.tbicon.set_icon(res['data']['occupied'])
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
self._initialize_icon()
def CreatePopupMenu(self):
menu = wx.Menu()
self._create_menu_item(menu, 'Open Project P website', self._open_site)
menu.AppendSeparator()
self._create_menu_item(menu, 'Exit', self._on_exit)
return menu
def set_icon(self, occupied):
if occupied:
icon = wx.IconFromBitmap(wx.Bitmap(TRAY_ICON_CLOSED))
else:
icon = wx.IconFromBitmap(wx.Bitmap(TRAY_ICON_OPEN))
self.SetIcon(icon, TRAY_TOOLTIP)
def _open_site(self, event):
webbrowser.open('https://project-p.xyz')
def _on_exit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
def _create_menu_item(self, menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item
def _initialize_icon(self):
try:
r = requests.get('%s/locations/%d' % (API_URL, LOCATION_ID))
except Exception as e:
print e
self.set_icon(True)
return
location = r.json()
self.set_icon(location['data']['occupied'])
def main():
app = App(False)
socketIO = SocketIO(API_URL, 443, LoggingNamespace)
socketIO.on('location', app.on_ws_location)
Thread(target=socketIO.wait).start()
app.MainLoop()
if __name__ == '__main__':
main()