Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'crap'

  • Loading branch information...
commit 7b2dca51356eb1217cd9b27d6b60304472ee1294 2 parents 5038f9f + 1286938
authored October 26, 2011
111  examples/twitter/index.html
@@ -2,99 +2,84 @@
2 2
 <head>
3 3
 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
4 4
 	<title>Processing.js LiveTwitter Demo</title>
5  
-	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" ></script>
6 5
   <script src="../../processing.js" type="text/javascript"></script>
7  
-	<script src="livetwitter.js" type="text/javascript"></script>
  6
+  <script src="processing-twitter.js" type="text/javascript"></script>
8 7
 </head>
9 8
 <body>
10 9
   <script id="sketch" type="application/processing">
11 10
     // Dynamic list of Tweet objects
12  
-    HashMap tweets;
  11
+    HashMap uniqueTweets;
13 12
 
14  
-    // A class representing a Tweet
  13
+    // A class representing a Tweet.  Tweet objects are created in JS
  14
+    // and automatically placed in the tweets ArrayList (created in JS)
15 15
     class Tweet {
16  
-      String id;
17  
-      String profileName;
18  
-      PImage profileImage;
19  
-      String text;
20  
-      Date time;
21  
-      int randomPosX;
22  
-      int randomPosY;
23  
-
24  
-      Tweet(String aId,
25  
-            String aProfileName,
26  
-            String aProfileImageURL,
27  
-            String aText,
28  
-            Date aTime) {
29  
-        id = aId;
30  
-        profileName = aProfileName;
31  
-        profileImage = requestImage(aProfileImageURL);
32  
-        text = aText;
33  
-        time = aTime;
34  
-
35  
-        // Draw profile images to the same place each frame
36  
-        randomPosX = random(0, width);
37  
-        randomPosY = random(0, height);
38  
-      }
  16
+      public String id;
  17
+      public String profileName;
  18
+      public String profileImageUrl;
  19
+      public PImage profileImage; // needs to be loaded manually
  20
+      public String text;
  21
+      public Date time;
  22
+      public int randomPosX; // needs to be set -- randomPosX = random(0, width);
  23
+      public int randomPosY; // needs to be set -- randomPosY = random(0, height);
39 24
     }
40 25
 
41 26
     void setup() {
42 27
       size(1024, 1024);
43  
-      tweets = new HashMap();
  28
+
  29
+      uniqueTweets = new HashMap();
  30
+      frameRate(1);
  31
+
  32
+      // Start a live feed loading, results will be available in a global
  33
+      // ArrayList called tweets.
  34
+      loadTweets('bacon');
  35
+
  36
+      // You can also pass geolocation data to limit tweets to a geographic area
  37
+      // loadTweets('class', '43.7496,-79.4886,1km'); // York University
44 38
     }
45 39
 
