Browse Source

Add id to Event, and modify main loop

Olivier Marty 8 years ago
parent
commit
8917917753
3 changed files with 22 additions and 15 deletions
  1. 5 2
      event.py
  2. 3 3
      gcal.py
  3. 14 10
      main.py

+ 5 - 2
event.py

@@ -2,17 +2,20 @@ import heapq
 
 class Event():
     """
+      id : str
       date : type datetime.datetime
       location : str
       description : str
     """
-    def __init__(self, date, location, description):
+    def __init__(self, id, date, location, description):
+        self.id = id
         self.date = date
         self.location = location
         self.description = description
 
     def __str__(self):
-        return """<Date>: """ + str(self.date) + """
+        return """<Id>: """ + str(self.id) + """
+<Date>: """ + str(self.date) + """
 <Location>: """ + self.location + """
 <Description>:  + """ + self.description
 

+ 3 - 3
gcal.py

@@ -72,16 +72,16 @@ def find_list_event_gcal(events):
         else:
             # TODO strptime, quel est le format dans ce cas ?
             date = event['start']['date']
-        
+
         date = date.replace(tzinfo = None)
         location = event.get('location', b'')
         if (not isinstance(location,str)):
             location = location.decode()
-            
+
         description = event.get('description', 'no description')
         if (not isinstance(description,str)):
             description = description.decode()
-        list_event.append(Event(date, location, description))
+        list_event.append(Event('gcal_' + event['id'], date, location, description))
     return list_event
 
 def get_list_event_gcal():

+ 14 - 10
main.py

@@ -19,20 +19,23 @@ def make_tz_aware(dt, tz='UTC', is_dst=None):
 
 def main():
     heap = HeapEvent()
-    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=10), "Villejuif", "descr Villejuif"))
-    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=3), "Cachan", "descr Cachan"))
-    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=12), "université paris 7", "descr p7"))
+    heap.push(Event('manual_1', datetime.now()+timedelta(minutes=30, seconds=10), "Villejuif", "descr Villejuif"))
+    heap.push(Event('manual_2', datetime.now()+timedelta(minutes=30, seconds=3), "Cachan", "descr Cachan"))
+    heap.push(Event('manual_3', datetime.now()+timedelta(minutes=30, seconds=12), "université paris 7", "descr p7"))
     gap = timedelta(minutes=30) # 30 minutes : time to check trafic before an event
-
-    list_event = get_events()
-    for event in list_event:
-        heap.push(event)
-
+    refresh = timedelta(seconds=30) # grab events every 30 secondes
     while True:
-        # TODO feed heap
+        # feed heap
+        ids = [e[1].id for e in heap.data]
+        for event in get_events():
+            # check if we already know it
+            if event.id not in ids:
+                print("Add event:")
+                print(str(event))
+                heap.push(event)
 
         # sleep the min between 1 minute and the next event - gap
-        next = timedelta(seconds=1) # TODO 1 minute
+        next = refresh
         if not heap.empty():
             next = min(next, heap.top().date-datetime.now()-gap)
         if next.total_seconds() > 0:
@@ -42,6 +45,7 @@ def main():
         # next event
         if not heap.empty() and heap.top().date-datetime.now() < gap:
             event = heap.pop()
+            print("Check event:")
             print(str(event))
 
             # get useful ids of sources for this location