[PYTHON] Generate daily reports from Google Calendar

My company used Google Calrendar to write that "what I did today" in my daily report should be pulled from the calendar.

You need to install gdata and dateutil from PyPI.

daily.py


# -*- coding: utf-8 -*-

import gdata.acl.data
import gdata.calendar.data
import gdata.calendar.service
import gdata.service

from datetime import date, timedelta
from dateutil import parser
import re


class DailyReport(object):
    def __init__(self, email, password, name, when=0):
        self._name     = name
        self.set_date(when)
        self._password = password
        self._email    = email

        self._calendar   = gdata.calendar.service.CalendarService()

    def set_date(self, when=0):
        today = date.today()
        t = timedelta(days = when * -1)
        d = today - t

        #Since it is a business day, I will remove Saturdays and Sundays.
        if d.weekday() >= 5:
            t = timedelta(days = d.weekday() - 4)
            self._date = d - t
        else:
            self._date = d

    def login(self):
        self._calendar.email = self._email
        self._calendar.password = self._password
        self._calendar.source = 'daily_report'
        self._calendar.ssl = True
        self._calendar.ProgrammaticLogin()

    def get_schedule(self):
        query = self.get_calendar_event_query()
        feed = self._calendar.CalendarQuery(query)

        schedule = []
        for i, event in zip(xrange(len(feed.entry)), feed.entry):
            row = {}
            row['subject'] = event.title.text

            if event.when and event.when[0].start_time:
                row['start_time'] = parser.parse( event.when[0].start_time ).strftime('%H:%M')

            if event.when and event.when[0].end_time:
                row['end_time'] = parser.parse( event.when[0].end_time ).strftime('%H:%M')

            schedule.append(row)

        self._schedule = schedule
        return schedule


    def get_calendar_event_query(self):
        query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full')
        query.start_min = self._date.strftime('%Y-%m-%dT00:00:00')
        query.start_max = self._date.strftime('%Y-%m-%dT23:59:59')
        query.orderby   = 'starttime'
        query.sortorder = 'ascending'
        query.max_results = 200

        return query

    def report_with_template(self, template):
        template = re.sub("__subject__", self._date.strftime('%Y-%m-%d'), template)
        template = re.sub("__name__",    self._name, template)
        template = re.sub("__contents__", self.make_contents_from_schedule(), template)
        return template


    def make_contents_from_schedule(self):
        contents = ""
        for item in self._schedule:
            contents += "%s〜%s %s\n" % (item['start_time'], item['end_time'], item['subject'])

        return contents

template = """
[Daily report]__name__ __subject__


◆ This month's goal


◆ Today's business
__contents__


◆ Tomorrow's schedule


◆ Share, information, etc.
"""


if __name__ == "__main__":
    dr = DailyReport("your email", "your password", "your name", 0)
    dr.login()
    dr.get_calendar_event_query()
    dr.get_schedule()
    print dr.report_with_template(template)

The fourth argument to pass to DailyReport is 0 for today and -1 for yesterday. Sometimes I write a daily report the next day. I wonder if this should be taken with raw_input.

By the way, if you are using 2-step authentication, you cannot use it unless you generate a password for the application. I hope I can do it with OAuth, but is that okay?

Recommended Posts

Generate daily reports from Google Calendar
Access Google Calendar from the iOS app
Import the schedule obtained from "Schedule-kun" into Google Calendar
Scraping immediately from google images!
Generate URL query from JSON