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

feat: refactor ShowClient to LibreTime v2 live-info API #127

Merged
merged 1 commit into from
Dec 25, 2021
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
1 change: 1 addition & 0 deletions .github/workflows/lint-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches-ignore:
- main
- gh-pages
pull_request:

jobs:
pre-commit:
Expand Down
2 changes: 1 addition & 1 deletion nowplaying/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self):
"--show",
dest="currentShowUrl",
help="Current Show URL",
default="http://intranet.rabe.ch/pub/show.php",
default="https://airtime.service.int.rabe.ch/api/live-info-v2/format/json",
)
self.__args.add_argument(
"--input-file",
Expand Down
56 changes: 31 additions & 25 deletions nowplaying/show/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import datetime
import logging
import logging.handlers
import urllib.error
import urllib.parse
import urllib.request
import xml.dom.minidom

import isodate
import pytz
import requests

from . import show

Expand All @@ -21,7 +17,7 @@ class ShowClientError(Exception):


class ShowClient:
"""Client which fetches the show informations from the cast-web-service.
"""Client which fetches the show informations from the LibreTime now-playing v2 endpoint.

Every show has a name, a start and endtime and an optional URL.
"""
Expand Down Expand Up @@ -67,7 +63,7 @@ def update(self):
try:
# try to get the current show informations from loopy's cast web
# service
dom = xml.dom.minidom.parse(urllib.request.urlopen(self.current_show_url))
data = requests.get(self.current_show_url).json()

except Exception as e:
logger.error("%s: Unable to get current show informations" % self.__class__)
Expand All @@ -77,39 +73,49 @@ def update(self):
# raise ShowClientError('Unable to get show informations: %s' % e)
return

if not data["shows"]["current"]:
# ignore if no current show is playing
return

# get the name of the show, aka real_name
# ex.: Stereo Freeze
real_name = dom.getElementsByTagName("real_name")
real_name = data["shows"]["current"]["name"]

if len(real_name) == 0 or real_name[0].hasChildNodes() is False:
if len(real_name) == 0:
# keep the default show information
logger.error("%s: No <real_name> tag found" % self.__class__)
logger.error("%s: No show name found" % self.__class__)
raise ShowClientError("Missing show name")

self.show.set_name(real_name[0].firstChild.data.strip())
self.show.set_name(real_name)

showtz = pytz.timezone(data["station"]["timezone"])

# get the show's end time in order to time the next lookup.
# ex.: 2012-04-28T19:00:00+0200
end_time = dom.getElementsByTagName("end_time")
# ex.: 2012-04-28 19:00:00 (missing a tzoffset and localized!)
end_time = data["shows"]["current"]["ends"]

if len(end_time) == 0 or end_time[0].hasChildNodes() is False:
logger.error("%s: No <end_time> tag found" % self.__class__)
if len(end_time) == 0:
logger.error("%s: No end found" % self.__class__)
raise ShowClientError("Missing show end time")

endtime = isodate.parse_datetime(end_time[0].firstChild.data.strip())
endtime = showtz.localize(
datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
)

# store as UTC datetime object
self.show.set_endtime(endtime.astimezone(pytz.timezone("UTC")))

# get the show's start time
# ex.: 2012-04-28T18:00:00+0200
start_time = dom.getElementsByTagName("start_time")
# ex.: 2012-04-28 18:00:00
start_time = data["shows"]["current"]["starts"]

if len(start_time) == 0 or start_time[0].hasChildNodes() is False:
logger.error("%s: No <start_time> tag found" % self.__class__)
if len(start_time) == 0:
logger.error("%s: No start found" % self.__class__)
raise ShowClientError("Missing show start time")

starttime = isodate.parse_datetime(start_time[0].firstChild.data.strip())
starttime = showtz.localize(
datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
)

# store as UTC datetime object
self.show.set_starttime(starttime.astimezone(pytz.timezone("UTC")))
Expand All @@ -129,12 +135,12 @@ def update(self):

# get the show's URL
# ex.: http://www.rabe.ch/sendungen/entertainment/onda-libera.html
url = dom.getElementsByTagName("url")
url = data["shows"]["current"]["url"]

if len(url) == 0 or start_time[0].hasChildNodes() is False:
logger.error("%s: No <url> tag found" % self.__class__)
if len(url) == 0:
logger.error("%s: No url found" % self.__class__)
else:
self.show.set_url(url[0].firstChild.data.strip())
self.show.set_url(url)

logger.info(
'Show "%s" started and runs from %s till %s'
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pytz==2021.3
isodate==0.6.1
pylast==4.4.0
lxml==4.7.1
requests==2.26.0
32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_during_show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "Voice of Hindu Kush",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "https://www.rabe.ch/stimme-der-kutuesch/",
"image_path": "",
"starts": "2019-01-27 14:00:00",
"ends": "2319-01-27 15:00:00"
},
"next": []
}
}
8 changes: 0 additions & 8 deletions tests/fixtures/cast_now_during_show.xml

This file was deleted.

32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_no_end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "Voice of Hindu Kush",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "https://www.rabe.ch/stimme-der-kutuesch/",
"image_path": "",
"starts": "2019-01-27 14:00:00",
"ends": ""
},
"next": []
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_no_name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "https://www.rabe.ch/stimme-der-kutuesch/",
"image_path": "",
"starts": "2019-01-27 14:00:00",
"ends": "2319-01-27 15:00:00"
},
"next": []
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_no_start.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "Voice of Hindu Kush",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "https://www.rabe.ch/stimme-der-kutuesch/",
"image_path": "",
"starts": "",
"ends": "2319-01-27 15:00:00"
},
"next": []
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_no_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "Voice of Hindu Kush",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "",
"image_path": "",
"starts": "2019-01-27 14:00:00",
"ends": "2319-01-27 15:00:00"
},
"next": []
}
}
7 changes: 0 additions & 7 deletions tests/fixtures/cast_now_no_url.xml

This file was deleted.

32 changes: 32 additions & 0 deletions tests/fixtures/cast_now_past_show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"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": "Voice of Hindu Kush",
"description": "",
"genre": "",
"id": 85,
"instance_id": 11605,
"record": 0,
"url": "https://www.rabe.ch/stimme-der-kutuesch/",
"image_path": "",
"starts": "2019-01-27 14:00:00",
"ends": "2019-01-27 15:00:00"
},
"next": []
}
}
8 changes: 0 additions & 8 deletions tests/fixtures/cast_now_past_show.xml

This file was deleted.

19 changes: 19 additions & 0 deletions tests/fixtures/cast_now_show_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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": {},
"next": []
}
}
Loading