From 2ab1f3ae89eb74c43acbcf8addfcdb5a52b31106 Mon Sep 17 00:00:00 2001 From: Artem Utin Date: Mon, 23 Jan 2017 23:15:14 +1000 Subject: [PATCH] More general approach to multiple on_play calls for the same song - now it ignores such calls, if time between calls was below threshold --- beetsplug/mpdstats.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 896a34415..9f63181b9 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -263,25 +263,27 @@ class MPDStats(object): played, duration = map(int, status['time'].split(':', 1)) remaining = duration - played - if self.now_playing and self.now_playing['path'] != path: - skipped = self.handle_song_change(self.now_playing) - # mpd responds twice on a natural new song start - going_to_happen_twice = not skipped - else: - going_to_happen_twice = False + if self.now_playing: + if self.now_playing['path'] != path: + self.handle_song_change(self.now_playing) + else: # in case we got mpd play event with same song playing multiple times + # assume low diff means redundant second play event after natural song start + diff = abs(time.time() - self.now_playing['started']) - if not going_to_happen_twice: - self._log.info(u'playing {0}', displayable_path(path)) + if diff <= self.time_threshold: + return - self.now_playing = { - 'started': time.time(), - 'remaining': remaining, - 'path': path, - 'beets_item': self.get_item(path), - } + self._log.info(u'playing {0}', displayable_path(path)) - self.update_item(self.now_playing['beets_item'], - 'last_played', value=int(time.time())) + self.now_playing = { + 'started': time.time(), + 'remaining': remaining, + 'path': path, + 'beets_item': self.get_item(path), + } + + self.update_item(self.now_playing['beets_item'], + 'last_played', value=int(time.time())) def run(self): self.mpd.connect()