Skip to content
Snippets Groups Projects
Commit 302381bd authored by David Trattnig's avatar David Trattnig
Browse files

Fixed cleaning of unnessecary API records.

parent e99937f6
No related branches found
No related tags found
No related merge requests found
...@@ -116,7 +116,9 @@ class CalendarFetcher: ...@@ -116,7 +116,9 @@ class CalendarFetcher:
schedule = simplejson.loads(html_response) schedule = simplejson.loads(html_response)
# check data # 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) #self.fetched_schedule_data = self.remove_unnecessary_data(schedule)
return self.remove_unnecessary_data(schedule) return self.remove_unnecessary_data(schedule)
...@@ -139,7 +141,7 @@ class CalendarFetcher: ...@@ -139,7 +141,7 @@ class CalendarFetcher:
#schedule["show_fallback"] = self.__fetch_schedule_playlist__(schedule, "show_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["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: except Exception as e:
self.logger.error("Error: "+str(e)) self.logger.error("Error: "+str(e))
...@@ -227,15 +229,26 @@ class CalendarFetcher: ...@@ -227,15 +229,26 @@ class CalendarFetcher:
self.has_already_fetched = True self.has_already_fetched = True
return html_response.decode("utf-8") return html_response.decode("utf-8")
# ------------------------------------------------------------------------------------------ #
def __build_url__(self, type, placeholder=None, value=None): def __build_url__(self, type, placeholder=None, value=None):
"""
Builds an API request URL using passed placeholder and value.
"""
url = self.url[type] url = self.url[type]
if placeholder: if placeholder:
url = url.replace(placeholder, value) url = url.replace(placeholder, value)
# print("built URL: "+url) # print("built URL: "+url)
return url return url
# ------------------------------------------------------------------------------------------ #
def remove_unnecessary_data(self, schedule): def remove_unnecessary_data(self, schedule):
"""
Removes all schedules which are not relevant for
further processing.
"""
count_before = len(schedule) count_before = len(schedule)
schedule = self.remove_data_more_than_24h_in_the_future(schedule) schedule = self.remove_data_more_than_24h_in_the_future(schedule)
schedule = self.remove_data_in_the_past(schedule) schedule = self.remove_data_in_the_past(schedule)
...@@ -243,8 +256,16 @@ class CalendarFetcher: ...@@ -243,8 +256,16 @@ class CalendarFetcher:
self.logger.info("Removed %d unnecessary schedules from response. Entries left: %d" % ((count_before - count_after), count_after)) self.logger.info("Removed %d unnecessary schedules from response. Entries left: %d" % ((count_before - count_after), count_after))
return schedule return schedule
# ------------------------------------------------------------------------------------------ #
def remove_data_more_than_24h_in_the_future(self, schedule_from_pv): 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 = [] act_list = []
now = datetime.now() now = datetime.now()
now_plus_24hours = now + timedelta(hours=24) now_plus_24hours = now + timedelta(hours=24)
...@@ -253,29 +274,38 @@ class CalendarFetcher: ...@@ -253,29 +274,38 @@ class CalendarFetcher:
date_start = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S") date_start = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S")
# append only elements which are close enough to now # 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) act_list.append(s)
return act_list return act_list
# ------------------------------------------------------------------------------------------ #
def remove_data_in_the_past(self, schedule_from_pv): 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 = [] act_list = []
now = datetime.now() 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_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: if date_start >= now:
act_list.append(curr) act_list.append(curr)
# append the one which is now playing # Append the one which is now playing
if date_start <= now and date_next_start >= now: elif date_start < now and date_next_start >= now:
act_list.append(curr) act_list.append(curr)
return act_list return act_list
# ------------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------------ #
def create_test_data(self, id_name, schedule): def create_test_data(self, id_name, schedule):
import random import random
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment