-
Notifications
You must be signed in to change notification settings - Fork 3
/
musnify-mpd.py
239 lines (191 loc) · 6.59 KB
/
musnify-mpd.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# coding: utf-8
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
import json
import os
import sys
import time
import re
import gi
import requests
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from gi.repository.Gio import File
from gi.repository.GdkPixbuf import Pixbuf
from mpd import MPDClient
configFile = os.path.expanduser("~/.config/musnify-mpd/musnify-mpd.config")
if not os.path.isfile(configFile):
print("Loading default config")
configFile = "/etc/musnify-mpd.config"
config = ConfigParser()
config.read(configFile)
host = config.get("mpd","host", fallback=os.environ.get("MPD_HOST", "localhost"))
port = config.get("mpd","port", fallback=os.environ.get("MPD_PORT", 6600))
if config.has_option("apiKey", "lastfm"):
apiKey = config.get("apiKey", "lastfm")
musicLibrary = os.path.expanduser(config.get("mpd","musiclibrary", fallback='~/Music')) + "/"
debug = False
class MPDWrapper:
def __init__(self, host="localhost", port="6600"):
self.client = MPDClient()
self.client.timeout = 1
self.client.idletimeout = None
self.client.connect(host, port)
def getCurrentSong(self):
song = self.client.currentsong()
try:
artist = song["artist"]
except KeyError:
song["artist"] = "Unknown Artist"
try:
album = song["album"]
except KeyError:
song["album"] = "Unknown Album"
try:
title = song["title"]
except KeyError:
try:
song["title"] = song["file"].split("/")[-1]
except KeyError:
song["title"] = "Unknown Title"
return song
def getStatus(self):
return self.client.status()["state"]
def waitForChange(self):
return self.client.idle("player")
class NotificationWrapper:
def __init__(self):
Notify.init("musnify-mpd")
self.notification = Notify.Notification.new("Initializing Musnify..")
def notify(self, artist, album, title, cover):
self.notification.clear_hints()
if cover == None:
self.notification.update(title, ("by " + artist + "\n" + album).replace("&", "&"), "music")
else:
self.notification.update(title, ("by " + artist + "\n" + album).replace("&","&"))
self.notification.set_image_from_pixbuf(cover)
self.notification.show()
def notifyStatus(self, status):
self.notification.clear_hints()
if status == "pause":
self.notification.update("MPD Paused",icon="music")
elif status == "stop":
self.notification.update("MPD Stopped",icon="music")
self.notification.show()
class CoverArt:
@staticmethod
def fetchAlbumCoverURL(artist, album, size=1):
apiUrl = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo'
if not 'apiKey' in globals():
return False
apiReqUrl = apiUrl + '&artist=' + artist + '&album=' + album + '&api_key=' + apiKey + '&format=json'
r = requests.get(apiReqUrl)
dataInfo = json.loads(r.content)
try:
assert dataInfo["error"] > 0
if debug:
print("Nothing found on last fm")
return False
except:
url = dataInfo["album"]["image"][size]["#text"]
if url == "":
if debug:
print("Nothing found on last fm")
return False
return url
@staticmethod
def downloadPixbufAlbumCover(url):
if debug:
print("downloading album cover from " + url)
f = File.new_for_uri(url)
stream = f.read()
cover = Pixbuf.new_from_stream(stream)
stream.close()
return cover
@staticmethod
def fetchLocalCover(path):
regex = re.compile(r'(album|cover|\.?folder|front).*\.(gif|jpeg|jpg|png)$', re.I | re.X)
try:
for e in os.listdir(path):
if regex.match(e) != None:
if debug:
print("local cover found at " + path + e)
return Pixbuf.new_from_file(path + e)
except:
pass
if debug:
print("Nothing found on local directory")
return False
class Musnify(object):
def __init__(self):
self.nw = NotificationWrapper()
self.lastfmCoverPath = "/tmp/musnifyCurrentCover.png"
def start(self):
mpd = MPDWrapper(host, port)
status = ""
song = ""
while True:
actualStatus = mpd.getStatus()
actualSong = mpd.getCurrentSong()
if status != actualStatus:
status = mpd.getStatus()
if actualStatus == "play":
song = mpd.getCurrentSong()
self.handle(song)
else:
self.nw.notifyStatus(status)
if (song != actualSong) and status != "stop":
song = mpd.getCurrentSong()
self.handle(song)
if debug:
print(song)
mpd.waitForChange()
def handle(self, song):
localCoverPath = CoverArt.fetchLocalCover(musicLibrary + self._separa(song["file"]))
artist = song["artist"]
album = song["album"]
title = song["title"]
coverUrl = CoverArt.fetchAlbumCoverURL(artist, album)
if coverUrl != False:
path = CoverArt.downloadPixbufAlbumCover(coverUrl)
elif localCoverPath != False:
path = localCoverPath
else:
path = None
self.nw.notify(artist, album, title, path)
@staticmethod
def _separa(url):
url0 = url.split("/")
url1 = ""
for i in range(len(url0) - 1):
url1 += url0[i] + "/"
return url1
def stop(self):
Notify.uninit()
def help():
print("""musnify-mpd\n\nOptions:
--help\tShow this help and exit
-h\t\tSpecify your MPD host (default: localhost)
-p\t\tSpecify your MPD port (default: 6600)
-d\t\tRun with debug mode enabled
""")
if __name__ == "__main__":
for i in range(len(sys.argv)):
if sys.argv[i] == "-h":
host = sys.argv[i + 1]
if sys.argv[i] == "-p":
port = sys.argv[i + 1]
if sys.argv[i] == "-d":
debug = True
if sys.argv[i] == "--help":
help()
exit()
musnify = Musnify()
try:
musnify.start()
except KeyboardInterrupt:
pass
finally:
musnify.stop()