Last active
April 11, 2016 11:40
-
-
Save trpfrog/c9babf2ba8bae0108cbf90e3a5fac6df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class Twitter { | |
| import twitter4j.Twitter; | |
| import twitter4j.TwitterFactory; | |
| import twitter4j.TwitterStream; | |
| import twitter4j.TwitterStreamFactory; | |
| import twitter4j.conf.ConfigurationBuilder; | |
| protected TwitterStream stream; | |
| protected final ConfigurationBuilder getBuilder(){ //ConfigurationBuilderの設定 | |
| ConfigurationBuilder builder = new ConfigurationBuilder() | |
| .setDebugEnabled(/*デバッグするか、trueかfalse*/) | |
| .setOAuthConsumerKey("consumerKey") | |
| .setOAuthConsumerSecret("consumerSecret") | |
| .setOAuthAccessToken("accessToken") | |
| .setOAuthAccessTokenSecret("accessTokenSecret"); | |
| return builder; | |
| } | |
| public static void main (String[] args){ | |
| stream = new TwitterStreamFactory(getBuilder().build()).getInstance(); | |
| //↑ TwitterStreamFactoryにConfigurationBuilderをbuild()したものを乗っける | |
| try { | |
| this.stream.addListener(new StatusListener(){ //流れてきたツイートに対して実行するものをここで指定する | |
| @Override | |
| public void onException(Exception arg0) {} | |
| @Override | |
| public void onDeletionNotice(StatusDeletionNotice arg0) {} | |
| @Override | |
| public void onScrubGeo(long arg0, long arg1) {} | |
| @Override | |
| public void onStallWarning(StallWarning arg0) {} | |
| @Override | |
| public void onTrackLimitationNotice(int arg0) {} | |
| @Override | |
| public void onStatus(Status status) { | |
| //流れてきたツイートに行う処理は主にここ | |
| System.out.println(status.getText()); | |
| //statusはジオタグ、画像、日付などツイート情報が全て記憶されている | |
| //ここではstatus.getText();でツイート内容をprintln()している | |
| } | |
| }); | |
| } catch (TwitterException e) { | |
| //そういうこともあるさ | |
| e.printStackTrace(); | |
| } | |
| Runtime.getRuntime().addShutdownHook(new Thread() { //開いたストリームは必ず閉じようね。ここではシャットダウンホックを使う。 | |
| @Override | |
| public void run() { | |
| stream.shutdown(); | |
| } | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment