fix(AllDebrid): Implement filters & fix file paths

This commit is contained in:
Sculas 2025-01-04 04:29:26 +01:00
parent 8518cc1e10
commit 0026541dca
No known key found for this signature in database
GPG key ID: 1530BFF96D1EEB89

View file

@ -1,4 +1,5 @@
using AllDebridNET; using System.Text.RegularExpressions;
using AllDebridNET;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
@ -6,6 +7,7 @@ using RdtClient.Data.Models.TorrentClient;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using System.Web; using System.Web;
using File = AllDebridNET.File; using File = AllDebridNET.File;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
namespace RdtClient.Service.Services.TorrentClients; namespace RdtClient.Service.Services.TorrentClients;
@ -62,7 +64,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
Added = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.UploadDate), Added = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.UploadDate),
Files = torrent.Links.Select((m, i) => new TorrentClientFile Files = torrent.Links.Select((m, i) => new TorrentClientFile
{ {
Path = m.Filename, Path = GetFiles(m.Files),
Bytes = m.Size, Bytes = m.Size,
Id = i, Id = i,
Selected = true, Selected = true,
@ -242,6 +244,53 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
Log($"Found {links.Count} files that match the minimum file size criterea", torrent); Log($"Found {links.Count} files that match the minimum file size criterea", torrent);
} }
if (!String.IsNullOrWhiteSpace(torrent.IncludeRegex))
{
Log($"Using regular expression {torrent.IncludeRegex} to include only files matching this regex", torrent);
var newLinks = new List<Link>();
foreach (var link in links)
{
var path = GetFiles(link.Files);
if (Regex.IsMatch(path, torrent.IncludeRegex))
{
Log($"* Including {path}", torrent);
newLinks.Add(link);
}
else
{
Log($"* Excluding {path}", torrent);
}
}
links = newLinks;
Log($"Found {newLinks.Count} files that match the regex", torrent);
}
else if (!String.IsNullOrWhiteSpace(torrent.ExcludeRegex))
{
Log($"Using regular expression {torrent.IncludeRegex} to ignore files matching this regex", torrent);
var newLinks = new List<Link>();
foreach (var link in links)
{
var path = GetFiles(link.Files);
if (!Regex.IsMatch(path, torrent.ExcludeRegex))
{
Log($"* Including {path}", torrent);
newLinks.Add(link);
}
else
{
Log($"* Excluding {path}", torrent);
}
}
links = newLinks;
Log($"Found {newLinks.Count} files that match the regex", torrent);
}
if (links.Count == 0) if (links.Count == 0)
{ {
@ -250,15 +299,16 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
links = magnet.Links; links = magnet.Links;
} }
Log($"Selecting links:"); if (logger.IsEnabled(LogLevel.Debug))
foreach (var link in links)
{ {
var fileList = GetFiles(link.Files, ""); Log($"Selecting links:");
Log($"{link.Filename} ({link.Size}b) {link.LinkUrl}, contains files:{Environment.NewLine}{String.Join(Environment.NewLine, fileList)}"); foreach (var link in links)
{
Log($"{GetFiles(link.Files)} ({link.Size}b) {link.LinkUrl}");
}
} }
Log("", torrent); Log("", torrent);
return links.Select(m => m.LinkUrl.ToString()).ToList(); return links.Select(m => m.LinkUrl.ToString()).ToList();
@ -282,7 +332,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
return Map(result); return Map(result);
} }
private static List<String> GetFiles(IList<File> files, String parent) private static String GetFiles(IList<File> files)
{ {
var result = new List<String>(); var result = new List<String>();
@ -290,19 +340,24 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
{ {
if (!String.IsNullOrWhiteSpace(file.N)) if (!String.IsNullOrWhiteSpace(file.N))
{ {
result.Add($"{parent}/{file.N}"); result.Add(file.N);
} }
if (file.E != null && file.E.Value.PurpleEArray != null && file.E.Value.PurpleEArray.Count > 0) if (file.E != null && file.E.Value.PurpleEArray != null && file.E.Value.PurpleEArray.Count > 0)
{ {
result.AddRange(GetFiles(file.E.Value.PurpleEArray, file.N)); if (file.E.Value.PurpleEArray.Count != 1)
{
throw new("Unexpected number of nested files");
}
result.AddRange(GetFiles(file.E.Value.PurpleEArray));
} }
} }
return result; return String.Join("/", result);
} }
private static List<String> GetFiles(IList<FileE1> files, String parent) private static List<String> GetFiles(IList<FileE1> files)
{ {
var result = new List<String>(); var result = new List<String>();
@ -310,19 +365,19 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
{ {
if (!String.IsNullOrWhiteSpace(file.N)) if (!String.IsNullOrWhiteSpace(file.N))
{ {
result.Add($"{parent}/{file.N}"); result.Add(file.N);
} }
if (file.E != null && file.E.Count > 0) if (file.E != null && file.E.Count > 0)
{ {
result.AddRange(GetFiles(file.E, file.N)); result.AddRange(GetFiles(file.E));
} }
} }
return result; return result;
} }
private static List<String> GetFiles(IList<FileE2> files, String parent) private static List<String> GetFiles(IList<FileE2> files)
{ {
var result = new List<String>(); var result = new List<String>();
@ -330,7 +385,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
{ {
if (!String.IsNullOrWhiteSpace(file.N)) if (!String.IsNullOrWhiteSpace(file.N))
{ {
result.Add($"{parent}/{file.N}"); result.Add(file.N);
} }
} }