Twitter4JのStream APIを使用して、特定キーワードのtweetを監視する
「Twitter4JのStream APIを使用して、特定ユーザのtweetを監視する」の続き。特定キーワードを監視する。
試しに、「飲み会」というキーワードを監視してみた。
◎メインクラス
package main; import listener.TwitterStatusListenerImpl; import logic.impl.EditImageLogicImpl; import twitter4j.FilterQuery; import twitter4j.TwitterException; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; public class MainEngine { public static void main(String[] args) throws TwitterException { // Streaming TwitterStream twStream = TwitterStreamFactory.getSingleton(); TwitterStatusListenerImpl twitterStatusListenerImpl = new TwitterStatusListenerImpl(); twStream.addListener(twitterStatusListenerImpl); // Narrow down tweets to specific users FilterQuery filterquery = new FilterQuery(); String[] track = new String[]{"飲み会"}; FilterQuery fQuery = filterquery.track(track); twStream.filter(filterquery); } }
リスナーのコード。
package listener; import twitter4j.StallWarning; import twitter4j.Status; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener; public class TwitterStatusListenerImpl implements StatusListener{ @Override public void onStatus(Status tweetStatus) { String tweetText = ""; tweetText = tweetStatus.getText(); System.out.println("####getGeoLocation########"+tweetStatus.getGeoLocation()); System.out.println("####getCreatedAt########"+tweetStatus.getCreatedAt().toString()); System.out.println("####getPlace########"+ tweetStatus.getPlace()); if(tweetStatus.getPlace() != null){ System.out.println("####getPlace.getName########"+ tweetStatus.getPlace().getName()); System.out.println("####getPlace.geStreetAddress########"+ tweetStatus.getPlace().getStreetAddress()); System.out.println("####getPlace.getCountryCode########"+ tweetStatus.getPlace().getCountryCode()); System.out.println("####getPlace.getCountry########"+ tweetStatus.getPlace().getCountry()); } System.out.println("####getUser########"+tweetStatus.getUser()); if(tweetStatus.getUser() != null){ System.out.println("####getId########"+tweetStatus.getUser().getId()); System.out.println("####getLocation########"+ tweetStatus.getUser().getLocation()); System.out.println("####getScreenName########"+ tweetStatus.getUser().getScreenName()); System.out.println("####getName########"+ tweetStatus.getUser().getName()); } System.out.println("####getSource########"+ tweetStatus.getSource()); System.out.println("####getScopes########"+ tweetStatus.getScopes()); System.out.println("####getText########"+ tweetStatus.getText()); System.out.println("####getRetweetCount########"+ tweetStatus.getRetweetCount()); System.out.println("####getRetweetedStatus########"+ tweetStatus.getRetweetedStatus()); System.out.println("\n"); System.out.println("**********" + tweetText + "**********"); } }
業者の飲み会の宣伝用tweetがほとんどだった。
それぞれ下記のように情報が取得できる。
####getGeoLocation########null ####getCreatedAt########Sun Mar 30 23:00:22 GMT+09:00 2014 ####getPlace########null ####getId########1911880094 ####getLocation######## ####getScreenName########osaka_kyobashi ####getName########梅田 京橋 居酒屋@相互フォロ- ####getSource########<a href="http://makebot.sh" rel="nofollow">osaka_kyobashi</a> ####getScopes########null ####getText########女子会 合コン 飲み会 宴会にクーポン予約!!! http://t.co/SJd5TYlEw1 【居酒屋割引】 #大阪 #梅田 #京橋 #居酒屋 #クーポン ####getRetweetCount########0 ####getRetweetedStatus########null