在twitter4j中填充一个arraylist

时间:2016-03-09 20:34:54

标签: java twitter arraylist twitter4j

我需要使用在线推文填充我的arraylist,而不是硬编码字符串。我正在使用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,以及OSX el capitan 10.11.3的macbook pro

1 个答案:

答案 0 :(得分:0)

就是这样,使用

的简单实现
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();
}