Last active
August 29, 2015 14:07
-
-
Save igorbrigadir/dd47b7865b108bdc0f9d to your computer and use it in GitHub Desktop.
Search REST API for Users Tweeting URLs
This file contains 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
package org.insight.twitter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import twitter4j.Query; | |
import twitter4j.QueryResult; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.URLEntity; | |
import twitter4j.User; | |
import twitter4j.conf.ConfigurationBuilder; | |
public class SearchTest { | |
public static void main(String[] args) throws TwitterException { | |
//Credentials: https://apps.twitter.com/ | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setDebugEnabled(true) | |
.setOAuthConsumerKey("*********************") | |
.setOAuthConsumerSecret("******************************************") | |
.setOAuthAccessToken("**************************************************") | |
.setOAuthAccessTokenSecret("******************************************"); | |
TwitterFactory tf = new TwitterFactory(cb.build()); | |
Twitter twitter = tf.getInstance(); | |
// Example URL: | |
String[] urls = { | |
"gu.com/p/422a6/", | |
"theguardian.com/world/2014/sep/30/-sp-hong-kong-umbrella-revolution-pro-democracy-protests" | |
}; | |
// Store Tweets: | |
List<Status> tweets = new ArrayList<Status>(); | |
// All known variations of URLs for a single article: | |
for (String url : urls) { | |
Query q = new Query(); | |
q.setCount(100); | |
q.setQuery(url); // | |
// Get 100 results from first page of results only: | |
QueryResult r = twitter.search(q); | |
tweets.addAll( r.getTweets() ); | |
} | |
// Store Unique Users found tweeting the URLs: | |
Map<Long,User> users = new HashMap<Long,User>(); | |
// Tweets have the User Object embeded in them: | |
for (Status tweet : tweets) { | |
// Store User... | |
User user = tweet.getUser(); | |
users.put(user.getId(), user); | |
// Process Retweeted User too, if this is a retweet: | |
if (tweet.isRetweet()) { | |
User rtuser = tweet.getRetweetedStatus().getUser(); | |
users.put(rtuser.getId(), rtuser); | |
} | |
System.out.println("Debug: " + user.getScreenName() + ": " + tweet.getText()); | |
for (URLEntity u : tweet.getURLEntities()) { | |
// u.getText() identical. What is actually in the tweet text (t.co) | |
// System.out.println("Debug: Text URL=" + u.getURL() ); | |
// What you see in the tweet in web UI | |
// System.out.println("Debug: Display URL=" + u.getDisplayURL()); | |
// "Expanded" URL may actually be bit.ly ow.ly etc. | |
// System.out.println("Debug: Expand URL=" + u.getExpandedURL() ); | |
} | |
} | |
// Do something with Users: | |
for (User u : users.values()) { | |
System.out.println( | |
String.format("@%s %s tweeted about hong kong umbrella revolution", | |
u.getScreenName(), u.getName()) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment