twitter4j - 计算24小时内推文的数量,返回一个整数

时间:2014-10-17 17:01:27

标签: java twitter processing twitter4j twitter-search

我试图在24小时内检索某个关键字的推文数量的单个整数。 所以说关键字是"流量"我想用“" traffic"”这个词来计算推文的数量。在过去24天内,将其存储为数字,用于生成其他内容。

现在我可以使用query.setCount提供一个特定的号码,并在过去的24小时内检索任意数量(1024)的推文,但我无法在24小时内告知这是否是所有推文,我真的想要是一个数字,我不需要推文的实际文本或其他信息。此外,随着新推文的推出,请更新该号码。

我怎么能这样做?

到目前为止,这是我的getNewTweets方法:

    void getNewTweets(){
    SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");

  Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -24);

  String yesterday = sdf.format(calendar.getTime());

Query query = new Query("traffic"); 
  query.setSince(yesterday);
  int numberOfTweets = 1024;
  long lastID = Long.MAX_VALUE;
  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());
      println("Gathered " + tweets.size() + " tweets");
      for (Status t: tweets) 
        if(t.getId() < lastID) lastID = t.getId();

    }

    catch (TwitterException te) {
      println("Couldn't connect: " + te);
    }; 
    query.setMaxId(lastID-1);
  }

}

2 个答案:

答案 0 :(得分:0)

您无法确定特定过滤器/搜索查询的推文数量, 这两个API都是速率限制的。 您必须使用firehose来获取所有推文数据并付款。

以下是摘录形式的twitter dev -

Before getting involved, it’s important to know that the Search API is focused on relevance and not completeness. This means that some Tweets and users may be missing from search results. If you want to match for completeness you should consider using a Streaming API instead

请阅读以下链接,以便更好地了解Streaming API的速率限制 - https://twittercommunity.com/t/how-much-data-returned-when-using-streaming-api/8407

答案 1 :(得分:0)

那说(@mbaxi回答)我认为对于一个不太流行的词,Stream API将适合该任务。我使用非常受欢迎的&#34; love&#34;运行此代码5分钟到目前为止没有任何警告,也有约25000条爱情的推文...... 我只是为了这个例子而把这个非常简单而且不精确的计时器...虽然你说你不想要文本,但它正被打印到控制台......

这是一个例子

import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;
int startTime;
int tweetNumber;
PFont f ;
String theWord = "love";


TwitterStream twitterStream;

void setup() {     
  size(800, 100);    
  background(0); 
  f  = createFont("SourceCodePro-Regular", 25);
  textFont(f);
  openTwitterStream();
  startTime = minute();
}  


void draw() {     
  background(0);
  int passedTime = minute() - startTime;
  text("Received " + nf(tweetNumber, 5) + " tweets with the word: " + theWord, 30, height - 50); 
  text("in last " +  nf(passedTime, 3) + " minutes", 30, height - 25);
}  



// Stream it
void openTwitterStream() {  

  ConfigurationBuilder cb = new ConfigurationBuilder();  
  cb.setOAuthConsumerKey("-----FILL-----");
  cb.setOAuthConsumerSecret("-----FILL-----");
  cb.setOAuthAccessToken("-----FILL-----");
  cb.setOAuthAccessTokenSecret("-----FILL-----"); 

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

  FilterQuery filtered = new FilterQuery();

  // if you enter keywords here it will filter, otherwise it will sample
  String keywords[] = {
    theWord
  };

  filtered.track(keywords);

  twitterStream.addListener(listener);

  if (keywords.length==0) {
    // sample() method internally creates a thread which manipulates TwitterStream 
    twitterStream.sample(); // and calls these adequate listener methods continuously.
  } else { 
    twitterStream.filter(filtered);
  }
  println("connected");
} 


// Implementing StatusListener interface
StatusListener listener = new StatusListener() {

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

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

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

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

  //@Override
  public void onStallWarning(StallWarning warning) {
    System.out.println("Got stall warning:" + warning);
  }

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