Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is a processing sketch that uses twitter api to check the frequency of the occurrence of the predefined strings. In this one, we used love and hat, but you can also change them and try things like obama and bush. After, the frequencies are compared and a degree value is obtained through this comparison. this degree value is sent through serial port to arduino to change the angle of the servo motor.
- you can see the example video here:
- http://vimeo.com/20046967
- */
- Twitter myTwitter;
- String string1 = "love";
- String string2 = "hate";
- int TimeDiff1 = 0, TimeDiff2 = 0;
- int timeValLast1, timeValFirst1, timeValLast2, timeValFirst2;
- float Freq1 = 0, Freq2 = 0;
- byte degree;
- import processing.serial.*;
- Serial port;
- void setup() {
- size(500,250);
- frameRate(30); //retrieves tweets 20 times a second
- }
- void draw ()
- {
- background(0);
- try {
- myTwitter = new Twitter("taslan87", "moat47bin");
- Query query = new Query(string1);
- query.setRpp(100);
- QueryResult result = myTwitter.search(query);
- ArrayList tweets = (ArrayList) result.getTweets();
- for (int i = 0; i < tweets.size(); i++) {
- Tweet t = (Tweet) tweets.get(i);
- String user = t.getFromUser();
- String msg = t.getText();
- Date d = t.getCreatedAt();
- //println("Tweet by " + user + " at " + d + ": " + msg);
- //println(d.getSeconds());
- if( i == 0) // case of the latest twitter
- {
- timeValLast1 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
- }
- else if (i == 99) // case of the last twitter
- {
- timeValFirst1 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
- }
- }
- //println("hate");
- TimeDiff1 = timeValLast1-timeValFirst1; //100 tweets in this seconds. --> freq
- //println(TimeDiff1);
- Freq1= 60*100/float(TimeDiff1);
- text(string1, 125,75);
- text(int(Freq1), 125,125);
- }
- catch (TwitterException te) {
- println("Couldn't connect: " + te);
- }
- try {
- myTwitter = new Twitter("taslan87", "moat47bin");
- Query query = new Query(string2);
- query.setRpp(200);
- QueryResult result = myTwitter.search(query);
- ArrayList tweets = (ArrayList) result.getTweets();
- for (int i = 0; i < tweets.size(); i++) {
- Tweet t = (Tweet) tweets.get(i);
- String user = t.getFromUser();
- String msg = t.getText();
- Date d = t.getCreatedAt();
- //println("Tweet by " + user + " at " + d + ": " + msg);
- //println(d.getSeconds());
- if( i == 0) // case of the latest twitter
- {
- timeValLast2 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
- }
- else if (i == 99) // case of the last twitter
- {
- timeValFirst2 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
- }
- }
- //println("love");
- //println(timeValLast);
- //println(timeValFirst);
- TimeDiff2 = timeValLast2-timeValFirst2; //100 tweets in this seconds. --> freq
- //println(TimeDiff2);
- Freq2= 60*100/float(TimeDiff2);
- text(string2, 375, 75);
- text(int(Freq2), 375,125);
- }
- catch (TwitterException te) {
- println("Couldn't connect: " + te);
- }
- degree = byte(Freq2/(Freq1 + Freq2)*180);
- println(int(degree));
- port.write(degree);
- }
Advertisement
Add Comment
Please, Sign In to add comment