-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotif_handler.py
84 lines (62 loc) · 2.85 KB
/
notif_handler.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
# Handles sending notifications on Windows, Mac, and Linux.
import platform
import os
import tempfile
import time
from urllib.request import urlopen
from urllib.error import URLError
current_os = platform.system() # This method returns 'Darwin' for macs.
notif_icon_path = os.path.join(os.path.dirname(__file__), 'resources/spo.png')
if current_os == 'Linux':
import subprocess
if current_os == 'Windows':
from win10toast import ToastNotifier
def windows_notify(title, text, icon_path, duration):
# Send the notifications from separate threads as there can be issues when sending
# multiple notifications in quick succession.
# t = threading.Thread(target=windows_notif.send_notif, args=(title, text, icon_path, duration))
# t.daemon = True
# t.start()
t = ToastNotifier()
t.show_toast(title, text, icon_path, duration, threaded=True)
def apple_notify(title, text):
# Directly run AppleScript to send the notification - cannot set image for the notification
# or its duration.
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
def linux_notify(title, text, icon_path, duration):
# bus = dbus.SessionBus()
#
# notify = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
# notify = notify.get_dbus_method('Notify', 'org.freedesktop.Notifications')
#
# # The 2nd parameter being a 0 means this notification will not replace another (see
# # https://developer.gnome.org/notification-spec/) - the lists are for more
# # complex notifications and are therefore empty.
# notify("save-song-spotify", 0, icon_path, title, text, [], [], duration)
subprocess.call(['notify-send', '--expire-time={}'.format(duration * 1000),
'--icon={}'.format(icon_path), title, text])
def send_notif(title, text, icon_path=notif_icon_path, duration=3):
if current_os == 'Linux':
linux_notify(title, text, icon_path, duration * 1000)
elif current_os == 'Darwin':
apple_notify(title, text)
elif current_os == 'Windows':
windows_notify(title, text, icon_path, duration)
def send_notif_with_web_image(title, text, image_url, timeout=2):
# We have to temporarily write the image contents to a file to use it in notifications.
try:
# Don't want to delay the notification too long
with urlopen(image_url, timeout=timeout) as response:
data = response.read()
file = tempfile.NamedTemporaryFile(delete=False)
file.write(data)
send_notif(title, text, file.name)
# Without this the file gets deleted too quick or something and
# doesn't show up
time.sleep(0.1)
file.close()
os.unlink(file.name)
except URLError:
send_notif(title, text)