1 #! /usr/bin/env python
\r
2 # -*- coding: utf-8 -*-
\r
4 # Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
\r
6 # This program is free software: you can redistribute it and/or modify
\r
7 # it under the terms of the GNU General Public License as published by
\r
8 # the Free Software Foundation, either version 3 of the License, or
\r
9 # (at your option) any later version.
\r
11 # This program is distributed in the hope that it will be useful,
\r
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 # GNU General Public License for more details.
\r
16 # You should have received a copy of the GNU General Public License
\r
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
24 The script randomly pick up a song to queue from the library.
\r
25 JINGLE_TAG variable allows you to filter the list of songs the
\r
26 choice is made from.
\r
28 Adjust MPD settings and TAG filtering editing config.py.
\r
30 JINGLE_TAG is a dictionary, ie. a list of "key:value" couples, keys
\r
31 among "artist", "title", "genre", "comment" and many others (cf.
\r
34 The search made is a non exact search (search vs. find MPD command),
\r
35 it allows you /some/ fuzziness in JINGLE_TAG settings.
\r
37 To retrieve easily jingles with this script you may set the
\r
38 "comment" tag to "my jingle" for all your jingle sound files (or
\r
39 anything else you want as long it is a unique identifier for your
\r
40 jingles). Then set up JINGLE_TAG to fit this choice:
\r
41 JINGLE_TAG = {'comment': 'my jingle'}
\r
44 Be sure tags your using in JINGLE_TAG are among tag types being
\r
45 extracted during audio track discovery in your MPD server settings.
\r
46 Refer to “metadata_to_use” option in MPD server configuration.
\r
48 An alternate way is to pick up songs from a specific directory
\r
49 setting "filename" key in JINGLE_TAG:
\r
50 JINGLE_TAG = {'filename': 'partial/path/to/my jingle'}
\r
51 JINGLE_TAG = {'filename': 'jingle'}
\r
54 Refer to config.py file for some examples.
\r
57 MPD 0.15 is not correctly updating file tags, so you migth set
\r
58 comment or another tag to filter jingles and not been able to get
\r
59 files queued with this script because of that. Please check your
\r
60 tag is known in MPD database running something like that:
\r
62 mpc search <tag> <my value>
\r
64 Issue fixed in MPD 0.16
\r
65 MPD bugs: http://musicpd.org/mantis/view.php?id=498
\r
68 __version__ = u'0.3'
\r
69 __author__ = u'$Author: kaliko $'
\r
70 __date__ = u'$LastChangedDate: 2009-11-17 19:23:34 +0100 (mar. 17 nov. 2009) $'[18:28]
\r
76 from random import choice
\r
78 from mpd import (CommandError)
\r
79 except ImportError, err:
\r
80 print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err
\r
84 from config import JINGLE_TAG
\r
85 except ImportError, err:
\r
86 print 'ERROR: "%s"\n' % err
\r
87 print '\tPlease set JINGLE_TAG in config.py'
\r
90 from lib.mpdutils import (mconnect)
\r
94 def search(tags):#{{{
\r
96 Construct search string for MPD search function.
\r
99 for k, v in tags.iteritems():
\r
100 search_str.extend([str(k), str(v)])
\r
101 return search_str#}}}
\r
105 client = mconnect()
\r
106 ## List tracks matching JINGLE_TAG and randomly chose one.
\r
108 trk = choice(client.search(*search(JINGLE_TAG)))
\r
109 except CommandError, err:
\r
110 print 'Wrong search command, check JINGLE_TAG settings: %s' % err
\r
112 except IndexError, err:
\r
113 print ('Search for "%s" returned nothing: %s' %
\r
114 (' '.join(search(JINGLE_TAG)), err))
\r
117 ## Uncomment to print out file added to playlist
\r
118 #print 'add "%s"' % trk.get('file')
\r
120 client.add(trk.get('file', None))
\r
121 client.disconnect()
\r
124 # Script starts here
\r
125 if __name__ == "__main__":
\r
126 if len(sys.argv) > 1:
\r
132 # vim: ai ts=4 sw=4 sts=4 expandtab
\r