2009-11-05

PythonさんでGoogleカレンダーのイベント内容を取得する

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
PythonでGoogleカレンダーに登録されているイベントを取得してみました。

データの取得にはgdata-python-clientというのを使うのがとても便利です。
以下から取得できます。
http://code.google.com/p/gdata-python-client/

使い方は、以下を見る感じです。
http://code.google.com/intl/ja/apis/calendar/data/1.0/developers_guide_python.html

認証には、AuthSub,OAuth,ClientLoginと種類があるのですが、以下の例ではClientLoginを利用しています。

import gdata.calendar.service

def print_event():
#認証部分
username = 'グーグルアカウント'
calendar_service = gdata.calendar.service.CalendarService()
calendar_service.email = username
calendar_service.password = 'グーグルアカウントのパスワード'
#適当にプログラム名を指定すればよい感じ
calendar_service.source = 'Google_Calendar_Event_Get_test'
calendar_service.ProgrammaticLogin()

#デフォルトのカレンダーから取得する
visibility = 'private'
projection = 'full'
query = gdata.calendar.service.CalendarEventQuery(username, visibility, projection)
#検索条件の指定
#検索範囲を指定
query.start_min = '2009-11-01'
query.start_max = '2009-12-02'
#繰り返しイベントを別のイベントとして取得
query.singleevents = 'true'
#イベントの開始日時の昇順でソート
query.orderby='starttime'
query.sortorder='a'
feed = calendar_service.CalendarQuery(query)

for ev in feed.entry:
for e in ev:
print '%s. [%s]-[%s]' % (ev.title.text,e.start_time,e.end_time)


ちなみにCalendarEventQueryの検索条件に指定できるのは、以下を参照すると分かります。
http://code.google.com/intl/ja/apis/calendar/data/2.0/reference.html#Parameters
テキストで検索するには以下を参照するとわかります。
http://code.google.com/intl/ja/apis/calendar/data/1.0/developers_guide_python.html#RetrievingWithQuery

0 件のコメント: