【发布时间】:2013-05-03 20:50:32
【问题描述】:
我按照本教程创建了一个自定义 Twitter 小部件。 它基本上使用 Twitter API、Json 来拉推文。
http://www.evoluted.net/thinktank/web-development/creating-your-own-twitter-trends-widget
在教程中,我收到了推文。 但我想将推文的数量限制在 2 条以内。
【问题讨论】:
我按照本教程创建了一个自定义 Twitter 小部件。 它基本上使用 Twitter API、Json 来拉推文。
http://www.evoluted.net/thinktank/web-development/creating-your-own-twitter-trends-widget
在教程中,我收到了推文。 但我想将推文的数量限制在 2 条以内。
【问题讨论】:
在url中添加rpp=参数
$(document).ready(function() {
// json call to twitter to request tweets containing our keyword, in this case 'sheffield'
$.getJSON("http://search.twitter.com/search.json?q=sheffield&rpp=2&callback=?", function(data) {
// loop around the result
$.each(data.results, function() {
var text = this.text;
if(text.charAt(0) != '@') {
// construct tweet and add append to our #tweets div
var tweet = $("<div></div>").addClass('tweet').html(text);
// analyse our tweet text and turn urls into working links, hash tags into search links, and @replies into profile links.
tweet.html('<div class="content">' +
tweet.html()
.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
+ '<br /><a href="http://www.twitter.com/' + this.from_user + '/status/' + this.id_str + '" class="view" target="_blank">' + $.timeSinceTweet(this.created_at) + '</a></div>'
)
.prepend('<a href="http://www.twitter.com/' + this.from_user + '" target="_blank"><img src="' + this.profile_image_url + '" width="48" height="48" /></a>')
.appendTo('#tweets')
.fadeIn();
}
});
});
});
别忘了切换到 API 1.1。 See API 1.1 search documentation。 API 1.1 使用计数参数来指定推文限制。
【讨论】: