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