Skip to content

Instantly share code, notes, and snippets.

@mach3
Created May 7, 2011 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach3/960423 to your computer and use it in GitHub Desktop.
Save mach3/960423 to your computer and use it in GitHub Desktop.
Twitterの複数のハッシュタグの検索結果をマージする
var MultiHashResult = function( tags ){
var d, self, _onLoad, _onComplete, _reset ;
self = this;
d = {
api : "http://search.twitter.com/search.json",
callback : null,
tags : null,
loaded : 0,
results : null,
tweets : null,
ids : null
};
this.type = "MultiHashResult";
this.setTags = function( tags ){
d.tags = tags;
};
this.setCallback = function( callback ){
d.callback = callback;
};
this.load = function(){
_reset();
$.each( d.tags, function( i, tag ){
var url = d.api + "?" + $.param({ q : "#" + tag, rpp : 50 });
$.ajax({
url : url,
dataType : "jsonp",
success : _onLoad
});
});
};
this.getTweets = function(){
return d.tweets;
};
_reset = function(){
d.loaded = 0;
d.results = [];
d.tweets = [];
d.ids = [];
};
_onLoad = function( data, status ){
d.loaded ++;
d.results.push( data.results );
if( d.loaded === d.tags.length ){
_onComplete();
}
};
_onComplete = function(){
$.each( d.results, function( i, r ){
$.each( r, function( i, t ){
if( $.inArray( t.id_str, d.ids ) >= 0 ){ return; }
d.ids.push( t.id_str );
d.tweets.push( t );
});
});
d.tweets.sort( function( a, b ){
return a.id < b.id
});
if( d.callback ){
d.callback( self );
}
};
this.setTags( tags );
};
/**
* Test code
*/
var mh = new MultiHashResult(
["cookingsongs", "mamasays"]
);
mh.setCallback( function( m ){
var tweets = m.getTweets();
$.each( tweets, function( i, t ){
console.log( t.text );
});
});
mh.load();
/**
* 差分を取ってこれるreload()メソッドを追加しよう
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment