コード例 #1
0
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();


            RssNewsProvider rssNewsProvider = new RssNewsProvider();
            RssFeed         feed            = rssNewsProvider.GetData();



            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalSeconds);
        }
コード例 #2
0
        private void Update_BBC_CNN_REUTERS_NewsFeed()
        {
            NewsDbContext _newsDbContext = new NewsDbContext(new DbContextOptionsBuilder <NewsDbContext>()
                                                             .UseSqlServer(_configuration.GetConnectionString("NewsFeedConnection"))
                                                             .Options);

            RssNewsProvider rssNewsProvider = new RssNewsProvider();
            RssFeed         feed            = rssNewsProvider.GetData();

            try
            {
                var _bbcProvider = _newsDbContext.NewsFeedProviders.Where(p => p.Name == "BBC")?.FirstOrDefault();
                if (_bbcProvider == null)
                {
                    _bbcProvider = _newsDbContext.NewsFeedProviders.Add(new NewsFeedProvider()
                    {
                        Name = "BBC",
                        Logo = GetLogo(RssSource.BBC)
                    }).Entity;
                }

                var _cnnProvider = _newsDbContext.NewsFeedProviders.Where(p => p.Name == "CNN")?.FirstOrDefault();
                if (_cnnProvider == null)
                {
                    _cnnProvider = _newsDbContext.NewsFeedProviders.Add(new NewsFeedProvider()
                    {
                        Name = "CNN",
                        Logo = GetLogo(RssSource.CNN)
                    }).Entity;
                }

                var _reutersProvider = _newsDbContext.NewsFeedProviders.Where(p => p.Name == "REUTERS")?.FirstOrDefault();
                if (_reutersProvider == null)
                {
                    _reutersProvider = _newsDbContext.NewsFeedProviders.Add(new NewsFeedProvider()
                    {
                        Name = "REUTERS",
                        Logo = GetLogo(RssSource.Reuters)
                    }).Entity;
                }

                if (feed != null)
                {
                    //Add for BBC
                    if (feed.BbcFeed != null && feed.BbcFeed.Channel != null & feed.BbcFeed.Channel.Item != null)
                    {
                        foreach (var bbcNews in feed.BbcFeed.Channel.Item)
                        {
                            if (ContainsCovid19RelatedData(bbcNews.Title) || ContainsCovid19RelatedData(bbcNews.Description))
                            {
                                try
                                {
                                    string t = FormatNewsFeed(bbcNews.Title);
                                    string d = FormatNewsFeed(bbcNews.Description);
                                    //bbcNews.Title = FormatNewsFeed(bbcNews.Title);
                                    if (_newsDbContext.NewsData.Count() == 0 || _newsDbContext.NewsData.Where(n => n.Title == t)?.FirstOrDefault() == null)
                                    {
                                        var bbcNewsData = new NewsData()
                                        {
                                            Description        = d,
                                            Title              = t,
                                            Link               = bbcNews.Link,
                                            PublishedDate      = DateTime.Parse(bbcNews.PubDate),
                                            Source             = "BBC",
                                            NewsFeedProvider   = _bbcProvider,
                                            NewsFeedProviderId = _bbcProvider.Id
                                        };
                                        _newsDbContext.NewsData.Add(bbcNewsData);
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                    //Add for CNN
                    if (feed.CnnFeed != null && feed.CnnFeed.Channel != null & feed.CnnFeed.Channel.Item != null)
                    {
                        foreach (var cnnNews in feed.CnnFeed.Channel.Item)
                        {
                            if (ContainsCovid19RelatedData(cnnNews.Title) || ContainsCovid19RelatedData(cnnNews.Description))
                            {
                                try
                                {
                                    string t = FormatNewsFeed(cnnNews.Title);
                                    string d = FormatNewsFeed(cnnNews.Description);
                                    //cnnNews.Title = FormatNewsFeed(cnnNews.Title);
                                    if (_newsDbContext.NewsData.Count() == 0 || _newsDbContext.NewsData.Where(n => n.Title == t)?.FirstOrDefault() == null)
                                    {
                                        var cnnNewsData = new NewsData()
                                        {
                                            Description        = d,
                                            Title              = t,
                                            Link               = cnnNews.Link,
                                            PublishedDate      = DateTime.Parse(cnnNews.PubDate),
                                            Source             = "CNN",
                                            NewsFeedProvider   = _cnnProvider,
                                            NewsFeedProviderId = _cnnProvider.Id
                                        };
                                        _newsDbContext.NewsData.Add(cnnNewsData);
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                    //Add for REUTERS
                    if (feed.ReutersFeed != null && feed.ReutersFeed.Channel != null & feed.ReutersFeed.Channel.Item != null)
                    {
                        foreach (var reutersNews in feed.ReutersFeed.Channel.Item)
                        {
                            if (ContainsCovid19RelatedData(reutersNews.Title) || ContainsCovid19RelatedData(reutersNews.Description))
                            {
                                try
                                {
                                    string t = FormatNewsFeed(reutersNews.Title);
                                    string d = FormatNewsFeed(reutersNews.Description);
                                    //reutersNews.Title = FormatNewsFeed(reutersNews.Title);
                                    if (_newsDbContext.NewsData.Count() == 0 || _newsDbContext.NewsData.Where(n => n.Title == t)?.FirstOrDefault() == null)
                                    {
                                        var reutersNewsData = new NewsData()
                                        {
                                            Description        = d,
                                            Title              = t,
                                            Link               = reutersNews.Link,
                                            PublishedDate      = DateTime.Parse(reutersNews.PubDate),
                                            Source             = "REUTERS",
                                            NewsFeedProvider   = _reutersProvider,
                                            NewsFeedProviderId = _reutersProvider.Id
                                        };
                                        _newsDbContext.NewsData.Add(reutersNewsData);
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogWarning("Unable To Extract News Feed", DateTimeOffset.Now);
            }

            try
            {
                //newsDB
                _newsDbContext.SaveChanges();
                _newsDbContext.Dispose();
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, "Unable to save data", DateTimeOffset.Now);
            }
        }