diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index e5e82d480..423cde2b8 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -107,17 +107,17 @@ class MPDClientWrapper(object): self.connect() return self.get(command, retries=retries - 1) - def playlist(self): - """Return the currently active playlist. Prefixes paths with the + def currentsong(self): + """Return the path to the currently playing song. Prefixes paths with the music_directory, to get the absolute path. """ - result = {} - for entry in self.get('playlistinfo'): + result = None + entry = self.get('currentsong') + if 'file' in entry: if not is_url(entry['file']): - result[entry['id']] = os.path.join( - self.music_directory, entry['file']) + result = os.path.join(self.music_directory, entry['file']) else: - result[entry['id']] = entry['file'] + result = entry['file'] return result def status(self): @@ -250,12 +250,16 @@ class MPDStats(object): self.now_playing = None def on_play(self, status): - playlist = self.mpd.playlist() - path = playlist.get(status['songid']) + + path = self.mpd.currentsong() if not path: return + if is_url(path): + self._log.info(u'playing stream {0}', displayable_path(path)) + return + played, duration = map(int, status['time'].split(':', 1)) remaining = duration - played @@ -272,14 +276,6 @@ class MPDStats(object): if diff <= self.time_threshold: return - if self.now_playing['path'] == path and played == 0: - self.handle_song_change(self.now_playing) - - if is_url(path): - self._log.info(u'playing stream {0}', displayable_path(path)) - self.now_playing = None - return - self._log.info(u'playing {0}', displayable_path(path)) self.now_playing = { diff --git a/docs/changelog.rst b/docs/changelog.rst index ce04d93b4..b2c8437b6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -190,6 +190,11 @@ Fixes: :bug:`3200` * :doc:`/plugins/lastgenre`: The `force` config option now actually works. :bug:`2704` :bug:`3054` +* :doc:`/plugins/mpdstats`: Use the ``currentsong`` MPD command instead of + ``playlist`` to get the current song, improving performance when the playlist + is long. + Thanks to :user:`ray66`. + :bug:`3207` :bug:`2752` .. _python-itunes: https://github.com/ocelma/python-itunes diff --git a/test/test_mpdstats.py b/test/test_mpdstats.py index 7452be86b..0117e22aa 100644 --- a/test/test_mpdstats.py +++ b/test/test_mpdstats.py @@ -65,7 +65,7 @@ class MPDStatsTest(unittest.TestCase, TestHelper): @patch("beetsplug.mpdstats.MPDClientWrapper", return_value=Mock(**{ "events.side_effect": EVENTS, "status.side_effect": STATUSES, - "playlist.return_value": {1: item_path}})) + "currentsong.return_value": item_path})) def test_run_mpdstats(self, mpd_mock): item = Item(title=u'title', path=self.item_path, id=1) item.add(self.lib)