tameraslan

Tweet Scale

Dec 20th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. /*
  2. 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.
  3. you can see the example video here:
  4. http://vimeo.com/20046967
  5. */
  6.  
  7. Twitter myTwitter;
  8. String string1 = "love";
  9. String string2 = "hate";
  10.  
  11. int TimeDiff1 = 0, TimeDiff2 = 0;
  12. int timeValLast1, timeValFirst1, timeValLast2, timeValFirst2;
  13. float Freq1 = 0, Freq2 = 0;
  14. byte degree;
  15. import processing.serial.*;
  16. Serial port;
  17. void setup() {
  18.  
  19. size(500,250);
  20. frameRate(30); //retrieves tweets 20 times a second
  21.  
  22. }
  23.  
  24. void draw ()
  25. {
  26. background(0);
  27. try {
  28. myTwitter = new Twitter("taslan87", "moat47bin");
  29. Query query = new Query(string1);
  30. query.setRpp(100);
  31. QueryResult result = myTwitter.search(query);
  32. ArrayList tweets = (ArrayList) result.getTweets();
  33.  
  34. for (int i = 0; i < tweets.size(); i++) {
  35. Tweet t = (Tweet) tweets.get(i);
  36. String user = t.getFromUser();
  37. String msg = t.getText();
  38. Date d = t.getCreatedAt();
  39. //println("Tweet by " + user + " at " + d + ": " + msg);
  40. //println(d.getSeconds());
  41.  
  42. if( i == 0) // case of the latest twitter
  43. {
  44. timeValLast1 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
  45. }
  46. else if (i == 99) // case of the last twitter
  47. {
  48. timeValFirst1 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
  49. }
  50.  
  51.  
  52.  
  53.  
  54. }
  55. //println("hate");
  56. TimeDiff1 = timeValLast1-timeValFirst1; //100 tweets in this seconds. --> freq
  57. //println(TimeDiff1);
  58. Freq1= 60*100/float(TimeDiff1);
  59. text(string1, 125,75);
  60. text(int(Freq1), 125,125);
  61. }
  62. catch (TwitterException te) {
  63. println("Couldn't connect: " + te);
  64. }
  65.  
  66. try {
  67. myTwitter = new Twitter("taslan87", "moat47bin");
  68. Query query = new Query(string2);
  69. query.setRpp(200);
  70. QueryResult result = myTwitter.search(query);
  71. ArrayList tweets = (ArrayList) result.getTweets();
  72. for (int i = 0; i < tweets.size(); i++) {
  73. Tweet t = (Tweet) tweets.get(i);
  74. String user = t.getFromUser();
  75. String msg = t.getText();
  76. Date d = t.getCreatedAt();
  77. //println("Tweet by " + user + " at " + d + ": " + msg);
  78. //println(d.getSeconds());
  79.  
  80. if( i == 0) // case of the latest twitter
  81. {
  82. timeValLast2 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
  83. }
  84. else if (i == 99) // case of the last twitter
  85. {
  86. timeValFirst2 = d.getHours()*60*60 + d.getMinutes()*60 + d.getSeconds();
  87. }
  88.  
  89.  
  90.  
  91.  
  92. }
  93. //println("love");
  94. //println(timeValLast);
  95. //println(timeValFirst);
  96. TimeDiff2 = timeValLast2-timeValFirst2; //100 tweets in this seconds. --> freq
  97. //println(TimeDiff2);
  98. Freq2= 60*100/float(TimeDiff2);
  99. text(string2, 375, 75);
  100. text(int(Freq2), 375,125);
  101. }
  102. catch (TwitterException te) {
  103. println("Couldn't connect: " + te);
  104. }
  105.  
  106. degree = byte(Freq2/(Freq1 + Freq2)*180);
  107. println(int(degree));
  108. port.write(degree);
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment