public async Task StartStreamMatchingAnyConditionAsync() { _filterStreamTweetMatcher = _filterStreamTweetMatcherFactory.Create(_streamTrackManager, _locations, _followingUserIds); Func <ITwitterQuery> generateWebRequest = () => { var queryBuilder = GenerateORFilterQuery(); AddBaseParametersToQuery(queryBuilder); return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.POST, Credentials)); }; Action <string> tweetReceived = json => { RaiseJsonObjectReceived(json); var tweet = _tweetFactory.GenerateTweetFromJson(json, TweetMode); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } var matchingTracksEvenArgs = _filterStreamTweetMatcher.GetMatchingTweetEventArgsAndRaiseMatchingElements(tweet, json, MatchOn); var matchingTracks = matchingTracksEvenArgs.MatchingTracks; var matchingLocations = matchingTracksEvenArgs.MatchingLocations; var matchingFollowers = matchingTracksEvenArgs.MatchingFollowers; var isTweetMatching = matchingTracks.Length != 0 || matchingLocations.Length != 0 || matchingFollowers.Length != 0; var quotedTweetMatchingTracks = matchingTracksEvenArgs.QuotedTweetMatchingTracks; var quotedTweetMatchingLocations = matchingTracksEvenArgs.QuotedTweetMatchingLocations; var quotedTweetMatchingFollowers = matchingTracksEvenArgs.QuotedTweetMatchingFollowers; var isQuotedTweetMatching = quotedTweetMatchingTracks.Length != 0 || quotedTweetMatchingLocations.Length != 0 || quotedTweetMatchingFollowers.Length != 0; RaiseTweetReceived(matchingTracksEvenArgs); if (isTweetMatching || isQuotedTweetMatching) { RaiseMatchingTweetReceived(matchingTracksEvenArgs); } else { RaiseNonMatchingTweetReceived(new TweetEventArgs(tweet, json)); } }; await _streamResultGenerator.StartStreamAsync(tweetReceived, generateWebRequest); }
private ITokenRateLimits GetTokenRateLimitsFromTwitter(ITwitterCredentials credentials) { if (_isRetrievingData) { return(null); } var isApplicationOnlyCreds = string.IsNullOrEmpty(credentials.AccessToken) || string.IsNullOrEmpty(credentials.AccessTokenSecret); if (isApplicationOnlyCreds && string.IsNullOrEmpty(credentials.ApplicationOnlyBearerToken)) { return(null); } _isRetrievingData = true; var result = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () => { var twitterQuery = _twitterQueryFactory.Create(_helpQueryGenerator.GetCredentialsLimitsQuery(), HttpMethod.GET, credentials); try { string jsonResponse = _twitterRequester.ExecuteQuery(twitterQuery); return(_jsonObjectConverter.DeserializeObject <ITokenRateLimits>(jsonResponse)); } catch (TwitterException) { return(null); } }); _isRetrievingData = false; return(result); }
public async Task StartStreamAsync(string url) { Func <HttpWebRequest> generateWebRequest = delegate { var queryBuilder = new StringBuilder(url); AddBaseParametersToQuery(queryBuilder); var streamQuery = _twitterQueryFactory.Create(queryBuilder.ToString()); return(_twitterRequestGenerator.GetQueryWebRequest(streamQuery)); }; Action <string> generateTweetDelegate = json => { RaiseJsonObjectReceived(json); var tweet = _tweetFactory.GenerateTweetFromJson(json); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } var detectedTracksAndActions = _streamTrackManager.GetMatchingTracksAndActions(tweet.Text); var detectedTracks = detectedTracksAndActions.Select(x => x.Item1); if (detectedTracksAndActions.Any()) { this.Raise(MatchingTweetReceived, new MatchedTweetReceivedEventArgs(tweet, detectedTracks)); } }; await _streamResultGenerator.StartStreamAsync(generateTweetDelegate, generateWebRequest); }
public string ExecuteQueryWithTemporaryCredentials(string queryURL, HttpMethod httpMethod, ITemporaryCredentials temporaryCredentials, IEnumerable <IOAuthQueryParameter> queryParameters) { CleanupQueryURL(ref queryURL); var twitterQuery = _twitterQueryFactory.Create(queryURL, httpMethod, temporaryCredentials); twitterQuery.QueryParameters = queryParameters; _tweetinviEvents.RaiseBeforeQueryExecute(new QueryBeforeExecuteEventArgs(twitterQuery)); var result = _twitterRequester.ExecuteQuery(twitterQuery); _tweetinviEvents.RaiseAfterQueryExecuted(new QueryAfterExecuteEventArgs(twitterQuery, result)); return(result); }
private ICredentialsRateLimits GetTokenRateLimitsFromTwitter(ITwitterCredentials credentials) { var isApplicationOnlyCreds = string.IsNullOrEmpty(credentials.AccessToken) || string.IsNullOrEmpty(credentials.AccessTokenSecret); if (isApplicationOnlyCreds && string.IsNullOrEmpty(credentials.ApplicationOnlyBearerToken)) { return(null); } var result = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () => { var twitterQuery = _twitterQueryFactory.Create(_helpQueryGenerator.GetCredentialsLimitsQuery(), HttpMethod.GET, credentials); try { var webRequestResult = _webRequestExecutor.ExecuteQuery(twitterQuery); var json = webRequestResult.Text; return(_jsonObjectConverter.DeserializeObject <ICredentialsRateLimits>(json)); } catch (TwitterException) { return(null); } }); return(result); }
public async Task StartStream(string url) { Func <ITwitterQuery> generateTwitterQuery = delegate { var queryBuilder = new StringBuilder(url); AddBaseParametersToQuery(queryBuilder); return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.GET, Credentials)); }; Action <string> generateTweetDelegate = json => { this.Raise(JsonObjectReceived, new JsonObjectEventArgs(json)); var tweet = _tweetFactory.GenerateTweetFromJson(json); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } this.Raise(TweetReceived, new TweetReceivedEventArgs(tweet)); }; await _streamResultGenerator.StartStreamAsync(generateTweetDelegate, generateTwitterQuery).ConfigureAwait(false); }
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); }
public async Task StartStream(string url) { Func <HttpWebRequest> generateWebRequest = delegate { var queryBuilder = new StringBuilder(url); AddBaseParametersToQuery(queryBuilder); var streamQuery = _twitterQueryFactory.Create(queryBuilder.ToString()); return(_twitterRequestGenerator.GetQueryWebRequest(streamQuery)); }; Action <string> generateTweetDelegate = json => { this.Raise(JsonObjectReceived, new JsonObjectEventArgs(json)); var tweet = _tweetFactory.GenerateTweetFromJson(json); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } this.Raise(TweetReceived, new TweetReceivedEventArgs(tweet)); }; await _streamResultGenerator.StartStreamAsync(generateTweetDelegate, generateWebRequest); }
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); }
public async Task StartStreamMatchingAnyConditionAsync() { Func <HttpWebRequest> generateWebRequest = () => { var queryBuilder = GenerateORFilterQuery(); AddBaseParametersToQuery(queryBuilder); var streamQuery = _twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.POST); return(_twitterRequestGenerator.GetQueryWebRequest(streamQuery)); }; Action <string> tweetReceived = json => { RaiseJsonObjectReceived(json); var tweet = _tweetFactory.GenerateTweetFromJson(json); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } var matchingTrackAndActions = _streamTrackManager.GetMatchingTracksAndActions(tweet.Text); var matchingTracks = matchingTrackAndActions.Select(x => x.Item1); var machingLocationAndActions = GetMatchedLocations(tweet); var matchingLocations = machingLocationAndActions.Select(x => x.Key); CallMultipleActions(tweet, matchingTrackAndActions.Select(x => x.Item2)); CallMultipleActions(tweet, machingLocationAndActions.Select(x => x.Value)); CallFollowerAction(tweet); RaiseMatchingTweetReceived(new MatchedTweetReceivedEventArgs(tweet, matchingTracks)); this.Raise(MatchingTweetAndLocationReceived, new MatchedTweetAndLocationReceivedEventArgs(tweet, matchingTracks, matchingLocations)); }; await _streamResultGenerator.StartStreamAsync(tweetReceived, generateWebRequest); }
public async Task StartStreamAsync() { _loggedUser = await _taskFactory.ExecuteTaskAsync(() => _userFactory.GetLoggedUser()); if (_loggedUser == null) { StopStream(new WebException("Could not receive information related with currently logged user.")); return; } Func <HttpWebRequest> generateWebRequest = delegate { var queryBuilder = new StringBuilder(Resources.Stream_UserStream); AddBaseParametersToQuery(queryBuilder); var streamQuery = _twitterQueryFactory.Create(queryBuilder.ToString()); return(_twitterRequestGenerator.GetQueryWebRequest(streamQuery)); }; Action <string> eventReceived = json => { RaiseJsonObjectReceived(json); // We analyze the different types of message from the stream if (TryGetEvent(json)) { return; } if (TryGetTweet(json)) { return; } if (TryGetMessage(json)) { return; } if (TryGetWarning(json)) { return; } if (TryGetFriends(json)) { return; } TryInvokeGlobalStreamMessages(json); }; await _streamResultGenerator.StartStreamAsync(eventReceived, generateWebRequest); }
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); }
public async Task StartStreamAsync() { _authenticatedUser = await _taskFactory.ExecuteTaskAsync(() => _userFactory.GetAuthenticatedUser(Credentials)); if (_authenticatedUser == null) { StopStream(new UserStreamFailedToInitializeException("Could not receive information related with currently authenticated user.")); return; } Func <ITwitterQuery> generateTwitterQuery = delegate { var queryBuilder = new StringBuilder(Resources.Stream_UserStream); AddBaseParametersToQuery(queryBuilder); return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.GET, Credentials)); }; Action <string> eventReceived = json => { RaiseJsonObjectReceived(json); // We analyze the different types of message from the stream if (TryGetEvent(json)) { return; } if (TryGetTweet(json)) { return; } if (TryGetMessage(json)) { return; } if (TryGetWarning(json)) { return; } if (TryGetFriends(json)) { return; } TryInvokeGlobalStreamMessages(json); }; await _streamResultGenerator.StartStreamAsync(eventReceived, generateTwitterQuery); }
public async Task StartStreamAsync(string url) { Func <ITwitterQuery> generateTwitterQuery = delegate { var queryBuilder = new StringBuilder(url); AddBaseParametersToQuery(queryBuilder); return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.GET, Credentials)); }; Action <string> generateTweetDelegate = json => { RaiseJsonObjectReceived(json); var tweet = _tweetFactory.GenerateTweetFromJson(json); if (tweet == null) { TryInvokeGlobalStreamMessages(json); return; } var detectedTracksAndActions = _streamTrackManager.GetMatchingTracksAndActions(tweet.Text); var detectedTracks = detectedTracksAndActions.Select(x => x.Item1); var eventArgs = new MatchedTweetReceivedEventArgs(tweet) { MatchingTracks = detectedTracks.ToArray(), }; if (detectedTracksAndActions.Any()) { eventArgs.MatchOn = MatchOn.TweetText; RaiseTweetReceived(eventArgs); RaiseMatchingTweetReceived(eventArgs); } else { RaiseTweetReceived(eventArgs); RaiseNonMatchingTweetReceived(new TweetEventArgs(tweet)); } }; await _streamResultGenerator.StartStreamAsync(generateTweetDelegate, generateTwitterQuery); }
private ITokenRateLimits GetTokenRateLimitsFromTwitter(IOAuthCredentials credentials) { if (_isRetrievingData) { return(null); } _isRetrievingData = true; var result = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () => { var twitterQuery = _twitterQueryFactory.Create(_helpQueryGenerator.GetCredentialsLimitsQuery(), HttpMethod.GET, credentials); string jsonResponse = _twitterRequester.ExecuteQuery(twitterQuery); return(_jsonObjectConverter.DeserializeObject <ITokenRateLimits>(jsonResponse)); }); _isRetrievingData = false; return(result); }
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); }