Producer / Consumer / Collector rewrite (#160)

* Converted metadata service to redis

* move to postgres instead

* fix global usings

* [skip ci] optimize wolverine by prebuilding static types

* [skip ci] Stop indexing mac folder indexes

* [skip ci] producer, metadata and migrations

removed mongodb
added redis cache
imdb meta in postgres
Enable pgtrm
Create trigrams index
Add search meta postgres function

* [skip ci] get rid of node folder, replace mongo with redis in consumer

also wire up postgres metadata searches

* [skip ci] change mongo to redis in the addon

* [skip ci] jackettio to redis

* Rest of mongo removed...

* Cleaner rerunning of metadata - without conflicts

* Add akas import as well as basic metadata

* Include episodes file too

* cascade truncate pre-import

* reverse order to avoid cascadeing

* separate out clean to separate handler

* Switch producer to use metadata matching pre-preocessing dmm

* More work

* Still porting PTN

* PTN port, adding tests

* [skip ci] Codec tests

* [skip ci] Complete Collection handler tests

* [skip ci] container tests

* [skip ci] Convert handlers tests

* [skip ci] DateHandler tests

* [skip ci] Dual Audio matching tests

* [skip ci] episode code tests

* [skip ci] Extended handler tests

* [skip ci] group handler tests

* [skip ci] some broken stuff right now

* [skip ci] more ptn

* [skip ci] PTN now in a separate nuget package, rebased this on the redis changes - i need them.

* [skip ci] Wire up PTN port. Tired - will test tomorrow

* [skip ci] Needs a lot of work - too many titles being missed now

* cleaner. done?

* Handle the date in the imdb search

- add integer function to confirm its a valid integer
- use the input date as a range of -+1 year

* [skip ci] Start of collector service for RD

[skip ci] WIP

Implemented metadata saga, along with channels to process up to a maximum of 100 infohashes each time
The saga will rety for each infohas by requeuing up to three times, before just marking as complete for that infoHash - meaning no data will be updated in the db for that torrent.

[skip ci] Ready to test with queue publishing

Will provision a fanout exchange if it doesn't exist, and create and bind a queue to it. Listens to the queue with 50 prefetch count.
Still needs PTN rewrite bringing in to parse the filename response from real debrid, and extract season and episode numbers if the file is a tvshow

[skip ci] Add Debrid Collector Build Job

Debrid Collector ready for testing

New consumer, new collector, producer has meta lookup and anti porn measures

[skip ci] WIP - moving from wolverine to MassTransit.

 not happy that wolverine cannot effectively control saga concurrency. we need to really.

[skip ci] Producer and new Consumer moved to MassTransit

Just the debrid collector to go now, then to write the optional qbit collector.

Collector now switched to mass transit too

hide porn titles in logs, clean up cache name in redis for imdb titles

[skip ci] Allow control of queues

[skip ci] Update deployment

Remove old consumer, fix deployment files, fix dockerfiles for shared project import

fix base deployment

* Add collector missing env var

* edits to kick off builds

* Add optional qbit deployment which qbit collector will use

* Qbit collector done

* reorder compose, and bring both qbit and qbitcollector into the compose, with 0 replicas as default

* Clean up compose file

* Ensure debrid collector errors if no debrid api key
This commit is contained in:
iPromKnight
2024-03-25 23:32:28 +00:00
committed by GitHub
parent 9c6c1ac249
commit 9a831e92d0
443 changed files with 4154 additions and 476262 deletions

View File

@@ -0,0 +1,56 @@
namespace QBitCollector.Features.Qbit;
public class QbitRequestProcessor(IQBittorrentClient client, ITrackersService trackersService, ILogger<QbitRequestProcessor> logger)
{
public async Task<IReadOnlyList<TorrentContent>?> ProcessAsync(string infoHash, CancellationToken cancellationToken = default)
{
var trackers = await trackersService.GetTrackers();
var magnetLink = CreateMagnetLink(infoHash, trackers);
await client.AddTorrentsAsync(new AddTorrentUrlsRequest(new[] { new Uri(magnetLink) }), cancellationToken);
IReadOnlyList<TorrentContent> metadata = null;
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutCts.CancelAfter(TimeSpan.FromSeconds(30));
try
{
while (!timeoutCts.Token.IsCancellationRequested)
{
var torrentInfo = await client.GetTorrentContentsAsync(infoHash, timeoutCts.Token);
if (torrentInfo is not null && torrentInfo.Count > 0)
{
await client.DeleteAsync(new[] { infoHash }, deleteDownloadedData: true, timeoutCts.Token);
metadata = torrentInfo;
logger.LogInformation("Got metadata for torrent {InfoHash}", infoHash);
break;
}
await Task.Delay(TimeSpan.FromSeconds(1), timeoutCts.Token);
}
}
catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested)
{
await client.DeleteAsync(new[] { infoHash }, deleteDownloadedData: true, cancellationToken);
return null;
}
return metadata;
}
private static string CreateMagnetLink(string infoHash, List<string> trackers)
{
var magnetLink = $"magnet:?xt=urn:btih:{infoHash}";
if (trackers.Count > 0)
{
magnetLink += $"&tr={string.Join("&tr=", trackers)}";
}
return magnetLink;
}
}

