]> kaliko git repositories - mpd-goodies.git/blob - jingle.py
* fixes some bugs on mtopls
[mpd-goodies.git] / jingle.py
1 #! /usr/bin/env python\r
2 # -*- coding: utf-8 -*-\r
3 \r
4 # Copyright (c) 2009 Efrim <efrim@azylum.org> {{{\r
5 #\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
10 #\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
15 #\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
18 #\r
19 #  }}}\r
20 \r
21 DOC = """\r
22 DOC:\r
23 \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
27 \r
28     Adjust MPD settings and TAG filtering editing config.py.\r
29 \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
32     man 1 mpc).\r
33 \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
36 \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
42 \r
43     PAY ATTENTION:\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
47 \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
52         …etc.\r
53 \r
54     Refer to config.py file for some examples.\r
55 \r
56 BUGS:\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
61 \r
62         mpc search <tag> <my value>\r
63 \r
64     Issue fixed in MPD 0.16\r
65     MPD bugs: http://musicpd.org/mantis/view.php?id=498\r
66 """\r
67 \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
71 \r
72 \r
73 # IMPORTS {{{\r
74 import sys\r
75 \r
76 from random import choice\r
77 try:\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
81     sys.exit(1)\r
82 \r
83 try:\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
88     sys.exit(1)\r
89 \r
90 from lib.mpdutils import (mconnect)\r
91 #}}}\r
92 \r
93 \r
94 def search(tags):#{{{\r
95     """\r
96     Construct search string for MPD search function.\r
97     """\r
98     search_str = list()\r
99     for k, v in tags.iteritems():\r
100         search_str.extend([str(k), str(v)])\r
101     return search_str#}}}\r
102 \r
103 \r
104 def main():\r
105     client = mconnect()\r
106     ## List tracks matching JINGLE_TAG and randomly chose one.\r
107     try:\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
111         sys.exit(1)\r
112     except IndexError, err:\r
113         print ('Search for "%s" returned nothing: %s' %\r
114                (' '.join(search(JINGLE_TAG)), err))\r
115         sys.exit(1)\r
116 \r
117     ## Uncomment to print out file added to playlist\r
118     #print 'add "%s"' % trk.get('file')\r
119 \r
120     client.add(trk.get('file', None))\r
121     client.disconnect()\r
122     sys.exit(0)\r
123 \r
124 # Script starts here\r
125 if __name__ == "__main__":\r
126     if len(sys.argv) > 1:\r
127         print DOC\r
128         sys.exit(0)\r
129     main()\r
130 \r
131 # VIM MODLINE\r
132 # vim: ai ts=4 sw=4 sts=4 expandtab\r