コード例 #1
0
        private async Task CreateCaseNumbersPublishedEvent(string url, HttpClient client)
        {
            var request  = new HttpRequestMessage(HttpMethod.Get, url);
            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var model = JsonConvert.DeserializeObject <ArcGisResponse>(content);
                if (model.features.Any())
                {
                    var caseNumbers  = model.features.FirstOrDefault().attributes.ConfirmedCovidCases_sum;
                    var eventMessage = new CaseNumbersPublishedEvent(caseNumbers);
                    await _publishEndPoint.Publish(eventMessage);

                    _logger.LogInformation($"CaseNumbersPublihsedEvent for {eventMessage.Date.ToString("dd/MM/yyyy")} successfully queued");
                }
                else
                {
                    _logger.LogInformation($"Unexpected ArcGis response received");
                }
            }
            else
            {
                _logger.LogInformation($"Unsuccessful ArcGis response received");
            }
        }
コード例 #2
0
        private async Task <bool> CheckCaseNumbers(string url, HttpClient client)
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _configuration["TwitterApiSettings:BearerToken"]);
            var request  = new HttpRequestMessage(HttpMethod.Get, url);
            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var model           = JsonConvert.DeserializeObject <TwitterResponse>(content);
                var dateToSearchFor = DateTime.Now.AddDays(-1);

                var dateText1 = dateToSearchFor.ToString("dd MMMM");
                var dateText2 = dateToSearchFor.ToString("dd MMM");
                var dateText3 = dateToSearchFor.ToString("d MMMM");
                var dateText4 = dateToSearchFor.ToString("d MMM");

                var latestCaseTweet = model.data.Where(x => (x.text.Contains(dateText1) ||
                                                             x.text.Contains(dateText2) ||
                                                             x.text.Contains(dateText3) ||
                                                             x.text.Contains(dateText4)) &&
                                                       x.text.Contains("confirmed cases")).OrderByDescending(y => y.id).FirstOrDefault();

                if (latestCaseTweet != null)
                {
                    var caseNumbers = ParseCaseNumbers(latestCaseTweet);

                    if (caseNumbers != -1)
                    {
                        var eventMessage = new CaseNumbersPublishedEvent(caseNumbers);
                        await _publishEndPoint.Publish(eventMessage);

                        _logger.LogInformation($"CaseNumbersPublihsedEvent for {eventMessage.Date.ToString("dd/MM/yyyy")} successfully queued");
                        _hostApplicationLifeTime.StopApplication();
                        return(true);
                    }
                    _logger.LogInformation($"Latest tweet identified but case numbers could not be parsed from it ");
                    //return true here to stop the worker service, no point in keeping it running if it's found a tweet for today but
                    //can't parse the case numbers from it
                    _hostApplicationLifeTime.StopApplication();
                    return(true);
                }
                return(false);
            }
            else
            {
                _logger.LogInformation($"Unsuccessful Twitter API response received");
                return(false);
            }
        }