From 1e3de38db77679753eca6477dcfb5b7415399b7a Mon Sep 17 00:00:00 2001 From: KZ Date: Mon, 31 Aug 2015 20:52:15 +0100 Subject: [PATCH] CouchPotato - Change prefilter to allow results with no year #158 --- src/Jackett/Utils/TorznabCapsUtil.cs | 149 ++++++++++++++++----------- 1 file changed, 86 insertions(+), 63 deletions(-) diff --git a/src/Jackett/Utils/TorznabCapsUtil.cs b/src/Jackett/Utils/TorznabCapsUtil.cs index 338feea56..d4b593750 100644 --- a/src/Jackett/Utils/TorznabCapsUtil.cs +++ b/src/Jackett/Utils/TorznabCapsUtil.cs @@ -1,63 +1,86 @@ -using Jackett.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; - -namespace Jackett.Utils -{ - public class TorznabUtil - { - static Regex reduceSpacesRegex = new Regex("\\s{2,}", RegexOptions.Compiled); - - public static TorznabCapabilities CreateDefaultTorznabTVCaps() - { - var caps = new TorznabCapabilities(); - caps.Categories.AddRange(new[] { - TorznabCatType.TV, - TorznabCatType.TVSD, - TorznabCatType.TVHD - }); - return caps; - } - - public static IEnumerable FilterResultsToTitle(IEnumerable results, string name, int year) - { - if (string.IsNullOrWhiteSpace(name)) - return results; - - name = CleanTitle(name); - var filteredResults = new List(); - foreach (var result in results) - { - if (result.Title == null) - continue; - if (CleanTitle(result.Title).Contains(name) && - (year ==0 || result.Title.Contains(year.ToString()))) - { - filteredResults.Add(result); - } - } - - return filteredResults; - } - - public static IEnumerable FilterResultsToImdb(IEnumerable results, string imdb) - { - if (string.IsNullOrWhiteSpace(imdb)) - return results; - // Filter out releases that do have a valid imdb ID, that is not equal to the one we're searching for. - return - results.Where( - result => !result.Imdb.HasValue || result.Imdb.Value == 0 || ("tt" + result.Imdb.Value).Equals(imdb)); - } - - private static string CleanTitle(string title) - { - title = title.Replace(':', ' ').Replace('.', ' ').Replace('-', ' ').Replace('_', ' ').Replace('+', ' '); - return reduceSpacesRegex.Replace(title, " ").ToLowerInvariant(); - } - } -} +using Jackett.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Jackett.Utils +{ + public class TorznabUtil + { + static Regex reduceSpacesRegex = new Regex("\\s{2,}", RegexOptions.Compiled); + + static Regex findYearRegex = new Regex(@"(?<=\[|\(|\s)(\d{4})(?=\]|\)|\s)", RegexOptions.Compiled); + + public static TorznabCapabilities CreateDefaultTorznabTVCaps() + { + var caps = new TorznabCapabilities(); + caps.Categories.AddRange(new[] { + TorznabCatType.TV, + TorznabCatType.TVSD, + TorznabCatType.TVHD + }); + return caps; + } + + private static int GetYearFromTitle(string title) + { + var match = findYearRegex.Match(title); + if (match.Success) + { + var year = ParseUtil.CoerceInt(match.Value); + if(year>1850 && year < 2100) + { + return year; + } + } + + return 0; + } + + public static IEnumerable FilterResultsToTitle(IEnumerable results, string name, int imdbYear) + { + if (string.IsNullOrWhiteSpace(name)) + return results; + + name = CleanTitle(name); + var filteredResults = new List(); + foreach (var result in results) + { + if (result.Title == null) + continue; + + // Match on title + if (CleanTitle(result.Title).Contains(name)) + { + // Match on year + var titleYear = GetYearFromTitle(result.Title); + if (imdbYear == 0 || titleYear == 0 || titleYear == imdbYear) + { + filteredResults.Add(result); + } + } + } + + return filteredResults; + } + + public static IEnumerable FilterResultsToImdb(IEnumerable results, string imdb) + { + if (string.IsNullOrWhiteSpace(imdb)) + return results; + // Filter out releases that do have a valid imdb ID, that is not equal to the one we're searching for. + return + results.Where( + result => !result.Imdb.HasValue || result.Imdb.Value == 0 || ("tt" + result.Imdb.Value).Equals(imdb)); + } + + private static string CleanTitle(string title) + { + title = title.Replace(':', ' ').Replace('.', ' ').Replace('-', ' ').Replace('_', ' ').Replace('+', ' '); + return reduceSpacesRegex.Replace(title, " ").ToLowerInvariant(); + } + } +}