我目前正在查看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!
}
};
当我尝试发送一条推文说“嘿,伙计们,你已经达到极限了,请稍等(剩下的时间, )”
如果那个包装甚至有可能。
答案 0 :(得分:0)
请参见以下示例,希望对您有所帮助。
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!
}
}
}
}