コード例 #1
0
        private async void RunSportNews()
        {
            logger.LogInformation("RunSportNews starts at " + Environment.TickCount);
            DataContext dataContext = GetDataContext();

            logger.LogInformation("dataContext == null ? " + (dataContext == null));
            NewsService newsService = new NewsService(dataContext, configuration, logger);

            if (newsService != null)
            {
                NewsAPIModel sport = await newsService.GetHTTPNews(CountriesEnum.Australia.ToString(), Category.Sport.ToString(), DateTime.Now.AddDays(-1), DateTime.Now);

                if (sport != null)
                {
                    for (int i = 0; i < sport.articles.Count() - 1; i++)
                    {
                        News news = new News();
                        news.NewsTitle    = sport.articles[i].title;
                        news.NewsURL      = sport.articles[i].url;
                        news.NewsSource   = sport.articles[i].source.name;
                        news.NewsCategory = Category.Sport.ToString();
                        news.NewsCountry  = CountriesEnum.Australia.ToString();
                        DateTime tempDate;
                        if (DateTime.TryParse(sport.articles[i].publishedAt, out tempDate))
                        {
                            news.DatePublished = tempDate;
                        }
                        bool result = await newsService.Create(news);
                    }
                }
            }
            logger.LogInformation("RunSportNews stops at " + Environment.TickCount);
        }
コード例 #2
0
        public async Task <NewsAPIModel> GetHTTPNews(string countryCode, string category, DateTime from, DateTime to)
        {
            StringBuilder log = new StringBuilder();

            this.logger.LogWarning("GetHTTPNews - Starts");
            this.logger.LogWarning("GetHTTPNews - countryCode = " + countryCode);
            this.logger.LogWarning("GetHTTPNews - category = " + category);
            this.logger.LogWarning("GetHTTPNews - from = " + string.Format("{0:yyyy/MM/dd hh:mm tt}", from));
            this.logger.LogWarning("GetHTTPNews - to = " + string.Format("{0:yyyy/MM/dd hh:mm tt}", to));
            NewsAPIModel news = null;

            try
            {
                string url = this.configuration["NewsAPI:Url"];
                this.logger.LogWarning("url => " + url);
                string apiKey = this.configuration["NewsAPI:ApiKey"];
                this.logger.LogWarning("apiKey => " + apiKey);
                StringBuilder tempURL = new StringBuilder(url);
                tempURL = tempURL.Replace("{{countrycode}}", countryCode).Replace("{{category}}", category).Replace("{{from}}", string.Format("{0:yyyy-MM-dd 00:00:00 AM}", from)).Replace("{{to}}", string.Format("{0:yyyy-MM-dd 59:59:59 PM}", to)).Replace("{{apikey}}", apiKey);
                this.logger.LogWarning("tempURL => " + tempURL.ToString());
                var serializer = new DataContractJsonSerializer(typeof(NewsAPIModel));
                this.logger.LogWarning("var serializer");
                Stream stream = await this.client.GetStreamAsync(tempURL.ToString());

                this.logger.LogWarning("Stream stream");
                news = (serializer.ReadObject(stream) as NewsAPIModel);
                this.logger.LogWarning("serializer.ReadObject(stream)");
            }
            catch (Exception ex) {
                this.logger.LogWarning("GetHTTPNews - Errors - " + ex.GetBaseException().ToString());
            }
            this.logger.LogWarning("GetHTTPNews - articles no = " + news.articles.Count());
            this.logger.LogWarning("GetHTTPNews - Stops");
            return(news);
        }