2023-02-16 22:19:05 -08:00
|
|
|
using System.ServiceModel.Syndication;
|
|
|
|
using System.Xml;
|
2023-02-19 21:39:03 -08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Newsbot.Collector.Database.Repositories;
|
|
|
|
using Newsbot.Collector.Domain.Consts;
|
2023-02-16 22:19:05 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Services.HtmlParser;
|
|
|
|
using Serilog;
|
2023-02-16 22:19:05 -08:00
|
|
|
|
|
|
|
namespace Newsbot.Collector.Services.Jobs;
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public class RssWatcherJobOptions
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-19 21:39:03 -08:00
|
|
|
public string ConnectionString { get; set; } = "";
|
|
|
|
}
|
2023-02-16 22:19:05 -08:00
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
// This class was made to work with Hangfire and it does not support constructors.
|
|
|
|
public class RssWatcherJob : IHangfireJob
|
|
|
|
{
|
|
|
|
|
|
|
|
private IArticlesRepository _articles;
|
|
|
|
private IDiscordQueueRepository _queue;
|
|
|
|
private ISourcesRepository _source;
|
2023-02-16 22:19:05 -08:00
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public RssWatcherJob()
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-19 21:39:03 -08:00
|
|
|
_articles = new ArticlesTable("");
|
|
|
|
_queue = new DiscordQueueTable("");
|
|
|
|
_source = new SourcesTable("");
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public void InitAndExecute(RssWatcherJobOptions options)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information("RssWatcherJob - Job was triggered");
|
|
|
|
Log.Information("RssWatcherJob - Setting up the job");
|
2023-02-19 21:39:03 -08:00
|
|
|
Init(options.ConnectionString);
|
|
|
|
|
|
|
|
var articles = new List<ArticlesModel>();
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information("RssWatcherJob - Requesting sources");
|
2023-02-19 21:39:03 -08:00
|
|
|
var sources = _source.ListByType(SourceTypes.Rss);
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information($"RssWatcherJob - Got {sources.Count()} back");
|
2023-02-19 21:39:03 -08:00
|
|
|
foreach (var source in sources)
|
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information($"RssWatcherJob - Starting to proces '{source.Name}'");
|
|
|
|
Log.Information("RssWatcherJob - Starting to request feed to be processed");
|
|
|
|
var results = Collect(source.Url, source.ID);
|
2023-02-19 21:39:03 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information($"RssWatcherJob - Collected {results.Count()} posts");
|
2023-02-19 21:39:03 -08:00
|
|
|
articles.AddRange(results);
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
Log.Information("RssWatcherJob - Sending posts over to the database");
|
2023-02-19 21:39:03 -08:00
|
|
|
UpdateDatabase(articles);
|
2023-02-26 09:40:04 -08:00
|
|
|
|
|
|
|
Log.Information("RssWatcherJob - Done!");
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
2023-02-16 22:19:05 -08:00
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public void InitAndExecute(IConfiguration config)
|
|
|
|
{
|
|
|
|
// reach out to the db and find all the rss feeds
|
|
|
|
var connectionString = config.GetConnectionString("database");
|
|
|
|
if (connectionString is null)
|
|
|
|
{
|
|
|
|
connectionString = "";
|
|
|
|
}
|
|
|
|
Init(connectionString);
|
|
|
|
|
|
|
|
var articles = new List<ArticlesModel>();
|
|
|
|
|
|
|
|
var sources = _source.ListByType(SourceTypes.Rss);
|
|
|
|
foreach (var source in sources)
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-26 09:40:04 -08:00
|
|
|
var results = Collect(source.Url, source.ID);
|
2023-02-19 21:39:03 -08:00
|
|
|
|
|
|
|
articles.AddRange(results);
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
UpdateDatabase(articles);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Init(string connectionString)
|
|
|
|
{
|
|
|
|
_articles = new ArticlesTable(connectionString);
|
|
|
|
_queue = new DiscordQueueTable(connectionString);
|
|
|
|
_source = new SourcesTable(connectionString);
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
public List<ArticlesModel> Collect(string url, Guid SourceID, int sleep = 3000)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
var CollectedPosts = new List<ArticlesModel>();
|
|
|
|
|
|
|
|
using var reader = XmlReader.Create(url);
|
2023-02-16 22:19:05 -08:00
|
|
|
var feed = SyndicationFeed.Load(reader);
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
foreach (var post in feed.Items.ToList())
|
2023-02-16 22:19:05 -08:00
|
|
|
{
|
2023-02-19 21:39:03 -08:00
|
|
|
var articleUrl = post.Links[0].Uri.AbsoluteUri;
|
2023-02-16 22:19:05 -08:00
|
|
|
|
|
|
|
// Check if we have seen the url before
|
|
|
|
// If we have, skip and save the site bandwidth
|
2023-02-19 21:39:03 -08:00
|
|
|
if (IsThisUrlKnown(articleUrl) == true)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-16 22:19:05 -08:00
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
var meta = new HtmlPageReader(articleUrl);
|
2023-02-26 09:40:04 -08:00
|
|
|
meta.Parse();
|
2023-02-16 22:19:05 -08:00
|
|
|
|
|
|
|
var article = new ArticlesModel
|
|
|
|
{
|
|
|
|
Title = post.Title.Text,
|
|
|
|
Tags = FetchTags(post),
|
2023-02-19 21:39:03 -08:00
|
|
|
URL = articleUrl,
|
2023-02-16 22:19:05 -08:00
|
|
|
PubDate = post.PublishDate.DateTime,
|
2023-02-26 09:40:04 -08:00
|
|
|
Thumbnail = meta.Data.Header.Image,
|
|
|
|
Description = meta.Data.Header.Description,
|
|
|
|
SourceID = SourceID
|
2023-02-16 22:19:05 -08:00
|
|
|
};
|
2023-02-19 21:39:03 -08:00
|
|
|
|
2023-02-16 22:19:05 -08:00
|
|
|
CollectedPosts.Add(article);
|
|
|
|
|
|
|
|
// try to not be too greedy
|
2023-02-19 21:39:03 -08:00
|
|
|
Thread.Sleep(sleep);
|
2023-02-16 22:19:05 -08:00
|
|
|
}
|
|
|
|
return CollectedPosts;
|
|
|
|
}
|
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
public void UpdateDatabase(List<ArticlesModel> items)
|
|
|
|
{
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2023-03-05 20:12:59 -08:00
|
|
|
if (item.URL is null)
|
|
|
|
{
|
|
|
|
Log.Warning($"RSS Watcher collected a blank url and was skipped.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
if (IsThisUrlKnown(item.URL) == true)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var p = _articles.New(item);
|
|
|
|
_queue.New(new DiscordQueueModel
|
|
|
|
{
|
|
|
|
ArticleID = p.ID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsThisUrlKnown(string url)
|
|
|
|
{
|
|
|
|
var isKnown = _articles.GetByUrl(url);
|
|
|
|
if (isKnown.URL == url)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:19:05 -08:00
|
|
|
private string FetchTags(SyndicationItem post)
|
|
|
|
{
|
|
|
|
string result = "";
|
|
|
|
foreach (var tag in post.Categories)
|
|
|
|
{
|
|
|
|
result += $"{tag.Name},";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|