private bool TryPrepareRequest( string query, HttpMethod httpMethod, RateLimitTrackerOptions rateLimitTrackerOption, ITwitterCredentials credentials, out ITwitterQuery twitterQuery) { twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials ?? _credentialsAccessor.CurrentThreadCredentials); var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery); _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs); if (beforeQueryExecuteEventArgs.Cancel) { twitterQuery = null; return(false); } if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait) { _rateLimitAwaiter.WaitForCurrentCredentialsRateLimit(query); } return(true); }
private bool TryPrepareRequest( string query, HttpMethod httpMethod, RateLimitTrackerOptions rateLimitTrackerOption, ITwitterCredentials credentials, out ITwitterQuery twitterQuery) { credentials = credentials ?? _credentialsAccessor.CurrentThreadCredentials; if (credentials == null) { throw new TwitterNullCredentialsException(); } twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials); var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery); if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackOnly || rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait) { var timeToWait = _rateLimitAwaiter.TimeToWaitBeforeTwitterRequest(query, twitterQuery.TwitterCredentials); twitterQuery.DateWhenCredentialsWillHaveRequiredRateLimits = DateTime.Now.AddMilliseconds(timeToWait); _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs); if (beforeQueryExecuteEventArgs.Cancel) { twitterQuery = null; return(false); } if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait) { _rateLimitAwaiter.Wait(timeToWait); } } else { _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs); if (beforeQueryExecuteEventArgs.Cancel) { twitterQuery = null; return(false); } } _tweetinviEvents.RaiseBeforeExecuteAfterRateLimitAwait(beforeQueryExecuteEventArgs); return(true); }
private StreamReader CreateStreamReaderFromWebRequest(WebRequest webRequest) { if (webRequest == null) { SetStreamState(StreamState.Stop); return(null); } StreamReader reader = null; try { var twitterQuery = _twitterQueryFactory.Create(webRequest.RequestUri.AbsoluteUri); var queryBeforeExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery); _tweetinviEvents.RaiseBeforeQueryExecute(queryBeforeExecuteEventArgs); if (queryBeforeExecuteEventArgs.Cancel) { SetStreamState(StreamState.Stop); return(null); } // TODO : LINVI - THIS CODE HAS CHANGED AND NEEDS TO BE CHECKED WITH ASP.NET var responseStream = _webHelper.GetResponseStreamAsync(webRequest).Result; if (responseStream != null) { reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); } } catch (WebException wex) { HandleWebException(wex); } catch (Exception ex) { if (ex is ArgumentException) { if (ex.Message == "Stream was not readable.") { webRequest.Abort(); } } _lastException = ex; SetStreamState(StreamState.Stop); } return(reader); }
private static void RateLimitCheck(object?sender, QueryBeforeExecuteEventArgs args) { var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL); if (queryRateLimits != null) { if (queryRateLimits.Remaining > 0) { return; } using (var spinner = new Spinner("You have been rate limited by Twitter until " + $"{queryRateLimits.ResetDateTime.ToLongTimeString()}. Please wait or Ctrl+C to quit")) { Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds); spinner.Done(); } } }
private void CheckRateLimits(object sender, QueryBeforeExecuteEventArgs 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) { return; } if (queryRateLimits.Remaining > 0) { // We have enough resource to execute the query return; } // Wait for RateLimits to be available this.logger.LogInformation("Waiting for RateLimits until: {0}", queryRateLimits.ResetDateTime.ToLongTimeString()); Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds); }
private HttpClient GetHttpClient(ITwitterQuery twitterQuery) { if (twitterQuery == null) { SetStreamState(StreamState.Stop); return(null); } twitterQuery.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite); var queryBeforeExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery); _tweetinviEvents.RaiseBeforeQueryExecute(queryBeforeExecuteEventArgs); if (queryBeforeExecuteEventArgs.Cancel) { SetStreamState(StreamState.Stop); return(null); } return(_httpClientWebHelper.GetHttpClient(twitterQuery)); }
private bool TryPrepareRequest( IHttpRequestParameters requestParameters, RateLimitTrackerMode rateLimitTrackerMode, ITwitterCredentials credentials, out ITwitterQuery twitterQuery) { credentials = credentials ?? _credentialsAccessor.CurrentThreadCredentials; if (credentials == null) { throw new TwitterNullCredentialsException(); } twitterQuery = _twitterQueryFactory.Create(requestParameters.Query, requestParameters.HttpMethod, credentials); twitterQuery.HttpContent = requestParameters.HttpContent; twitterQuery.Timeout = requestParameters.Timeout ?? twitterQuery.Timeout; var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery); if (rateLimitTrackerMode == RateLimitTrackerMode.TrackOnly || rateLimitTrackerMode == RateLimitTrackerMode.TrackAndAwait) { // Use the RateLimitCacheManager instead of RateLimitHelper to get the queryRateLimits to ensure the cache is up to date! var credentialRateLimits = _rateLimitCacheManager.GetCredentialsRateLimits(twitterQuery.TwitterCredentials); IEndpointRateLimit queryRateLimit = null; // If we were not able to retrieve the credentials few ms before there is no reason why it would work now. if (credentialRateLimits != null) { queryRateLimit = _rateLimitCacheManager.GetQueryRateLimit(requestParameters.Query, twitterQuery.TwitterCredentials); } var timeToWait = _rateLimitAwaiter.GetTimeToWaitFromQueryRateLimit(queryRateLimit); twitterQuery.CredentialsRateLimits = credentialRateLimits; twitterQuery.QueryRateLimit = queryRateLimit; twitterQuery.DateWhenCredentialsWillHaveTheRequiredRateLimits = DateTime.Now.AddMilliseconds(timeToWait); _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs); if (beforeQueryExecuteEventArgs.Cancel) { twitterQuery = null; return(false); } if (rateLimitTrackerMode == RateLimitTrackerMode.TrackAndAwait) { _rateLimitAwaiter.Wait(timeToWait); } } else { _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs); if (beforeQueryExecuteEventArgs.Cancel) { twitterQuery = null; return(false); } } _tweetinviEvents.RaiseBeforeExecuteAfterRateLimitAwait(beforeQueryExecuteEventArgs); return(true); }
public void RaiseQueryBeforeExecuteAfterRateLimitAwait(object sender, QueryBeforeExecuteEventArgs args) { this.Raise(sender, QueryBeforeExecuteAfterRateLimitAwait, args); }
public void RaiseQueryBeforeExecute(object sender, QueryBeforeExecuteEventArgs args) { this.Raise(sender, QueryBeforeExecute, args); }
public void RaiseBeforeExecuteAfterRateLimitAwait(QueryBeforeExecuteEventArgs queryExecutedEventArgs) { this.Raise(QueryBeforeExecuteAfterRateLimitAwait, queryExecutedEventArgs); }
public void RaiseBeforeQueryExecute(QueryBeforeExecuteEventArgs queryAfterExecuteEventArgs) { this.Raise(QueryBeforeExecute, queryAfterExecuteEventArgs); }