From 0fe7879b16ac9b5ff3b7a0560fa9ace7bf09ab4b Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Sat, 13 Jan 2024 16:09:15 +0100 Subject: [PATCH] SpotifyRecorder: handle unplayable songs --- SpotifyRecorder.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/SpotifyRecorder.py b/SpotifyRecorder.py index 9aab8d6..899a31c 100644 --- a/SpotifyRecorder.py +++ b/SpotifyRecorder.py @@ -111,7 +111,7 @@ class SpotifyRecorder: recorder = SpotifyAudioRecorder(self._audioDeviceName, filePath) with recorder.record(): self.__play_track(deviceId, track['track']['uri']) - timeWaitedForPlaying = self.__wait_for_track_playing() + timeWaitedForPlaying = self.__wait_for_track_playing(track['track']['id']) self.__wait_for_track_end(track, timeWaitedForPlaying) recordedTracks.append(track['track']['name']) @@ -182,15 +182,19 @@ class SpotifyRecorder: def __play_track(self, deviceId: str, trackUri: str): self._spotify.start_playback(device_id=deviceId, uris=[trackUri]) - def __wait_for_track_playing(self) -> float: + def __wait_for_track_playing(self, expectedTrackId: str) -> float: LOGGER.debug(f'\t\tWait for track to start playing...') startTime = time.time() duration = 0 while time.time() - startTime < self._MAX_WAIT_TIME_TRACK_STARTING_IN_S: - if self._spotify.current_playback()['is_playing']: + currentPlayback = self._spotify.current_playback() + if currentPlayback['is_playing']: duration = time.time() - startTime - LOGGER.debug(f'\t\tTrack started playing after {duration:.1f}s') - break + if currentPlayback['item']['id'] == expectedTrackId: + LOGGER.debug(f'\t\tTrack started playing after {duration:.1f}s') + break + else: + raise RuntimeError('Wrong track started playing') time.sleep(1) -- GitLab