後輩「ツイートをGoogleカレンダーに登録して、みんなの予定管理ができるようにできないかなぁ。」
寝る前にTwitter見るかなぁ、とTLを眺めてたら某後輩が「ツイートをGoogleカレンダーに登録して、みんなの予定管理ができるようにできないかなぁ。」などといったことを呟いていた。
ついこないだ初めて会ったような気がしていて、そのときはまだ1年生の終わりだか2年生の始まりだったのに、もう4年生になっていて、サークルの部室の管理とか、そういうのやってるのかな、などと、後輩の年齢や学年は本当に分からない。
後輩たちは後輩というクラスのメンバーであって、そのクラスには下位クラスは特にないのです。
といった経緯で、0.5時間弱を掛けて「なんとなく今度国立行った時に遊んでもらおー」とか考えながら、なんとなくコードを書いた。特に面白いことはしてません。
#!/usr/bin/env python # -*- coding: utf-8 -*- import tweepy import gdata.calendar.service import gdata.service import atom.service import gdata.calendar import getopt, sys, string, time, atom # Googleカレンダーにログイン calendar_service = gdata.calendar.service.CalendarService() calendar_service.email = 'あなたのGmailアドレス' calendar_service.password = 'あなたのパスワード' calendar_service.source = 'Twitter2Calendar' calendar_service.ProgrammaticLogin() # *******は、hogehoge@gmail.comの場合、hogehoge%40gmail.com(初期設定) feedURI = 'http://www.google.com/calendar/feeds/********/private/full' # dev.twitter.comからアプリケーションを登録して取得 consumer_token = "" consumer_secret = "" access_token = "" access_token_secret = "" # TwitterにOAuth認証 auth = tweepy.OAuthHandler(consumer_token, consumer_secret) auth.set_access_token(access_token, access_token_secret) oauth_api = tweepy.API(auth) # Googleカレンダーにイベントを作成する def creatEvent(title, start, end): event = gdata.calendar.CalendarEventEntry() event.title = atom.Title(text = title) event.when.append(gdata.calendar.When(start_time = start, end_time = end)) new_event = calendar_service.InsertEvent(event, feedURI) # Twitterのメンションを20件取得し、 # 「@hoge 09-18T15:00,09-18T18:00,カフェに行く」という形式のものを # パースして、Googleカレンダーにイベントを作成する。 # (*正規表現使って精度を上げたいけど、眠いからまた今度やる。) def parseTweetandCreateEvent(): mentions = oauth_api.mentions() for mention in mentions: len_screen_name = len(mention.in_reply_to_screen_name) + 2 calData = mention.text[len_screen_name:].split(",") if len(calData) == 3: try: start = u'2012-' + calData[0] + u':00.000+09:00' end = u'2012-' + calData[1] + u':00.000+09:00' title = calData[2] creatEvent(title, start, end) except: pass if __name__ == '__main__': parseTweetandCreateEvent()
※これをcronとかに設定して、自動実行させるためにはFLOWERSのカレーとハチティーが必要です。