Changeset 27 for mpd-status/trunk


Ignore:
Timestamp:
Feb 17, 2007, 3:49:56 PM (18 years ago)
Author:
simon
Message:

Rewrite to use mpdclient instead of invoking the command-line mpc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mpd-status/trunk/mpd-status.py

    r26 r27  
    1818# 02110-1301, USA
    1919
    20 from subprocess import Popen, PIPE
    21 from os.path import basename, splitext
     20from mpdclient2 import connect
     21from os.path import sep, splitext
     22from sre import compile
    2223
    23 mpc_fmt = '[[%artist% - ][%album% - ][%track% - ]%title%]\n[%file%]'
    24 cmd = ['mpc', '--format', mpc_fmt]
    25 lines = Popen(cmd, stdout=PIPE).communicate()[0].split('\n')
    26 outstr = lines[0] or splitext(basename(lines[1]))[0]
     24state_map = {
     25    'play': 'Playing',
     26    'pause': 'Paused',
     27    'stop': 'Stopped'
     28    }
     29fields = ['artist', 'album', 'track', 'title']
     30format = '♫ %(state)s: %(song)s'
     31strip_paths = [
     32    'albums',
     33    'classical',
     34    'houseofdoom',
     35    'mods',
     36    'original',
     37    'soundtracks'
     38    ]
    2739
    28 if len(lines) >= 4:
    29     (status, pnum, time, percent) = lines[2].split()
    30     outstr = status + ' ' + outstr
     40strip_expr = compile('(%s)/' % r'|'.join(strip_paths))
    3141
    32 print outstr
    33 
     42songinfo = {}
     43mpc = connect()
     44songinfo['state'] = state_map[mpc.status()['state']]
     45song = mpc.currentsong()
     46songinfo['song'] = \
     47    ' - '.join([song[f] for f in fields if song.has_key(f)]) \
     48    or strip_expr.sub('', splitext(song['file'])[0]).replace(sep, ' - ')
     49print format % songinfo
Note: See TracChangeset for help on using the changeset viewer.