java - changing the color of an ellipse using hashtag data using Twitter4j 3.0.3 -
okay, dont know writing code, yet. working on project uses twitter api data. goal project use hash tags represent both , bad things (for sake of simplicity, lets use #good , #bad).
i want hashtag data modify color of simple ellipse shade of color in between red , green, depending on number of #good , #bad tweets. think of +100/-100 spectrum. each #good tweet +1, each #bad -1. if @ -100 tweets, ellipse full red. if @ +100 tweets, ellipse full green.
i know little complicated, art project im doing. followed tutorial , have twitter data responding on simple array list of tweets (tutorial @ https://www.youtube.com/watch?v=gws6irtgk-c) using processing, java, twitter4j 3.0.3, , macbook pro osx el capitan 10.11.3
any appreciated. pointing me in direction on how code myself. if need more information me, ill respond 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; (int = 0; < numgood; i++) { tweets.add("#good"); } (int = 0; < numbad; i++) { tweets.add("#bad"); } } //setup arraylist<string> tweets = new arraylist<string>(); //create function counts tweets //that contain hashtag int counttweets(string hashtag){ int total = 0; for(string tweet : tweets){ if(tweet.contains(hashtag)){ total++; } } return total; } void draw(){ //count , 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); }
you have break problem down smaller steps.
step 1: create function returns arraylist
of tweets.
step 2: create function takes arraylist
, string
value, , returns number of times string
occurs in tweets in arraylist
.
this code assumes have arraylist<string> tweets
:
int counttweets(string hashtag){ int total = 0; for(string tweet : tweets){ if(tweet.contains(hashtag)){ total++; } } return total; }
step 3: calculate color based on number of tweets containing each word. said you'll have 100 tweets, can divide tweet count 100, multiply 255 color value.
putting together, looks this:
arraylist<string> tweets = new arraylist<string>(); void setup() { //you these twitter, //but testing let's fill them ourselves int numgood = 50; int numbad = 50; (int = 0; < numgood; i++) { tweets.add("#good"); } (int = 0; < numbad; i++) { tweets.add("#bad"); } } //create function counts tweets //that contain hashtag int counttweets(string hashtag){ int total = 0; for(string tweet : tweets){ if(tweet.contains(hashtag)){ total++; } } return total; } void draw(){ //count , 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); }
Comments
Post a Comment