|
|
|
@ -1,12 +1,26 @@ |
|
|
|
from gapi.api import API |
|
|
|
if __name__ == "__main__": |
|
|
|
from api import API |
|
|
|
else: |
|
|
|
from gapi.api import API |
|
|
|
|
|
|
|
import datetime |
|
|
|
import argparse |
|
|
|
import tzlocal |
|
|
|
from dateutil import rrule,tz |
|
|
|
from dateutil.parser import parse as date_parse |
|
|
|
from oauth2client import tools |
|
|
|
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() |
|
|
|
APPLICATION_NAME = 'Google Calendar API Python' |
|
|
|
|
|
|
|
def dateTime(datetime): |
|
|
|
SUN = 0 |
|
|
|
MON = 1 |
|
|
|
TUE = 2 |
|
|
|
WED = 3 |
|
|
|
THU = 4 |
|
|
|
FRI = 5 |
|
|
|
SAT = 6 |
|
|
|
|
|
|
|
def to_dateTime(datetime): |
|
|
|
if not datetime.tzinfo: |
|
|
|
datetime = datetime.astimezone() |
|
|
|
zone = tzlocal.get_localzone().zone |
|
|
|
@ -15,42 +29,86 @@ def dateTime(datetime): |
|
|
|
"timeZone":zone, |
|
|
|
"dateTime":datetime, |
|
|
|
} |
|
|
|
def from_dateTime(dateTime): |
|
|
|
timezone = tz.gettz(dateTime['timeZone']) |
|
|
|
datetime = date_parse(dateTime['dateTime']) |
|
|
|
datetime.replace(tzinfo=timezone) |
|
|
|
return datetime |
|
|
|
|
|
|
|
#~~BODY EXAMPLE~~# |
|
|
|
# {'attendees': [{'email': 'lpage@example.com'}, |
|
|
|
# {'email': 'sbrin@example.com'}], |
|
|
|
# 'description': "A chance to hear more about Google's developer products.", |
|
|
|
# 'end': {'dateTime': '2015-05-28T17:00:00-07:00', |
|
|
|
# 'timeZone': 'America/Los_Angeles'}, |
|
|
|
# 'location': '800 Howard St., San Francisco, CA 94103', |
|
|
|
# 'recurrence': ['RRULE:FREQ=DAILY;COUNT=2'], |
|
|
|
# 'reminders': {'overrides': [{'method': 'email', 'minutes': 1440}, |
|
|
|
# {'method': 'popup', 'minutes': 10}], |
|
|
|
# 'useDefault': False}, |
|
|
|
# 'start': {'dateTime': '2015-05-28T09:00:00-07:00', |
|
|
|
# 'timeZone': 'America/Los_Angeles'}, |
|
|
|
# 'summary': 'Google I/O 2015'} |
|
|
|
|
|
|
|
class event: |
|
|
|
def __init__(self, |
|
|
|
start, |
|
|
|
end, |
|
|
|
summary, |
|
|
|
name, |
|
|
|
description = None, |
|
|
|
recurrence = None |
|
|
|
recurrence = None, |
|
|
|
location = None, |
|
|
|
id = None |
|
|
|
): |
|
|
|
self.start = start, |
|
|
|
self.end = end, |
|
|
|
self.summary = summary, |
|
|
|
self.description = description, |
|
|
|
self.start = start |
|
|
|
self.end = end |
|
|
|
self.summary = name |
|
|
|
self.description = description |
|
|
|
self.location = location |
|
|
|
def add_reminder(self,minutes_until,method = 'popup'): |
|
|
|
pass |
|
|
|
def add_weekly_recurrence(self,*days): |
|
|
|
pass |
|
|
|
def json(self): |
|
|
|
self.id = id |
|
|
|
self.reminders = {'useDefault':True} |
|
|
|
|
|
|
|
def add_reminder(self,until,method = 'popup'): |
|
|
|
self.recurrence['useDefault'] = False |
|
|
|
if isinstance(until,datetime.timedelta): |
|
|
|
minutes_until = until.days * 24 * 60 |
|
|
|
minutes_until += until.seconds//60 |
|
|
|
else: |
|
|
|
minutes_until = until |
|
|
|
self.recurrence['overrides'] = { |
|
|
|
'method':minutes_until, |
|
|
|
'method':method, |
|
|
|
} |
|
|
|
|
|
|
|
def add_weekly_recurrence(self,until,*days): |
|
|
|
ret = rrule.rrule(freq=rrule.WEEKLY,dtstart=self.start,wkst=SUN,until=until,byweekday=days) |
|
|
|
ret_str = str(ret).split('\n')[-1] |
|
|
|
ret_str=re.sub(r'(UNTIL=[^;]+)',r'\1Z',ret_str) |
|
|
|
try: |
|
|
|
self.recurrence |
|
|
|
except AttributeError: |
|
|
|
self.recurrence = [] |
|
|
|
self.recurrence.append(ret_str) |
|
|
|
|
|
|
|
def to_json(self): |
|
|
|
keys = ('attendees', 'description', 'location', 'recurrence', 'reminders', 'summary') |
|
|
|
ret = { |
|
|
|
'start': to_dateTime(self.start), |
|
|
|
'end': to_dateTime(self.end), |
|
|
|
} |
|
|
|
for key in keys: |
|
|
|
try: |
|
|
|
value = self.__getattribute__(key) |
|
|
|
if value: |
|
|
|
ret[key] = value |
|
|
|
except AttributeError: |
|
|
|
pass |
|
|
|
return ret |
|
|
|
|
|
|
|
def add_attendees(self): |
|
|
|
pass |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def from_json(cls,body): |
|
|
|
args = {} |
|
|
|
args['name'] = body.get('summary','unamed') |
|
|
|
args['start'] = from_dateTime(body['start']) |
|
|
|
args['end'] = from_dateTime(body['end']) |
|
|
|
instance = cls(**args) |
|
|
|
keys = ('attendees', 'description', 'location', 'recurrence', 'reminders', 'summary') |
|
|
|
for key in keys: |
|
|
|
try: |
|
|
|
args[key] = body[key] |
|
|
|
except KeyError: |
|
|
|
pass |
|
|
|
|
|
|
|
class calendar_api(API): |
|
|
|
def __init__(self, |
|
|
|
app_name, |
|
|
|
@ -117,4 +175,17 @@ class calendar_api(API): |
|
|
|
break |
|
|
|
return ret |
|
|
|
if __name__ == "__main__": |
|
|
|
my_api = calendar_api(APPLICATION_NAME,r'..\test\calendar\client_secret.json',r'..\test\calendar') |
|
|
|
example = {'attendees': [{'email': 'lpage@example.com'}, |
|
|
|
{'email': 'sbrin@example.com'}], |
|
|
|
'description': "A chance to hear more about Google's developer products.", |
|
|
|
'end': {'dateTime': '2015-05-28T17:00:00-07:00', |
|
|
|
'timeZone': 'America/Los_Angeles'}, |
|
|
|
'location': '800 Howard St., San Francisco, CA 94103', |
|
|
|
'recurrence': ['RRULE:FREQ=DAILY;COUNT=2'], |
|
|
|
'reminders': {'overrides': [{'method': 'email', 'minutes': 1440}, |
|
|
|
{'method': 'popup', 'minutes': 10}], |
|
|
|
'useDefault': False}, |
|
|
|
'start': {'dateTime': '2015-05-28T09:00:00-07:00', |
|
|
|
'timeZone': 'America/Los_Angeles'}, |
|
|
|
'summary': 'Google I/O 2015'} |
|
|
|
e = event.from_json(example) |