]> git.0d.be Git - django-panik-nonstop.git/commitdiff
stamina: compute shorter playlists
authorFrédéric Péters <fpeters@0d.be>
Fri, 19 Jun 2020 17:02:16 +0000 (19:02 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 2 Jul 2020 08:01:29 +0000 (10:01 +0200)
nonstop/management/commands/stamina.py

index 246ba52d8bb56ca155369b98b1a9c88bf204cf1c..09b5e1bd71dc232cbf25b969e93f2f439d7fc744 100644 (file)
@@ -24,6 +24,13 @@ class Command(BaseCommand):
         current_datetime = start_datetime
         if self.last_jingle_datetime is None:
             self.last_jingle_datetime = current_datetime
         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:
         playlist = []
         adjustment_counter = 0
         try:
@@ -32,6 +39,9 @@ class Command(BaseCommand):
             jingles = []
 
         while current_datetime < end_datetime and adjustment_counter < 5:
             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
 
             if jingles and current_datetime - self.last_jingle_datetime > datetime.timedelta(minutes=20):
                 # jingle time, every ~20 minutes