Commit 2d0c3061 authored by David Trattnig's avatar David Trattnig
Browse files

Fixed timeslot fetching. #41

parent 05b6b59b
......@@ -60,7 +60,6 @@ class ApiFetcher(threading.Thread):
self.tank_playlist_url = self.config.get("api_tank_playlist")
self.tank_session = self.config.get("api_tank_session")
self.tank_secret = self.config.get("api_tank_secret")
self.queue = queue.Queue()
self.stop_event = threading.Event()
threading.Thread.__init__(self)
......@@ -94,6 +93,7 @@ class ApiFetcher(threading.Thread):
return
#
# METHODS
#
......
......@@ -55,6 +55,7 @@ class ProgrammeService():
self.programme_store = ProgrammeStore()
def refresh(self):
"""
Fetch the latest programme from `ProgrammeStore` which stores it to the database.
......@@ -68,7 +69,7 @@ class ProgrammeService():
self.api_fetcher = ApiFetcher(self.config)
self.api_fetcher.start()
response = self.api_fetcher.get_fetched_data()
# Reset last successful fetch state
lsf = self.last_successful_fetch
self.last_successful_fetch = None
......@@ -77,18 +78,17 @@ class ProgrammeService():
msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.")
self.logger.warning(msg)
elif type(response) is list:
self.timeslots = response
if self.timeslots is not None and len(self.timeslots) > 0:
self.last_successful_fetch = datetime.now()
if len(response) > 0:
self.last_successful_fetch = datetime.now()
self.programme_store.store_timeslots(response)
self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)})"))
if len(self.timeslots) == 0:
self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)} timeslots)"))
else:
self.logger.critical("Programme fetched from Steering/Tank has no entries!")
elif response.startswith("fetching_aborted"):
msg = SU.red("Trying to load programme from database only, because fetching was being aborted from ApiFetcher! Reason: ")
self.logger.warning(msg + response[16:])
msg = SU.red("Load programme from DB, because fetching was aborted by ApiFetcher! Reason: " + response[17:])
self.logger.warning(msg)
else:
msg = SU.red("Trying to load programme from database only, because of an unknown response from ApiFetcher: " + response)
msg = SU.red("Load programme from DB, because of an unknown response from ApiFetcher: " + response)
self.logger.warning(msg)
# Always load latest programme from the database
......@@ -177,6 +177,9 @@ class ProgrammeService():
now_unix = Engine.engine_time()
next_timeslots = []
if not self.timeslots:
return []
for timeslot in self.timeslots:
if timeslot.start_unix > now_unix:
if (len(next_timeslots) < max_count) or max_count == 0:
......@@ -200,7 +203,7 @@ class ProgrammeService():
window_end = self.config.get("scheduling_window_end")
timeslots = list(filter(lambda s: (s.start_unix - window_end) > now_unix and (s.start_unix - window_start) < now_unix, timeslots))
len_after = len(timeslots)
self.logger.info("For now, skipped %s future timeslot(s) which are out of the scheduling window (-%ss <-> -%ss)" % ((len_before - len_after), window_start, window_end))
self.logger.info("For now, skipped %s future timeslot(s) which are out of the scheduling window (T-%ss to T-%ss)" % ((len_before - len_after), window_start, window_end))
return timeslots
......
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