简体   繁体   中英

how to get the full tweet of the user timeline with twitter4j?

I want to get tweets from certain user timelines using java library twitter4j, currently I have source code which can get ~ 3200 tweets from user time line but I can't get full tweet. I have searched in various sources on the internet but I can't find a solution to my problem. anyone can help me or can anyone provide an alternative to get a full tweet from the user timeline with java programming?

my source code :

public static void main(String[] args) throws SQLException {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("aaa")
      .setOAuthConsumerSecret("aaa")
      .setOAuthAccessToken("aaa")
      .setOAuthAccessTokenSecret("aaa");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    int pageno = 1;
    String user = "indtravel";
    List statuses = new ArrayList();
    while (true) {
        try {
            int size = statuses.size();
            Paging page = new Paging(pageno++, 100);
            statuses.addAll(twitter.getUserTimeline(user, page));
            System.out.println("***********************************************");
            System.out.println("Gathered " + twitter.getUserTimeline(user, page).size() + " tweets");

            //get status dan user
            for (Status status: twitter.getUserTimeline(user, page)) {
                //System.out.println("*********Place Tweets :**************\npalce country :"+status.getPlace().getCountry()+"\nplace full name :"+status.getPlace().getFullName()+"\nplace name :"+status.getPlace().getName()+"\nplace id :"+status.getPlace().getId()+"\nplace tipe :"+status.getPlace().getPlaceType()+"\nplace addres :"+status.getPlace().getStreetAddress());
                System.out.println("["+(no++)+".] "+"Status id : "+status.getId());
                System.out.println("id user : "+status.getUser().getId());
                System.out.println("Length status :  "+status.getText().length());
                System.out.println("@" + status.getUser().getScreenName() +" . "+status.getCreatedAt()+ " : "+status.getUser().getName()+"--------"+status.getText());
                System.out.println("url :"+status.getUser().getURL());
                System.out.println("Lang :"+status.getLang());
            }
            if (statuses.size() == size)
                break;
        }catch(TwitterException e) {
            e.printStackTrace();
        }
    }
    System.out.println("Total: "+statuses.size());
}

Update : After the answer given by @AndyPiper

the my problem is every tweet that I get will be truncated or not complete. a tweets that I get will be truncated if the length of tweet more than 140 characters. I found the reference tweet_mode=extended , but I do not know how to use it. if you know something please tell me.

Your Configuration should be like this:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
  .setOAuthConsumerKey("aaa")
  .setOAuthConsumerSecret("aaa")
  .setOAuthAccessToken("aaa")
  .setOAuthAccessTokenSecret("aaa")
  .setTweetModeExtended(true);

It is explained well here

The Twitter API limits the timeline history to 3200 Tweets. To get more than that you would need to use the (commercial) premium or enterprise APIs to search for Tweets by a specific user.

if you are streaming tweets

fist: you have to add .setTweetModeExtended(true); into your configurationbuilder

second(here is the code)

       TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener(){
        public void onStatus(Status status) {

            System.out.println("-------------------------------");

            if(status.isRetweet()){
                System.out.println(status.getRetweetedStatus().getText());
            }
            else{
                System.out.println(status.getText());
            }`

its totally works for me. take care yourself :)

If you are implementing twitter api using twitter4j.properties file and getting truncated timeline text value,simply add the below property in it at the end. tweetModeExtended=TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM