-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoogleCalendar.py
executable file
·70 lines (60 loc) · 2.37 KB
/
GoogleCalendar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
import atom
import datetime
import getpass
import gdata.calendar.service
BadAuthentication = gdata.service.BadAuthentication
class UTC(datetime.tzinfo):
def utcoffset(self, date):
return datetime.timedelta()
def dst(self, date):
return datetime.timedelta()
def tzname(self, date):
return "UTC"
class LOCAL(datetime.tzinfo):
timediff = datetime.datetime.now().replace(second=0,microsecond=0) - \
datetime.datetime.utcnow().replace(second=0,microsecond=0)
def utcoffset(self, date):
return self.timediff
def dst(self, date):
return datetime.timedelta()
def tzname(self, date):
return "localtime"
def fixNaiveDatetime(dt):
'''If dt is a naive datetime, add our best guess of local timezone'''
if dt.utcoffset() is None:
return dt.replace(tzinfo=LOCAL())
class GoogleCalendar():
def __init__(self, user, passwd,
calendarpath='/calendar/feeds/default/private/full'):
self.user = user
self.calendarpath = calendarpath
self.service = gdata.calendar.service.CalendarService()
self.service.ssl = True
self.service.ClientLogin(user,passwd)
def _doInsertEvent(self, title, when, content, where):
event = gdata.calendar.CalendarEventEntry()
event.title = atom.Title(text=title)
if content:
event.content = atom.Content(text=content)
if where:
event.where = gdata.calendar.Where(where)
event.when.append(when)
return self.service.InsertEvent(event, self.calendarpath)
def insertAllDayEvent(self, title, date, content=None, where=None):
when = gdata.calendar.When(
start_time = date.strftime('%F'),
end_time = (date+datetime.timedelta(days=1)).strftime('%F')
)
return self._doInsertEvent(title, when, content, where)
def insertEvent(self, title, start, end=None, duration=None,
content=None, where=None):
if not (end or duration):
raise ValueError("must specify end time or duration")
if not end:
end = start + duration
when = gdata.calendar.When(
start_time = fixNaiveDatetime(start).isoformat(),
end_time = fixNaiveDatetime(end).isoformat()
)
return self._doInsertEvent(title, when, content, where)