diff --git a/libraries/database/broadcasts.py b/libraries/database/broadcasts.py index 736f472dea52b579261c865e20be17c105c39cba..a93b8d11a8d31a1c313c6cf259926f0801d2bcd9 100644 --- a/libraries/database/broadcasts.py +++ b/libraries/database/broadcasts.py @@ -217,6 +217,13 @@ class Schedule(DB.Model, AuraDatabaseModel): return all_entries + + def __str__(self): + """ + String representation of the object. + """ + return "ID#%s [Show: %s, ShowID: %s]" % (str(self.schedule_id), self.show_name, str(self.show_id)) + # ------------------------------------------------------------------------------------------ # class Playlist(DB.Model, AuraDatabaseModel): __tablename__ = 'playlist' @@ -247,18 +254,84 @@ class Playlist(DB.Model, AuraDatabaseModel): @staticmethod def select_playlist_for_schedule(datetime, playlist_id): - return DB.session.query(Playlist).filter(Playlist.schedule_start == datetime, Playlist.playlist_id == playlist_id).first() + """ + Retrieves the playlist for the given schedule identified by `start_date` and `playlist_id` + + Args: + start_date (datetime): Date and time when the playlist is scheduled + playlist_id (Integer): The ID of the playlist + + Returns: + (Playlist): The playlist, if existing for schedule + + Raises: + Exception: In case there a inconsistent database state, such es multiple playlists for given date/time. + """ + playlists = DB.session.query(Playlist).filter(Playlist.schedule_start == datetime and Playlist.playlist_id == playlist_id).all() + if playlists and len(playlists) > 1: + raise Exception("Inconsistent Database State: Multiple playlists for given schedule '%s' and playlist id#%d available!" % (str(datetime), playlist_id)) + if not playlists: + return None + return playlists[0] + @staticmethod def select_playlist(playlist_id): - # TODO Returns multiple playlists - Hence, order playlists also by "latest version". Maybe cleanup others before? + """ + Retrieves all paylists for that given playlist ID. + + Args: + playlist_id (Integer): The ID of the playlist + + Returns: + (Array<Playlist>): An array holding the playlists + """ return DB.session.query(Playlist).filter(Playlist.playlist_id == playlist_id).order_by(Playlist.schedule_start).all() + @hybrid_property def start_unix(self): + """ + Start time of the playlist in UNIX time. + """ return time.mktime(self.schedule_start.timetuple()) + @hybrid_property + def end_unix(self): + """ + End time of the playlist in UNIX time. + """ + return time.mktime(self.schedule_start.timetuple()) + self.duration + + + @hybrid_property + def duration(self): + """ + Returns the total length of the playlist in seconds. + + Returns: + (Integer): Length in seconds + """ + total = 0 + + for entry in self.entries: + total += entry.duration + return total + + + def __str__(self): + """ + String representation of the object. + """ + time_start = self.fmt_time(self.start_unix) + time_end = self.fmt_time(self.end_unix) + return "ID#%s [items: %s, start: %s, end: %s]" % (str(self.playlist_id), str(self.entry_count), str(time_start), str(time_end)) + + + def fmt_time(self, timestamp): + return datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S') + # ------------------------------------------------------------------------------------------ # class PlaylistEntry(DB.Model, AuraDatabaseModel): __tablename__ = 'playlist_entry' @@ -273,6 +346,7 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): uri = Column(String(1024)) duration = Column(BigInteger) filename = Column(String(1024)) + entry_start = Column(DateTime) # relationships playlist = relationship("Playlist", uselist=False, back_populates="entries") @@ -282,9 +356,13 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): def select_playlistentry_for_playlist(artificial_playlist_id, entry_num): return DB.session.query(PlaylistEntry).filter(PlaylistEntry.artificial_playlist_id == artificial_playlist_id, PlaylistEntry.entry_num == entry_num).first() - # @hybrid_property - # def start_unix(self): - # return time.mktime(self.entry_start.timetuple()) + @hybrid_property + def start_unix(self): + return time.mktime(self.entry_start.timetuple()) + + @hybrid_property + def end_unix(self): + return time.mktime(self.entry_start.timetuple()) + self.duration @hybrid_property def volume(self): @@ -309,6 +387,18 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): return ScheduleEntryType.LIVE_4 + def __str__(self): + """ + String representation of the object. + """ + time_start = self.fmt_time(self.start_unix) + time_end = self.fmt_time(self.end_unix) + track = self.filename[-15:] + return "PlaylistEntry ID#%s [%s - %s | %ssec | Track: ...%s]" % (str(self.artificial_id), time_start, time_end, self.duration, track) + + def fmt_time(self, timestamp): + return datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S') + # ------------------------------------------------------------------------------------------ # class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel): __tablename__ = "playlist_entry_metadata"