]> kaliko git repositories - mpd-goodies.git/blobdiff - misc/jingle.py
* reorganized source.
[mpd-goodies.git] / misc / jingle.py
diff --git a/misc/jingle.py b/misc/jingle.py
new file mode 100755 (executable)
index 0000000..3d25dd6
--- /dev/null
@@ -0,0 +1,132 @@
+#! /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
+DOC = """\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.3'\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
+try:\r
+    from mpd import (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 JINGLE_TAG\r
+except ImportError, err:\r
+    print 'ERROR: "%s"\n' % err\r
+    print '\tPlease set JINGLE_TAG in config.py'\r
+    sys.exit(1)\r
+\r
+from lib.mpdutils import (mconnect)\r
+#}}}\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
+def main():\r
+    client = mconnect()\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' %\r
+               (' '.join(search(JINGLE_TAG)), 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
+    client.disconnect()\r
+    sys.exit(0)\r
+\r
+# Script starts here\r
+if __name__ == "__main__":\r
+    if len(sys.argv) > 1:\r
+        print DOC\r
+        sys.exit(0)\r
+    main()\r
+\r
+# VIM MODLINE\r
+# vim: ai ts=4 sw=4 sts=4 expandtab\r