如何处理LinqtoTwitter Rate(v2.1)限制超出错误 [英] How to handle LinqtoTwitter Rate(v2.1) limit exceeded Error

查看:107
本文介绍了如何处理LinqtoTwitter Rate(v2.1)限制超出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.您好,我创建了一个用于在屏幕名称的帮助下从Twitter提取用户推文的类.我的问题是我经常超出速率限制.

1 .Hi SO, I have a created a class for fetching user's tweets from twitter with the help of screen name. My problem is I'm getting rate limit exceeded very frequently.

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

2 .I had created table for screen name in which I'm saving all screen names and

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

3 .I had created another table to store user's tweets.

以下是我的代码:

 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;
    }

  1. 我正在使用上面的方法有下面的方法.

  1. I am using above method has below..

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

        foreach (var obj in getTweets)
        {
            using (DBcontext = new DBContext())
            {
                tweets.Name = obj.Name;
                tweets.ProfileImage = obj.ProfileImagePath;
                tweets.PostedOn = obj.PostedDate;
                tweets.Tweets = obj.Tweets;
                tweets.CreatedOn = DateTime.Now;
                tweets.ModifiedOn = DateTime.Now;
                tweets.Status = EntityStatus.Active;
                tweets.ScreenName = obj.ScreenName;
                var exist = context.UserTweets.Any(user => user.Tweets.Equals(obj.Tweets));
                if (!exist)
                    context.UserTweets.Add(tweets);
                context.SaveChanges();
            }
        }
    }

推荐答案

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

I see that you found the Help/RateLimits query. There are various approaches you can take. e.g. add a delay between queries, delay the next query if the limit has been exceeded, or catch the exception and delay until the next 15 minute window.

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

If you want to monitor interactively, you can watch the rate limit for each query. The TwitterContext instance you use for performing the query contains RateLimitXxx properties that populate after every query. You'll need to read those values after the query, which appears to be inside your GetAllTweets method. You have to expose those values to your loop somehow, via return object, out params, static field, or whatever logic you feel is necessary.

// 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

RateLimitRemaining is how many queries you can do in the current 15 minute window and RateLimitReset is the number of epoch seconds remaining until the rate limit resets (when you can start querying again).

汇率限制上查看Twitter文档会很有帮助.

It would be helpful to review the Twitter docs on Rate Limiting.

作为参考,这里还有另外两个可能会提供更多想法的问题:

For reference, here are a couple other questions that might provide more ideas:

使用LINQ在Twitter上吸引所有关注者

这篇关于如何处理LinqtoTwitter Rate(v2.1)限制超出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