View File

@@ -0,0 +1,9 @@
namespace QBitCollector.Features.Qbit;
public class QbitConfiguration
{
private const string Prefix = "QBIT";
private const string ConnectionStringVariable = "HOST";
public string? Host { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(ConnectionStringVariable);
}

View File

@@ -0,0 +1,6 @@
namespace QBitCollector.Features.Trackers;
public interface ITrackersService
{
Task<List<string>> GetTrackers();
}

View File

@@ -0,0 +1,8 @@
namespace QBitCollector.Features.Trackers;
public class TrackersBackgroundService(ITrackersService trackersService) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => trackersService.GetTrackers();
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

View File

@@ -0,0 +1,55 @@
namespace QBitCollector.Features.Trackers;
public class TrackersService(IDistributedCache cache, HttpClient client, IMemoryCache memoryCache) : ITrackersService
{
private const string TrackersListUrl = "https://ngosang.github.io/trackerslist/trackers_all.txt";
private const string CacheKey = "trackers";
public async Task<List<string>> GetTrackers()
{
if (memoryCache.TryGetValue(CacheKey, out List<string> memoryCachedTrackers))
{
return memoryCachedTrackers;
}
var cachedTrackers = await cache.GetStringAsync(CacheKey);
if (!string.IsNullOrWhiteSpace(cachedTrackers))
{
var trackersList = JsonSerializer.Deserialize<List<string>>(cachedTrackers);
memoryCache.Set(CacheKey, trackersList, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(4),
});
return trackersList;
}
var trackers = await GetTrackersAsync();
var cacheOptions = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(2),
};
await cache.SetStringAsync(CacheKey, JsonSerializer.Serialize(trackers), cacheOptions);
memoryCache.Set(CacheKey, trackers, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(4),
});
return trackers;
}
private async Task<List<string>> GetTrackersAsync()
{
var response = await client.GetStringAsync(TrackersListUrl);
var lines = response.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
var nonEmptyLines = lines.Where(line => !string.IsNullOrWhiteSpace(line)).ToList();
return nonEmptyLines;
}
}

View File

@@ -0,0 +1,104 @@
namespace QBitCollector.Features.Worker;
public static class Filetypes
{
public static IReadOnlyList<string> VideoFileExtensions =
[
".3g2",
".3gp",
".3gp2",
".3gpp",
".60d",
".ajp",
".asf",
".asx",
".avchd",
".avi",
".bik",
".bix",
".box",
".cam",
".dat",
".divx",
".dmf",
".dv",
".dvr-ms",
".evo",
".flc",
".fli",
".flic",
".flv",
".flx",
".gvi",
".gvp",
".h264",
".m1v",
".m2p",
".m2ts",
".m2v",
".m4e",
".m4v",
".mjp",
".mjpeg",
".mjpg",
".mkv",
".moov",
".mov",
".movhd",
".movie",
".movx",
".mp4",
".mpe",
".mpeg",
".mpg",
".mpv",
".mpv2",
".mxf",
".nsv",
".nut",
".ogg",
".ogm",
".omf",
".ps",
".qt",
".ram",
".rm",
".rmvb",
".swf",
".ts",
".vfw",
".vid",
".video",
".viv",
".vivo",
".vob",
".vro",
".wm",
".wmv",
".wmx",
".wrap",
".wvx",
".wx",
".x264",
".xvid",
];
public static IReadOnlyList<string> SubtitleFileExtensions =
[
".a",
".srt",
".ass",
".ssa",
".stl",
".scc",
".ttml",
".sbv",
".dks",
".qtx",
".jss",
".vtt",
".smi",
".usf",
".idx"
];
}

