-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.py
executable file
·93 lines (73 loc) · 2.73 KB
/
event.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
#! /usr/bin/python
# Copyright (C) 2012 Maxwell J. Koo <[email protected]>
#
# Based on code originally by John Reese, LeetCode.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import contextlib
import dbus
import hashlib
import os
import sys
from urllib2 import urlopen, URLError
def handle_event(type, **kwargs):
"""
Read event parameters from stdin and handle events appropriately.
"""
# Error from pianobar, disregard
if kwargs.get("pRet") != "1":
return
# Handle specific events
if type == "songstart":
title = kwargs.get("title")
cover_url = kwargs.get("coverArt")
artist_album = "by %s on %s" % (kwargs.get("artist"), kwargs.get("album"))
config_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
filename = os.path.join(config_dir, "covers", hashlib.sha1(cover_url).hexdigest())
cover = fetch_cover(cover_url, filename)
obj_path = "/org/freedesktop/Notifications"
bus_name = iface_name = "org.freedesktop.Notifications"
bus = dbus.SessionBus()
obj = bus.get_object(bus_name, obj_path)
iface = dbus.Interface(obj, iface_name)
iface.Notify("", 0, cover, title, artist_album, [], [], 5000)
def fetch_cover(url, filename):
"""
Fetches album art from the URL specified by pianobar, and saves to disk.
"""
# If the "covers" directory does not exist under the pianobar config
# directory, create it
if not os.path.isdir(os.path.dirname(filename)):
os.mkdir(os.path.dirname(filename))
if not os.path.exists(filename):
try:
with contextlib.closing(urlopen(url)) as inf, open(filename, "wb") as outf:
outf.write(inf.read())
except URLError:
return ""
return "file://%s" % filename
def main(argv=None):
if argv is None:
argv = sys.argv
# Read event type from command arguments
if len(sys.argv) < 2:
print "error reading event type from command arguments"
type = sys.argv[1]
# Read parameters from input
params = {}
for s in sys.stdin.readlines():
param, value = s.split("=", 1)
params[param.strip()] = value.strip()
# Call the event handler
handle_event(type, **params)
return 0
if __name__ == "__main__":
sys.exit(main())