diff --git a/nowplaying/daemon.py b/nowplaying/daemon.py index e7dde46c..df6191f1 100644 --- a/nowplaying/daemon.py +++ b/nowplaying/daemon.py @@ -67,7 +67,9 @@ def get_track_handler(self): ] [ handler.register_observer( - DabAudioCompanionTrackObserver(url, self.options.dab_send_dls) + DabAudioCompanionTrackObserver( + base_url=url, dls_enabled=self.options.dab_send_dls + ) ) for url in self.options.dab ] diff --git a/nowplaying/track/observers/dab_audio_companion.py b/nowplaying/track/observers/dab_audio_companion.py index e10157d9..5ac7b554 100644 --- a/nowplaying/track/observers/dab_audio_companion.py +++ b/nowplaying/track/observers/dab_audio_companion.py @@ -13,15 +13,12 @@ class DabAudioCompanionTrackObserver(TrackObserver): name = "DAB+ Audio Companion" - def __init__(self, baseUrl, dls_enabled: bool = False): - self.baseUrl = baseUrl + "/api/setDLS" + def __init__(self, base_url, dls_enabled: bool = False): + self.base_url = base_url + "/api/setDLS" self.dls_enabled = self.last_frame_was_dl_plus = dls_enabled logger.info( "DAB+ Audio Companion initialised with URL: %s, DLS+ enabled: %r" - % ( - self.baseUrl, - self.dls_enabled, - ) + % (self.base_url, self.dls_enabled) ) def track_started(self, track): @@ -40,11 +37,10 @@ def track_started(self, track): self.last_frame_was_dl_plus = True elif self.last_frame_was_dl_plus: logger.info( - "%s: Track has default info, using show instead. Sending DLS+ delete tags." - % self.__class__ + "Track has default info, using show instead. Sending DLS+ delete tags." ) # track.artist contains station name if no artist is set - message = "%s - %s" % (track.artist, track.show.name) + message = f"{track.artist} - {track.show.name}" param = "".join( ( "##### parameters { #####\n", @@ -59,15 +55,15 @@ def track_started(self, track): params["dls"] = param self.last_frame_was_dl_plus = False else: - logger.info( - "%s: Track has default info, using show instead" % self.__class__ - ) + logger.info("Track has default info, using show instead") # track.artist contains station name if no artist is set - params["dls"] = "%s - %s" % (track.artist, track.show.name) + params["dls"] = f"{track.artist} - {track.show.name}" - logger.info(f"DAB+ Audio Companion URL: {self.baseUrl} data: {params}") + logger.info( + f"DAB+ Audio Companion URL: {self.base_url} data: {params} is DL+: {self.last_frame_was_dl_plus}" + ) - resp = requests.post(self.baseUrl, params) + resp = requests.post(self.base_url, params) if resp.status_code != 200: logger.error(f"DAB+ Audio Companion API call failed: {resp.text}") @@ -88,7 +84,7 @@ def _track_started_plain(self, track): "%s - %s" % (track.artist.encode("utf8"), title.encode("utf8")) ) - update_url = f"{self.baseUrl}?dls={song_string}" + update_url = f"{self.base_url}?dls={song_string}" logger.info("DAB+ Audio Companion URL: " + update_url) diff --git a/tests/test_track_observer_dabaudiocompanion.py b/tests/test_track_observer_dabaudiocompanion.py index 3af156d8..d66deb3e 100644 --- a/tests/test_track_observer_dabaudiocompanion.py +++ b/tests/test_track_observer_dabaudiocompanion.py @@ -16,9 +16,9 @@ def test_init(): """Test class:`DabAudioCompanionTrackObserver`'s :meth:`.__init__` method.""" dab_audio_companion_track_observer = DabAudioCompanionTrackObserver( - baseUrl=_BASE_URL + base_url=_BASE_URL ) - assert dab_audio_companion_track_observer.baseUrl == f"{_BASE_URL}/api/setDLS" + assert dab_audio_companion_track_observer.base_url == f"{_BASE_URL}/api/setDLS" @mock.patch("requests.post") @@ -34,8 +34,7 @@ def test_track_started(mock_requests_post, track_factory, show_factory): track.show = show_factory() dab_audio_companion_track_observer = DabAudioCompanionTrackObserver( - baseUrl=_BASE_URL, - dls_enabled=True, + base_url=_BASE_URL, dls_enabled=True ) # assume that last frame was DL+ on startup so we always send delete tags when a show w/o dl+ starts assert dab_audio_companion_track_observer.last_frame_was_dl_plus @@ -92,7 +91,7 @@ def test_track_started_plain(mock_urlopen, track_factory, show_factory): track = track_factory() track.show = show_factory() - o = DabAudioCompanionTrackObserver(baseUrl="http://localhost:80") + o = DabAudioCompanionTrackObserver(base_url="http://localhost:80") # last frame cannot be dl+ since the feature is inactive assert not o.last_frame_was_dl_plus @@ -114,6 +113,6 @@ def test_track_started_plain(mock_urlopen, track_factory, show_factory): def test_track_finished(): """Test :class:`DabAudioCompanionTrackObserver`'s :meth:`track_finished` method.""" dab_audio_companion_track_observer = DabAudioCompanionTrackObserver( - baseUrl=_BASE_URL + base_url=_BASE_URL ) assert dab_audio_companion_track_observer.track_finished(Track())