46 40
     void draw() {
47  
-      background(255);
  41
+      background(121);
48 42
 
49  
-      if (tweets.size() == 0) {
50  
-        return;
  43
+      Tweet tweet;
  44
+
  45
+      // Store unique tweets that have come in
  46
+      for (int i = 0; i < tweets.size(); i++) {
  47
+        tweet = (tweet)tweets.get(i);
  48
+        addTweet(tweet);
51 49
       }
  50
+      tweets.clear();
52 51
 
53  
-      Tweet tweet;
54  
-      Iterator it = tweets.entrySet().iterator();
  52
+      // Loop through unique tweets we've stored
  53
+      Iterator it = uniqueTweets.entrySet().iterator();
55 54
       object elem;
56  
-
  55
+      int w, r;
57 56
       while (it.hasNext()) {
58 57
         elem = it.next();
59 58
         tweet = (Tweet)elem.getValue();
  59
+
  60
+        // Draw the profile image
60 61
         image(tweet.profileImage, tweet.randomPosX, tweet.randomPosY);
  62
+
  63
+        // Print the tweet to the Processing debug console
  64
+        println(tweet.text);
61 65
       }
62 66
     }
63 67
 
64  
-    // Called from external JS, passes new Tweet object
  68
+    // Store unique tweets (keyed on tweet.id), loading image data
65 69
     void addTweet(Tweet tweet) {
66  
-      // Only store each tweet once (twitter sends them over and over)
67  
-      if (!tweets.containsKey(tweet.id)) {
68  
-        tweets.put(tweet.id, tweet);
69  
-      }
70  
-    }
  70
+      if (!uniqueTweets.containsKey(tweet.id)) {
  71
+        // Load a PImage of the profile pic
  72
+        tweet.profileImage = requestImage(tweet.profileImageUrl);
71 73
 
72  
-    // Called from external JS, removes count tweets.
73  
-    void removeTweets(count) {
74  
-      while(count-- >= 0) {
75  
-        tweets.remove(tweets.size() - 1);
76  
-      }
77  
-    }
  74
+        // Draw profile images to the same place each frame
  75
+        tweet.randomPosX = random(0, width);
  76
+        tweet.randomPosY = random(0, height);
78 77
 
79  
-    void clearTweets() {
80  
-      background(255);
81  
-      tweets.clear();
  78
+        // Store the tweet in our list of unique tweets
  79
+        uniqueTweets.put(tweet.id, tweet);
  80
+      }
82 81
     }
83 82
   </script>
84  
-
85  
-  <div>
86  
-    <button onclick="twitter.start();">Start</button>
87  
-    <button onclick="twitter.stop();">Stop</button>
88  
-    <button onclick="twitter.clear();">Clear</button>
89  
-  </div>
90  
-
91 83
   <canvas id="tweets"></canvas>
92  
-
93  
-	<script type="text/javascript">
94  
-    var p = new Processing("tweets", document.getElementById("sketch").text);
95  
-//		$('#twitterSearch').liveTwitter('chicago', {limit: 10, rate: 5000, rpp: 10, geocode: '41.7897563,-87.5997711,1km'});
96  
-	  liveTwitter('toronto', {limit: 50, rate: 5000, rpp: 50}, p);
97  
-
98  
-	</script>
99 84
 </body>
100 85
 </html>
88  examples/twitter/livetwitter.js → examples/twitter/processing-twitter.js
@@ -8,25 +8,46 @@
8 8
  * Modified by David Humphrey (@humphd) for use with Processing.js
9 9
  */
10 10
 
11  
-/*
12  
- * Usage example:
13  
- * liveTwitter('bacon', {limit: 10, rate: 15000}, processingInstance);
14  
- */
15  
-
16  
-
17  
-(function ($) {
18  
-
19  
-  // Extend jQuery with a reverse function if it isn't already defined
20  
-  if (!$.fn.reverse) {
21  
-    $.fn.reverse = function () {
22  
-      return this.pushStack(this.get().reverse(), arguments);
  11
+(function (document, Processing) {
  12
+
  13
+  function getScript(success) {
  14
+    var script = document.createElement('script');
  15
+    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js";
  16
+    var head=document.getElementsByTagName('head')[0],
  17
+        done=false;
  18
+    script.onload = script.onreadystatechange = function(){
  19
+      if ( !done && (!this.readyState
  20
+           || this.readyState == 'loaded'
  21
+           || this.readyState == 'complete') ) {
  22
+        done=true;
  23
+        success(jQuery);
  24
+        script.onload = script.onreadystatechange = null;
  25
+      }
23 26
     };
  27
+    head.appendChild(script);
24 28
   }
25 29
 
26  
-  this.liveTwitter = function(query, options, processing, callback) {
  30
+  // Expose two new globals to Processing sketches:
  31
+  //   1) loadTweets() a function to start a live-feed query
  32
+  //   2) tweets an ArrayList of tweets that have been loaded
  33
+  var Pp = Processing.prototype;
  34
+
  35
+  Pp.tweets = new Pp.ArrayList();
  36
+
  37
+  Pp.loadTweets = function(query, geocode) {
  38
+    var options = {geocode: geocode};
  39
+    getScript(function(jQuery) {
  40
+      loadTweets$(jQuery, query, options);
  41
+    });
  42
+  };
27 43
 
28  
-    // Put the tweets on the global so Processing can get them too.
29  
-    window.tweets = [];
  44
+  function loadTweets$(jQuery, query, options, callback) {
  45
+    // Extend jQuery with a reverse function if it isn't already defined
  46
+    if (!$.fn.reverse) {
  47
+      $.fn.reverse = function () {
  48
+        return this.pushStack(this.get().reverse(), arguments);
  49
+      };
  50
+    }
30 51
 
31 52
     $(this).each(function () {
32 53
       var settings = {};
@@ -50,8 +71,8 @@
50 71
         // These are the default settings.
51 72
         settings = $.extend({
52 73
           mode:      'search', // Mode, valid options are: 'search', 'user_timeline', 'list', 'home_timeline'
53  
-          rate:      15000,    // Refresh rate in ms
54  
-          limit:     10,       // Limit number of results
  74
+          rate:      3000,    // Refresh rate in ms
  75
+          limit:     100,       // Limit number of results
55 76
           imageSize: 24,       // Size of image in pixels
56 77
           refresh:   true,
57 78
           timeLinks: true,
@@ -271,13 +292,13 @@
271 292
 
272 293
           // Create an object useable by Processing
273 294
           toP5: function (tweet) {
274  
-            return new processing.Tweet(
275  
-              tweet.id,
276  
-              tweet.screen_name,
277  
-              tweet.profile_image_url, // will get async loaded
278  
-              tweet.text,
279  
-              this.relativeTime(tweet.created_at)
280  
-            );
  295
+            return {
  296
+              id: tweet.id,
  297
+              profileName: tweet.screen_name,
  298
+              profileImageUrl: tweet.profile_image_url,
  299
+              text: tweet.text,
  300
+              date: this.relativeTime(tweet.created_at)
  301
+            };
281 302
           },
282 303
 
283 304
           // Handle reloading
@@ -286,8 +307,6 @@
286 307
             if (twitter.settings.refresh || initialize) {
287 308
 
288 309
               $.getJSON(twitter.apiURL(), function (json) {
289  
-                var newTweets = 0;
290  
-
291 310
                 // The search and regular APIs differ somewhat
292 311
                 var results = (twitter.settings.mode === 'search') ? json.results : json;
293 312
 
@@ -298,25 +317,13 @@
298 317
                   if (!twitter.settings.filter || twitter.settings.filter(this)) {
299 318
                     // Check if this is actually a new tweet
300 319
                     if (Date.parse(tweet.created_at) > twitter.lastTimeStamp) {
301  
-                      processing.addTweet(twitter.toP5(tweet));
  320
+                      Pp.tweets.add(twitter.toP5(tweet));
302 321
 
303 322
                       // Remember the last timestamp for the next refresh.
304 323
                       twitter.lastTimeStamp = Date.parse(tweet.created_at);
305  
-
306  
-                      newTweets++;
307 324
                     }
308 325
                   }
309 326
                 });
310  
-
311  
-                // Did we get any new tweets?
312  
-                if (newTweets > 0) {
313  
-                  // Remove old entries exceeding the limit
314  
-                  var extra = newTweets - twitter.settings.limit;
315  
-                  if (extra > 0) {
316  
-                    processing.removeTweets(newTweets - twitter.settings.limit);
317  
-                    newTweets -= extra;
318  
-                  }
319  
-                }
320 327
               });
321 328
             }
322 329
           },
@@ -342,7 +349,6 @@
342 349
 
343 350
           // Clear all tweets
344 351
           clear: function () {
345  
-            processing.clearTweets();
346 352
             this.lastTimeStamp = null;
347 353
           }
348 354
         };
@@ -359,4 +365,4 @@
359 365
     });
360 366
     return this;
361 367
   };
362  
-})(jQuery);
  368
+})(window.document, Processing);

0 notes on commit 7b2dca5

Please sign in to comment.
Something went wrong with that request. Please try again.