From 2d0c30619dd9eed1b0e65932b739f6b0e1d3005a Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Fri, 20 Nov 2020 15:22:12 +0100 Subject: [PATCH] Fixed timeslot fetching. #41 --- src/scheduling/api.py | 2 +- src/scheduling/programme.py | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/scheduling/api.py b/src/scheduling/api.py index 4046a299..dfc99573 100644 --- a/src/scheduling/api.py +++ b/src/scheduling/api.py @@ -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 # diff --git a/src/scheduling/programme.py b/src/scheduling/programme.py index 7060b857..037ea8c3 100644 --- a/src/scheduling/programme.py +++ b/src/scheduling/programme.py @@ -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 -- GitLab