From 29c68f9cfcf860f610c190c48b49c94c25b3b628 Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Fri, 21 Jan 2022 15:08:45 +0100 Subject: [PATCH] fix: cleanup entitites in show json from libretime --- nowplaying/show/client.py | 15 ++++++++++ .../fixtures/cast_now_show_encoding_fix.json | 30 +++++++++++++++++++ tests/test_show_client.py | 13 ++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/fixtures/cast_now_show_encoding_fix.json diff --git a/nowplaying/show/client.py b/nowplaying/show/client.py index 35684a39..5c958206 100644 --- a/nowplaying/show/client.py +++ b/nowplaying/show/client.py @@ -1,6 +1,8 @@ import datetime import logging import logging.handlers +import re +from html.entities import entitydefs import pytz import requests @@ -23,6 +25,7 @@ class ShowClient: """ __DEFAULT_SHOW_DURATION = 30 # 30 seconds + __cleanup_show_name_regexp = re.compile(r"&(\w+?);") def __init__(self, current_show_url): @@ -86,6 +89,7 @@ def update(self): logger.error("%s: No show name found" % self.__class__) raise ShowClientError("Missing show name") + real_name = self.__cleanup_show_name(real_name) self.show.set_name(real_name) showtz = pytz.timezone(data["station"]["timezone"]) @@ -148,3 +152,14 @@ def update(self): ) logger.info(self.show) + + def __cleanup_show_name(self, name) -> str: + """Cleanup name by undoing htmlspecialchars from libretime zf1 mvc.""" + + def __entityref_decode(m): + try: + return entitydefs[m.group(1)] + except KeyError: + return m.group(0) + + return self.__cleanup_show_name_regexp.sub(__entityref_decode, name) diff --git a/tests/fixtures/cast_now_show_encoding_fix.json b/tests/fixtures/cast_now_show_encoding_fix.json new file mode 100644 index 00000000..fe573914 --- /dev/null +++ b/tests/fixtures/cast_now_show_encoding_fix.json @@ -0,0 +1,30 @@ +{ + "station": { + "env": "production", + "schedulerTime": "2019-01-27 16:44:44", + "source_enabled": "Scheduled", + "timezone": "Europe/Zurich", + "AIRTIME_API_VERSION": "1.1" + }, + "tracks": { + "previous": null, + "current": null, + "next": null + }, + "shows": { + "previous": [], + "current": { + "name": "Rhythm & Blues Juke Box öç", + "description": "", + "genre": "", + "id": 85, + "instance_id": 11605, + "record": 0, + "url": "https://rabe.ch/rhythm-blues-juke-box/", + "image_path": "", + "starts": "2019-01-27 14:00:00", + "ends": "2319-01-27 15:00:00" + }, + "next": [] + } +} diff --git a/tests/test_show_client.py b/tests/test_show_client.py index 6a87debf..5a4890b1 100644 --- a/tests/test_show_client.py +++ b/tests/test_show_client.py @@ -173,3 +173,16 @@ def test_update_show_empty(mock_requests_get): show_client.update() assert show_client.show.name is None assert show_client.show.url == "https://www.rabe.ch" + + +@mock.patch("requests.get") +def test_update_show_encoding_fix_in_name(mock_requests_get): + """Test :class:`ShowClient`'s :meth:`update` method when the show name has an encoding fix.""" + mock_requests_get.return_value.json = Mock( + return_value=json.loads( + file_get_contents("tests/fixtures/cast_now_show_encoding_fix.json") + ) + ) + show_client = ShowClient(_BASE_URL) + show_client.update() + assert show_client.show.name == "Rhythm & Blues Juke Box öç"