Is it possible to get more than 100 tweets?

后端 未结 7 1605
甜味超标
甜味超标 2020-12-09 10:20

Is it possible to get more than 100 tweets using the Twitter4j API?
If so can anyone point out the way to do so??

相关标签:
7条回答
  • 2020-12-09 10:54

    To add to Luke's approach Twitter4j does provide pagination to the queries. You can try fetching more than one pages for your query. Set results per page and the page number.

    But I suggest first try the since_id and then try pagination.

    0 讨论(0)
  • 2020-12-09 10:59

    You can't load more than 100 tweet per request but i don't know why you want this, instead you can load all tweets in "Endless page" i.e loading 10 items each time the user scroll a list .

    for example

    Query query = new Query("stackoverflow");
    query.setCount(10);// sets the number of tweets to return per page, up to a max of 100
    QueryResult  result = twitter.search(query);
    

    now if you want to load the next page simple easy

    if(result.hasNext())//there is more pages to load
    {
    query = result.nextQuery();
    result = twitter.search(query);
    }
    

    and so on.

    0 讨论(0)
  • 2020-12-09 11:05

    Would need to see your code to provide a code example specific to your case, but you can do this through since_id or max_id.

    This information is for the Twitter API.

    To get the previous 100 tweets:

    1. find the the lowest id in the set that you just retrieved with your query
    2. perform the same query with the max_id option set to the id you just found.

    To get the next 100 tweets:

    1. find the the highest id in the set that you just retrieved with your query
    2. perform the same query with the since_id option set to the id you just found.

    In Twitter4j, your Query object has two fields that represent the above API options: sinceId and maxId.

    0 讨论(0)
  • 2020-12-09 11:05

    When you get a response containing your first 100 results, you also get the next id with the response. This id can be used as a query parameter "next"= {the id you received from the previous call} while making the call again and it will give you the next 100 tweets.

    0 讨论(0)
  • 2020-12-09 11:09

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

    import java.util.*;
    import twitter4j.*;
    import twitter4j.conf.*;
    
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("");
    cb.setOAuthConsumerSecret("");
    cb.setOAuthAccessToken("");
    cb.setOAuthAccessTokenSecret("");
    
    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());
    
    0 讨论(0)
  • 2020-12-09 11:14

    Some Java code that iterates to older pages by using lowest id might look like:

    Query query = new Query("test");
    query.setCount(100);
    
    int searchResultCount;
    long lowestTweetId = Long.MAX_VALUE;
    
    do {
        QueryResult queryResult = twitterInstance.search(query);
    
        searchResultCount = queryResult.getTweets().size();
    
        for (Status tweet : queryResult.getTweets()) {
    
            // do whatever with the tweet
    
            if (tweet.getId() < lowestTweetId) {
                lowestTweetId = tweet.getId();
                query.setMaxId(lowestTweetId);
            }
        }
    
    } while (searchResultCount != 0 && searchResultCount % 100 == 0);
    
    0 讨论(0)
提交回复
热议问题