Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleaner logging for dab companion #164

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion nowplaying/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
28 changes: 12 additions & 16 deletions nowplaying/track/observers/dab_audio_companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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",
Expand All @@ -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}")

Expand All @@ -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)

Expand Down
11 changes: 5 additions & 6 deletions tests/test_track_observer_dabaudiocompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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())