0

我正在使用 twitter4j twitter Streaming API 来获取特定标签的推文。我有很多关键字。我想搜索包含该标签的 100 条推文,目前我正在做的是我编写了用于获取单个单词的推文的代码

public class StreamAPI {

    public static void main(String[] args) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("xxxx");
        cb.setOAuthConsumerSecret("xxxxx");
        cb.setOAuthAccessToken("xxxx");
        cb.setOAuthAccessTokenSecret("xxxx");
        cb.setUseSSL(true);
        cb.setUserStreamRepliesAllEnabled(true);


        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();


        twitterStream.setOAuthAccessToken(accestoken);
        StatusListener listener = new StatusListener() {
            int countTweets = 0;

            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                countTweets++;
                System.out.println(countTweets);
            }

            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            public void onScrubGeo(long userId, long upToStatusId) {
                System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

            @Override
            public void onStallWarning(StallWarning stallWarning) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        };

        FilterQuery fq = new FilterQuery();
        String keywords[] = {"ipl"};

        fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);

    }

}

在达到计数 100 后我将如何停止该过程,并应将该 100 条推文作为列表返回。

请帮我。

4

1 回答 1

1

看下面的代码可能对你有帮助

String token= "Key Word";
Query query = new Query(token);
FileWriter outFile = new FileWriter(token.replaceAll("^#","").concat(".txt"), true);
int numberOfTweets = 1500;
long lastID = Long.MAX_VALUE;
ArrayList<Status> tweets = new ArrayList<Status>();
while (tweets.size () < numberOfTweets) {
if (numberOfTweets - tweets.size() > 100)
  query.setCount(100);
else 
 query.setCount(numberOfTweets - tweets.size());
try {
  QueryResult result = twitter.search(query);
 tweets.addAll(result.getTweets());
 System.out.println("Gathered " + tweets.size() + " tweets");
 for (Status t: tweets) 
   if(t.getId() < lastID) lastID = t.getId();  }
catch (TwitterException te) {
   System.out.println("Couldn't connect: " + te);  }; 
query.setMaxId(lastID-1);
}
PrintWriter out1 = new PrintWriter(outFile);
for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);

GeoLocation loc = t.getGeoLocation();

String user = t.getUser().getScreenName();
String msg = t.getText();
String time = "";
if (loc!=null) {
 Double lat = t.getGeoLocation().getLatitude();
 Double lon = t.getGeoLocation().getLongitude();

 System.out.println(i + " USER: " + user + " wrote: " + msg + " located at " + lat +  ", " + lon);} 
 else 
 //  System.out.println(i + " USER: " + user + " wrote: " + msg.replaceAll("\n",""));
   out1.append(i + " USER: " + user + " wrote: " +msg.replaceAll("\n"," ")  );
   out1.print("\n");
} 
System.out.println("file write succefully");
于 2013-09-19T05:03:28.613 回答