mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
Improve producer matching - Add tissue service
Tissue service will sanitize the existign database of ingested torrents by matching existing titles with new banned word lists. Now with added kleenex
This commit is contained in:
44
src/tissue/Features/DataProcessing/DapperDataStorage.cs
Normal file
44
src/tissue/Features/DataProcessing/DapperDataStorage.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace Tissue.Features.DataProcessing;
|
||||
|
||||
public class DapperDataStorage(PostgresConfiguration configuration, ILogger<DapperDataStorage> logger) : IDataStorage
|
||||
{
|
||||
public async Task<IReadOnlyCollection<Torrent>?> GetAllTorrents(CancellationToken cancellationToken = default)
|
||||
{
|
||||
const string GetAllTorrentsSql = "SELECT * FROM torrents";
|
||||
|
||||
try
|
||||
{
|
||||
await using var connection = await CreateAndOpenConnection(cancellationToken);
|
||||
var torrents = await connection.QueryAsync<Torrent>(GetAllTorrentsSql);
|
||||
|
||||
return torrents.ToList();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Error while torrents from database");
|
||||
return new List<Torrent>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteTorrentsByInfoHashes(IReadOnlyCollection<string> infoHashes, CancellationToken cancellationToken = default)
|
||||
{
|
||||
const string deleteTorrentsSql = "DELETE FROM torrents WHERE \"infoHash\" = ANY(@infoHashes)";
|
||||
|
||||
try
|
||||
{
|
||||
await using var connection = await CreateAndOpenConnection(cancellationToken);
|
||||
await connection.ExecuteAsync(deleteTorrentsSql, new { infoHashes });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Error while deleting torrents from database");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<NpgsqlConnection> CreateAndOpenConnection(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var connection = new NpgsqlConnection(configuration.StorageConnectionString);
|
||||
await connection.OpenAsync(cancellationToken);
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
7
src/tissue/Features/DataProcessing/IDataStorage.cs
Normal file
7
src/tissue/Features/DataProcessing/IDataStorage.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Tissue.Features.DataProcessing;
|
||||
|
||||
public interface IDataStorage
|
||||
{
|
||||
Task<IReadOnlyCollection<Torrent>?> GetAllTorrents(CancellationToken cancellationToken = default);
|
||||
Task DeleteTorrentsByInfoHashes(IReadOnlyCollection<string> infoHashes, CancellationToken cancellationToken = default);
|
||||
}
|
||||
19
src/tissue/Features/DataProcessing/PostgresConfiguration.cs
Normal file
19
src/tissue/Features/DataProcessing/PostgresConfiguration.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace Tissue.Features.DataProcessing;
|
||||
|
||||
public class PostgresConfiguration
|
||||
{
|
||||
private const string Prefix = "POSTGRES";
|
||||
private const string HostVariable = "HOST";
|
||||
private const string UsernameVariable = "USER";
|
||||
private const string PasswordVariable = "PASSWORD";
|
||||
private const string DatabaseVariable = "DB";
|
||||
private const string PortVariable = "PORT";
|
||||
|
||||
private string Host { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(HostVariable);
|
||||
private string Username { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(UsernameVariable);
|
||||
private string Password { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(PasswordVariable);
|
||||
private string Database { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(DatabaseVariable);
|
||||
private int PORT { get; init; } = Prefix.GetEnvironmentVariableAsInt(PortVariable, 5432);
|
||||
|
||||
public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};";
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Tissue.Features.DataProcessing;
|
||||
|
||||
internal static class ServiceCollectionExtensions
|
||||
{
|
||||
internal static IServiceCollection AddDataStorage(this IServiceCollection services)
|
||||
{
|
||||
services.LoadConfigurationFromEnv<PostgresConfiguration>();
|
||||
services.AddTransient<IDataStorage, DapperDataStorage>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
20
src/tissue/Features/DataProcessing/Torrent.cs
Normal file
20
src/tissue/Features/DataProcessing/Torrent.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Tissue.Features.DataProcessing;
|
||||
|
||||
public class Torrent
|
||||
{
|
||||
public string? InfoHash { get; set; }
|
||||
public string? Provider { get; set; }
|
||||
public string? TorrentId { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public long? Size { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public DateTime UploadDate { get; set; }
|
||||
public short? Seeders { get; set; }
|
||||
public string? Trackers { get; set; }
|
||||
public string? Languages { get; set; }
|
||||
public string? Resolution { get; set; }
|
||||
public bool Reviewed { get; set; }
|
||||
public bool Opened { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user