source: mpd-status/trunk/mpd-status.py @ 28

Last change on this file since 28 was 28, checked in by simon, 18 years ago

Stuck the status string grabby thing into a function so I could use it as a
module.

File size: 1.5 KB
Line 
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
20from os.path import sep, splitext
21from sre import compile
22
23state_map = {
24    'play': 'Playing',
25    'pause': 'Paused',
26    'stop': 'Stopped'
27    }
28fields = ['artist', 'album', 'track', 'title']
29format = '♫ %(state)s: %(song)s'
30strip_paths = [
31    'albums',
32    'classical',
33    'houseofdoom',
34    'mods',
35    'original',
36    'soundtracks'
37    ]
38
39strip_expr = compile('(%s)/' % r'|'.join(strip_paths))
40
41def get_status(connection):
42    songinfo = {}
43    songinfo['state'] = state_map[connection.status()['state']]
44    song = connection.currentsong()
45    songinfo['song'] = \
46        ' - '.join([song[f] for f in fields if song.has_key(f)]) \
47        or strip_expr.sub('', splitext(song['file'])[0]).replace(sep, ' - ')
48    return format % songinfo
49
50if __name__ == '__main__':
51    from mpdclient2 import connect
52    print get_status(connect())
Note: See TracBrowser for help on using the repository browser.