Browse Source

Tested Event methods and fixed some bugs (python api only takes kwargs)

master
Raphael Roberts 7 years ago
parent
commit
230a3a7eb4
  1. 49
      gapi/calendar_api.py

49
gapi/calendar_api.py

@ -22,7 +22,7 @@ FRI = 5
SAT = 6
def to_dateTime(datetime):
def to_dateTime(datetime: datetime.datetime):
"""converts a datetime into json format for rest api"""
if not datetime.tzinfo:
datetime = datetime.astimezone()
@ -65,7 +65,8 @@ class calendar_api(API):
pass
service = self.service
event_service = service.events()
event_service.insert(calendarId=calendar_id, body=body).execute()
event = event_service.insert(
calendarId=calendar_id, body=body).execute()
return event['id']
def update_event(self, calendar_id, event_id, body):
@ -123,30 +124,39 @@ class calendar_api(API):
"""Retrieves event from cal_id and event_id"""
service = self.service
event_service = service.events()
return event_service.get(cal_id, event_id).execute()
return event_service.get(calendarId=cal_id, eventId=event_id).execute()
class event:
class Event:
"""Model for a calendar event that can be uploaded"""
def __init__(self, api,
def __init__(self,
start,
end,
name,
description=None,
recurrence=None,
reminders=None,
attendees=None,
location=None,
id=None
):
self.start = start
self.attendees = attendees
self.end = end
self.summary = name
self.name = name
self.description = description
self.location = location
self.id = id
if reminders is None:
self.reminders = {'useDefault': True}
else:
self.reminders = reminders
def add_reminder(self, until, method='popup'):
"""Add a reminder minutes before an event.
Use either a notification (popup) or email"""
assert method in ("email", "popup")
self.reminders['useDefault'] = False
if isinstance(until, datetime.timedelta):
minutes_until = until.days * 24 * 60
@ -154,8 +164,8 @@ class event:
else:
minutes_until = until
self.reminders.setdefault('overrides', []).append({
'minuutes': minutes_until,
'method': method,
'minutes': minutes_until,
})
def add_weekly_recurrence(self, until: datetime.datetime, *days):
@ -171,8 +181,9 @@ class event:
def to_json(self):
keys = ('attendees', 'description', 'location',
'recurrence', 'reminders', 'summary')
'recurrence', 'reminders')
ret = {
'summary': self.name,
'start': to_dateTime(self.start),
'end': to_dateTime(self.end),
}
@ -194,14 +205,14 @@ class event:
args['name'] = body.get('summary', 'unnamed')
args['start'] = from_dateTime(body['start'])
args['end'] = from_dateTime(body['end'])
instance = cls(**args)
keys = ('attendees', 'description', 'location',
'recurrence', 'reminders', 'summary')
'recurrence', 'reminders', 'id')
for key in keys:
try:
args[key] = body[key]
except KeyError:
pass
instance = cls(**args)
return instance
@classmethod
@ -214,12 +225,14 @@ class event:
"""Upload an event to calendar.
Either modifies an event in place or creates a new event."""
if self.id is not None:
api.update_event(calendar_id, self.id, self.to_json)
event_id = api.update_event(calendar_id, self.id, self.to_json())
else:
api.create_event(calendar_id, self.to_json)
event_id = api.create_event(calendar_id, self.to_json())
return event_id
if __name__ == "__main__":
import os
# ~~BODY EXAMPLE~~##~~BODY EXAMPLE~~
example = {
'attendees': [{'email': 'lpage@example.com'},
@ -245,4 +258,14 @@ if __name__ == "__main__":
'timeZone': 'America/Los_Angeles'},
'summary': 'Google I/O 2015'
}
e = event.from_json(example)
e = Event(
date_parse('march 16, 2019 10:00 am'),
date_parse('march 16, 2019 3:30 pm'),
'Hang out with Matt'
)
path = r"C:\Users\Raphael\Documents\local_repo\cred"
my_api = my_api = calendar_api('python', os.path.join(
path, 'client_secret.json', path), path)
cal_id = 'raphael.roberts48@gmail.com'
e2 = Event.from_id(my_api, cal_id, 'qmrsd88ma8ko67ri98d8pbhd7s')
# e2.upload(my_api, cal_id)
Loading…
Cancel
Save