当您达到每小时限制时,有没有办法"notify"?



我目前正在查看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!
    }
};

当我尝试发送一条推文说"嘿,慢着,伙计,你已经达到极限,请稍候(剩余时间("时,我将如何实现它并实际收到通知

如果用那个包装器可以的话。

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

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

相关内容

  • 没有找到相关文章

最新更新