1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # Copyright © 2005, Simon E. Ward |
---|
5 | # |
---|
6 | # This program is free software; you can redistribute it and/or |
---|
7 | # modify it under the terms of version 2 of the GNU General Public |
---|
8 | # License as published by the Free Software Foundation. |
---|
9 | # |
---|
10 | # This program is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | # General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU General Public License |
---|
16 | # along with this program; if not, write to the Free Software |
---|
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
18 | # 02110-1301, USA |
---|
19 | |
---|
20 | from os.path import sep, splitext |
---|
21 | from sre import compile |
---|
22 | import sys |
---|
23 | state_map = { |
---|
24 | 'play': u'Playing', |
---|
25 | 'pause': u'Paused', |
---|
26 | 'stop': u'Stopped' |
---|
27 | } |
---|
28 | fields = ['artist', 'album', 'track', 'title'] |
---|
29 | strip_paths = [ |
---|
30 | u'albums', |
---|
31 | u'classical', |
---|
32 | u'houseofdoom', |
---|
33 | u'mods', |
---|
34 | u'original', |
---|
35 | u'soundtracks' |
---|
36 | ] |
---|
37 | |
---|
38 | strip_expr = compile(u'(%s)/' % ur'|'.join(strip_paths)) |
---|
39 | |
---|
40 | def get_status(connection): |
---|
41 | songinfo = {} |
---|
42 | songinfo['state'] = state_map[connection.status()['state']] |
---|
43 | song = connection.currentsong() |
---|
44 | songinfo['song'] = \ |
---|
45 | u' - '.join([unicode(song[f]) |
---|
46 | for f in fields |
---|
47 | if song.has_key(f)]) \ |
---|
48 | or unicode(strip_expr.sub(u'', |
---|
49 | splitext(unicode(song['file']))[0]).replace(sep, u' - ')) |
---|
50 | return songinfo |
---|
51 | |
---|
52 | if __name__ == '__main__': |
---|
53 | import locale, codecs |
---|
54 | from mpdclient2 import connect |
---|
55 | enc = locale.getpreferredencoding() |
---|
56 | sys.stdout = codecs.getwriter(enc)(sys.__stdout__) |
---|
57 | format = u'♫ %(state)s: %(song)s' |
---|
58 | print format % get_status(connect()) |
---|