current_datetime = start_datetime
if self.last_jingle_datetime is None:
self.last_jingle_datetime = current_datetime
+ # Define a max duration (1 hour), if it is reached, and far enough
+ # from end_datetime (30 minutes), return the playlist as is, not aligned
+ # on end time, so a new playlist gets computed once it's over.
+ # This avoids misalignments due to track durations not matching exactly
+ # or additional delays caused by the player program.
+ max_duration = datetime.timedelta(hours=1)
+ max_duration_leftover = datetime.timedelta(minutes=30)
playlist = []
adjustment_counter = 0
try:
jingles = []
while current_datetime < end_datetime and adjustment_counter < 5:
+ if (current_datetime - start_datetime) > max_duration and (
+ (end_datetime - current_datetime) > max_duration_leftover):
+ break
if jingles and current_datetime - self.last_jingle_datetime > datetime.timedelta(minutes=20):
# jingle time, every ~20 minutes