13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Calendar から日報を生成する

Last updated at Posted at 2013-04-08

うちの会社は Google Calrendar を使ってて、日報に含める「今日やったこと」的なものは calendar から引っ張ってくればいいよね、ということで書いた。

PyPI から gdata と dateutil を install する必要があります。

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

        # 営業日なので、土日を外す。
        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 = """
【日報】__name__ __subject__


◆今月の目標


◆本日の業務
__contents__


◆明日の予定


◆シェア・情報など
"""


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)

DailyReport に渡す4番目の引数は、今日なら 0、昨日なら -1。
日報を翌日に書くこともあるからね。
これは raw_input とかで取ればいいかなぁ。

ちなみに2段階認証してる場合は、パスワードにはアプリ用のパスワードを生成してあげないと使えないです。
OAuth とかでやれるといいんだけど、まぁいいか。

13
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?