dr.botzo/mpdbot/ircplugin.py

64 lines
2.1 KiB
Python
Raw Normal View History

"""Access to and interface with mpd through bot commands."""
import logging
from django.conf import settings
from mpd import MPDClient
from ircbot.lib import Plugin
log = logging.getLogger(__name__)
class MpdBot(Plugin):
"""Access to and interface with mpd through bot commands."""
playing_regex = (r'(what\s+is|what\'s)\s+playing\?$')
def start(self):
"""Set up handlers."""
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.playing_regex,
self.handle_playing, -20)
super(MpdBot, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_playing)
super(MpdBot, self).stop()
def handle_playing(self, connection, event, match):
"""Watch IRC for inquiries about the playing track in mpd."""
log.debug("in Mpd.handle_playing")
if event.in_privmsg or event.addressed:
client = MPDClient(use_unicode=True)
client.connect(settings.MPD_HOST, settings.MPD_PORT)
if settings.MPD_PASS:
client.password(settings.MPD_PASS)
status = client.status()
log.debug(status)
state = status.get('state', None)
if state == 'play':
current = client.currentsong()
log.debug(current)
filename = current.get('file', None)
if filename:
artist = current.get('artist', "Unknown")
title = current.get('title', "Unknown")
album = current.get('album', "Unknown")
np = "{0:s} - {1:s} [{2:s}] 14[{3:s}]".format(artist, title, album, settings.MPD_SITE)
return self.bot.reply(event, np, stop=True)
else:
return self.bot.reply(event, "something's playing, but i don't know what!?", stop=True)
else:
return self.bot.reply(event, "nothing is playing", stop=True)
plugin = MpdBot