View File

@@ -0,0 +1,21 @@
namespace QBitCollector.Features.Worker;
public class PerformQbitMetadataRequestConsumer(QbitRequestProcessor processor) : IConsumer<PerformQbitMetadataRequest>
{
public async Task Consume(ConsumeContext<PerformQbitMetadataRequest> context)
{
var request = context.Message;
var metadata = await processor.ProcessAsync(request.InfoHash, context.CancellationToken);
if (metadata is null)
{
var emptyresponse = new QBitMetadataResponse(request.CorrelationId, []);
await context.Publish(new GotQbitMetadata(emptyresponse));
return;
}
var response = new QBitMetadataResponse(request.CorrelationId, metadata);
await context.Publish(new GotQbitMetadata(response));
}
}

View File

@@ -0,0 +1,4 @@
namespace QBitCollector.Features.Worker;
[EntityName("torrent-metadata-response")]
public record QBitMetadataResponse(Guid CorrelationId, IReadOnlyList<TorrentContent> Metadata) : CorrelatedBy<Guid>;

View File

@@ -0,0 +1,78 @@
namespace QBitCollector.Features.Worker;
public static class QbitMetaToTorrentMeta
{
public static IReadOnlyList<TorrentFile> MapMetadataToFilesCollection(
IParseTorrentTitle torrentTitle,
Torrent torrent,
string ImdbId,
IReadOnlyList<TorrentContent> Metadata)
{
try
{
var files = new List<TorrentFile>();
foreach (var metadataEntry in Metadata.Where(m => Filetypes.VideoFileExtensions.Any(ext => m.Name.EndsWith(ext))))
{
var file = new TorrentFile
{
ImdbId = ImdbId,
KitsuId = 0,
InfoHash = torrent.InfoHash,
FileIndex = metadataEntry.Index ?? 0,
Title = metadataEntry.Name,
Size = metadataEntry.Size,
};
var parsedTitle = torrentTitle.Parse(file.Title);
file.ImdbSeason = parsedTitle.Seasons.FirstOrDefault();
file.ImdbEpisode = parsedTitle.Episodes.FirstOrDefault();
files.Add(file);
}
return files;
}
catch (Exception)
{
return [];
}
}
public static async Task<IReadOnlyList<SubtitleFile>> MapMetadataToSubtitlesCollection(IDataStorage storage, string InfoHash, IReadOnlyList<TorrentContent> Metadata)
{
try
{
var files = new List<SubtitleFile>();
var torrentFiles = await storage.GetTorrentFiles(InfoHash.ToLowerInvariant());
if (torrentFiles.Count == 0)
{
return files;
}
foreach (var metadataEntry in Metadata.Where(m => Filetypes.SubtitleFileExtensions.Any(ext => m.Name.EndsWith(ext))))
{
var fileId = torrentFiles.FirstOrDefault(t => Path.GetFileNameWithoutExtension(t.Title) == Path.GetFileNameWithoutExtension(metadataEntry.Name))?.Id ?? 0;
var file = new SubtitleFile
{
InfoHash = InfoHash,
FileIndex = metadataEntry.Index ?? 0,
FileId = fileId,
Title = metadataEntry.Name,
};
files.Add(file);
}
return files;
}
catch (Exception)
{
return [];
}
}
}

View File

@@ -0,0 +1,13 @@
namespace QBitCollector.Features.Worker;
public class QbitMetadataSagaState : SagaStateMachineInstance, ISagaVersion
{
public Torrent? Torrent { get; set; }
public string? Title { get; set; }
public string? ImdbId { get; set; }
public QBitMetadataResponse? Metadata { get; set; }
public Guid CorrelationId { get; set; }
public int Version { get; set; }
public int CurrentState { get; set; }
}

View File

