Commit 96bfc486 authored by David Trattnig's avatar David Trattnig
Browse files

Reuse timeslots which got just stored. #41

parent 51cc9001
......@@ -70,17 +70,13 @@ class ProgrammeService():
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
if response is None:
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:
if len(response) > 0:
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)"))
else:
self.logger.critical("Programme fetched from Steering/Tank has no entries!")
......@@ -91,10 +87,11 @@ class ProgrammeService():
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
self.last_successful_fetch = lsf
self.timeslots = self.programme_store.load_timeslots()
self.logger.info(SU.green("Finished loading current programme from database (%s timeslots)" % str(len(self.timeslots))))
# Load latest programme from the database
if not self.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))))
for timeslot in self.timeslots:
self.logger.debug("\tTimeslot %s with Playlist %s" % (str(timeslot), str(timeslot.playlist)))
......@@ -186,7 +183,7 @@ class ProgrammeService():
next_timeslots.append(timeslot)
else:
break
return self.filter_scheduling_window(next_timeslots)
......@@ -268,10 +265,13 @@ class ProgrammeStore():
return timeslots
def store_timeslots(self, fetched_timeslots):
"""
Stores the fetched timeslots to the database.
"""
timeslots = []
# Check if existing timeslots have been deleted
self.update_deleted_timeslots(fetched_timeslots)
......@@ -288,15 +288,18 @@ class ProgrammeStore():
# Store the 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"])
if timeslot_db.schedule_fallback_id:
self.store_playlist(timeslot_db, timeslot_db.schedule_fallback_id, timeslot["schedule_fallback"])
if timeslot_db.show_fallback_id:
self.store_playlist(timeslot_db, timeslot_db.show_fallback_id, timeslot["show_fallback"])
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
......
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