Skip to content

Commit

Permalink
refactor: include imdbfiles which dont have a year (0)
Browse files Browse the repository at this point in the history
  • Loading branch information
iPromKnight committed Jan 24, 2025
1 parent 565ac77 commit b943ba0
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/Zilean.Database/Services/TorrentInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,22 @@ private static double CalculateScore(TorrentInfo torrent, ImdbFile imdb) =>
? CloseMatchTitleYearScore * 100
: Fuzz.Ratio(torrent.ParsedTitle, imdb.Title, PreprocessMode.Full);

private bool HasFilteredPartitionsWithYear(ConcurrentDictionary<int, List<ImdbFile>> imdbTvFilesByYear, ConcurrentDictionary<int, List<ImdbFile>> imdbMovieFilesByYear, TorrentInfo torrent,
private bool HasFilteredPartitionsWithYear(
ConcurrentDictionary<int, List<ImdbFile>> imdbTvFilesByYear,
ConcurrentDictionary<int, List<ImdbFile>> imdbMovieFilesByYear,
TorrentInfo torrent,
out IEnumerable<ImdbFile> relevantImdbFiles)
{
switch (torrent.Category)
{
case "tvSeries":
relevantImdbFiles = Enumerable.Range(torrent.Year!.Value - 1, 3)
.Where(year => imdbTvFilesByYear.TryGetValue(year, out var _))
.SelectMany(year => imdbTvFilesByYear[year]);
relevantImdbFiles = GetFilteredFiles(imdbTvFilesByYear, torrent.Year!.Value, includeYearZero: true);
break;

case "movie":
relevantImdbFiles = Enumerable.Range(torrent.Year!.Value - 1, 3)
.Where(year => imdbMovieFilesByYear.TryGetValue(year, out var _))
.SelectMany(year => imdbMovieFilesByYear[year]);
relevantImdbFiles = GetFilteredFiles(imdbMovieFilesByYear, torrent.Year!.Value, includeYearZero: true);
break;

default:
logger.LogWarning("Torrent '{Title}' has an unknown category '{Category}', skipping", torrent.NormalizedTitle, torrent.Category);
relevantImdbFiles = [];
Expand All @@ -341,6 +342,33 @@ private bool HasFilteredPartitionsWithYear(ConcurrentDictionary<int, List<ImdbFi
return true;
}

private static IEnumerable<ImdbFile> GetFilteredFiles(
ConcurrentDictionary<int, List<ImdbFile>> filesByYear,
int baseYear,
bool includeYearZero)
{
var years = new[] { baseYear - 1, baseYear, baseYear + 1 };

foreach (var year in years)
{
if (filesByYear.TryGetValue(year, out var files))
{
foreach (var file in files)
{
yield return file;
}
}
}

if (includeYearZero && filesByYear.TryGetValue(0, out var zeroYearFiles))
{
foreach (var file in zeroYearFiles)
{
yield return file;
}
}
}

private IEnumerable<ImdbFile> GetAllImdbFilesWithoutYear(ConcurrentDictionary<int, List<ImdbFile>> imdbTvFilesByYear, ConcurrentDictionary<int, List<ImdbFile>> imdbMovieFilesByYear, TorrentInfo torrent)
{
switch (torrent.Category)
Expand Down

0 comments on commit b943ba0

Please sign in to comment.