From ce5981b885d6619b0b72272e4072962679d6723d Mon Sep 17 00:00:00 2001 From: Rainer Unseld Date: Tue, 5 Dec 2017 17:20:00 +0000 Subject: [PATCH 1/3] mpdstats: use currentsong instead of playlist Improved the method to get the path of the current song. Before, the complete playlist was fetched from the server. Now, the command "currentsong" is used for this purpose. This improves performance when a huge playlist is active. --- beetsplug/mpdstats.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) 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 = { From 4158bdb95eeeb3e2130d82f31533d6bfbec0863c Mon Sep 17 00:00:00 2001 From: Carl Suster Date: Sat, 6 Apr 2019 15:31:03 +1100 Subject: [PATCH 2/3] mpdstats: update tests for currentsong --- test/test_mpdstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From f35eda717be19b67e32d3b01b1491494bec50f93 Mon Sep 17 00:00:00 2001 From: Carl Suster Date: Sat, 6 Apr 2019 15:36:52 +1100 Subject: [PATCH 3/3] Changelog for #3207 --- docs/changelog.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1acfdb9c3..5de1fae6d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -188,6 +188,11 @@ Fixes: :bug:`3201` :bug:`3202` * :doc:`/plugins/bpd`: Fix crashes in the bpd server during exception handling. :bug:`3200` +* :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