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.2'
\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
77 from socket import error as SocketError
\r
79 from mpd import (MPDClient, CommandError)
\r
80 except ImportError, err:
\r
81 print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err
\r
85 from config import (HOST, PORT, PASSWORD, JINGLE_TAG)
\r
86 except ImportError, err:
\r
87 print 'ERROR: missing settings in config.py: %s' % err
\r
92 ## Formating connection id (do not change this)
\r
93 CON_ID = {'host': HOST, 'port': PORT}
\r
95 ## Some functions#{{{
\r
98 def mpdConnect(client, con_id):#{{{
\r
100 Simple wrapper to connect MPD.
\r
103 client.connect(**con_id)
\r
104 except SocketError:
\r
109 def mpdAuth(client, secret):#{{{
\r
114 client.password(secret)
\r
115 except CommandError:
\r
120 def search(tags):#{{{
\r
122 Construct search string for MPD search function.
\r
124 search_str = list()
\r
125 for k, v in tags.iteritems():
\r
126 search_str.extend([str(k), str(v)])
\r
127 return search_str#}}}
\r
132 ## MPD object instance
\r
133 client = MPDClient()
\r
134 if mpdConnect(client, CON_ID):
\r
135 #print 'Got connected!'
\r
138 print 'ERROR: fail to connect MPD server.'
\r
141 ## Auth if password is set non False
\r
143 if mpdAuth(client, PASSWORD):
\r
144 #print 'Pass auth!'
\r
147 print 'ERROR: fail trying to pass auth. Check password?'
\r
148 client.disconnect()
\r
151 ## List tracks matching JINGLE_TAG and randomly chose one.
\r
153 trk = choice(client.search(*search(JINGLE_TAG)))
\r
154 except CommandError, err:
\r
155 print 'Wrong search command, check JINGLE_TAG settings: %s' % err
\r
157 except IndexError, err:
\r
158 print 'Search for "%s" returned nothing: %s' % (' '.join(search(JINGLE_TAG)),
\r
162 ## Uncomment to print out file added to playlist
\r
163 #print 'add "%s"' % trk.get('file')
\r
165 client.add(trk.get('file', None))
\r
167 client.disconnect()
\r
171 # Script starts here
\r
172 if __name__ == "__main__":
\r
176 # vim: ai ts=4 sw=4 sts=4 expandtab
\r