在 twitter4j 中填充数组列表

populating an arraylist in twitter4j

我需要使用来自在线的推文而不是硬编码字符串来填充我的数组列表。我正在使用 twitter4j 3.0.3.

我对这个项目感到很困惑。因为它是学校的,我真的需要弄明白。我是一个初级程序员,我很困惑。

   int numGood = 50;
   int numBad = 50;
  for (int i = 0; i < numGood; i++) {
    tweets.add("test");
  }
  for (int i = 0; i < numBad; i++) {
    tweets.add("#bad");
  }

}

ArrayList<String> tweets = new ArrayList<String>();



//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

完整版:

ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;

//ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("xxxx");
  cb.setOAuthConsumerSecret("xxxx");
  cb.setOAuthAccessToken("xxxx");
  cb.setOAuthAccessTokenSecret("xxxx");
  cb.setUseSSL(true);

  size(640,440);

} //setup

//ArrayList<String> tweets = new ArrayList<String>();

public static void main (String args[]) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    List<Status> statuses = twitter.getUserTimeline("google");
    String hashtag = "#AlphaGo";
    System.out.println("The Twitter page contains " 
                        + countTweets(hashtag, statuses) 
                        + " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
    return (int) statuses.stream()
                         .filter(x -> x.getText().contains(hashtag))
                         .count();
}

//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

void draw(){

  //count the good and bad tweets
  int goodTweets = countTweets("#good");
  int badTweets = countTweets("#bad");

  //calculate color based on tweet counts
  float r = badTweets/100.0 * 255;
  float g = goodTweets/100.0 * 255;
  float b = 0;

  background(r, g, b);

}

编辑:我希望标签数据根据#good 和#bad 推文的数量将背景颜色修改为介于红色和绿色之间的色调。我喜欢将其视为 +100/-100 频谱。每个#good 推文都是+1,每个#bad 是-1。如果是 -100 条推文,则椭圆为全红色。如果是 +100 条推文,则椭圆为全绿色。

我知道这有点复杂,但这是我正在做的一个艺术项目。我遵循了一个教程,目前有 twitter 数据响应一个简单的推文数组列表(教程@ https://www.youtube.com/watch?v=gwS6irtGK-c)我正在使用处理,java,twitter4j 3.0.3 和带有 [= 的 macbook pro 24=] 酋长 10.11.3

在这里,使用 and a simple implementation of

public static void main (String args[]) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    List<Status> statuses = twitter.getUserTimeline("google");
    String hashtag = "#AlphaGo";
    System.out.println("The Twitter page contains " 
                        + countTweets(hashtag, statuses) 
                        + " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
    return (int) statuses.stream()
                         .filter(x -> x.getText().contains(hashtag))
                         .count();
}