Twitter4J でざっくりデータを引っこ抜く
事前に https://dev.twitter.com/ でアカウント登録と、アプリケーション登録を済ませておくこと。
参考はこれ
使うのはこれ
http://twitter4j.org/ja/index.html
で、application.conf *1に
twitter {
consumerKey = "ZZZZZZZZZZZ...."
consumerSecret = "AAAAAAAAAAA...."
accessToken = "CCCCCCCCCCC...."
accessSecret = "LLLLLLLLLLL...."
}
で、Scala側でほげほげっと
package tweets
import com.typesafe.config.ConfigFactory
import twitter4j.TwitterFactory
import java.util.Properties
import twitter4j.conf.PropertyConfiguration
object Sample extends App {
val conf = ConfigFactory.load()
val prop = new Properties()
prop.setProperty("oauth.consumerKey", conf.getString("twitter.consumerKey"))
prop.setProperty("oauth.consumerSecret", conf.getString("twitter.consumerSecret"))
prop.setProperty("oauth.accessToken", conf.getString("twitter.accessToken"))
prop.setProperty("oauth.accessTokenSecret", conf.getString("twitter.accessSecret"))
val twitter = new TwitterFactory(new PropertyConfiguration(prop)).getInstance()
import scala.collection.JavaConversions._
twitter.getHomeTimeline.filter(_.getUser.getName != "buzztter").foreach(status => println("%s : %s".format(status.getUser.getName, status.getText)))
}すると(公開ツイートのみ)
SR[アイドライジング]猫ロキP+ : ただいまー! ・友達を新しく招待する呼ぶ まであと 12:23:01金が切れて力が出ない」 H.Hiro : ふと思ったこと:GoogleとかのWeb検索エンジンって、検索結果を即座に得られるように索引が必要なわけだけど、Webページの量に対して索引がどのくらい大きいのだろうか。例え ば「Webページ1TBあたり索引はおよそ100GB必要」みたいな。 H.Hiro : RT @hoppouSGM: 来週は北大祭ですね.ニコニコBAR北大支店へぜひお立ち寄りください. http://t.co/PDGP4EmaAt #hsgm しばやん : RT @pani_op: だるさん、男性と既婚だったとは Shinya Okano : RT @wozozo: django.contrib.comments も deprecated。django-admin.py diffsettings で settings.py のデフォルトとの diff を表示してくれるコマンドは良さげ Shinya Okano : RT @wozozo: django.db.models.BinaryField が増えた。EmailField が type=“email” とかの html5 っぽい出力できるようになった。 PIL が deprecation になって Pillow に Shinya Okano : RT @wozozo: デフォで BASE_DIR って変数定義、admin と i18n と timezone 有効、clickjacking も有効とかに変わってる Shinya Okano : RT @wozozo: django 1.6 の startproject したときの settings.py の行数、半分以下になってるな しばやん : RT @daruyanagi: @shibayan 「アラブの石油王をやっていた夫が、つちのこに噛まれて死にました……」 地震速報 : [気象庁情報]27日 22時03分頃 大分県中部(N33.3/E131.4)にて 最大震度1(M1.9)の地震が発生。 震源の深さは10km未満。( http://t.co/JjW5Wtwvf6 ) #saigai #jishin #earthquake SR[アイドライジング]猫ロキP+ : RT @kirisakineko: 「オリンライジング!」1巻読了。素晴らしい外伝であった。ケルベロスのあのあたりとか読んでて涙ぐんでたよ。いい漫画家に出会った なあ。「アイドライジング!」原作ファンはもちろん原作知らないひとも(特にモバマス愛好家には)超オススメ! [success] Total time: 2 s, completed 2013/05/27 22:42:55
で、お次はストリームで引っこ抜く。
package tweets
import com.typesafe.config.ConfigFactory
import twitter4j._
import java.util.Properties
import twitter4j.conf.PropertyConfiguration
object Sample extends App {
val conf = ConfigFactory.load()
val prop = new Properties()
val listener = new StatusListener {
def onStallWarning(p1: StallWarning) {}
def onException(p1: Exception) {}
def onDeletionNotice(p1: StatusDeletionNotice) {}
def onScrubGeo(p1: Long, p2: Long) {}
def onStatus(p1: Status) {
println("%s : %s".format(p1.getUser.getName, p1.getText))
}
def onTrackLimitationNotice(p1: Int) {}
}
prop.setProperty("oauth.consumerKey", conf.getString("twitter.consumerKey"))
prop.setProperty("oauth.consumerSecret", conf.getString("twitter.consumerSecret"))
prop.setProperty("oauth.accessToken", conf.getString("twitter.accessToken"))
prop.setProperty("oauth.accessTokenSecret", conf.getString("twitter.accessSecret"))
val twitter = new TwitterStreamFactory(new PropertyConfiguration(prop)).getInstance()
twitter.addListener(listener)
twitter.sample()
readLine()
twitter.shutdown()
}ちなみにツイート速度甘く見たらいかん。。。。
秒間一体何百件更新あるんだ(;'∀')
*1:多分普通に Properties 使ったほうが早い