Commit 302381bd authored by David Trattnig's avatar David Trattnig
Browse files

Fixed cleaning of unnessecary API records.

parent e99937f6
......@@ -116,7 +116,9 @@ class CalendarFetcher:
schedule = simplejson.loads(html_response)
# check data
self.logger.critical("no JSON data checks. I believe what i get here")
if not schedule:
self.logger.warn("Got no schedule via Playout API (Steering)!")
return None
#self.fetched_schedule_data = self.remove_unnecessary_data(schedule)
return self.remove_unnecessary_data(schedule)
......@@ -139,7 +141,7 @@ class CalendarFetcher:
#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)
self.logger.info(str(schedule))
#self.logger.info(str(schedule))
except Exception as e:
self.logger.error("Error: "+str(e))
......@@ -227,15 +229,26 @@ class CalendarFetcher:
self.has_already_fetched = True
return html_response.decode("utf-8")
# ------------------------------------------------------------------------------------------ #
def __build_url__(self, type, placeholder=None, value=None):
"""
Builds an API request URL using passed placeholder and value.
"""
url = self.url[type]
if placeholder:
url = url.replace(placeholder, value)
# print("built URL: "+url)
return url
# ------------------------------------------------------------------------------------------ #
def remove_unnecessary_data(self, schedule):
"""
Removes all schedules which are not relevant for
further processing.
"""
count_before = len(schedule)
schedule = self.remove_data_more_than_24h_in_the_future(schedule)
schedule = self.remove_data_in_the_past(schedule)
......@@ -243,8 +256,16 @@ class CalendarFetcher:
self.logger.info("Removed %d unnecessary schedules from response. Entries left: %d" % ((count_before - count_after), count_after))
return schedule
# ------------------------------------------------------------------------------------------ #
def remove_data_more_than_24h_in_the_future(self, schedule_from_pv):
"""
Removes entries 24h in the future and 12 hours in the past.
Note: This might influence resuming (in case of a crash)
single schedules which are longer than 12 hours long.
Think e.g. live broadcasts.
"""
act_list = []
now = datetime.now()
now_plus_24hours = now + timedelta(hours=24)
......@@ -253,29 +274,38 @@ class CalendarFetcher:
date_start = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S")
# append only elements which are close enough to now
if date_start <= now_plus_24hours and date_start >= now - timedelta(hours=1):
if date_start <= now_plus_24hours and date_start >= now - timedelta(hours=12):
act_list.append(s)
return act_list
# ------------------------------------------------------------------------------------------ #
def remove_data_in_the_past(self, schedule_from_pv):
"""
Removes all schedules from the past, except the one which is
currently playing.
"""
act_list = []
now = datetime.now()
count = len(schedule_from_pv)
for index,curr in enumerate(schedule_from_pv[:-1]):
for index, curr in enumerate(schedule_from_pv):
date_start = datetime.strptime(curr["start"], "%Y-%m-%dT%H:%M:%S")
date_next_start = datetime.strptime(schedule_from_pv[index+1]["start"], "%Y-%m-%dT%H:%M:%S")
if index+1 < count:
date_next_start = datetime.strptime(schedule_from_pv[index+1]["start"], "%Y-%m-%dT%H:%M:%S")
# append all elements in the future
# Append all elements in the future
if date_start >= now:
act_list.append(curr)
# append the one which is now playing
if date_start <= now and date_next_start >= now:
# Append the one which is now playing
elif date_start < now and date_next_start >= now:
act_list.append(curr)
return act_list
# ------------------------------------------------------------------------------------------ #
def create_test_data(self, id_name, schedule):
import random
......
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