Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twitter4j - get tweets by ID

Tags:

twitter4j

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

like image 575
Mr_and_Mrs_D Avatar asked Dec 01 '12 17:12

Mr_and_Mrs_D


People also ask

How do I find tweets using Tweet ID?

You don't need to use the API or know the user account. Just drop it into the following URL format, where XXXXXXXXX is the tweet id. Show activity on this post.

What is a tweet ID?

Today, Twitter IDs are unique 64-bit unsigned integers, which are based on time, instead of being sequential. The full ID is composed of a timestamp, a worker number, and a sequence number.


1 Answers

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
    }
like image 56
Mr_and_Mrs_D Avatar answered Oct 29 '22 03:10

Mr_and_Mrs_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!