Commit 23bc0fad authored by David Trattnig's avatar David Trattnig
Browse files

Proper fallback ID fetch, local station fallback.

parent d7ea809d
......@@ -184,6 +184,7 @@ class CalendarFetcher:
# store fetched entries => do not have to fetch playlist_id more than once
local_station_fallback_id = str(self.config.get("scheduling_station_fallback_id"))
for schedule in self.fetched_schedule_data:
......@@ -191,15 +192,27 @@ class CalendarFetcher:
# Extend schedule with details of show (e.g. slug)
schedule = self.__fetch_show_details__(schedule)
# Get IDs of playlists
playlist_id = self.get_playlist_id(schedule, "playlist_id")
schedule_fallback_id = self.get_playlist_id(schedule, "schedule_fallback_id")
show_fallback_id = self.get_playlist_id(schedule, "show_fallback_id")
station_fallback_id = self.get_playlist_id(schedule, "station_fallback_id")
# Retrieve playlist and the fallback playlists for every schedule.
# If a playlist (like station_fallback) is already fetched, it is not fetched again but reused
schedule["playlist"] = self.__fetch_schedule_playlist__(schedule, "playlist_id", fetched_entries)
schedule["schedule_fallback"] = self.__fetch_schedule_playlist__(schedule, "schedule_fallback_id", fetched_entries)
schedule["show_fallback"] = self.__fetch_schedule_playlist__(schedule, "show_fallback_id", fetched_entries)
schedule["station_fallback"] = self.__fetch_schedule_playlist__(schedule, "station_fallback_id", fetched_entries)
schedule["playlist"] = self.__fetch_schedule_playlist__(schedule, playlist_id, fetched_entries)
schedule["schedule_fallback"] = self.__fetch_schedule_playlist__(schedule, schedule_fallback_id, fetched_entries)
schedule["show_fallback"] = self.__fetch_schedule_playlist__(schedule, show_fallback_id, fetched_entries)
schedule["station_fallback"] = self.__fetch_schedule_playlist__(schedule, station_fallback_id, fetched_entries)
# If Steering doesn't provide a station fallback, the local one is used
if not schedule["station_fallback"] and int(local_station_fallback_id) > 0:
schedule["station_fallback"] = self.__fetch_schedule_playlist__(schedule, local_station_fallback_id, fetched_entries)
if schedule["station_fallback"]:"Set station fallback to playlist #%s" % local_station_fallback_id)
except Exception as e:
self.logger.error("Error: "+str(e))
self.logger.error("Error while fetching playlists from API endpoints: " + str(e), e)
......@@ -229,7 +242,20 @@ class CalendarFetcher:
def __fetch_schedule_playlist__(self, schedule, id_name, fetched_playlists):
def get_playlist_id(self, schedule, id_name):
Extracts the playlist ID for a given playlist (fallback) type.
playlist_id = str(schedule[id_name])
if not playlist_id or playlist_id == "None":"No value defined for '%s' in schedule '#%s'" % (id_name, schedule["schedule_id"]))
return None
return playlist_id
def __fetch_schedule_playlist__(self, schedule, playlist_id, fetched_playlists):
Fetches the playlist for a given schedule.
......@@ -239,71 +265,35 @@ class CalendarFetcher:
fetched_playlists ([]): Previously fetched playlists to avoid re-fetching
([Schedule]): Array of playlists
(Playlist): Playlist of type `id_name`
servicetype = "api_tank_playlist"
playlist = None
# fetch playlists from TANK
if not "show_slug" in schedule:
raise ValueError("Missing 'show_slug' for schedule", schedule)
slug = str(schedule["show_slug"])
url = self.__build_url__(servicetype, "${SLUG}", slug)
json_response = self.__fetch_data__(servicetype, url)
# if a playlist is already fetched, do not fetch it again
# If playlist is already fetched, use the existing one
for playlist in fetched_playlists:
# FIXME schedule["playlist_id"] is always None, review if playlist["id"] is valid
if playlist["id"] == schedule[id_name]:
self.logger.debug("Playlist #" + str(schedule[id_name]) + " already fetched")
if playlist["id"] == playlist_id:
self.logger.debug("Playlist #%s already fetched" % playlist_id)
return playlist
if self.config.get("use_test_data"):
# FIXME move hardcoded test-data to separate testing logic.
self.logger.warn("Using test-data for fetch-schedule-playlist")
json_response = self.create_test_data(id_name, schedule)
# convert to list
playlists = simplejson.loads(json_response)
pl = None
if "results" in playlists:
# FIXME Currently we use hardcoded playlist and fallback assignments due to issues in Dashboard/Steering/Tank
self.logger.warn("FIXME Currently we use hardcoded playlist and fallback assignments due to issues in Dashboard/Steering/Tank")
i = 0
for playlist in playlists["results"]:
pl = playlist
# FIXME Always use the first playlist, since the schedule.playlist_id is currently not set via Dashboard:
if i == 0 and id_name == "playlist_id":
schedule["playlist_id"] = playlist["id"]
# FIXME Currently it's not possible to set & query the fallback for a timeslot/show/station; therefore hardcode it:
elif i == 1 and id_name == "schedule_fallback_id":
schedule["schedule_fallback_id"] = playlist["id"]
elif i == 2 and id_name == "show_fallback_id":
schedule["show_fallback_id"] = playlist["id"]
elif i == 3 and id_name == "station_fallback_id":
schedule["station_fallback_id"] = playlist["id"]
pl = None
i += 1
if pl:
# Note: playlists without entries are allowed -> will trigger fallbacks
if "entries" in pl:
for entry in pl["entries"]:
if entry["uri"].startswith("file"):
entry["filename"] = self.convert_to_filename(entry["uri"])
return pl
if not playlist_id:
return None
url = self.__build_url__(servicetype, "${ID}", playlist_id)
json_response = self.__fetch_data__(servicetype, url)
if not json_response:
self.logger.critical("Playlist #%s could not be fetched or is not available! JSON response: '%s'" % (playlist_id, json_response)))
return None
playlist = simplejson.loads(json_response)
except Exception as e:
self.logger.critical("Error while parsing JSON response for playlist: %s" % json_response), e)
return playlist
def convert_to_filename(self, uri):
......@@ -366,7 +356,7 @@ class CalendarFetcher:
url = self.url[type]
if placeholder:
url = url.replace(placeholder, value)
#"built URL: "+url)
self.logger.debug("Built API URL: "+url)
return url
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment