Commit 9046b661 authored by David Trattnig's avatar David Trattnig
Browse files

Update/delete playlist entries. #31

parent 76444b8f
This diff is collapsed.
......@@ -85,7 +85,7 @@ class TrackServiceHandler():
"""
Posts the current and next show information to the Engine API.
"""
current_playlist = self.soundsystem.scheduler.get_active_playlist()
current_playlist = self.soundsystem.scheduler.get_active_playlist()
current_schedule = current_playlist.schedule
next_schedule = self.soundsystem.scheduler.get_next_schedules(1)
if next_schedule: next_schedule = next_schedule[0]
......
......@@ -124,8 +124,6 @@ class AuraCalendarService(threading.Thread):
msg = "Schedule #%s has been deleted remotely. Since the scheduling window has already started, it won't be deleted locally." % local_schedule.schedule_id
self.logger.warn(SimpleUtil.red(msg))
# Process fetched schedules
for schedule in fetched_schedule_data:
......@@ -149,8 +147,6 @@ class AuraCalendarService(threading.Thread):
if schedule_db.station_fallback_id:
self.store_playlist(schedule_db, schedule_db.station_fallback_id, schedule["station_fallback"], PlaylistType.STATION.id)
result.append(schedule_db)
# Release the mutex
......@@ -195,10 +191,6 @@ class AuraCalendarService(threading.Thread):
schedule_db.topic = schedule["show_topics"]
schedule_db.musicfocus = schedule["show_musicfocus"]
# if schedule["playlist_id"] is None:
# # FIXME Manually assigned playlist ID.
# schedule["playlist_id"] = 1
schedule_db.playlist_id = schedule["playlist_id"]
schedule_db.schedule_fallback_id = schedule["schedule_fallback_id"]
schedule_db.show_fallback_id = schedule["show_fallback_id"]
......@@ -236,7 +228,7 @@ class AuraCalendarService(threading.Thread):
playlist_db.entry_count = 0
playlist_db.store(havetoadd, commit=True)
if playlist_db.entry_count > 0:
self.store_playlist_entries(schedule_db, playlist_db, fetched_playlist)
......@@ -251,6 +243,10 @@ class AuraCalendarService(threading.Thread):
entry_num = 0
time_marker = playlist_db.start_unix
# If existing playlist entries are beyond the count of the new playlist entries,
# then delete those first
self.delete_orphaned_entries(playlist_db, fetched_playlist)
for entry in fetched_playlist["entries"]:
entry_db = PlaylistEntry.select_playlistentry_for_playlist(playlist_db.artificial_id, entry_num)
havetoadd = False
......@@ -289,6 +285,25 @@ class AuraCalendarService(threading.Thread):
def delete_orphaned_entries(self, playlist_db, fetched_playlist):
"""
Deletes all playlist entries which are beyond the current playlist's `entry_count`.
Such entries might be existing due to a remotely changed playlist, which now has
less entries than before.
"""
new_last_idx = len(fetched_playlist["entries"])
existing_last_idx = PlaylistEntry.count_entries(playlist_db.artificial_id)-1
if existing_last_idx < new_last_idx:
return
for entry_num in range(new_last_idx, existing_last_idx+1, 1):
PlaylistEntry.delete_entry(playlist_db.artificial_id, entry_num)
self.logger.info(SimpleUtil.yellow("Deleted playlist entry %s:%s" % (playlist_db.artificial_id, entry_num)))
entry_num += 1
def store_playlist_entry_metadata(self, entry_db, metadata):
"""
Stores the meta-data for a PlaylistEntry.
......@@ -301,20 +316,20 @@ class AuraCalendarService(threading.Thread):
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 ''" % entry_db.source)
metadata_db.artist = ""
else:
if "artist" in metadata:
metadata_db.artist = metadata["artist"]
else:
metadata_db.artist = ""
if "album" in metadata:
metadata_db.album = metadata["album"]
else:
metadata_db.album = ""
if "title" not in metadata:
self.logger.warning("Title not found in metadata for track '%s'. Setting to 'n/a'" % entry_db.source)
metadata_db.title = ""
if "title" in metadata:
metadata_db.title = metadata["title"]
else:
metadata_db.artist = metadata["title"]
metadata_db.title = ""
metadata_db.store(havetoadd, commit=True)
......
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