Skip to content
Snippets Groups Projects
Commit 4c77e89e authored by Robert Goldmann's avatar Robert Goldmann
Browse files

SpotifyRecorder: adjust sleep time according to remaining track duration

parent 582d75f2
Branches
Tags
No related merge requests found
...@@ -15,7 +15,6 @@ LOGGER = DefaultLogger().create_logger_if_not_exists('SpotifyRecorder', logForma ...@@ -15,7 +15,6 @@ LOGGER = DefaultLogger().create_logger_if_not_exists('SpotifyRecorder', logForma
class SpotifyRecorder: class SpotifyRecorder:
_MAX_WAIT_TIME_TRACK_STARTING_IN_S = 10 _MAX_WAIT_TIME_TRACK_STARTING_IN_S = 10
_WAIT_TIME_TRACK_PLAYING_IN_S = 10
_WAIT_TIME_TRACK_PLAYING_SHORT_IN_S = 1 _WAIT_TIME_TRACK_PLAYING_SHORT_IN_S = 1
_THRESHOLD_TRACK_END_IN_MS = 5000 _THRESHOLD_TRACK_END_IN_MS = 5000
...@@ -132,25 +131,24 @@ class SpotifyRecorder: ...@@ -132,25 +131,24 @@ class SpotifyRecorder:
currentPlayback = self._spotify.current_playback() currentPlayback = self._spotify.current_playback()
if currentPlayback['is_playing']: if currentPlayback['is_playing']:
remainingTimeInMs = trackDurationInMs - currentPlayback['progress_ms'] remainingTimeInMs = trackDurationInMs - currentPlayback['progress_ms']
remainingTimeInSeconds = remainingTimeInMs // 1000
sleepTime = self._WAIT_TIME_TRACK_PLAYING_SHORT_IN_S
if remainingTimeInMs > self._THRESHOLD_TRACK_END_IN_MS:
sleepTime = remainingTimeInSeconds / 2
if remainingTimeInMs < self._THRESHOLD_TRACK_END_IN_MS:
LOGGER.debug(f'Waiting for track to end (remaining: '
f'{self.__convert_seconds_to_duration(remainingTimeInMs // 1000)}, '
f'sleep: {self._WAIT_TIME_TRACK_PLAYING_SHORT_IN_S}s)...')
time.sleep(self._WAIT_TIME_TRACK_PLAYING_SHORT_IN_S)
else:
LOGGER.debug(f'Waiting for track to end (remaining: ' LOGGER.debug(f'Waiting for track to end (remaining: '
f' {self.__convert_seconds_to_duration(remainingTimeInMs // 1000)}, ' f'{self.__convert_seconds_to_duration(remainingTimeInSeconds)}, '
f'sleep: {self._WAIT_TIME_TRACK_PLAYING_IN_S}s)...') f'sleep: {self.__convert_seconds_to_duration(sleepTime)}s)...')
time.sleep(self._WAIT_TIME_TRACK_PLAYING_IN_S) time.sleep(sleepTime)
continue
else: else:
waitDuration = int(time.time() - startTime) waitDuration = int(time.time() - startTime)
if waitDuration < trackDurationInSeconds: if waitDuration < trackDurationInSeconds:
raise RuntimeError( raise RuntimeError(
f'Track finished too early (waited: {waitDuration}s, expected: {trackDurationInSeconds}s)') f'Track finished too early (waited: {waitDuration}s, expected: {trackDurationInSeconds}s)')
if waitDuration > trackDurationInSeconds + self._WAIT_TIME_TRACK_PLAYING_IN_S: if waitDuration > trackDurationInSeconds + self._WAIT_TIME_TRACK_PLAYING_SHORT_IN_S * 3:
raise RuntimeError( raise RuntimeError(
f'Track finished too late (waited: {waitDuration}s, expected: {trackDurationInSeconds})') f'Track finished too late (waited: {waitDuration}s, expected: {trackDurationInSeconds})')
...@@ -180,7 +178,7 @@ class SpotifyRecorder: ...@@ -180,7 +178,7 @@ class SpotifyRecorder:
startTime = time.time() startTime = time.time()
while time.time() - startTime < self._MAX_WAIT_TIME_TRACK_STARTING_IN_S: while time.time() - startTime < self._MAX_WAIT_TIME_TRACK_STARTING_IN_S:
if self._spotify.current_playback()['is_playing']: if self._spotify.current_playback()['is_playing']:
LOGGER.debug(f'Track started playing after {time.time() - startTime}s') LOGGER.debug(f'Track started playing after {time.time() - startTime:.1f}s')
break break
time.sleep(1) time.sleep(1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment