【问题标题】:Twitter Search In user_timeline.json在 user_timeline.json 中搜索 Twitter
【发布时间】:2011-06-02 14:18:35
【问题描述】:

我正在使用 jQuery 检索 .json 中的推文。我的应用程序的一部分需要旧推文,而推特搜索仅返回最近 6 天的推文。这条旧推文必须包含某些主题标签。 我知道 user_timeline API 方法可以返回 3200 条推文,但我不明白如何获取 3200 条推文,遍历它们并找到我需要的推文。 这是我现在所拥有的:

function TwitterAPI(){}
TwitterAPI.Statuses = function Statuses(){}

TwitterAPI.Statuses.hashed = function(username, hashtag, count, to, callback){
   requestURL = "http://search.twitter.com/search.json?q=%23" + hashtag + "&rpp=" + count + "&from=" + username + "&to=" + to + "&callback=?";
   //requestURL =  "http://otter.topsy.com/search.json?q=from:" + username + "+" + hashtag + "&perpage=1"
   $.getJSON(requestURL, callback);
  } 

var user_info = ["name", "birthday", "hobby", "homepage"];  
  $("#info ul li.userinfo").append('<img src="images/lilspinner.gif" />');
  $.each(user_info, function() {
   var hashtag = this;
   TwitterAPI.Statuses.hashed("<%= @profile.name %>", hashtag, 1, "twisnt", function(json, status){
    var content = "";
    $.each(json.results, function(i, info){ 
     text = info.text.replace( /(\#)\w+\b/,"").replace( /(\@)\w+\b/,"");
     if (hashtag == "homepage") {
      content = "<a style='display:none' href='"+text+"'>"+text+"</a>"; 
     }
     else {
      content = "<span style='display:none'>"+text+"</span>"; 
     }          
    });  
    $("li#" + hashtag).append(content);
    $("li#" + hashtag + " img").remove();
    $("li#" + hashtag + " *").fadeIn("slow");
   })
});

它可以满足我的一切需求,但仅限于在过去 6 天内发布的需要消息时,

【问题讨论】:

    标签: jquery json twitter getjson


    【解决方案1】:

    根据 twitter API,您一次只能获得 100 个结果。使用 rpp 搜索项请求 100 并从第 1 页开始。然后您可以循环浏览页面,一次获取 100 项。这样做 15 次以获得 1500 个项目。这样做的原因是 twitter 只允许您获取最多 1500 个项目的最后 15 页。

    例子:

    var maxRpp = 100;
    
    var i; //pages
    for (i = 1;i <= 15; i++) {
         $.getJSON('http://search.twitter.com/search.json?q=pants&result_type=recent&page=' + i + '&rpp=' + maxRpp + '&callback=?',function(data){
              //do thing  
         });    
    };
    

    来源: Twitter API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2013-05-23
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多