Skip to content

Commit

Permalink
Merge pull request #105 from iPromKnight/community-powered-idea
Browse files Browse the repository at this point in the history
Woke up to see a discussion and comments such as: powered by community tagline
  • Loading branch information
Gabisonfire authored Mar 3, 2024
2 parents 98115e0 + 95fa48c commit cd05013
Show file tree
Hide file tree
Showing 59 changed files with 733 additions and 261 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,5 @@ FodyWeavers.xsd

dist/
deployment/docker/docker-compose-dev.yaml

src/producer/.run/
5 changes: 5 additions & 0 deletions src/producer/Configuration/scrapers.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"Name": "SyncDmmJob",
"IntervalSeconds": 1800,
"Enabled": true
},
{
"Name": "SyncTorrentioJob",
"IntervalSeconds": 604800,
"Enabled": true
}
]
}
Expand Down
14 changes: 14 additions & 0 deletions src/producer/Configuration/torrentio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"TorrentioConfiguration": {
"Instances": [
{
"Name": "Official",
"Url": "https://torrentio.strem.fun",
"RateLimit": {
"RequestLimit": 300,
"IntervalInSeconds": 3600
}
}
]
}
}
25 changes: 25 additions & 0 deletions src/producer/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,36 @@ public static IConfigurationBuilder AddScrapeConfiguration(this IConfigurationBu

configuration.AddJsonFile(LoggingConfig, false, true);
configuration.AddJsonFile(ScrapeConfiguration.Filename, false, true);
configuration.AddJsonFile(TorrentioConfiguration.Filename, false, true);

configuration.AddEnvironmentVariables();

configuration.AddUserSecrets<Program>();

return configuration;
}

public static TConfiguration LoadConfigurationFromConfig<TConfiguration>(this IServiceCollection services, IConfiguration configuration, string sectionName)
where TConfiguration : class
{
var instance = configuration.GetSection(sectionName).Get<TConfiguration>();

ArgumentNullException.ThrowIfNull(instance, nameof(instance));

services.TryAddSingleton(instance);

return instance;
}

public static TConfiguration LoadConfigurationFromEnv<TConfiguration>(this IServiceCollection services)
where TConfiguration : class
{
var instance = Activator.CreateInstance<TConfiguration>();

ArgumentNullException.ThrowIfNull(instance, nameof(instance));

services.TryAddSingleton(instance);

return instance;
}
}
160 changes: 0 additions & 160 deletions src/producer/Extensions/ServiceCollectionExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Interfaces;
namespace Producer.Features.Amqp;

public interface IMessagePublisher
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace Producer.Jobs;
using Literals = Producer.Features.JobSupport.Literals;

namespace Producer.Features.Amqp;

[DisallowConcurrentExecution]
[ManualJobRegistration]
public class PublisherJob(IMessagePublisher publisher, IDataStorage storage, ILogger<PublisherJob> logger) : IJob
{
private const string JobName = nameof(PublisherJob);
public static readonly JobKey Key = new(JobName, nameof(Jobs));
public static readonly TriggerKey Trigger = new($"{JobName}-trigger", nameof(Jobs));
public static readonly JobKey Key = new(JobName, nameof(Literals.PublishingJobs));
public static readonly TriggerKey Trigger = new($"{JobName}-trigger", nameof(Literals.PublishingJobs));

public async Task Execute(IJobExecutionContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Models.Configuration;
namespace Producer.Features.Amqp;

public class RabbitMqConfiguration
{
Expand Down
24 changes: 24 additions & 0 deletions src/producer/Features/Amqp/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Producer.Features.Amqp;

internal static class ServiceCollectionExtensions
{
internal static IServiceCollection RegisterMassTransit(this IServiceCollection services)
{
var rabbitConfig = services.LoadConfigurationFromEnv<RabbitMqConfiguration>();

services.AddMassTransit(busConfigurator =>
{
busConfigurator.SetKebabCaseEndpointNameFormatter();
busConfigurator.UsingRabbitMq((_, busFactoryConfigurator) =>
{
busFactoryConfigurator.Host(rabbitConfig.Host, hostConfigurator =>
{
hostConfigurator.Username(rabbitConfig.Username);
hostConfigurator.Password(rabbitConfig.Password);
});
});
});

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Services;
namespace Producer.Features.Amqp;

public class TorrentPublisher(
ISendEndpointProvider sendEndpointProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Crawlers;
namespace Producer.Features.CrawlerSupport;

public abstract class BaseCrawler(ILogger<BaseCrawler> logger, IDataStorage storage) : ICrawler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Producer.Crawlers;
namespace Producer.Features.CrawlerSupport;

public abstract class BaseJsonCrawler(IHttpClientFactory httpClientFactory, ILogger<BaseJsonCrawler> logger, IDataStorage storage) : BaseCrawler(logger, storage)
{
private readonly HttpClient _client = httpClientFactory.CreateClient("Scraper");
private readonly HttpClient _client = httpClientFactory.CreateClient(Literals.CrawlerClient);

protected virtual async Task Execute(string collectionName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Producer.Crawlers;
namespace Producer.Features.CrawlerSupport;

public abstract class BaseXmlCrawler(IHttpClientFactory httpClientFactory, ILogger<BaseXmlCrawler> logger, IDataStorage storage) : BaseCrawler(logger, storage)
{
public override async Task Execute()
{
logger.LogInformation("Starting {Source} crawl", Source);

using var client = httpClientFactory.CreateClient("Scraper");
using var client = httpClientFactory.CreateClient(Literals.CrawlerClient);
var xml = await client.GetStringAsync(Url);
var xmlRoot = XElement.Parse(xml);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Crawlers;
namespace Producer.Features.CrawlerSupport;

public class CrawlerProvider(IServiceProvider serviceProvider) : ICrawlerProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Interfaces;
namespace Producer.Features.CrawlerSupport;

public interface ICrawler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Interfaces;
namespace Producer.Features.CrawlerSupport;

public interface ICrawlerProvider
{
Expand Down
6 changes: 6 additions & 0 deletions src/producer/Features/CrawlerSupport/Literals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Producer.Features.CrawlerSupport;

public static class Literals
{
public const string CrawlerClient = "Scraper";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Models;
namespace Producer.Features.CrawlerSupport;

public record InsertTorrentResult(bool Success, int InsertedCount = 0, string? ErrorMessage = null);
public record UpdatedTorrentResult(bool Success, int UpdatedCount = 0, string? ErrorMessage = null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Models;
namespace Producer.Features.CrawlerSupport;

public class Scraper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Producer.Features.CrawlerSupport;

internal static class ServiceCollectionExtensions
{
internal static IServiceCollection AddCrawlers(this IServiceCollection services)
{
services.AddHttpClient(Literals.CrawlerClient);

var crawlerTypes = Assembly.GetAssembly(typeof(ICrawler))
.GetTypes()
.Where(t => t is {IsClass: true, IsAbstract: false} && typeof(ICrawler).IsAssignableFrom(t));

foreach (var type in crawlerTypes)
{
services.AddKeyedTransient(typeof(ICrawler), type.Name, type);
}

services
.AddSingleton<ICrawlerProvider, CrawlerProvider>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Producer.Models;
namespace Producer.Features.CrawlerSupport;

// Torrent represents a crawled torrent from one of our
// supported sources.
Expand Down
Loading

0 comments on commit cd05013

Please sign in to comment.