Okay, so i dont really know much about writing code, yet. I am working on a project that uses twitter API data. My goal for the project is to use hash tags to represent both good and bad things (for sake of simplicity, lets use #good and #bad).
I want that hashtag data to modify the color of a simple ellipse to a shade of color in between red and green, depending on the number of #good and #bad tweets. I like to think of it as a +100/-100 spectrum. each #good tweet is +1, each #bad is -1. If it is at -100 tweets, then the ellipse is full red. If it is at +100 tweets, then the ellipse is full green.
I know this is a little complicated, but its for an art project im doing. I followed a tutorial and currently have the twitter data responding on a simple array list of tweets (tutorial @ https://www.youtube.com/watch?v=gwS6irtGK-c) I am using processing, java, twitter4j 3.0.3, and a macbook pro with OSX el capitan 10.11.3
Any help would be greatly appreciated. Even pointing me in the direction on how to code it myself. If you need more information from me, ill respond as quickly as I see it!
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()
SECOND VERSION:
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);
}