Пример #1
0
        public QueueHub(IHttpClientProvider httpClientProvider, IIncommingTweetQueue incommingTweetQueue, IConfiguration configuration,
                        IRuntimeStatsService runtimeStatsService, IStreamService streamService, IStreamReaderHandler streamReaderHandler)
        {
            _httpClientProvider  = httpClientProvider;
            _incommingTweetQueue = incommingTweetQueue;
            _runtimeStatsService = runtimeStatsService;
            _streamService       = streamService;
            _streamReaderHandler = streamReaderHandler;

            _twitterConfiguration = new TwitterConfiguration()
            {
                BaseUrl         = configuration.GetValue <string>(Constants.TwitterBaseUrlName).ToString(),
                OAuthToken      = configuration.GetValue <string>(Constants.TwitterOAuthTokenName).ToString(),
                SampleStreamUrl = configuration.GetValue <string>(Constants.TwitterSampleStreamUrlName).ToString()
            };
        }
Пример #2
0
        public async Task <string> ReciveTweets(IHttpClientProvider client, TwitterConfiguration twitterConfiguration
                                                , IIncommingTweetQueue incommingTweetQueue, IStreamReaderHandler streamReaderHandler, CancellationToken cancellationToken)
        {
            int      tweetCount = 0;
            var      tasks      = new List <Task>();
            DateTime startRun   = DateTime.UtcNow;

            try
            {
                client.BaseAddress = new Uri(twitterConfiguration.BaseUrl);

                if (client != null)
                {
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + twitterConfiguration.OAuthToken);
                    var stream = await client.GetStreamAsync(twitterConfiguration.SampleStreamUrl);

                    try
                    {
                        using (var reader = streamReaderHandler.GetStreamReader(stream))
                        {
                            do
                            {
                                var rawTweetData = await reader.ReadLineAsync();

                                RawTweetModel messageWithData = new RawTweetModel()
                                {
                                    TweetCount = tweetCount, RawTweetData = rawTweetData
                                };
                                // send new tweet onto queue for processing
                                tasks.Add(Task.Run(() => incommingTweetQueue.SendMessageOntoQueue(JsonConvert.SerializeObject(messageWithData))));
                                tweetCount++;
                                cancellationToken.ThrowIfCancellationRequested();
                            } while (true);
                        }
                        //
                    }
                    catch (OperationCanceledException)
                    {
                        // canceled
                    }
                    catch (Exception exc)
                    {
                        throw exc;
                    }
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
            Task.WaitAll(tasks.ToArray());

            return("Done Collecting Tweets... ");
        }