2 # Ptyhon script to send a notification when an event with an alert occurs
3 # Source: http://julien.danjou.info/blog/2012/google-calendar-pynotify
4 # By Julien Danjou <julien@danjou.info>
6 # First, we need to import GTK+ and pynotify, and initialize it.
9 pynotify.init(sys.argv[0])
11 # Then, we need to import gdata Calendar API and connect to the calendar. I'll
12 # use the simple email/password way to login, which is clearly not the best, but
13 # it's also the simplest. Feel free to use OAuth 2.0. :-)
14 calendar_service = gdata.calendar.service.CalendarService()
15 calendar_service.email = 'mygooglelogin'
16 calendar_service.password = 'mygooglepassword'
17 calendar_service.ProgrammaticLogin()
19 # Now we're ready to request stuff and notify! First, request the events from
20 # the default calendar.
21 feed = calendar_service.GetCalendarEventFeed()
23 # Now we can iterate over feed and do various checks.
24 for event in feed.entry:
25 # If the event status is not confirmed, go to the next event.
26 if event.event_status.value != "CONFIRMED":
28 # Now iterate over all the event dates (usually it has one)
29 for when in event.when:
30 # Parse start and end time
32 start_time = datetime.datetime.strptime(when.start_time.split(".")[0], "%Y-%m-%dT%H:%M:%S")
33 end_time = datetime.datetime.strptime(when.end_time.split(".")[0], "%Y-%m-%dT%H:%M:%S")
35 # ValueError happens on parsing error. Parsing errors
36 # usually happen for "all day" events since they have
37 # not time, but we do not care about this events.
39 now = datetime.datetime.now()
40 # Check that the event hasn't already ended
43 for reminder in when.reminder:
44 # We handle only reminders with method "alert"
45 # and whose start time minus the reminder delay has passed
46 if reminder.method == "alert" \
47 and start_time - datetime.timedelta(0, 60 * int(reminder.minutes)) < now:
48 # Build the notification
49 notification = pynotify.Notification(summary=event.title.text,
50 message=event.content.text)
51 # Set an icon from the GTK+ stock icons
52 notification.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_DIALOG_INFO,
53 gtk.ICON_SIZE_LARGE_TOOLBAR))
54 notification.set_timeout(0)
55 # Show the notification