-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
73 lines (61 loc) · 2.69 KB
/
notify.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
import subprocess
import logging
import webbrowser
import threading
import sys
# lunix
try:
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
except ImportError:
dbus = None
# window
try:
import winotify
except ImportError:
winotify = None
# i could have used an external library for this but they all suck bcus "cross platform"
# probably gonna rewrite this a bit to work on windows + cross platform with fancy features
def send_notification(message, link):
if sys.platform.startswith("linux"):
if dbus:
try:
# try dbusing it first
bus = dbus.SessionBus()
notifications = bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
interface = dbus.Interface(notifications, "org.freedesktop.Notifications")
hints = {
"resident": dbus.Boolean(True),
"urgency": dbus.Byte(2) # the highest one. you wanna be the first to view those pics right
}
actions = ["default", "View"]
notification_id = interface.Notify("pixiv-monitor", 0, "printer", "pixiv-monitor alert!", message, actions, hints, 0) # printer icon chosen for no reason
def on_action_invoked(iden, action):
if iden == notification_id and action == "default":
webbrowser.open(link)
interface.CloseNotification(notification_id)
loop.quit()
bus.add_signal_receiver(
on_action_invoked,
dbus_interface="org.freedesktop.Notifications",
signal_name="ActionInvoked"
)
def run_loop():
global loop
loop = GLib.MainLoop()
loop.run()
threading.Thread(target=run_loop, daemon=True).start()
return
except Exception as exc:
logging.getLogger().warn(f"Unable to send dbus notification: {exc}; trying notify-send instead")
# fallback in case we don't have dbus or it fail
subprocess.run(["notify-send", "-i", "dialog-information", "pixiv-monitor alert!", message, "-t", "0"])
elif sys.platform.startswith("win"):
if winotify:
toast = winotify.Notification(app_id="pixiv-monitor", title="pixiv-monitor alert!", msg=message)
toast.add_actions(label="View", launch=link)
toast.show()
else:
logging.getLogger().warn("Can't send notification because winofity isn't installed")