-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation-checker-applet.py
executable file
·95 lines (69 loc) · 2.39 KB
/
location-checker-applet.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
#!/usr/bin/env python
import threading
import time
import json
import re
from lxml import html
import requests
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('MatePanelApplet', '4.0')
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from gi.repository import Gtk
from gi.repository import MatePanelApplet
appletName = 'LocationCheckerApplet'
appletFactoryName = appletName + 'Factory'
def get_location():
location = 'unknown'
try:
pageContent = requests.get('https://ipx.ac', timeout=1)
tree = html.fromstring(pageContent.content)
raw = tree.xpath('/html/body/script[9]/text()')
match = re.match(r'.+ DATA = ({.+});.+', raw[0], re.S)
if match:
data = json.loads(match.group(1))
location = data['geo']['countryName'] + \
", " + data['geo']['cityName']
except requests.exceptions.Timeout:
location = 'timeout'
except requests.exceptions.RequestException as e:
print(e)
location = 'exception'
return location
class Scheduler(threading.Thread):
def __init__(self, interval_sec, func):
threading.Thread.__init__(self)
self.daemon = True
self.interval_sec = interval_sec
self.func = func
def run(self):
while True:
self.func()
time.sleep(self.interval_sec)
def applet_factory(applet, iid, data):
if iid != appletName:
return False
# you can use this path with gio/gsettings
settings_path = applet.get_preferences_path()
initial_location = get_location()
label = Gtk.Label(initial_location)
# Without init libnotify crashes
Notify.init(appletName)
notification_title = 'Location has changed'
n = Notify.Notification.new(notification_title, initial_location, '')
def update_label():
current_location = label.get_text()
new_location = get_location()
if new_location != current_location:
label.set_text(new_location)
n.update(notification_title, new_location, '')
n.show()
sched = Scheduler(interval_sec=5, func=update_label)
sched.start()
applet.add(label)
applet.show_all()
return True
MatePanelApplet.Applet.factory_main(appletFactoryName, True,
MatePanelApplet.Applet.__gtype__,
applet_factory, None)