【问题标题】:TwitterContext Limitations outside of LinqToTwitter API callsLinqToTwitter API 调用之外的 TwitterContext 限制
【发布时间】:2018-02-15 11:41:44
【问题描述】:

关注Extract BearerToken from LinqToTwitter IAuthorizer

虽然我没有将 LTT 库用于任何过去的授权(此时),但当使用

直接调用 /statuses/user_timeline API 时,我似乎仍然被限制为 200 条推文

{参数字符串:user_id={0}&screen_name={1}&count=3200&exclude_replies=true&include_rts=false&trim_user=false&contributor_details=false}

webClient.Headers.Add(String.Format("Authorization:  Bearer {0}", BearerToken));

这是TwitterContext 授权的限制吗?如果是这样,我如何在不使用库调用的情况下更改该限制?

即我使用

statusResponse = (from tweet in twitterCtx.Status ...)

我不使用该库,因为它使用 Twitter 搜索对象,根据 Twitter 对搜索对象的限制,这可能会产生不一致的结果。

提前谢谢你!

【问题讨论】:

    标签: twitter linq-to-twitter


    【解决方案1】:

    根据 Twitter 的 statuses/user_timeline documentationcount 的最大值为 200。但是,这是每个查询的最大值。您可以进行多个查询以检索多达 3200 条推文。 Twitter 在他们的Working with Timelines page 中有一个很好的解释,关于如何使用时间线来检索这 3200 条推文。我知道您不是在使用 LINQ to Twitter 进行查询,但是为了其他找到此答案的人的利益,LINQ to Twitter 是这样做的:

    static async Task RunUserTimelineQueryAsync(TwitterContext twitterCtx)
    {
        //List<Status> tweets =
        //    await
        //    (from tweet in twitterCtx.Status
        //     where tweet.Type == StatusType.User &&
        //           tweet.ScreenName == "JoeMayo"
        //     select tweet)
        //    .ToListAsync();
    
        const int MaxTweetsToReturn = 200;
        const int MaxTotalResults = 100;
    
        // oldest id you already have for this search term
        ulong sinceID = 1;
    
        // used after the first query to track current session
        ulong maxID;
    
        var combinedSearchResults = new List<Status>();
    
        List<Status> tweets =
            await
            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.User &&
                   tweet.ScreenName == "JoeMayo" &&
                   tweet.Count == MaxTweetsToReturn &&
                   tweet.SinceID == sinceID &&
                   tweet.TweetMode == TweetMode.Extended
             select tweet)
            .ToListAsync();
    
        if (tweets != null)
        {
            combinedSearchResults.AddRange(tweets);
            ulong previousMaxID = ulong.MaxValue;
            do
            {
                // one less than the newest id you've just queried
                maxID = tweets.Min(status => status.StatusID) - 1;
    
                Debug.Assert(maxID < previousMaxID);
                previousMaxID = maxID;
    
                tweets =
                    await
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.User &&
                           tweet.ScreenName == "JoeMayo" &&
                           tweet.Count == MaxTweetsToReturn &&
                           tweet.MaxID == maxID &&
                           tweet.SinceID == sinceID &&
                           tweet.TweetMode == TweetMode.Extended
                     select tweet)
                    .ToListAsync();
    
                combinedSearchResults.AddRange(tweets);
    
            } while (tweets.Any() && combinedSearchResults.Count < MaxTotalResults);
    
            PrintTweetsResults(tweets);
        }
        else
        {
            Console.WriteLine("No entries found.");
        }
    }
    

    您可以在Querying the User Timeline 上的 LINQ to Twitter 文档中找到它。我还写了一篇博文,Working with Timelines with LINQ to Twitter,来解释 LINQ to Twitter 的分页方法。它适用于较早的(非异步)版本,但概念仍然相同。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多