Skip to content

Commit

Permalink
fix: cleanup entitites in show json from libretime
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Jan 21, 2022
1 parent 320ea84 commit 29c68f9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nowplaying/show/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import logging
import logging.handlers
import re
from html.entities import entitydefs

import pytz
import requests
Expand All @@ -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):

Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)
30 changes: 30 additions & 0 deletions tests/fixtures/cast_now_show_encoding_fix.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
13 changes: 13 additions & 0 deletions tests/test_show_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 öç"

0 comments on commit 29c68f9

Please sign in to comment.