Browse Source

Change Event and adapt gcal.py

Olivier Marty 8 years ago
parent
commit
bf3e9f157b
2 changed files with 29 additions and 72 deletions
  1. 10 18
      event.py
  2. 19 54
      gcal.py

+ 10 - 18
event.py

@@ -1,24 +1,21 @@
 import heapq
 
 class Event():
-    def __init__(self, date, location, description, eid = "c"):
+    """
+      date : type datetime.datetime
+      location : str
+      description : str
+    """
+    def __init__(self, date, location, description):
         self.date = date
         self.location = location
         self.description = description
-        self.eid = eid
 
     def affiche(self):
-        print('<Date>: %s' % self.date)
-        print('<Location>: %s' % self.location)
-
-        if (self.eid == "m"):
-            print('<Description>: %s' % self.description.decode())
-        elif (self.eid == "c"):
-            print('<Body>: %s' % self.description)       
-              
-    def problem(self):
-        #TODO: Call the function of RATP
-        return "TODO"
+        print('<Date>: ' + str(self.date))
+        print('<Location>: ' + self.location)
+        print('<Description>: ' + self.description)
+
 
 class HeapEvent():
     """Heap for event : sort event according to their dates"""
@@ -36,8 +33,3 @@ class HeapEvent():
 
     def empty(self):
         return not self.data
-
-        
- 
-    
-

+ 19 - 54
gcal.py

@@ -6,9 +6,9 @@ from apiclient import discovery
 import oauth2client
 from oauth2client import client
 from oauth2client import tools
-from event import *
+from event import Event
 
-import datetime
+from datetime import datetime
 
 try:
     import argparse
@@ -55,61 +55,27 @@ def get_credentials():
 
 def find_list_event_gcal(events):
     list_event = []
-    
+
     if not events:
         print('No upcoming events found.')
         return list_event
 
-    i = 0
-
     for event in events:
-        i = i + 1
-        #print('The event #', i,'------------------------')
-        start = event['start'].get('dateTime', event['start'].get('date'))
-
-        #status :  confirmed or not
-        status = event['status']
-        #print('  Status:', event['status'])     
-        
-        #htmlLink:  the url of the event
-        #print('  HtmlLink:', event['htmlLink'])
-        
-        #created:   the created time of the event
-        #print('  Created time:', event['created'])
-        
-        #updateed:   the updated time of the event
-        #print('  Updatedtime:', event['updated'])
-        
-        body = ""
-        if ('description' in event):
-            body = event['description']
-            #print('  Description:', event['description'])
-       
-        subject = ""
-        if ('summary' in event):
-            subject = event['summary']
-            #print('  Sumarry:', event['summary'])
-        
-        location = ""
-        if ('location' in event):
-            location = event['location']
-            if (type(location) == 'bytes'):
-                location = location.decode()
-            #print('  Location:', event['location'])
-
-        #print("  Organizer's name:", event['organizer'].get('displayName', 'unknown'))
-        withwho =  event['organizer'].get('displayName', 'unknown')
-
-        #print("  Organizer's email:", event['organizer']['email'])
-        withwhomail =  event['organizer']['email']
-
-        #print("  Start time:", event['start']['dateTime'])
-        #print(start, event['summary']);
-
-
-        list_event.append(Event(start,location,body,"c"))
-        return list_event
- 
+        if 'dateTime' in event['start']:
+            # replace last ':' by ''
+            start = event['start']['dateTime']
+            start = start.rsplit(':', 1)
+            start = "".join(start)
+            date = datetime.strptime(start, '%Y-%m-%dT%H:%M:%S%z')
+        else:
+            # TODO strptime, quel est le format dans ce cas ?
+            date = event['start']['date']
+
+        location = event.get('location', b'').decode()
+        description = event.get('description', 'no description')
+        list_event.append(Event(date, location, description))
+    return list_event
+
 def get_list_event_gcal():
     """Shows basic usage of the Google Calendar API.
 
@@ -120,7 +86,7 @@ def get_list_event_gcal():
     http = credentials.authorize(httplib2.Http())
     service = discovery.build('calendar', 'v3', http=http)
 
-    now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
+    now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
     print('Getting the upcoming 10 events...')
     eventsResult = service.events().list(
         calendarId='primary', timeMin=now, maxResults=10, singleEvents=True,
@@ -128,4 +94,3 @@ def get_list_event_gcal():
     events = eventsResult.get('items', [])
 
     return find_list_event_gcal(events)
-