From 217b8a15cd73a206ccf2ea0839e7c335c0918312 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 10 Mar 2017 20:36:56 -0600 Subject: [PATCH] simple mpd client thing gets now playing status --- dr_botzo/settings.py | 6 ++++ mpdbot/__init__.py | 1 + mpdbot/ircplugin.py | 63 +++++++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 1 + requirements-server.txt | 1 + requirements.in | 1 + requirements.txt | 1 + 7 files changed, 74 insertions(+) create mode 100644 mpdbot/__init__.py create mode 100644 mpdbot/ircplugin.py diff --git a/dr_botzo/settings.py b/dr_botzo/settings.py index 43f8ad9..d19dc40 100644 --- a/dr_botzo/settings.py +++ b/dr_botzo/settings.py @@ -184,6 +184,12 @@ IRCBOT_XMLRPC_PORT = 13132 KARMA_IGNORE_CHATTER_TARGETS = [] KARMA_IGNORE_COMMAND_TARGETS = [] +# mpd + +MPD_HOST = "" +MPD_PORT = None +MPD_PASS = None + # storycraft STORYCRAFT_MASTER_CHANNEL = '#dr.botzo' diff --git a/mpdbot/__init__.py b/mpdbot/__init__.py new file mode 100644 index 0000000..5c6f335 --- /dev/null +++ b/mpdbot/__init__.py @@ -0,0 +1 @@ +"""Interface with an mpd server.""" diff --git a/mpdbot/ircplugin.py b/mpdbot/ircplugin.py new file mode 100644 index 0000000..7d83d02 --- /dev/null +++ b/mpdbot/ircplugin.py @@ -0,0 +1,63 @@ +"""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}]".format(artist, title, album) + 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 diff --git a/requirements-dev.txt b/requirements-dev.txt index 0082d1d..ffed95c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -49,6 +49,7 @@ pylint==1.6.5 # via prospector, pylint-celery, pylint-common, pylint pyparsing==2.1.10 # via packaging python-dateutil==2.6.0 python-gitlab==0.18 +python-mpd2==0.5.5 pytz==2016.10 pyyaml==3.12 # via prospector requests-oauthlib==0.7.0 # via twython diff --git a/requirements-server.txt b/requirements-server.txt index 2e124c3..42a1eb4 100644 --- a/requirements-server.txt +++ b/requirements-server.txt @@ -27,6 +27,7 @@ ply==3.10 psycopg2==2.6.2 python-dateutil==2.6.0 python-gitlab==0.18 +python-mpd2==0.5.5 pytz==2016.10 requests-oauthlib==0.7.0 # via twython requests==2.13.0 # via python-gitlab, requests-oauthlib, twython diff --git a/requirements.in b/requirements.in index 511688d..dd0d320 100644 --- a/requirements.in +++ b/requirements.in @@ -9,5 +9,6 @@ parsedatetime # relative date stuff in countdown ply # dice lex/yacc compiler python-dateutil # countdown relative math python-gitlab # client for the gitlab bot +python-mpd2 # client for mpd pytz # timezone awareness twython # twitter client diff --git a/requirements.txt b/requirements.txt index 46a053c..b3c45e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,6 +26,7 @@ parsedatetime==2.2 ply==3.10 python-dateutil==2.6.0 python-gitlab==0.18 +python-mpd2==0.5.5 pytz==2016.10 requests-oauthlib==0.7.0 # via twython requests==2.13.0 # via python-gitlab, requests-oauthlib, twython