5

How can I get the tweets when I have the tweet ID and the user ID ? I have a file containing lines like :

userID  tweetID

I guess I should go by :

Query query = new Query("huh ?");
QueryResult result = twitter.search(query);
List<Status> tweets = result.getTweets();

but I have no clue how to spell the query

Thanks

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

3 Answers3

15

Well it was no search call. The tweet apparently is called Status and the code to retrieve one by ID is :

    final Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
    AccessToken accessToken = new AccessToken(TWITTER_TOKEN,
            TWITTER_TOKEN_SECRET);
    twitter.setOAuthAccessToken(accessToken);
    try {
        Status status = twitter.showStatus(Long.parseLong(tweetID));
        if (status == null) { // 
            // don't know if needed - T4J docs are very bad
        } else {
            System.out.println("@" + status.getUser().getScreenName()
                        + " - " + status.getText());
        }
    } catch (TwitterException e) {
        System.err.print("Failed to search tweets: " + e.getMessage());
        // e.printStackTrace();
        // DON'T KNOW IF THIS IS THROWN WHEN ID IS INVALID
    }
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
0

The accepted answer is no longer valid. Based on the answer in this page, the code should be changed to the following:

    String consumerKey = xxxxxxx,
            consumerSecret = xxxxxxx,
            twitterAccessToken = xxxxxxx,
            twitterAccessTokenSecret = xxxxxxx,
            Tweet_ID = xxxxxxx;

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(consumerKey);
    builder.setOAuthConsumerSecret(consumerSecret);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    final Twitter twitter = factory.getInstance();

    //twitter.setOAuthConsumer(consumerKey, consumerSecret);
    AccessToken accessToken = new AccessToken(twitterAccessToken, twitterAccessTokenSecret);
    twitter.setOAuthAccessToken(accessToken);
    try {
        Status status = twitter.showStatus(Long.parseLong(Tweet_ID));
        if (status == null) { //
            // don't know if needed - T4J docs are very bad
        } else {
            System.out.println("@" + status.getUser().getScreenName()
                    + " - " + status.getText());
        }
    } catch (
            TwitterException e) {
        System.err.print("Failed to search tweets: " + e.getMessage());
        // e.printStackTrace();
        // DON'T KNOW IF THIS IS THROWN WHEN ID IS INVALID
    }
Community
  • 1
  • 1
Habib Karbasian
  • 556
  • 8
  • 18
  • Did you run my code and does not run ? Note that in the question you linked `TwitterFactory.getSingleton()` is called which I do not use here – Mr_and_Mrs_D Sep 25 '16 at 10:34
  • Yes. Your code doesn't work and to pass the authentication information to twitter object, 'ConfigurationBuilder' should be used. – Habib Karbasian Sep 25 '16 at 19:35
0

A very easy way to get a list of tweets by their ID is to use the lookup function like this:

public static void main(String[] args) throws TwitterException {
        ConfigurationBuilder cfg = new ConfigurationBuilder();
        cfg.setOAuthAccessToken("key");
        cfg.setOAuthAccessTokenSecret("key");
        cfg.setOAuthConsumerKey("key");
        cfg.setOAuthConsumerSecret("key");

        Twitter twitter = new TwitterFactory(cfg.build()).getInstance();
        long[] ids = new long [3];
        ids[0] = 568363361278296064L;
        ids[1] = 568378166512726017L;
        ids[2] = 570544187394772992L;

        ResponseList<Status> statuses = twitter.lookup(ids);

        for (Status status : statuses) {
            System.out.println(status.getText());
        }
    }

The advantage of using lookup is that you can get with a sigle call up to 100 tweets, this means that if you have to download a big number of tweets you will need to do a lot less calls to the twitter API and speed up the process (this is because twitter limits the number of calls you can do).

You can even check the number of calls that you can do before twitter puts you in timeout like this:

RateLimitStatus searchLimits = twitter.getRateLimitStatus("statuses").get("/statuses/lookup");

int remain = searchLimits.getRemaining();
int limit = searchLimits.getLimit();
int secToReset = searchLimits.getSecondsUntilReset();

System.out.println(remain); // this returns the number of calls you have left
System.out.println(limit); // this returns how many calls you have max(this is a fixed number)
System.out.println(secToReset); // this returns the number of second before the reset
// after the reset you return to have the number of calls specified by "limit"