James Tombleson
17e97b4e09
* inital table clients are almost ready * adding new repo interfaces * adding MS DI to the tables * adding tables and MS DI to jobs * updated how the config builder works and pass Iconfig to jobs * Updated how articles interface returns a value * updated constructors to support DI and removed static call * added source consts and model notes * updated sources.type to contain the value to link what job collects it * added RssWatcherJob to hangfire * DI and hangfire jobs wont work. Defering to options classes * Services updated to have options exposed over DI * Tests have been updated.. more to come
158 lines
4.3 KiB
C#
158 lines
4.3 KiB
C#
using System.Runtime.InteropServices;
|
|
using System.ServiceModel.Syndication;
|
|
using System.Xml;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newsbot.Collector.Database.Repositories;
|
|
using Newsbot.Collector.Domain.Consts;
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
namespace Newsbot.Collector.Services.Jobs;
|
|
|
|
public class RssWatcherJobOptions
|
|
{
|
|
public string ConnectionString { get; set; } = "";
|
|
}
|
|
|
|
// 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;
|
|
|
|
public RssWatcherJob()
|
|
{
|
|
_articles = new ArticlesTable("");
|
|
_queue = new DiscordQueueTable("");
|
|
_source = new SourcesTable("");
|
|
}
|
|
|
|
public void InitAndExecute(RssWatcherJobOptions options)
|
|
{
|
|
Console.WriteLine("Job was triggered");
|
|
Console.WriteLine("Setting up the job");
|
|
Init(options.ConnectionString);
|
|
|
|
var articles = new List<ArticlesModel>();
|
|
|
|
Console.WriteLine("Requesting sources");
|
|
var sources = _source.ListByType(SourceTypes.Rss);
|
|
Console.WriteLine($"Got {sources.Count()} back");
|
|
foreach (var source in sources)
|
|
{
|
|
Console.WriteLine("Starting to request feed to be processed");
|
|
var results = Collect(source.Url);
|
|
|
|
articles.AddRange(results);
|
|
}
|
|
|
|
UpdateDatabase(articles);
|
|
}
|
|
|
|
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)
|
|
{
|
|
var results = Collect(source.Url);
|
|
|
|
articles.AddRange(results);
|
|
}
|
|
|
|
UpdateDatabase(articles);
|
|
}
|
|
|
|
public void Init(string connectionString)
|
|
{
|
|
_articles = new ArticlesTable(connectionString);
|
|
_queue = new DiscordQueueTable(connectionString);
|
|
_source = new SourcesTable(connectionString);
|
|
}
|
|
|
|
public List<ArticlesModel> Collect(string url, int sleep = 3000)
|
|
{
|
|
var CollectedPosts = new List<ArticlesModel>();
|
|
|
|
using var reader = XmlReader.Create(url);
|
|
var feed = SyndicationFeed.Load(reader);
|
|
|
|
foreach (var post in feed.Items.ToList())
|
|
{
|
|
var articleUrl = post.Links[0].Uri.AbsoluteUri;
|
|
|
|
// Check if we have seen the url before
|
|
// If we have, skip and save the site bandwidth
|
|
if (IsThisUrlKnown(articleUrl) == true)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var meta = new HtmlPageReader(articleUrl);
|
|
|
|
var article = new ArticlesModel
|
|
{
|
|
Title = post.Title.Text,
|
|
Tags = FetchTags(post),
|
|
URL = articleUrl,
|
|
PubDate = post.PublishDate.DateTime,
|
|
Thumbnail = meta.Data.Header.Meta.Image,
|
|
Description = meta.Data.Header.Meta.Description,
|
|
};
|
|
|
|
CollectedPosts.Add(article);
|
|
|
|
// try to not be too greedy
|
|
Thread.Sleep(sleep);
|
|
}
|
|
return CollectedPosts;
|
|
}
|
|
|
|
public void UpdateDatabase(List<ArticlesModel> items)
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
if (IsThisUrlKnown(item.URL) == false)
|
|
{
|
|
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;
|
|
}
|
|
|
|
private string FetchTags(SyndicationItem post)
|
|
{
|
|
string result = "";
|
|
foreach (var tag in post.Categories)
|
|
{
|
|
result += $"{tag.Name},";
|
|
}
|
|
return result;
|
|
}
|
|
} |