PublicTimelineやらFriendsTimelineやらを取ってきて保存する。
urllib2使って、xml落としてきてもいいが、認証とかあるのでメンドクサイ。
素直にGoogleCodeに上がっていたTwitter-Pythonというのを使わせてもらう。
インスコする。
#適当にDLするフォルダーに移動する。 $wget http://python-twitter.googlecode.com/files/python-twitter-0.5.tar.gz $tar xvfz python-twitter-0.5.tar.gz $cd python-twitter-0.5 $sudo python setup.py install Password:
これで使えるようになる。PublicTimelineとFriendsTimelineの取得の仕方は
$python > import twitter > api = twitter.Api(username='あなたのID', password='あなたのPass') > ptl = api.GetPublicTimeline() > print ptl パブリックタイムラインがずらずら > ftl = api.GetFriendsTimeline() > print ftl パブリックタイムラインがずらずら
こんな感じになるはずである。
これらを内容にあわせてバラバラにして、前日示したモデルにはめて保存していく。
日付の処理がよくわかんないので、Twitterからやってくる日時は文字列として保存してしまった。
Pytter/one/views.py
# TwitterからFriendTimeLineをゲットしてひたすら保存する def getFriendTimeline(): print '-- makeFriends start --' print twitter_id print twitter_password api = twitter.Api(username=twitter_id, password=twitter_password) fTimeline = api.GetFriendsTimeline() for i in fTimeline: twit_model = Twit.objects.get_or_create( date=i.created_at, text=i.text, usr_id=i.user.id, usr_name=i.user.name, usr_screen=i.user.screen_name )[0] twit_model.save() return 0 # TwitterからPublicTimeLineをゲットしてひたすら保存する def getPubTwitter(): """docstring for getPubTwitter""" api = twitter.Api(username=twitter_id, password=twitter_password) fTimeline = api.GetPublicTimeline() for i in fTimeline: twit_model = Twit.objects.get_or_create( date=i.created_at, text=i.text, usr_id=i.user.id, usr_name=i.user.name, usr_screen=i.user.screen_name )[0] print type(twit_model.text) #print type(i.text) twit_model.save() return 0