ストリーミングAPI

ストリーミングAPI
http://yusuke.homeip.net/twitter4j/ja/javadoc/index.html
作者様に教えて頂いた、ストリーミングAPIを早速試してみよう
実のところドキュメントを読んでも(というか読めないw)あまり使い方がわからなかったりする。そしてぐぐってもサンプルは見あたらない。

とりあえずStreamAPITest.javaやTwitterStream.javaを読んで、動かしてどういう挙動をするのか確認してみよう

とりあえずnewしてみる。
TwitterStream ts = new TwitterStream(id, pass);
Twitterクラスのようにユーザーの情報が必要らしい
直感としてfirehose(int count) が臭い

Parameters:
count - Indicates the number of previous statuses to stream before transitioning to the live stream.
引数:
count - ライブストリームの移り変わりの前のストリームの以前の状態の数を指し示す。

これであってる?とりあえずよくわからないw
適当に数字を入れて実行してみよう。

import twitter4j.TwitterException;
import twitter4j.TwitterStream;

public class Test {
	public static void main(String[] args) throws TwitterException {
		String id = "id";
		String pass = "pass";
		TwitterStream ts = new TwitterStream(id, pass);
		ts.firehose(1);
	}

}
Exception in thread "main" java.lang.IllegalStateException: StatusListener is not set.
	at twitter4j.TwitterStream.startHandler(TwitterStream.java:296)
	at twitter4j.TwitterStream.firehose(TwitterStream.java:69)
	at Test.main(Test.java:9)

StatusListener is not set.ということらしい
それらしいものを探そう
APIによるとStatusListenerはインターフェースであってimplementsしなきゃいけないらしい。そして実装しなきゃいけないメソッドを追加しようと思うけど、何書けばいいかさっぱりなのでStreamAPITest.javaからパクる。

import twitter4j.Status;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;

public class Test implements StatusListener{
	public static void main(String[] args)  throws TwitterException {
		String id = "";
		String pass = "";
		TwitterStream ts = new TwitterStream(id, pass);
		ts.firehose(1);
	}

	private synchronized void notifyResponse(){
        this.notify();
    }
    Status status;

    public void onStatus(Status status) {
        this.status = status;
        System.out.println("got status-------------------:" + status);
        notifyResponse();
    }

    public void onException(Exception ex) {
        notifyResponse();
    }

}

実行しても結果は変わらず。さてもう詰まったぞw

どうもstatusListener.onStatus(status);でエラーらしい
statusListenerに値が何も入ってないのかな
TwitterStream(java.lang.String userId, java.lang.String password, StatusListener listener)
こっちでnewしてみよう。そもそもこのときStatusListener listenerってなんなんだろう・・・
interfaceを引数として与えるとかよくわからない

StatusStreamを忘れてた

import java.io.IOException;

import twitter4j.Status;
import twitter4j.StatusListener;
import twitter4j.StatusStream;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;

public class Test implements StatusListener{
	public static void main(String[] args)  throws TwitterException, IOException {
		String id = "";
		String pass = "";
		TwitterStream twitterStream = new TwitterStream(id, pass);
		StatusStream stream = twitterStream.getSpritzerStream();
		Status status;
        for(int i=0;i<10;i++){
            status = stream.next();
            System.out.println(status.getCreatedAt()+":"+status.getText()+" from:"+status.getSource());
        }
        stream.close();
	}

	private synchronized void notifyResponse(){
        this.notify();
    }
    Status status;

    public void onStatus(Status status) {
        this.status = status;
        System.out.println("got status-------------------:" + status);
        notifyResponse();
    }

    public void onException(Exception ex) {
        notifyResponse();
    }

}
Sun Jun 07 12:52:13 JST 2009:out Alaskan cruises specifically?) from:<a href="http://help.twitter.com/index.php?pg=kb.page&id=75">txt</a>
Sun Jun 07 12:52:14 JST 2009:Okay tweeple if I drop my phone in this tube water its twitter's fault. Im sitting here tweeting while in the tub ha awful from:<a href="http://help.twitter.com/index.php?pg=kb.page&id=75">txt</a>
Sun Jun 07 12:52:14 JST 2009:http://twitpic.com/6t23p - meet my new best friend... Lucy! from:<a href="http://twitpic.com/">TwitPic</a>
Sun Jun 07 12:52:14 JST 2009:@Stephimuss: and the wild life is so different and unique compared to america  i would just love to go there from:web
Sun Jun 07 12:52:14 JST 2009:im doing this all in your name my nigga!!! from:web
Sun Jun 07 12:52:14 JST 2009:@dallasbedle dayum. how do you have the best tattoos ever? yours are within the 1% of all tattoos that I actually appreciate. from:web
Sun Jun 07 12:52:14 JST 2009:@raleighgirl Thanks! from:web
Sun Jun 07 12:52:15 JST 2009:頭が痛い。寝過ぎたかな。 from:web
Sun Jun 07 12:52:15 JST 2009:I ... was just a face you never noticed, and I ... was just trying to be honest with myself ... With you ... With the world. from:web
Sun Jun 07 12:52:15 JST 2009:Back in MIA...I left the desert for lotsa rain! from:<a href="http://orangatame.com/products/twitterberry/">TwitterBerry</a>

なんか動いたぞーーー
ここまで来ればなんとかなりそう

ここではtwitterStream.getSpritzerStream();でやってるけど、他のStatusStreamを返すメソッドも実行してみよう。そうすればそれぞれの違いがなんとなくわかるはず
と思ったけど他を実行できない・・・
とりあえずtwitterStream.getSpritzerStream();で実行してからのpublictimelineの情報を得ることはできるようになった。
for()のところを永久ループにすればずっと情報を取得できるわけか、取得スピードよりpublictimelineのPOSTの方が速いw
日本語ユーザーだけ出力するようにしたけど、すべてのPOSTが表示されるわけじゃないみたい

public StatusStream getFirehoseStream(int count)

Returns a status stream for all public statuses. Available only to approved parties and requires a signed agreement to access. Please do not contact us about access to the firehose. If your service warrants access to it, we'll contact you.

承認してもらわなきゃ使えないAPIらしい
しかも連絡しないで下さい。私たちからコンタクトを取りますって書いてあるw
Twitter運営側はどうやってサービスの認定をするんだろうか

どうやら承認なしで使えるgetは以下の3つだけみたい
getFollowStream(int follow)
getShadowStream(int count, int
follow)
getSpritzerStream()