Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tweets of a public twitter profile

I have a list of usernames on Twitter whose profiles are public. I wish to get "all the tweets" they have posted from the day they formed their profile. I checked Twitter4J examples on GitHub.
According to the Twitter API documentation, only the 20 most recent tweets are returned. Is there anyway I could perform my task?

like image 366
Dexter Avatar asked May 31 '10 11:05

Dexter


People also ask

How do you pull a tweet from Twitter?

Navigate to your app dashboard. Select the app you've enabled with the Tweets and users preview, then click Details. Select the Keys and tokens tab. In the Consumer API Keys section, copy the values for API Key into consumer_key and API Secret Key into consumer_secret .

How do I Download all tweets from a user?

Login into your Twitter account. Click on the option 'More', then click on 'settings and privacy' Now under the 'Your Account' section, click on 'Download an archive of your data' Now click on the request archive option.


3 Answers

To use Twitter4J to get all posts from a user you'll have to make your request over multiple pages..

Below code based of an example on GitHub

Twitter unauthenticatedTwitter = new TwitterFactory().getInstance(); //First param of Paging() is the page number, second is the number per page (this is capped around 200 I think. Paging paging = new Paging(1, 100); List<Status> statuses = unauthenticatedTwitter.getUserTimeline("google",paging); 

Just loop and keep grabbing new pages until there are no new posts should work.

like image 196
Tyler Avatar answered Oct 05 '22 17:10

Tyler


Here is how to get ALL tweets for a user (or at least up to ~3200):

import java.util.*;
import twitter4j.*;
import twitter4j.conf.*;

public static void main(String[] a) {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("YOUR KEYS HERE");
    cb.setOAuthConsumerSecret("YOUR KEYS HERE");
    cb.setOAuthAccessToken("YOUR KEYS HERE");
    cb.setOAuthAccessTokenSecret("YOUR KEYS HERE");

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();

    int pageno = 1;
    String user = "cnn";
    List statuses = new ArrayList();

    while (true) {

      try {

        int size = statuses.size(); 
        Paging page = new Paging(pageno++, 100);
        statuses.addAll(twitter.getUserTimeline(user, page));
        if (statuses.size() == size)
          break;
      }
      catch(TwitterException e) {

        e.printStackTrace();
      }
    }

    System.out.println("Total: "+statuses.size());
}
like image 26
rednoyz Avatar answered Oct 05 '22 16:10

rednoyz


If you read through the Twitter's Documentation, you can retrieve up to 200 tweets at a time if you specify "count=200" in your API request.

You can also use "page=x" to get different paginated results; you could keep doing this until you've retrieved every tweet the user has posted.

I'm not sure how your Java application would create this, but your requests would probably look like:

http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=1
http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=2
http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=3

... etc.

Keep in mind that these requests are rate-limited so you'd need to be careful not to exceed the limit.

like image 24
Chris McFarland Avatar answered Oct 05 '22 16:10

Chris McFarland