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

Remove remotely deleted schedules. #11

parent f6abc39b
......@@ -169,18 +169,29 @@ class Schedule(DB.Model, AuraDatabaseModel):
primaryjoin="and_(Schedule.schedule_start==Playlist.schedule_start, Schedule.station_fallback_id==Playlist.playlist_id, Schedule.show_name==Playlist.show_name)",
back_populates="schedule")
@staticmethod
def select_show_on_datetime(datetime):
return DB.session.query(Schedule).filter(Schedule.schedule_start == datetime).first()
@staticmethod
def select_act_programme():
#DB.session.query(Schedule).filter
# fetching all from today to ..
today = datetime.date.today()
all_entries = DB.session.query(Schedule).filter(Schedule.schedule_start >= today).order_by(Schedule.schedule_start).all()
def select_programme(date_from=datetime.date.today()):
"""
Select all schedules starting from `date_from` or from today if no
parameter is passed.
return all_entries
Args:
date_from (datetime): Select schedules from this date and time on
Returns:
([Schedule]): List of schedules
"""
schedules = DB.session.query(Schedule).\
filter(Schedule.schedule_start >= date_from).\
order_by(Schedule.schedule_start).all()
return schedules
@staticmethod
......
......@@ -89,21 +89,42 @@ class AuraCalendarService(threading.Thread):
def run(self):
"""
Fetch calendar data and store it in the database
Returns
Schedule ([]): An arrar of retrieved schedules passed via `self.queue`
"""
result = []
try:
fetched_schedule_data = self.calendar_fetcher.fetch()
self.logger.debug("Schedule data fetched from API: " + str(fetched_schedule_data))
# if nothing is fetched, return
# If nothing is fetched, return
if not fetched_schedule_data:
self.queue.put("fetching_aborted Nothing fetched")
return
# Check what we've got
self.logger.debug("Schedule data: " + str(fetched_schedule_data))
ret_schedule = []
# Check if existing schedules have been deleted
# local_schedules = self.get_current_schedules()
local_schedules = Schedule.select_programme(datetime.now())
for local_schedule in local_schedules:
# Filter the local schedule from the fetched ones
existing_schedule = list(filter(lambda new_schedule: \
new_schedule["schedule_id"] == local_schedule.schedule_id, fetched_schedule_data))
if existing_schedule:
self.logger.debug("Schedule (schedule_id: %s) is still existing remotely:" % \
(local_schedule.schedule_id))
else:
self.logger.info("Schedule (schedule_id: %s) has been deleted remotely! [%s]" % \
(local_schedule.schedule_id, str(local_schedule)))
local_schedule.delete(commit=True)
self.logger.info("Deleted local schedule from database (schedule_id: %s)" % local_schedule.schedule_id)
# Process fetched schedules
for schedule in fetched_schedule_data:
# Check schedule for validity
......@@ -126,12 +147,14 @@ class AuraCalendarService(threading.Thread):
if schedule_db.station_fallback_id:
self.store_playlist(schedule_db, schedule_db.station_fallback_id, schedule["station_fallback"], 3)
ret_schedule.append(schedule_db)
# release the mutex
self.queue.put(ret_schedule)
result.append(schedule_db)
# Release the mutex
self.queue.put(result)
except Exception as e:
# release the mutex
# Release the mutex
self.logger.warning("Fetching aborted due to: %s" % str(e))
self.queue.put("fetching_aborted " + str(e))
......@@ -139,6 +162,12 @@ class AuraCalendarService(threading.Thread):
return
def get_current_schedules(self):
"""
Returns the future schedules from the local database.
"""
return Schedule.select_programme()
def store_schedule(self, schedule):
"""
......@@ -259,15 +288,22 @@ class AuraCalendarService(threading.Thread):
havetoadd = True
metadata_db.artificial_entry_id = entry_db.artificial_id
if "artist" not in metadata:
self.logger.warning("Artist not found in metadata for track '%s'. Setting to 'n/a'" % entry_db.filename)
self.logger.warning("Artist not found in metadata for track '%s'. Setting to ''" % entry_db.filename)
metadata_db.artist = ""
else:
metadata_db.artist = metadata["artist"]
metadata_db.title = metadata["title"]
if "album" in metadata:
metadata_db.album = metadata["album"]
if "artist" not in metadata:
self.logger.warning("Title not found in metadata for track '%s'. Setting to 'n/a'" % entry_db.filename)
metadata_db.artist = "n/a"
else:
metadata_db.artist = metadata["artist"]
metadata_db.store(havetoadd, commit=True)
......
......@@ -612,7 +612,7 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
them via `self.enable_entries(..)`. After that, the
current message queue is printed to the console.
"""
self.programme = Schedule.select_act_programme()
self.programme = Schedule.select_programme()
if not self.programme:
self.logger.critical("Could not load programme from database. We are in big trouble my friend!")
......
......@@ -58,9 +58,9 @@ def start_diskspace_watcher():
diskspace_watcher = DiskSpaceWatcher(config.config, logging.getLogger("AuraEngine"), LiquidSoapCommunicator(config.config))
diskspace_watcher.run()
def select_act_programme():
def select_current_programme():
# start_diskspace_watcher()
# select_act_programme()
# select_programme()
config = AuraConfig()
config.read_config()
......@@ -76,11 +76,11 @@ def select_act_programme():
print(show)
def fadeout(lsc):
entry = ScheduleEntry.select_act_programme()
entry = ScheduleEntry.select_programme()
lsc.fade_out(entry, 2)
def fadein(lsc):
entry = ScheduleEntry.select_act_programme()
entry = ScheduleEntry.select_programme()
lsc.fade_in(entry, 1)
def fetch_new_programme():
......
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