From bf5449ce9febd51cd6ecf54882b0681f7b5c9f53 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Wed, 19 Feb 2020 14:48:27 +0100
Subject: [PATCH] Fixed filters and simplified using timestamps.

---
 modules/scheduling/calender_fetcher.py | 54 +++++++++++++-------------
 1 file changed, 26 insertions(+), 28 deletions(-)

diff --git a/modules/scheduling/calender_fetcher.py b/modules/scheduling/calender_fetcher.py
index aa63b179..238cbfc9 100644
--- a/modules/scheduling/calender_fetcher.py
+++ b/modules/scheduling/calender_fetcher.py
@@ -6,7 +6,7 @@ import simplejson
 
 from datetime import datetime, timedelta
 #from modules.models.schedule import Schedule
-
+from modules.base.simpleutil import SimpleUtil
 
 class CalendarFetcher:
     url = dict()
@@ -259,51 +259,49 @@ class CalendarFetcher:
 
 
 
-    def remove_data_more_than_24h_in_the_future(self, schedule_from_pv):
+    def remove_data_more_than_24h_in_the_future(self, schedules):
         """ 
         Removes entries 24h in the future and 12 hours in the past.
         Note: This might influence resuming (in case of a crash)  
         single schedules which are longer than 12 hours long.
         Think e.g. live broadcasts.
         """
-        act_list = []
-        now = datetime.now()
-        now_plus_24hours = now + timedelta(hours=24)
-
-        for s in schedule_from_pv:
-            date_start = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S")
+        items = []
+        now = SimpleUtil.timestamp()
+        now_plus_24hours = now + (12*60*60)
+        now_minus_12hours = now - (12*60*60)
 
-            # append only elements which are close enough to now
-            if date_start <= now_plus_24hours and date_start >= now - timedelta(hours=12):
-                act_list.append(s)
+        for s in schedules:
+            start_time = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S")
+            start_time = SimpleUtil.timestamp(start_time)
 
-        return act_list
+            if start_time <= now_plus_24hours and start_time >= now_minus_12hours:
+                items.append(s)
 
+        return items
 
 
-    def remove_data_in_the_past(self, schedule_from_pv):
+    def remove_data_in_the_past(self, schedules):
         """
         Removes all schedules from the past, except the one which is 
         currently playing.
         """
-        act_list = []
-        now = datetime.now()
-        count = len(schedule_from_pv)
-
-        for index, curr in enumerate(schedule_from_pv):
-            date_start = datetime.strptime(curr["start"], "%Y-%m-%dT%H:%M:%S")
-            if index+1 < count:
-                date_next_start = datetime.strptime(schedule_from_pv[index+1]["start"], "%Y-%m-%dT%H:%M:%S")
+        items = []
+        now = SimpleUtil.timestamp()
+        for s in schedules:
+            start_time = datetime.strptime(s["start"], "%Y-%m-%dT%H:%M:%S")
+            start_time = SimpleUtil.timestamp(start_time)
+            end_time = datetime.strptime(s["end"], "%Y-%m-%dT%H:%M:%S")
+            end_time = SimpleUtil.timestamp(end_time)
 
             # Append all elements in the future
-            if date_start >= now:
-                act_list.append(curr)
-            # Append the one which is now playing
-            elif date_start < now and date_next_start >= now:
-                act_list.append(curr)
-
-        return act_list
+            if start_time >= now:
+                items.append(s)
+             # Append the one which is playing now
+            elif start_time < now < end_time:
+                items.append(s)
 
+        return items
 
 
     # ------------------------------------------------------------------------------------------ #
-- 
GitLab