@@ -0,0 +1,63 @@
namespace QBitCollector.Features.Worker;
public class QbitMetadataSagaStateMachine : MassTransitStateMachine<QbitMetadataSagaState>
{
public State Ingesting { get; private set; } = null!;
public State Writing { get; private set; } = null!;
public State Completed { get; private set; } = null!;
public Event<CollectMetadata> CollectMetadata { get; private set; } = null!;
public Event<GotQbitMetadata> GotMetadata { get; private set; } = null!;
public Event<QbitMetadataWritten> MetadataWritten { get; private set; } = null!;
public QbitMetadataSagaStateMachine(ILogger<QbitMetadataSagaStateMachine> logger)
{
InstanceState(x => x.CurrentState);
Event(() => CollectMetadata, x => x.CorrelateById(context => context.Message.CorrelationId));
Event(() => GotMetadata, x => x.CorrelateById(context => context.Message.CorrelationId));
Event(() => MetadataWritten, x => x.CorrelateById(context => context.Message.CorrelationId));
Initially(
When(CollectMetadata)
.ThenAsync(
async context =>
{
context.Saga.CorrelationId = context.Data.CorrelationId;
context.Saga.Torrent = context.Data.Torrent;
context.Saga.ImdbId = context.Data.ImdbId;
await context.Publish(new PerformQbitMetadataRequest(context.Saga.CorrelationId, context.Saga.Torrent.InfoHash));
logger.LogInformation("Collecting Metadata for torrent {InfoHash} in Saga {SagaId}", context.Instance.Torrent.InfoHash, context.Instance.CorrelationId);
})
.TransitionTo(Ingesting));
During(
Ingesting,
When(GotMetadata)
.ThenAsync(
async context =>
{
context.Saga.Metadata = context.Data.Metadata;
await context.Publish(new WriteQbitMetadata(context.Saga.Torrent, context.Saga.Metadata, context.Saga.ImdbId));
logger.LogInformation("Scheduling Write for torrent {InfoHash} in Saga {SagaId}", context.Saga.Torrent.InfoHash, context.Saga.CorrelationId);
})
.TransitionTo(Writing));
During(
Writing,
When(MetadataWritten)
.Then(
context =>
{
logger.LogInformation("Metadata Written for torrent {InfoHash} in Saga {SagaId}", context.Saga.Torrent.InfoHash, context.Saga.CorrelationId);
})
.TransitionTo(Completed)
.Finalize());
SetCompletedWhenFinalized();
}
}

View File

@@ -0,0 +1,22 @@
namespace QBitCollector.Features.Worker;
[EntityName("perform-metadata-request")]
public record PerformQbitMetadataRequest(Guid CorrelationId, string InfoHash) : CorrelatedBy<Guid>;
[EntityName("torrent-metadata-response")]
public record GotQbitMetadata(QBitMetadataResponse Metadata) : CorrelatedBy<Guid>
{
public Guid CorrelationId { get; init; } = Metadata.CorrelationId;
}
[EntityName("write-metadata")]
public record WriteQbitMetadata(Torrent Torrent, QBitMetadataResponse Metadata, string ImdbId) : CorrelatedBy<Guid>
{
public Guid CorrelationId { get; init; } = Metadata.CorrelationId;
}
[EntityName("metadata-written")]
public record QbitMetadataWritten(QBitMetadataResponse Metadata) : CorrelatedBy<Guid>
{
public Guid CorrelationId { get; init; } = Metadata.CorrelationId;
}

View File

@@ -0,0 +1,25 @@
namespace QBitCollector.Features.Worker;
public class WriteQbitMetadataConsumer(IParseTorrentTitle parseTorrentTitle, IDataStorage dataStorage) : IConsumer<WriteQbitMetadata>
{
public async Task Consume(ConsumeContext<WriteQbitMetadata> context)
{
var request = context.Message;
var torrentFiles = QbitMetaToTorrentMeta.MapMetadataToFilesCollection(parseTorrentTitle, request.Torrent, request.ImdbId, request.Metadata.Metadata);
if (torrentFiles.Any())
{
await dataStorage.InsertFiles(torrentFiles);
var subtitles = await QbitMetaToTorrentMeta.MapMetadataToSubtitlesCollection(dataStorage, request.Torrent.InfoHash, request.Metadata.Metadata);
if (subtitles.Any())
{
await dataStorage.InsertSubtitles(subtitles);
}
}
await context.Publish(new QbitMetadataWritten(request.Metadata));
}
}