【问题标题】:Is there a way to "notify" when you hit the hourly limit?当您达到每小时限制时,有没有办法“通知”?
【发布时间】:2019-09-08 06:12:03
【问题描述】:

我目前正在查看 TweetInvi 的文档,并查看了 RateLimit 类。 而且我一直在尝试实现它,我不知道这是对 Twitter 缺乏了解还是限制,但我似乎无法找到一种方法来发送 notification 在控制台消息方面说“嘿,你已经达到了每小时的限制,它将在 x 分钟内重置……”或类似的内容。

我正在查看此示例,您也可以在文档中找到该示例。

TweetinviEvents.QueryBeforeExecute += (sender, args) =>
{
    var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

    // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
    if (queryRateLimits != null)
    {
        if (queryRateLimits.Remaining > 0)
        {
            // We have enough resource to execute the query
            return;
        }

        // Strategy #1 : Wait for RateLimits to be available
        Console.WriteLine("Waiting for RateLimits until : {0}", queryRateLimits.ResetDateTime.ToLongTimeString());
        Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);

        // Strategy #2 : Use different credentials
        var alternateCredentials = TwitterCredentials.CreateCredentials("", "", "", "");
        var twitterQuery = args.TwitterQuery;
        twitterQuery.OAuthCredentials = alternateCredentials;

        // Strategy #3 : Cancel Query
        args.Cancel = true;

        // Strategy #4 : Implement yours!
    }
};

当我尝试发送一条推文说“嘿,慢点,伙计,你已经达到极限,请稍等(剩下的时间) )"

如果这个包装器甚至可以做到的话。

【问题讨论】:

    标签: c# twitter tweetinvi


    【解决方案1】:

    请参阅下面的示例,希望对您有所帮助。

    public class TwitterManager
    {
        private readonly ITwitterCredentials _credentials;
        private bool _isRateLimited;
    
        public TwitterManager()
        {
            _credentials = Auth.SetUserCredentials("", "", "", "");
            TweetinviEvents.QueryBeforeExecute += (sender, args) =>
            {
                var queryRateLimit = RateLimit.GetQueryRateLimit(args.QueryURL);
                if (queryRateLimit != null)
                {
                    if (queryRateLimit.Remaining > 0)
                         return;
                    else
                        _isRateLimited = true;
                }
            };
    
        }
    
        public void GetTweets() {
            UserTimelineParameters userTimelineParameters = new UserTimelineParameters();
            userTimelineParameters.MaximumNumberOfTweetsToRetrieve = 50;
            try
            {
                var tweets = Auth.ExecuteOperationWithCredentials(_credentials, () =>
                {
                    return Timeline.GetUserTimeline("cnn", userTimelineParameters);
                });
    
                if (_isRateLimited)
                {
                    Console.WriteLine("Request rate limit has been exceeded. Please try again later.");
                }
                else if (tweets != null) {
                    foreach (var item in tweets)
                    {
                        Console.WriteLine(item.FullText);
                    }
                }
                Console.ReadKey();
            }
            catch (WebException wex)
            {
                var statusCode = -1;
                if (statusCode == TweetinviConsts.STATUS_CODE_TOO_MANY_REQUEST)
                {
                    // The RateLimit is exhausted. Perform your code to manage it!
                }
            }
    
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2021-09-21
    • 2019-01-12
    相关资源
    最近更新 更多