1
votes

Using Twitter4j library I am trying to search for tweets, based on a keyword taken from another text file, and download these tweets into a new text file. The problem is that after a specific number of characters, the tweets are trimmed. Is there a way to get full tweets using Twitter4j? And if not, is there another library with which I can do so? This is my code:

public class TweetsCollection {

private static long sleepTime = 15*60*1000;

/**
 * @param args
 * @throws IOException 
 * @throws TwitterException 
 * @throws InterruptedException 
 */
public static void main(String[] args) throws IOException, TwitterException, InterruptedException {
    //key 1
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("4ccwVAaTWOpgq4sBlEdMchwZj").setOAuthConsumerSecret("wxwwiB2KzycrVwW2VBuiJfSQnZ8c40M8l1gyrsFwtOHLVjzayW").setOAuthAccessToken("4040566239-2pCR8GkvtGsrOnpUYOizm5Jeei9y3mTWU3ncV0q").setOAuthAccessTokenSecret("jv1OBHBGAn6DxmONH75Z7iIlhdTcQcrHAfgGvpPi5QtVQ");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter t = tf.getInstance();


    //key 2
    ConfigurationBuilder cb2 = new ConfigurationBuilder();
    cb2.setDebugEnabled(true).setOAuthConsumerKey("mDEaFgTxIn0KrEWeJIv2e4liU").setOAuthConsumerSecret("CrAXR7XhEgOwIWBBt0zprTb6LU0GLPwOgVKzFGIKsPm3ZgdVZq").setOAuthAccessToken("4040566239-J0n5HAkxU2bqiX9Vg3zEFRkRUcXzxjAsLd0qy3V").setOAuthAccessTokenSecret("XhIv3MY679WwVPIE9S6dIG75uydwokbLJgOko2Xz6xq6B");
    TwitterFactory tf2 = new TwitterFactory(cb2.build());
    Twitter t2 = tf2.getInstance();

    //key 3
    ConfigurationBuilder cb3 = new ConfigurationBuilder();
    cb3.setDebugEnabled(true).setOAuthConsumerKey("Q4dkKHY9A9V2y5be9A29RCpiv").setOAuthConsumerSecret("Zb6eRY7c3hduxi4K3nT4h29RzIgXJkZV4ucZhXZP5qvQtj5z7b").setOAuthAccessToken("4040566239-CN2BDnQ2cqHixd8njqxIDfjbq7JaEtzLLJMvLgp").setOAuthAccessTokenSecret("4fpoZ83Ncg2l9LSQ1RGTi5hRpBUtk1GkjdRMgwF0wH9c7");
    TwitterFactory tf3 = new TwitterFactory(cb3.build());
    Twitter t3 = tf3.getInstance();

    //key 4
    ConfigurationBuilder cb4 = new ConfigurationBuilder();
    cb4.setDebugEnabled(true).setOAuthConsumerKey("GRbm01er8V2AJX2buzr5SJ7we").setOAuthConsumerSecret("CheABRf2r5GJoa49wIuKNOn7e46JpupZLMQ9CdhTiseK3dSAlH").setOAuthAccessToken("4040566239-DKJKl7cD0ClYxS3ZItwIXfh3BCqStvD5fUuUcnI").setOAuthAccessTokenSecret("OVEoN49qHQWLcmCNk4KfBxNdYTx2D1qT2mxAM3JUEMtry");
    TwitterFactory tf4 = new TwitterFactory(cb4.build());
    Twitter t4 = tf4.getInstance();


    // TODO Auto-generated method stub
    BufferedReader bf = new BufferedReader(new FileReader("/home/george/Desktop/trends.txt"));
    String trendName = "";
    while ((trendName = bf.readLine())!=null){
        Query topicQuery1 = new Query(trendName);
        Query topicQuery2 = new Query(trendName);
        Query topicQuery3 = new Query(trendName);
        Query topicQuery4 = new Query(trendName);
        BufferedWriter bw = new BufferedWriter(new FileWriter("/home/george/Desktop/tweets/"+trendName+".txt",true));
        QueryResult queryResult1 = t.search(topicQuery1);
        QueryResult queryResult2 = t2.search(topicQuery2);
        QueryResult queryResult3 = t3.search(topicQuery3);
        QueryResult queryResult4 = t4.search(topicQuery4);


        for (Status aStatus : queryResult1.getTweets()){
            bw.write(aStatus.getText()+"\n\n");
        }
        for (Status aStatus : queryResult2.getTweets()){
            bw.write(aStatus.getText()+"\n\n");
        }
        for (Status aStatus : queryResult3.getTweets()){
            bw.write(aStatus.getText()+"\n\n");
        }

        for (Status aStatus : queryResult4.getTweets()){
            bw.write(aStatus.getText()+"\n\n");
        }
        bw.close();
        Thread.sleep(sleepTime);
    }

    bf.close();
}

}

1

1 Answers

0
votes

To get the full text of the tweet, add parameter tweet_mode with value extended to your request parameters.

twitter4j.conf.ConfigurationBuilder()
.setOAuthConsumerKey("")) 
.setOAuthConsumerSecret("") 
.setOAuthAccessToken("")
.setOAuthAccessTokenSecret("")
.setTweetModeExtended(true)