From 6c4282b6deca688760518757d7c5b35899cd802b Mon Sep 17 00:00:00 2001 From: Gabisonfire Date: Tue, 27 Feb 2024 10:08:39 -0500 Subject: [PATCH] Adds Nyaa Crawler --- src/producer/Configuration/scrapers.json | 5 +++ src/producer/Crawlers/Sites/NyaaCrawler.cs | 32 +++++++++++++++++++ .../Extensions/ServiceCollectionExtensions.cs | 3 ++ src/producer/Jobs/SyncNyaaJob.cs | 12 +++++++ 4 files changed, 52 insertions(+) create mode 100644 src/producer/Crawlers/Sites/NyaaCrawler.cs create mode 100644 src/producer/Jobs/SyncNyaaJob.cs diff --git a/src/producer/Configuration/scrapers.json b/src/producer/Configuration/scrapers.json index 5cd9b5d..a1ca76b 100644 --- a/src/producer/Configuration/scrapers.json +++ b/src/producer/Configuration/scrapers.json @@ -6,6 +6,11 @@ "IntervalSeconds": 60, "Enabled": true }, + { + "Name": "SyncNyaaJob", + "IntervalSeconds": 60, + "Enabled": true + }, { "Name": "SyncTpbJob", "IntervalSeconds": 60, diff --git a/src/producer/Crawlers/Sites/NyaaCrawler.cs b/src/producer/Crawlers/Sites/NyaaCrawler.cs new file mode 100644 index 0000000..cc03ecf --- /dev/null +++ b/src/producer/Crawlers/Sites/NyaaCrawler.cs @@ -0,0 +1,32 @@ +namespace Producer.Crawlers.Sites; + +public class NyaaCrawler(IHttpClientFactory httpClientFactory, ILogger logger, IDataStorage storage) : BaseXmlCrawler(httpClientFactory, logger, storage) +{ + protected override string Url => "https://nyaa.si/?page=rss&c=1_2&f=0"; + protected override string Source => "Nyaa"; + + private static readonly XNamespace XmlNamespace = "https://nyaa.si/xmlns/nyaa"; + + protected override IReadOnlyDictionary Mappings => + new Dictionary + { + [nameof(Torrent.Name)] = "title", + [nameof(Torrent.Size)] = "size", + [nameof(Torrent.Seeders)] = "seeders", + [nameof(Torrent.Leechers)] = "leechers", + [nameof(Torrent.InfoHash)] = "infoHash", + [nameof(Torrent.Category)] = "category", + }; + + protected override Torrent ParseTorrent(XElement itemNode) => + new() + { + Source = Source, + Name = itemNode.Element(Mappings[nameof(Torrent.Name)])?.Value, + Size = itemNode.Element(XmlNamespace + Mappings[nameof(Torrent.Size)])?.Value, + Seeders = int.Parse(itemNode.Element(XmlNamespace + Mappings[nameof(Torrent.Seeders)])?.Value ?? "0"), + Leechers = int.Parse(itemNode.Element(XmlNamespace + Mappings[nameof(Torrent.Leechers)])?.Value ?? "0"), + InfoHash = itemNode.Element(XmlNamespace + Mappings[nameof(Torrent.InfoHash)])?.Value, + Category = itemNode.Element(Mappings[nameof(Torrent.Category)])?.Value.ToLowerInvariant(), + }; +} \ No newline at end of file diff --git a/src/producer/Extensions/ServiceCollectionExtensions.cs b/src/producer/Extensions/ServiceCollectionExtensions.cs index 3461e77..2774e62 100644 --- a/src/producer/Extensions/ServiceCollectionExtensions.cs +++ b/src/producer/Extensions/ServiceCollectionExtensions.cs @@ -10,6 +10,7 @@ public static class ServiceCollectionExtensions services .AddKeyedTransient(nameof(EzTvCrawler)) + .AddKeyedTransient(nameof(NyaaCrawler)) .AddKeyedTransient(nameof(YtsCrawler)) .AddKeyedTransient(nameof(TpbCrawler)) .AddKeyedTransient(nameof(TgxCrawler)) @@ -60,6 +61,7 @@ public static class ServiceCollectionExtensions services .AddTransient() + .AddTransient() .AddTransient() .AddTransient() .AddTransient() @@ -75,6 +77,7 @@ public static class ServiceCollectionExtensions quartz => { AddJobWithTrigger(quartz, SyncEzTvJob.Key, SyncEzTvJob.Trigger, scrapeConfiguration); + AddJobWithTrigger(quartz, SyncNyaaJob.Key, SyncNyaaJob.Trigger, scrapeConfiguration); AddJobWithTrigger(quartz, SyncTpbJob.Key, SyncTpbJob.Trigger, scrapeConfiguration); AddJobWithTrigger(quartz, SyncYtsJob.Key, SyncYtsJob.Trigger, scrapeConfiguration); AddJobWithTrigger(quartz, SyncTgxJob.Key, SyncTgxJob.Trigger, scrapeConfiguration); diff --git a/src/producer/Jobs/SyncNyaaJob.cs b/src/producer/Jobs/SyncNyaaJob.cs new file mode 100644 index 0000000..9f28e54 --- /dev/null +++ b/src/producer/Jobs/SyncNyaaJob.cs @@ -0,0 +1,12 @@ +using Producer.Crawlers.Sites; + +namespace Producer.Jobs; + +[DisallowConcurrentExecution] +public class SyncNyaaJob(ICrawlerProvider crawlerProvider) : BaseJob(crawlerProvider) +{ + private const string JobName = nameof(NyaaCrawler); + public static readonly JobKey Key = new(JobName, nameof(Crawlers)); + public static readonly TriggerKey Trigger = new($"{JobName}-trigger", nameof(Crawlers)); + protected override string Crawler => nameof(NyaaCrawler); +} \ No newline at end of file