From 4fa7a080068ca30c6f15d7c9286eaf507930d04c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 13 Jan 2018 02:52:06 +0100 Subject: [PATCH] Added mpd backend --- platypush/backend/music/__init__.py | 0 platypush/backend/music/mpd/__init__.py | 61 +++++++++++++++++++++++ platypush/message/event/music/__init__.py | 32 ++++++++++++ platypush/plugins/music/mpd/__init__.py | 4 ++ 4 files changed, 97 insertions(+) create mode 100644 platypush/backend/music/__init__.py create mode 100644 platypush/backend/music/mpd/__init__.py create mode 100644 platypush/message/event/music/__init__.py diff --git a/platypush/backend/music/__init__.py b/platypush/backend/music/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/platypush/backend/music/mpd/__init__.py b/platypush/backend/music/mpd/__init__.py new file mode 100644 index 000000000..8b3b9632b --- /dev/null +++ b/platypush/backend/music/mpd/__init__.py @@ -0,0 +1,61 @@ +import logging +import re +import time + +from platypush.backend import Backend +from platypush.context import get_plugin +from platypush.message.event.music import \ + MusicPlayEvent, MusicPauseEvent, MusicStopEvent, NewPlayingTrackEvent + + +class MusicMpdBackend(Backend): + def __init__(self, server='localhost', port=6600, poll_seconds=3, **kwargs): + super().__init__(**kwargs) + + self.server = server + self.port = port + self.poll_seconds = poll_seconds + + + def send_message(self, msg): + pass + + + def run(self): + super().run() + + plugin = get_plugin('music.mpd') + last_state = None + last_track = None + + while not self.should_stop(): + status = plugin.status().output + state = status['state'].lower() + track = plugin.currentsong().output + + if state != last_state: + if state == 'stop': + self.bus.post(MusicStopEvent(status=status, track=track)) + elif state == 'pause': + self.bus.post(MusicPauseEvent(status=status, track=track)) + elif state == 'play': + self.bus.post(MusicPlayEvent(status=status, track=track)) + + if 'title' in track and ('artist' not in track + or not track['artist'] + or re.search('^tunein:', track_info['file'])): + m = re.match('^\s*(.+?)\s+-\s+(.*)\s*$', track['title']) + if m and m.group(1) and m.group(2): + track['artist'] = m.group(1) + track['title'] = m.group(2) + + if state == 'play' and track != last_track: + self.bus.post(NewPlayingTrackEvent(status=status, track=track)) + + last_state = state + last_track = track + time.sleep(self.poll_seconds) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/message/event/music/__init__.py b/platypush/message/event/music/__init__.py new file mode 100644 index 000000000..0f95746fd --- /dev/null +++ b/platypush/message/event/music/__init__.py @@ -0,0 +1,32 @@ +from platypush.message.event import Event + + +class MusicEvent(Event): + """ Base class for music events """ + + def __init__(self, status, track, *args, **kwargs): + super().__init__(*args, status=status, track=track, **kwargs) + + +class MusicPlayEvent(MusicEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +class MusicStopEvent(MusicEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +class MusicPauseEvent(MusicEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +class NewPlayingTrackEvent(MusicEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/plugins/music/mpd/__init__.py b/platypush/plugins/music/mpd/__init__.py index b0f4c8221..7298607a5 100644 --- a/platypush/plugins/music/mpd/__init__.py +++ b/platypush/plugins/music/mpd/__init__.py @@ -83,5 +83,9 @@ class MusicMpdPlugin(MusicPlugin): def status(self): return Response(output=self.client.status()) + def currentsong(self): + return Response(output=self.client.currentsong()) + + # vim:sw=4:ts=4:et: