【问题标题】:LINQ to Twitter: Get more than 200 tweetsLINQ to Twitter:获取超过 200 条推文
【发布时间】:2012-12-07 20:16:17
【问题描述】:

我正在尝试检索过去 2 个月的用户推文。但是,LINQ to Twitter 将您可以检索的推文数量限制为 200。有没有办法检索更多?

Twitter api 允许分页,例如:

http://api.twitter.com/1/statuses/user_timeline.json?id=username8&count=200&page=2

我在 LINQ to Twitter 库中找不到任何类似的东西。

我尝试了以下方法,但它不起作用

var statusTweets = (from tweet in twitterCtx.Status
                    where tweet.Type == StatusType.User &&
                    tweet.Count == 200 &&
                    tweet.ScreenName == "username"
                    select tweet).Skip(200);

【问题讨论】:

    标签: linq-to-twitter


    【解决方案1】:

    好吧,我现在觉得有点傻。原来有一个分页参数。

    解决方案

    for (int i = 0; i < 5; i++)
    { 
        var statusTweets = (from tweet in twitterCtx.Status
                            where tweet.Type == StatusType.User &&
                            tweet.Count == 200 &&
                            tweet.ScreenName == "username" &&
                            tweet.Page == i
                            select tweet)
    }
    

    【讨论】:

      【解决方案2】:

      这是获取用户所有推文的完整功能

      public static List<Status> searchUserTweet(string screenName, int maxPagination)
      {
          var twitterCtx = new TwitterContext(authorizer);
          List<Status> searchResults = new List<Status>();
          int maxNumberToFind = 200;
          int pagination = 0;
          ulong lastId = 0;
          int count = 0;
      
          var tweets = (from tweet in twitterCtx.Status
                          where tweet.Type == StatusType.User &&
                              tweet.ScreenName == screenName &&
                              tweet.Count == maxNumberToFind
                          select tweet).ToList();
      
          if (tweets.Count > 0)
          {
              lastId = ulong.Parse(tweets.Last().StatusID.ToString());
              searchResults.AddRange(tweets);
          }
      
          do
          {
              var id = lastId - 1;
              tweets = (from tweet in twitterCtx.Status
                              where tweet.Type == StatusType.User &&
                                  tweet.ScreenName == screenName &&
                                  tweet.Count == maxNumberToFind &&
                                  tweet.MaxID == id
                              select tweet).ToList();
      
              searchResults.AddRange(tweets);
              lastId = tweets.Min(x => x.StatusID);
              pagination++;
              count = (pagination > maxPagination) ? 0 : tweets.Count;
          } while (count == maxNumberToFind);
      
          return searchResults;
      }
      

      【讨论】:

      • 谢谢 - 这是一个比指定答案更完整的解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2020-03-10
      相关资源
      最近更新 更多