Exemplo n.º 1
0
        public static async Task RunAsync(CancellationToken cancellationToken)
        {
            Console.OutputEncoding = Encoding.UTF8;

            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            Func <RssDbContext> contextFactory =
                () => new RssDbContext(config.GetConnectionString("DefaultConnection"));

            _recordService = new RssRecordService(contextFactory);


            foreach (var rssSource in _rssSources)
            {
                Console.WriteLine(rssSource.SourceName);
                var itemsSaved  = 0;
                var itemsReaded = 0;

                var text = await Helpers.DownloadAsync(CodeHollow.FeedReader.FeedReader.GetAbsoluteUrl(rssSource.SourceUrl));

                if (text.StartsWith("\n"))
                {
                    text = text.Remove(0, 1);
                }

                var feed = FeedReader.ReadFromString(text);

                foreach (var feedItem in feed.Items)
                {
                    try
                    {
                        await _recordService.AddAsync(new RssRecord()
                        {
                            PublishDate =
                                !string.IsNullOrEmpty(feedItem.PublishingDateString)
                                        ? DateTimeOffset.Parse(feedItem.PublishingDateString)
                                        : throw new InvalidOperationException("No publish date"),

                            SourceUrl   = rssSource.SourceUrl,
                            NameSource  = rssSource.SourceName,
                            Description = feedItem.Description,
                            NewsUrl     = feedItem.Id,

                            Title = !string.IsNullOrEmpty(feedItem.Title)
                                    ? feedItem.Title
                                    : throw new InvalidOperationException("No publish date")
                        },
                                                      cancellationToken);

                        itemsSaved++;
                    }
Exemplo n.º 2
0
 public HomeController(RssRecordService recordService)
 {
     _recordService = recordService;
 }