Skip to content

Instantly share code, notes, and snippets.

@airwindow
Forked from ivamluz/twitter4j-find-replies.java
Last active August 29, 2015 14:21
Show Gist options
  • Save airwindow/5e39405706db6b80ad65 to your computer and use it in GitHub Desktop.
Save airwindow/5e39405706db6b80ad65 to your computer and use it in GitHub Desktop.
public static List<Status> findReplies() {
Query query = new Query("iluzcit");
List<Status> tweets = new ArrayList<Status>();
Twitter twitter = new TwitterFactory().getInstance();
try {
QueryResult result;
do {
result = twitter.search(query);
for (Status tweet : result.getTweets()) {
// Replace this logic to check if it's a response to a known tweet
if (tweet.getInReplyToStatusId() > 0) {
tweets.add(tweet);
}
}
} while ((query = result.nextQuery()) != null);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
}
return tweets;
}
private static void printTweets(List<Status> tweets) {
for (Status tweet : tweets) {
System.out.println("(" + tweet.getUser().getId() + ") " + tweet.getUser().getName() + ": (" + tweet.getId() + ") - " + tweet.getText());
for (MediaEntity media : tweet.getMediaEntities()) {
System.out.println("- " + media.getType() + ": " + media.getMediaURL());
}
}
}
public static void replyTo(List<Status> tweets) {
Twitter twitter = new TwitterFactory().getInstance();
Status reply = null;
for (Status tweet : tweets) {
try {
reply = twitter.updateStatus(new StatusUpdate(UUID.randomUUID() + " - @" + tweet.getUser().getScreenName() + " this is a reply to your tweet.").inReplyToStatusId(tweet.getId()));
System.out.println("Posted reply " + reply.getId() + " in response to tweet " + reply.getInReplyToStatusId());
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static List<Status> findAuthorizations() {
Query query = new Query("iluzcit");
List<Status> tweets = new ArrayList<Status>();
Twitter twitter = new TwitterFactory().getInstance();
try {
QueryResult result;
do {
result = twitter.search(query);
tweets.addAll(result.getTweets());
} while ((query = result.nextQuery()) != null);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
}
return tweets;
}
public static void main(String[] args) {
StatusListener listener = new StatusListener(){
@Override
public void onStatus(Status status) {
System.out.println(status.getUser().getName() + " : " + status.getText());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(numberOfLimitedStatuses + " were limited");
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onDeletionNotice(StatusDeletionNotice arg0) {
// TODO Auto-generated method stub
}
@Override
public void onScrubGeo(long arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
public void onStallWarning(StallWarning arg0) {
// TODO Auto-generated method stub
System.out.println("Connection stalled");
}
};
System.out.println("App started");
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
twitterStream.addListener(listener);
// sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
FilterQuery filter = new FilterQuery();
String[] keywordsArray = { "Jackson" };
filter.track(keywordsArray);
twitterStream.filter(filter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment