--- /dev/null
+#! /usr/bin/env python\r
+# -*- coding: utf-8 -*-\r
+\r
+# Copyright (c) 2009 Efrim <efrim@azylum.org> {{{\r
+#\r
+# This program is free software: you can redistribute it and/or modify\r
+# it under the terms of the GNU General Public License as published by\r
+# the Free Software Foundation, either version 3 of the License, or\r
+# (at your option) any later version.\r
+#\r
+# This program is distributed in the hope that it will be useful,\r
+# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+# GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License\r
+# along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+#\r
+# }}}\r
+\r
+"""\r
+DOC:\r
+\r
+ The script randomly pick up a song to queue from the library.\r
+ JINGLE_TAG variable allows you to filter the list of songs the\r
+ choice is made from.\r
+\r
+ Adjust MPD settings and TAG filtering editing config.py.\r
+\r
+ JINGLE_TAG is a dictionary, ie. a list of "key:value" couples, keys\r
+ among "artist", "title", "genre", "comment" and many others (cf.\r
+ man 1 mpc).\r
+\r
+ The search made is a non exact search (search vs. find MPD command),\r
+ it allows you /some/ fuzziness in JINGLE_TAG settings.\r
+\r
+ To retrieve easily jingles with this script you may set the\r
+ "comment" tag to "my jingle" for all your jingle sound files (or\r
+ anything else you want as long it is a unique identifier for your\r
+ jingles). Then set up JINGLE_TAG to fit this choice:\r
+ JINGLE_TAG = {'comment': 'my jingle'}\r
+\r
+ PAY ATTENTION:\r
+ Be sure tags your using in JINGLE_TAG are among tag types being\r
+ extracted during audio track discovery in your MPD server settings.\r
+ Refer to “metadata_to_use” option in MPD server configuration.\r
+\r
+ An alternate way is to pick up songs from a specific directory\r
+ setting "filename" key in JINGLE_TAG:\r
+ JINGLE_TAG = {'filename': 'partial/path/to/my jingle'}\r
+ JINGLE_TAG = {'filename': 'jingle'}\r
+ …etc.\r
+\r
+ Refer to config.py file for some examples.\r
+\r
+BUGS:\r
+ MPD 0.15 is not correctly updating file tags, so you migth set\r
+ comment or another tag to filter jingles and not been able to get\r
+ files queued with this script because of that. Please check your\r
+ tag is known in MPD database running something like that:\r
+\r
+ mpc search <tag> <my value>\r
+\r
+ Issue fixed in MPD 0.16\r
+ MPD bugs: http://musicpd.org/mantis/view.php?id=498\r
+"""\r
+\r
+__version__ = u'0.2'\r
+__author__ = u'$Author: kaliko $'\r
+__date__ = u'$LastChangedDate: 2009-11-17 19:23:34 +0100 (mar. 17 nov. 2009) $'[18:28]\r
+\r
+\r
+# IMPORTS {{{\r
+import sys\r
+\r
+from random import choice\r
+from socket import error as SocketError\r
+try:\r
+ from mpd import (MPDClient, CommandError)\r
+except ImportError, err:\r
+ print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err\r
+ sys.exit(1)\r
+\r
+try:\r
+ from config import (HOST, PORT, PASSWORD, JINGLE_TAG)\r
+except ImportError, err:\r
+ print 'ERROR: missing settings in config.py: %s' % err\r
+ sys.exit(1)\r
+#}}}\r
+\r
+\r
+## Formating connection id (do not change this)\r
+CON_ID = {'host': HOST, 'port': PORT}\r
+\r
+## Some functions#{{{\r
+\r
+\r
+def mpdConnect(client, con_id):#{{{\r
+ """\r
+ Simple wrapper to connect MPD.\r
+ """\r
+ try:\r
+ client.connect(**con_id)\r
+ except SocketError:\r
+ return False\r
+ return True#}}}\r
+\r
+\r
+def mpdAuth(client, secret):#{{{\r
+ """\r
+ Authenticate\r
+ """\r
+ try:\r
+ client.password(secret)\r
+ except CommandError:\r
+ return False\r
+ return True#}}}\r
+\r
+\r
+def search(tags):#{{{\r
+ """\r
+ Construct search string for MPD search function.\r
+ """\r
+ search_str = list()\r
+ for k, v in tags.iteritems():\r
+ search_str.extend([str(k), str(v)])\r
+ return search_str#}}}\r
+###}}}\r
+\r
+\r
+def main():\r
+ ## MPD object instance\r
+ client = MPDClient()\r
+ if mpdConnect(client, CON_ID):\r
+ #print 'Got connected!'\r
+ True\r
+ else:\r
+ print 'ERROR: fail to connect MPD server.'\r
+ sys.exit(1)\r
+\r
+ ## Auth if password is set non False\r
+ if PASSWORD:\r
+ if mpdAuth(client, PASSWORD):\r
+ #print 'Pass auth!'\r
+ True\r
+ else:\r
+ print 'ERROR: fail trying to pass auth. Check password?'\r
+ client.disconnect()\r
+ sys.exit(1)\r
+\r
+ ## List tracks matching JINGLE_TAG and randomly chose one.\r
+ try:\r
+ trk = choice(client.search(*search(JINGLE_TAG)))\r
+ except CommandError, err:\r
+ print 'Wrong search command, check JINGLE_TAG settings: %s' % err\r
+ sys.exit(1)\r
+ except IndexError, err:\r
+ print 'Search for "%s" returned nothing: %s' % (' '.join(search(JINGLE_TAG)),\r
+ err)\r
+ sys.exit(1)\r
+\r
+ ## Uncomment to print out file added to playlist\r
+ #print 'add "%s"' % trk.get('file')\r
+\r
+ client.add(trk.get('file', None))\r
+\r
+ client.disconnect()\r
+\r
+ sys.exit(0)\r
+\r
+# Script starts here\r
+if __name__ == "__main__":\r
+ main()\r
+\r
+# VIM MODLINE\r
+# vim: ai ts=4 sw=4 sts=4 expandtab\r