Skip to content

Commit

Permalink
Merge pull request #3725 from aereaux/mpdstats_stop_no_skip
Browse files Browse the repository at this point in the history
mpdstats: Don't record a skip when stopping MPD.
  • Loading branch information
sampsyo authored Aug 10, 2020
2 parents ff445ed + fce27e6 commit eb6bbae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 9 additions & 5 deletions beetsplug/mpdstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ def get(self, command, retries=RETRIES):
return self.get(command, retries=retries - 1)

def currentsong(self):
"""Return the path to the currently playing song. Prefixes paths with the
music_directory, to get the absolute path.
"""Return the path to the currently playing song, along with its
songid. Prefixes paths with the music_directory, to get the absolute
path.
"""
result = None
entry = self.get('currentsong')
Expand All @@ -118,7 +119,7 @@ def currentsong(self):
result = os.path.join(self.music_directory, entry['file'])
else:
result = entry['file']
return result
return result, entry.get('id')

def status(self):
"""Return the current status of the MPD.
Expand Down Expand Up @@ -240,7 +241,9 @@ def handle_skipped(self, song):
def on_stop(self, status):
self._log.info(u'stop')

if self.now_playing:
# if the current song stays the same it means that we stopped on the
# current track and should not record a skip.
if self.now_playing and self.now_playing['id'] != status.get('songid'):
self.handle_song_change(self.now_playing)

self.now_playing = None
Expand All @@ -251,7 +254,7 @@ def on_pause(self, status):

def on_play(self, status):

path = self.mpd.currentsong()
path, songid = self.mpd.currentsong()

if not path:
return
Expand Down Expand Up @@ -286,6 +289,7 @@ def on_play(self, status):
'started': time.time(),
'remaining': remaining,
'path': path,
'id': songid,
'beets_item': self.get_item(path),
}

Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ Fixes:
* Fix a bug that caused metadata starting with something resembling a drive
letter to be incorrectly split into an extra directory after the colon.
:bug:`3685`
* :doc:`/plugins/mpdstats`: Don't record a skip when stopping MPD, as MPD keeps
the current track in the queue.
Thanks to :user:`aereaux`.
:bug:`3722`

For plugin developers:

Expand Down
3 changes: 2 additions & 1 deletion test/test_mpdstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ def test_get_item(self):
{'state': u'stop'}]
EVENTS = [["player"]] * (len(STATUSES) - 1) + [KeyboardInterrupt]
item_path = util.normpath('/foo/bar.flac')
songid = 1

@patch("beetsplug.mpdstats.MPDClientWrapper", return_value=Mock(**{
"events.side_effect": EVENTS, "status.side_effect": STATUSES,
"currentsong.return_value": item_path}))
"currentsong.return_value": (item_path, songid)}))
def test_run_mpdstats(self, mpd_mock):
item = Item(title=u'title', path=self.item_path, id=1)
item.add(self.lib)
Expand Down

0 comments on commit eb6bbae

Please sign in to comment.