Twitter4j推文被修剪

时间:2018-03-21 11:58:13

标签: java twitter twitter4j

使用Twitter4j库我试图根据从另一个文本文件中获取的关键字搜索推文,并将这些推文下载到新的文本文件中。问题是在特定数量的字符之后,推文被修剪。有没有办法使用Twitter4j获取完整的推文?如果没有,是否还有另一个我可以这样做的图书馆?这是我的代码:

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 个答案:

答案 0 :(得分:0)

要获取推文的全文,请添加参数tweet_mode,并将值扩展到您的请求参数。

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