C# 4.0 如何处理超出LinqtoTwitter速率(v2.1)限制的错误

C# 4.0 如何处理超出LinqtoTwitter速率(v2.1)限制的错误,c#-4.0,twitter,linq-to-twitter,C# 4.0,Twitter,Linq To Twitter,1.你好,我已经创建了一个类,通过屏幕名称从twitter获取用户的推文。我的问题是我经常被超过利率限制 2.我已经为屏幕名创建了一个表,在其中我保存了所有的屏幕名和 3.我创建了另一个表来存储用户的推文 下面是我的代码: public List<TwitterProfileDetails> GetAllTweets(Func<SingleUserAuthorizer> AuthenticateCredentials,string screenname) {

1.你好,我已经创建了一个类,通过屏幕名称从twitter获取用户的推文。我的问题是我经常被超过利率限制

2.我已经为屏幕名创建了一个表,在其中我保存了所有的屏幕名和

3.我创建了另一个表来存储用户的推文

下面是我的代码:

 public List<TwitterProfileDetails> GetAllTweets(Func<SingleUserAuthorizer> AuthenticateCredentials,string screenname)
    {
        List<TwitterProfileDetails> lstofTweets = new List<TwitterProfileDetails>();
        TwitterProfileDetails details = new TwitterProfileDetails();
        var twitterCtx = new LinqToTwitter.TwitterContext(AuthenticateCredentials());
        var helpResult =
            (from help in twitterCtx.Help
             where help.Type == HelpType.RateLimits &&
             help.Resources == "search,users,socialgraph"
             select help)
            .SingleOrDefault();

        foreach (var category in helpResult.RateLimits)
        {
            Console.WriteLine("\nCategory: {0}", category.Key);

            foreach (var limit in category.Value)
            {
                Console.WriteLine(
                    "\n  Resource: {0}\n    Remaining: {1}\n    Reset: {2}\n    Limit: {3}",
                    limit.Resource, limit.Remaining, limit.Reset, limit.Limit);
            }
        }

            var tweets = from t in twitterCtx.Status
                         where t.Type == StatusType.User && t.ScreenName == screename && t.Count == 15
                         select t;

            if (tweets != null)
            {
                foreach (var tweetStatus in tweets)
                {
                    if (tweetStatus != null)
                    {
                        lstofTweets.Add(new TwitterProfileDetails { Name = tweetStatus.User.Name, ProfileImagePath = tweetStatus.User.ProfileImageUrl, Tweets = tweetStatus.Text, UserID = tweetStatus.User.Identifier.UserID, PostedDate = Convert.ToDateTime(tweetStatus.CreatedAt),ScreenName=screename });
                    }
                }
            }
            return lstofTweets;
    }

我看到您找到了Help/RateLimits查询。你可以采取多种方法。e、 g.在查询之间添加延迟,如果超过限制,则延迟下一个查询,或者捕获异常并延迟到下一个15分钟窗口

如果希望以交互方式进行监视,可以查看每个查询的速率限制。用于执行查询的TwitterContext实例包含在每次查询后填充的RateLimitXxx属性。您需要在查询之后读取这些值,该查询似乎位于GetAllTweets方法中。您必须以某种方式通过返回对象、输出参数、静态字段或任何您认为必要的逻辑将这些值公开给循环

// the first time through, you have the whole rate limit for the 15 minute window

foreach (var screenObj in screenName)
{
    var getTweets = api.GetAllTweets(api.AuthenticateCredentials, screenObj.UserName);

    // your processing logic ...

    // assuming you have the RateLimitXxx values in scope
    if (rateLimitRemaining == 0)
        Thread.Sleep(CalculateRemainingMilliseconds(RateLimitReset));


}
RateLimitRemaining是您在当前15分钟窗口内可以执行的查询数,RateLimitReset是在速率限制重置之前(您可以再次开始查询时)剩余的查询数

在上查看推特文档会很有帮助

以下几个问题可能会提供更多想法供参考:

// the first time through, you have the whole rate limit for the 15 minute window

foreach (var screenObj in screenName)
{
    var getTweets = api.GetAllTweets(api.AuthenticateCredentials, screenObj.UserName);

    // your processing logic ...

    // assuming you have the RateLimitXxx values in scope
    if (rateLimitRemaining == 0)
        Thread.Sleep(CalculateRemainingMilliseconds(RateLimitReset));


}