使用Twitter4j 3.0.3使用hashtag数据更改椭圆的颜色

时间:2016-02-29 02:51:12

标签: java twitter colors processing twitter4j

好的,所以我对编写代码还不太了解。我正在开发一个使用Twitter API数据的项目。我对该项目的目标是使用哈希标记来表示好的和坏的事情(为了简单起见,我们使用#good和#bad)。

我希望该标签数据能够将简单椭圆的颜色修改为红色和绿色之间的颜色阴影,具体取决于#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

非常感谢任何帮助。甚至指着我自己如何编码的方向。如果您需要我提供更多信息,我会尽快回复!

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

ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("****");
  cb.setOAuthConsumerSecret("****");
  cb.setOAuthAccessToken("****");
  cb.setOAuthAccessTokenSecret("****");
  cb.setUseSSL(true);
  twitterInstance = new TwitterFactory( cb.build()
                                  ).getInstance();
  queryForTwitter = new Query("#good");

  size(640,440);
  FetchTweets();

} //setup

void draw() {
  background(0);
  DrawTweets();
} //draw

void DrawTweets() {
  for(int i=0; i<tweets.size(); i++) {
    Status t = (Status) tweets.get(i);
    String user = t.getUser().getName();
    String msg = t.getText();
    text(user + ": " + msg,
         20,15+i*30-mouseY, width-20, 40);
  } //for
} //drawTweets

void FetchTweets(){
  try {
    QueryResult result = twitterInstance.search(
                                queryForTwitter );
    tweets = (ArrayList) result.getTweets();
  } catch(TwitterException te) {
    println("Couldn't connect: " +te);
  } // end of catch TwitterException
}// end of FetchAndDrawTweets()

第二版:

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

//ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("****");
  cb.setOAuthConsumerSecret("****");
  cb.setOAuthAccessToken("****");
  cb.setOAuthAccessTokenSecret("****");
  cb.setUseSSL(true);
  //twitterInstance = new TwitterFactory( cb.build()
  //                                ).getInstance();
  //queryForTwitter = new Query("#feelthebern");

  size(640,440);

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

} //setup

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;
}

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);

}

1 个答案:

答案 0 :(得分:0)

你必须将问题分解为更小的步骤。

第1步:创建一个只返回ArrayList推文的函数。

第2步:创建一个获取ArrayListString值的函数,并返回String在推文中出现的次数{ ArrayList

此代码假设您有ArrayList<String> tweets

int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

第3步:根据包含每个单词的推文数量计算颜色。你说你总会有100条推文,所以你可以将推文数除以100,再乘以255得到颜色值。

总而言之,它看起来像这样:

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

void setup() {

  //you would actually get these from twitter,
  //but for testing let's just fill them ourselves
  int numGood = 50;
  int numBad = 50;
  for (int i = 0; i < numGood; i++) {
    tweets.add("#good");
  }
  for (int i = 0; i < numBad; i++) {
    tweets.add("#bad");
  }
}

//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);

}