""" This script can be used to add a list of yearly events (birthdays, anniversaries, etc.) to a Google Calendar. It reads the info from a simple CSV (comma separated values) file and adds the events to the calendar. The CSV file called events.csv should look like this: # Empty lines and lines beginning with # are ignored # The rest of the file should be event, date pairs with the date in DD/MM/YYYY format: Momo's Birthday, 15/06/1980 Shuki's Birthday, 01/12/1958 """ from elementtree import ElementTree from gdata.calendar.service import CalendarService from gdata.calendar import CalendarEventEntry, Recurrence import atom import re ENCODING = "cp1255" # windows hebrew encoding # ENCODING = "ascii" # uncomment for a standard ascii file USER = "egooli@gmail.com" PASS = "ashkara" SOURCE_FILE = "events.csv" CALENDAR_NAME = "Events" class GoogleCalendar: def __init__(self, username, password): self.username = username self.password = password self.service = CalendarService() self.service.email = 'egooli@gmail.com' self.service.password = 'ashkara' self.service.source = 'Python Auto Events Adder' self.service.ProgrammaticLogin() def add_event(self, title, date, calendar='/calendar/feeds/default/private/full'): # date should be in DD/MM/YYYY format matches = re.match(r"^(\d\d)/(\d\d)/(\d\d\d\d)$", date) day, month, year = matches.groups(0) startDate = "%s%s%s" % (year, month, day) endDate = "%s%s%02d" % (year, month, int(day)+1) recurrence_data = ('DTSTART;VALUE=DATE:%(startDate)s\r\n' + 'DTEND;VALUE=DATE:%(endDate)s\r\n' + 'RRULE:FREQ=YEARLY;\r\n') % locals() event = CalendarEventEntry() event.author.append(atom.Author(name=atom.Name(text=self.username))) event.title = atom.Title(text=title) # Set a recurring event to the specified calendar event.recurrence = Recurrence(text=recurrence_data) newEvent = self.service.InsertEvent(event, calendar) return newEvent def get_calendars(self): calendars = {} feed = self.service.GetCalendarListFeed() for cal in feed.entry: calendars[cal.title.text] = cal.link[0].href return calendars def main(): import codecs gcal = GoogleCalendar(USER, PASS) calendars = gcal.get_calendars() for line in codecs.open(SOURCE_FILE, "r", ENCODING): line = line.strip() if line and line[0] != "#": values = line.split(u",") event = values[0] date = values[1] gcal.add_event(event, date, calendars[CALENDAR_NAME]) if __name__ == "__main__": main()