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

Reuse timeslots which got just stored. #41

parent 51cc9001
No related branches found
No related tags found
No related merge requests found
...@@ -70,17 +70,13 @@ class ProgrammeService(): ...@@ -70,17 +70,13 @@ class ProgrammeService():
self.api_fetcher.start() self.api_fetcher.start()
response = self.api_fetcher.get_fetched_data() response = self.api_fetcher.get_fetched_data()
# Reset last successful fetch state
lsf = self.last_successful_fetch
self.last_successful_fetch = None
if response is None: if response is None:
msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.") msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.")
self.logger.warning(msg) self.logger.warning(msg)
elif type(response) is list: elif type(response) is list:
if len(response) > 0: if len(response) > 0:
self.last_successful_fetch = datetime.now() self.last_successful_fetch = datetime.now()
self.programme_store.store_timeslots(response) self.timeslots = self.programme_store.store_timeslots(response)
self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)} timeslots)")) self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)} timeslots)"))
else: else:
self.logger.critical("Programme fetched from Steering/Tank has no entries!") self.logger.critical("Programme fetched from Steering/Tank has no entries!")
...@@ -91,10 +87,11 @@ class ProgrammeService(): ...@@ -91,10 +87,11 @@ class ProgrammeService():
msg = SU.red("Load programme from DB, 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) self.logger.warning(msg)
# Always load latest programme from the database # Load latest programme from the database
self.last_successful_fetch = lsf if not self.timeslots:
self.timeslots = self.programme_store.load_timeslots() self.timeslots = self.programme_store.load_timeslots()
self.logger.info(SU.green("Finished loading current programme from database (%s timeslots)" % str(len(self.timeslots)))) self.logger.info(SU.green("Finished loading current programme from database (%s timeslots)" % str(len(self.timeslots))))
for timeslot in self.timeslots: for timeslot in self.timeslots:
self.logger.debug("\tTimeslot %s with Playlist %s" % (str(timeslot), str(timeslot.playlist))) self.logger.debug("\tTimeslot %s with Playlist %s" % (str(timeslot), str(timeslot.playlist)))
...@@ -186,7 +183,7 @@ class ProgrammeService(): ...@@ -186,7 +183,7 @@ class ProgrammeService():
next_timeslots.append(timeslot) next_timeslots.append(timeslot)
else: else:
break break
return self.filter_scheduling_window(next_timeslots) return self.filter_scheduling_window(next_timeslots)
...@@ -268,10 +265,13 @@ class ProgrammeStore(): ...@@ -268,10 +265,13 @@ class ProgrammeStore():
return timeslots return timeslots
def store_timeslots(self, fetched_timeslots): def store_timeslots(self, fetched_timeslots):
""" """
Stores the fetched timeslots to the database. Stores the fetched timeslots to the database.
""" """
timeslots = []
# Check if existing timeslots have been deleted # Check if existing timeslots have been deleted
self.update_deleted_timeslots(fetched_timeslots) self.update_deleted_timeslots(fetched_timeslots)
...@@ -288,15 +288,18 @@ class ProgrammeStore(): ...@@ -288,15 +288,18 @@ class ProgrammeStore():
# Store the timeslot # Store the timeslot
timeslot_db = self.store_timeslot(timeslot) timeslot_db = self.store_timeslot(timeslot)
timeslots.append(timeslot_db)
# Store playlists to play # Store assigned playlists
self.store_playlist(timeslot_db, timeslot_db.playlist_id, timeslot["playlist"]) self.store_playlist(timeslot_db, timeslot_db.playlist_id, timeslot["playlist"])
if timeslot_db.schedule_fallback_id: if timeslot_db.schedule_fallback_id:
self.store_playlist(timeslot_db, timeslot_db.schedule_fallback_id, timeslot["schedule_fallback"]) self.store_playlist(timeslot_db, timeslot_db.schedule_fallback_id, timeslot["schedule_fallback"])
if timeslot_db.show_fallback_id: if timeslot_db.show_fallback_id:
self.store_playlist(timeslot_db, timeslot_db.show_fallback_id, timeslot["show_fallback"]) self.store_playlist(timeslot_db, timeslot_db.show_fallback_id, timeslot["show_fallback"])
if timeslot_db.station_fallback_id: if timeslot_db.station_fallback_id:
self.store_playlist(timeslot_db, timeslot_db.station_fallback_id, timeslot["station_fallback"]) self.store_playlist(timeslot_db, timeslot_db.station_fallback_id, timeslot["station_fallback"])
return timeslots
......
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