]> kaliko git repositories - mpd-goodies.git/blob - jingle.py
Initial import
[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 """\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.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
71 \r
72 \r
73 # IMPORTS {{{\r
74 import sys\r
75 \r
76 from random import choice\r
77 from socket import error as SocketError\r
78 try:\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
82     sys.exit(1)\r
83 \r
84 try:\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
88     sys.exit(1)\r
89 #}}}\r
90 \r
91 \r
92 ## Formating connection id (do not change this)\r
93 CON_ID = {'host': HOST, 'port': PORT}\r
94 \r
95 ## Some functions#{{{\r
96 \r
97 \r
98 def mpdConnect(client, con_id):#{{{\r
99     """\r
100     Simple wrapper to connect MPD.\r
101     """\r
102     try:\r
103         client.connect(**con_id)\r
104     except SocketError:\r
105         return False\r
106     return True#}}}\r
107 \r
108 \r
109 def mpdAuth(client, secret):#{{{\r
110     """\r
111     Authenticate\r
112     """\r
113     try:\r
114         client.password(secret)\r
115     except CommandError:\r
116         return False\r
117     return True#}}}\r
118 \r
119 \r
120 def search(tags):#{{{\r
121     """\r
122     Construct search string for MPD search function.\r
123     """\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
128 ###}}}\r
129 \r
130 \r
131 def main():\r
132     ## MPD object instance\r
133     client = MPDClient()\r
134     if mpdConnect(client, CON_ID):\r
135         #print 'Got connected!'\r
136         True\r
137     else:\r
138         print 'ERROR: fail to connect MPD server.'\r
139         sys.exit(1)\r
140 \r
141     ## Auth if password is set non False\r
142     if PASSWORD:\r
143         if mpdAuth(client, PASSWORD):\r
144             #print 'Pass auth!'\r
145             True\r
146         else:\r
147             print 'ERROR: fail trying to pass auth. Check password?'\r
148             client.disconnect()\r
149             sys.exit(1)\r
150 \r
151     ## List tracks matching JINGLE_TAG and randomly chose one.\r
152     try:\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
156         sys.exit(1)\r
157     except IndexError, err:\r
158         print 'Search for "%s" returned nothing: %s' % (' '.join(search(JINGLE_TAG)),\r
159                                                         err)\r
160         sys.exit(1)\r
161 \r
162     ## Uncomment to print out file added to playlist\r
163     #print 'add "%s"' % trk.get('file')\r
164 \r
165     client.add(trk.get('file', None))\r
166 \r
167     client.disconnect()\r
168 \r
169     sys.exit(0)\r
170 \r
171 # Script starts here\r
172 if __name__ == "__main__":\r
173     main()\r
174 \r
175 # VIM MODLINE\r
176 # vim: ai ts=4 sw=4 sts=4 expandtab\r