9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Twitter4j手習い

Posted at

#まずはアクセストークンの取得
アクセストークンの取得法などについては、phpだけど↓↓のサイトが、準備の流れやOAuthの仕組みがわかりやすい
Twitter APIの使い方まとめ
OAuth1.0の署名(Signature)を作成する方法

#とりあえず呟いてみる
まんまここを見て書いた

KinokoTwitterClient.java
package jp.kinokodata;

import twitter4j.*;
import twitter4j.conf.*;


/**
 * Created by superkinoko on 2016/11/19.
 */

public class KinokoTwitterClient {
    private Twitter twitter;

    public static void main(String[] args) {
        KinokoTwitterClient tweet = new KinokoTwitterClient();
        tweet.doHogehoge();
    }
    
    private void doHogehoge() {
        getTwitter();
        twitter.updateStatus("てすと");
    }
    
    private void getTwitter() {
        if(twitter == null) {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey("yourConsumerKey")
                    .setOAuthConsumerSecret("yourConsumerSecret")
                    .setOAuthAccessToken("yourhAccessToken")
                    .setOAuthAccessTokenSecret("yourAccessTokenSecret");
            TwitterFactory tf = new TwitterFactory(cb.build());
            twitter = tf.getInstance();
        }
    }

}

#ツイートの検索をしてみる

QueryにTwitterの検索クエリを入れてあげると、ツイートが検索できる。

KinokoTwitterClient.java
    private void doHogehoge() {
        getTwitter();

        Query query = new Query();
        query.setQuery("from:hyperkinoko");
        // 検索件数。setRppはなくなったもよう
        query.setCount(100);

        QueryResult result = null;
        try {
            result = twitter.search(query);
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        Iterator itr = result.getTweets().iterator();
        while (itr.hasNext()) {
            Status status = (Status)itr.next();
            System.out.println(status.getText() + "リツイート: " + status.getRetweetCount());
        }
    }

#Streamを取得してみる

Listenerをaddして、Status(ツイート)を取得すると反応するようになった
動きのある画面はまさにTwitterのTLっぽい
sampleというのはアレだ、化粧品のサンプルとかそういう意味じゃなくて、標本抽出の意味だね
ここを参考にして、filterをかけてみた

KinokoTwitterClient.java
    private void doHogehoge() {
        getTwitterStream();
        twitterStream.addListener(new MyTweetListener());
        FilterQuery filterQuery = new FilterQuery();
        filterQuery.track(new String[] {"#IPPONGP"});
        twitterStream.filter(filterQuery);
//        twitterStream.sample("ja");
    }

    private void getTwitterStream() {
        if(twitterStream == null) {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey("yourConsumerKey")
                    .setOAuthConsumerSecret("yourConsumerSecret")
                    .setOAuthAccessToken("yourhAccessToken")
                    .setOAuthAccessTokenSecret("yourAccessTokenSecret");
            TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
            twitterStream = tf.getInstance();
        }
    }

    class MyTweetListener extends StatusAdapter {
        @Override
        public void onStatus(Status status) {
            System.out.println(status.getText());
        }
    }
9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?