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 subprocess import Popen, PIPE |
---|
21 | from os.path import basename, splitext |
---|
22 | |
---|
23 | mpc_fmt = ( |
---|
24 | "{" |
---|
25 | "'artist': '''[%artist% - ]''', " |
---|
26 | "'album': '''[%album% - ]''', " |
---|
27 | "'track': '''[%track% - ]''', " |
---|
28 | "'title': '''[%title%]''', " |
---|
29 | "'time': '''[%time%]''', " |
---|
30 | "'file': '''[%file%]'''" |
---|
31 | "}" |
---|
32 | ) |
---|
33 | cmd = ['mpc', '--format', mpc_fmt] |
---|
34 | lines = Popen(cmd, stdout=PIPE).communicate()[0].split('\n') |
---|
35 | |
---|
36 | if len(lines) >= 3: |
---|
37 | fields = eval(lines[0]) |
---|
38 | line1 = lines[1].split() |
---|
39 | fields.update(zip(['status', 'pnum', 'length', 'percent'], line1)) |
---|
40 | status = fields['status'].strip('[]').capitalize() + ':' |
---|
41 | if fields['title']: |
---|
42 | title = '%(artist)s%(album)s%(track)s%(title)s' % fields |
---|
43 | else: |
---|
44 | title = splitext(basename(fields['file']))[0] |
---|
45 | time = '%(time)s/%(length)s %(percent)s' % fields |
---|
46 | outstr = '%s %s [%s]' % (status, title, time) |
---|
47 | else: |
---|
48 | outstr = '--Not playing--' |
---|
49 | |
---|
50 | print outstr |
---|
51 | |
---|