/lib/twitter.js

https://github.com/ajkovar/Geo-Tweeter · JavaScript · 107 lines · 60 code · 13 blank · 34 comment · 15 complexity · 0735a5651f7e6bbff829af42587e852c MD5 · raw file

  1. (function($){
  2. z.twitter = {};
  3. /**
  4. * Filters out tweets that don't have geocodes associated with them
  5. */
  6. z.twitter.filterNonGeocode = function(tweet){
  7. return !!tweet.geo;
  8. };
  9. /**
  10. * Filters out tweets that don't have geocodes associated with them
  11. */
  12. z.twitter.filterNonGeocodes = function(tweets){
  13. return $.grep(tweets, z.twitter.filterNonGeocode);
  14. };
  15. /**
  16. * Merges two lists of tweets, removing duplicates
  17. */
  18. z.twitter.mergeTweets = function(tweets1, tweets2){
  19. if(!tweets1 && !tweets2)
  20. return [];
  21. //if one is null or they both are the same object, return only one of them
  22. else if(tweets1 && !tweets2 || tweets1 === tweets2)
  23. return tweets1;
  24. else if(!tweets1 && tweets2)
  25. return tweets2;
  26. else {
  27. var result = $.merge(tweets1, tweets2);
  28. var idMap = [];
  29. return $.grep(result, function(tweet){
  30. if(idMap[tweet.id])
  31. return false;
  32. else {
  33. idMap[tweet.id]=true;
  34. return true;
  35. }
  36. });
  37. }
  38. };
  39. /**
  40. * search for tweets for given search term, returned tweets get passed into
  41. * callback function will continue to search until min has been reached
  42. * TODO handle situation where total tweets for given term is less than min
  43. */
  44. z.twitter.search = function(searchTerm, min, callback){
  45. var tweets = [];
  46. var calls = min/100 > 0 ? Math.ceil(min/100) : 1;
  47. var numberOfCallsMade = 0;
  48. $.getJSON("http://search.twitter.com/search.json" + "?q=%23"+searchTerm+"&rpp=100" + "&callback=?",
  49. function receiveTweets(data){
  50. numberOfCallsMade++;
  51. tweets = tweets.concat(data.results);
  52. if(numberOfCallsMade===calls)
  53. callback(tweets);
  54. else $.getJSON("http://search.twitter.com/search.json" + data.nextPage, receiveTweets);
  55. });
  56. };
  57. window.z.twitter.TweetAggregator = function(searchTerm, filter){
  58. this.searchTerm = searchTerm;
  59. this.filter = filter;
  60. this.tweets = z.storage.getLocal("tweets") || [];
  61. /*var tweetStore = z.storage.getLocal("tweets");
  62. if(typeof tweetStore !== "object")
  63. z.storage.setLocal("tweets", {})
  64. this.tweets = tweetStore[searchTerm] || [];*/
  65. }
  66. window.z.twitter.TweetAggregator.prototype = {
  67. search: function(min, callback){
  68. var self = this;
  69. z.twitter.search(self.searchTerm, min,
  70. function(tweets){
  71. self.tweets = z.twitter.mergeTweets(self.tweets, tweets);
  72. self.tweets = $.grep(self.tweets, self.filter);
  73. tweets = $.grep(tweets, self.filter);
  74. /*var tweetStore = z.storage.getLocal("tweets");
  75. tweetStore[self.searchTerm]=self.tweets;
  76. z.storage.setLocal("tweets", tweetStore);*/
  77. z.storage.setLocal("tweets", self.tweets);
  78. callback.call(self, tweets);
  79. });
  80. }
  81. };
  82. }(jQuery));
  83. /*
  84. profile_image_url
  85. created_at
  86. from_user
  87. to_user_id
  88. text
  89. id
  90. from_user_id
  91. geo
  92. iso_language_code
  93. source*/