先日の#pyfesでjsonファイルを操作したことないなー、と思ったのでこんなのを作ってました。
@search_accountの最新3ツイートを取得し、それぞれ5分以内のものか判定した上でsearch wordが含まれているかを判定します。
最後の部分でメール送信なり、自分にmentionなり、boxcarにプッシュなりを設定し、
数分間隔で定期実行させればキャンペーン開始通知として使えそうです。
ところでPythonの変数って推奨の銘々則ってあるんですかね?PEPとかに。
ObjCみたくキャメルケースで書いてしまってるけど。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib2 | |
import json | |
import datetime | |
userTimeline = urllib2.urlopen('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=%s&count=3' % 'search_account') | |
contents = json.loads(userTimeline.read()) | |
nowTime = datetime.datetime.now() | |
deltaTime = datetime.timedelta(minutes = 5) | |
for i in contents: | |
tweetedTime = datetime.datetime.strptime(i['created_at'], '%a %b %d %H:%M:%S +0000 %Y') | |
if tweetedTime > nowTime - deltaTime: | |
if u'search word' in i['text']: | |
print("Let's post notification!") | |