From 888b514dd85c114162ef812ccf40379d5fa1c311 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 23 Feb 2023 21:26:02 +0200 Subject: [PATCH] Fixed: (Cardigann) Switch to DateTime standard --- .../Definitions/Cardigann/CardigannBase.cs | 29 ++++++++++--------- .../Definitions/Cardigann/CardigannParser.cs | 9 +++--- src/NzbDrone.Core/Parser/DateTimeUtil.cs | 18 ++++++------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs index 140a26d31..01333eadd 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Net; using System.Text; @@ -613,14 +614,22 @@ namespace NzbDrone.Core.Indexers.Cardigann case "timeparse": case "dateparse": var layout = (string)filter.Args; - try + + if (layout.Contains("yy") && DateTime.TryParseExact(data, layout, CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate)) { - var date = DateTimeUtil.ParseDateTimeGoLang(data, layout); - data = date.ToString(DateTimeUtil.Rfc1123ZPattern); + data = parsedDate.ToString(DateTimeUtil.Rfc1123ZPattern); } - catch (InvalidDateException ex) + else { - _logger.Debug(ex.Message); + try + { + var date = DateTimeUtil.ParseDateTimeGoLang(data, layout); + data = date.ToString(DateTimeUtil.Rfc1123ZPattern); + } + catch (InvalidDateException ex) + { + _logger.Debug(ex.Message); + } } break; @@ -657,15 +666,7 @@ namespace NzbDrone.Core.Indexers.Cardigann break; case "trim": var cutset = (string)filter.Args; - if (cutset != null) - { - data = data.Trim(cutset[0]); - } - else - { - data = data.Trim(); - } - + data = cutset != null ? data.Trim(cutset[0]) : data.Trim(); break; case "prepend": var prependstr = (string)filter.Args; diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannParser.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannParser.cs index ed2fb6e75..76d81b121 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannParser.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannParser.cs @@ -72,6 +72,7 @@ namespace NzbDrone.Core.Indexers.Cardigann } var parsedJson = JToken.Parse(results); + if (parsedJson == null) { throw new IndexerException(indexerResponse, "Error Parsing Json Response"); @@ -80,12 +81,10 @@ namespace NzbDrone.Core.Indexers.Cardigann if (search.Rows.Count != null) { var countVal = HandleJsonSelector(search.Rows.Count, parsedJson, variables); - if (int.TryParse(countVal, out var count)) + + if (int.TryParse(countVal, out var count) && count < 1) { - if (count < 1) - { - return releases; - } + return releases; } } diff --git a/src/NzbDrone.Core/Parser/DateTimeUtil.cs b/src/NzbDrone.Core/Parser/DateTimeUtil.cs index 3e05f60a7..6afccf193 100644 --- a/src/NzbDrone.Core/Parser/DateTimeUtil.cs +++ b/src/NzbDrone.Core/Parser/DateTimeUtil.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; namespace NzbDrone.Core.Parser @@ -120,6 +121,13 @@ namespace NzbDrone.Core.Parser try { str = str.Trim(); + + // try parsing the str as an unix timestamp + if (str.All(char.IsDigit) && long.TryParse(str, out var unixTimeStamp)) + { + return UnixTimestampToDateTime(unixTimeStamp); + } + if (str.ToLower().Contains("now")) { return DateTime.UtcNow; @@ -212,14 +220,6 @@ namespace NzbDrone.Core.Parser return dt; } - // try parsing the str as an unix timestamp - if (long.TryParse(str, out var unixTimeStamp)) - { - return UnixTimestampToDateTime(unixTimeStamp); - } - - // it wasn't a timestamp, continue.... - // add missing year match = _MissingYearRegexp.Match(str); if (match.Success) @@ -312,7 +312,7 @@ namespace NzbDrone.Core.Parser } catch (FormatException ex) { - throw new InvalidDateException($"Error while parsing DateTime \"{date}\", using layout \"{layout}\" ({pattern}): {ex.Message}"); + throw new InvalidDateException($"Error while parsing DateTime \"{date}\", using layout \"{layout}\" ({pattern}): {ex.Message}", ex); } }