From 4015ff08a62a9b766e6364fefb89a1a16c321962 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Mon, 18 Feb 2019 00:09:41 +0100 Subject: [PATCH] @cosmetic Added Parsing Library: A dotnet library that "exports" our parsing interface. --- src/NzbDrone.Core/CustomFormats/FormatTag.cs | 5 +- src/NzbDrone.Core/Fluent.cs | 5 +- src/NzbDrone.Core/Parser/LanguageParser.cs | 7 +- .../Parser/Model/ParsedMovieInfo.cs | 42 ++++++-- src/NzbDrone.Core/Parser/Parser.cs | 29 +----- src/NzbDrone.Core/Qualities/Quality.cs | 8 +- src/NzbDrone.sln | 8 ++ src/ParsingLibrary/NzbDroneLogger.cs | 18 ++++ src/ParsingLibrary/ParsingLibrary.csproj | 95 +++++++++++++++++++ src/ParsingLibrary/PathExtensions.cs | 12 +++ 10 files changed, 190 insertions(+), 39 deletions(-) create mode 100644 src/ParsingLibrary/NzbDroneLogger.cs create mode 100644 src/ParsingLibrary/ParsingLibrary.csproj create mode 100644 src/ParsingLibrary/PathExtensions.cs diff --git a/src/NzbDrone.Core/CustomFormats/FormatTag.cs b/src/NzbDrone.Core/CustomFormats/FormatTag.cs index 0297231b1..083a4fc71 100644 --- a/src/NzbDrone.Core/CustomFormats/FormatTag.cs +++ b/src/NzbDrone.Core/CustomFormats/FormatTag.cs @@ -83,7 +83,9 @@ namespace NzbDrone.Core.CustomFormats var tuple = Value as (long, long)? ?? (0, 0); return size > tuple.Item1 && size < tuple.Item2; case TagType.Indexer: +#if !LIBRARY return (movieInfo.ExtraInfo.GetValueOrDefault("IndexerFlags") as IndexerFlags?)?.HasFlag((IndexerFlags) Value) == true; +#endif default: return false; } @@ -188,6 +190,7 @@ namespace NzbDrone.Core.CustomFormats Value = Parser.LanguageParser.ParseLanguages(value).First(); break; case "i": +#if !LIBRARY TagType = TagType.Indexer; var flagValues = Enum.GetValues(typeof(IndexerFlags)); @@ -198,7 +201,7 @@ namespace NzbDrone.Core.CustomFormats Value = flagValue; break; } - +#endif break; case "g": TagType = TagType.Size; diff --git a/src/NzbDrone.Core/Fluent.cs b/src/NzbDrone.Core/Fluent.cs index 6e2e3d2b2..dd63c1de9 100644 --- a/src/NzbDrone.Core/Fluent.cs +++ b/src/NzbDrone.Core/Fluent.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; +#if !LIBRARY using NzbDrone.Common.EnsureThat; +#endif namespace NzbDrone.Core { @@ -10,8 +12,9 @@ namespace NzbDrone.Core { public static string WithDefault(this string actual, object defaultValue) { +#if !LIBRARY Ensure.That(defaultValue, () => defaultValue).IsNotNull(); - +#endif if (string.IsNullOrWhiteSpace(actual)) { return defaultValue.ToString(); diff --git a/src/NzbDrone.Core/Parser/LanguageParser.cs b/src/NzbDrone.Core/Parser/LanguageParser.cs index d139dbe79..8c621c625 100644 --- a/src/NzbDrone.Core/Parser/LanguageParser.cs +++ b/src/NzbDrone.Core/Parser/LanguageParser.cs @@ -142,7 +142,9 @@ namespace NzbDrone.Core.Parser { try { +#if !LIBRARY Logger.Debug("Parsing language from subtitle file: {0}", fileName); +#endif var simpleFilename = Path.GetFileNameWithoutExtension(fileName); var languageMatch = SubtitleLanguageRegex.Match(simpleFilename); @@ -154,12 +156,15 @@ namespace NzbDrone.Core.Parser return isoLanguage?.Language ?? Language.Unknown; } - +#if !LIBRARY Logger.Debug("Unable to parse langauge from subtitle file: {0}", fileName); +#endif } catch (Exception ex) { +#if !LIBRARY Logger.Debug("Failed parsing langauge from subtitle file: {0}", fileName); +#endif } return Language.Unknown; diff --git a/src/NzbDrone.Core/Parser/Model/ParsedMovieInfo.cs b/src/NzbDrone.Core/Parser/Model/ParsedMovieInfo.cs index e7868c4b4..e3a930f7f 100644 --- a/src/NzbDrone.Core/Parser/Model/ParsedMovieInfo.cs +++ b/src/NzbDrone.Core/Parser/Model/ParsedMovieInfo.cs @@ -1,8 +1,7 @@ -using System.Collections.Generic; -using System.Linq; +using System; +using System.Collections.Generic; using Newtonsoft.Json; using NzbDrone.Common.Extensions; -using NzbDrone.Core.CustomFormats; using NzbDrone.Core.Qualities; namespace NzbDrone.Core.Parser.Model @@ -39,14 +38,39 @@ namespace NzbDrone.Core.Parser.Model public int Year { get; set; } public string ImdbId { get; set; } - public ParsedMovieInfo() - { - - } - public override string ToString() { - return string.Format("{0} - {1} {2}", MovieTitle, Year, Quality); + return String.Format("{0} - {1} {2}", MovieTitle, Year, Quality); } + +#if LIBRARY + public static ParsedMovieInfo ParseMovieInfo(string title) + { + var parsedMovie = Parser.ParseMovieTitle(title, false); + + if (parsedMovie == null) return null; + + parsedMovie.Languages = LanguageParser.ParseLanguages(parsedMovie.SimpleReleaseTitle); + + parsedMovie.Quality = QualityParser.ParseQuality(parsedMovie.SimpleReleaseTitle); + + if (parsedMovie.Edition.IsNullOrWhiteSpace()) + { + parsedMovie.Edition = Parser.ParseEdition(parsedMovie.SimpleReleaseTitle); + } + + parsedMovie.ReleaseGroup = Parser.ParseReleaseGroup(parsedMovie.SimpleReleaseTitle); + + parsedMovie.ImdbId = Parser.ParseImdbId(parsedMovie.SimpleReleaseTitle); + + parsedMovie.Languages = + LanguageParser.EnhanceLanguages(parsedMovie.SimpleReleaseTitle, parsedMovie.Languages); + + parsedMovie.Quality.Quality = Qualities.Quality.FindByInfo(parsedMovie.Quality.Source, parsedMovie.Quality.Resolution, + parsedMovie.Quality.Modifier); + + return parsedMovie; + } +#endif } } diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index fed3f027b..2f62c0146 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -4,13 +4,15 @@ using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; -using NLog; using NzbDrone.Common.Extensions; -using NzbDrone.Common.Instrumentation; -using NzbDrone.Core.Configuration; using NzbDrone.Core.Parser.Model; +using NLog; +using NzbDrone.Common.Instrumentation; +#if !LIBRARY +using NzbDrone.Core.Configuration; using NzbDrone.Core.Movies; using TinyIoC; +#endif namespace NzbDrone.Core.Parser { @@ -463,27 +465,6 @@ namespace NzbDrone.Core.Parser return title; } - private static SeriesTitleInfo GetSeriesTitleInfo(string title) - { - var seriesTitleInfo = new SeriesTitleInfo(); - seriesTitleInfo.Title = title; - - var match = YearInTitleRegex.Match(title); - - if (!match.Success) - { - seriesTitleInfo.TitleWithoutYear = title; - } - - else - { - seriesTitleInfo.TitleWithoutYear = match.Groups["title"].Value; - seriesTitleInfo.Year = Convert.ToInt32(match.Groups["year"].Value); - } - - return seriesTitleInfo; - } - private static ParsedMovieInfo ParseMovieMatchCollection(MatchCollection matchCollection) { if (!matchCollection[0].Groups["title"].Success || matchCollection[0].Groups["title"].Value == "(") diff --git a/src/NzbDrone.Core/Qualities/Quality.cs b/src/NzbDrone.Core/Qualities/Quality.cs index 9ffa09c0b..71f0e31e1 100644 --- a/src/NzbDrone.Core/Qualities/Quality.cs +++ b/src/NzbDrone.Core/Qualities/Quality.cs @@ -147,7 +147,7 @@ namespace NzbDrone.Core.Qualities { AllLookup[quality.Id] = quality; } - +#if !LIBRARY DefaultQualityDefinitions = new HashSet { new QualityDefinition(Quality.Unknown) { Weight = 1, MinSize = 0, MaxSize = 100 }, @@ -182,14 +182,16 @@ namespace NzbDrone.Core.Qualities new QualityDefinition(Quality.BRDISK) { Weight = 25, MinSize = 0, MaxSize = null }, new QualityDefinition(Quality.RAWHD) { Weight = 26, MinSize = 0, MaxSize = null } }; +#endif } + public static readonly List All; public static readonly Quality[] AllLookup; - +#if !LIBRARY public static readonly HashSet DefaultQualityDefinitions; - +#endif public static Quality FindById(int id) { if (id == 0) return Unknown; diff --git a/src/NzbDrone.sln b/src/NzbDrone.sln index 0bed10417..fdf02fc9a 100644 --- a/src/NzbDrone.sln +++ b/src/NzbDrone.sln @@ -90,6 +90,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogentriesNLog", "Logentrie EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CurlSharp", "ExternalModules\CurlSharp\CurlSharp\CurlSharp.csproj", "{74420A79-CC16-442C-8B1E-7C1B913844F0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParsingLibrary", "ParsingLibrary\ParsingLibrary.csproj", "{BAC762EF-4627-49C8-BC99-EB9D20682FA4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x86 = Debug|x86 @@ -276,6 +278,12 @@ Global {74420A79-CC16-442C-8B1E-7C1B913844F0}.Mono|x86.Build.0 = Release|Any CPU {74420A79-CC16-442C-8B1E-7C1B913844F0}.Release|x86.ActiveCfg = Release|Any CPU {74420A79-CC16-442C-8B1E-7C1B913844F0}.Release|x86.Build.0 = Release|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Debug|x86.ActiveCfg = Debug|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Debug|x86.Build.0 = Debug|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Mono|x86.ActiveCfg = Debug|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Mono|x86.Build.0 = Debug|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Release|x86.ActiveCfg = Release|Any CPU + {BAC762EF-4627-49C8-BC99-EB9D20682FA4}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ParsingLibrary/NzbDroneLogger.cs b/src/ParsingLibrary/NzbDroneLogger.cs new file mode 100644 index 000000000..5cc304191 --- /dev/null +++ b/src/ParsingLibrary/NzbDroneLogger.cs @@ -0,0 +1,18 @@ +using System; +using NLog; + +namespace NzbDrone.Common.Instrumentation +{ + public static class NzbDroneLogger + { + public static Logger GetLogger(Type type) + { + return LogManager.GetLogger(type.Name.Replace("NzbDrone.", "")); + } + + public static Logger GetLogger(object obj) + { + return GetLogger(obj.GetType()); + } + } +} diff --git a/src/ParsingLibrary/ParsingLibrary.csproj b/src/ParsingLibrary/ParsingLibrary.csproj new file mode 100644 index 000000000..ee8dd2389 --- /dev/null +++ b/src/ParsingLibrary/ParsingLibrary.csproj @@ -0,0 +1,95 @@ + + + + netstandard2.0 + + + + TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;LIBRARY; + + + + TRACE;RELEASE;NETSTANDARD;NETSTANDARD2_0;LIBRARY + + + + + NzbDroneException.cs + + + DictionaryExtensions.cs + + + IEnumerableExtensions.cs + + + Int64Extensions.cs + + + StringExtensions.cs + + + CustomFormat.cs + + + FormatTag.cs + + + IEmbeddedDocument.cs + + + ModelBase.cs + + + Fluent.cs + + + MediaFileExtensions.cs + + + InvalidDateException.cs + + + IsoLanguage.cs + + + IsoLanguages.cs + + + Language.cs + + + LanguageParser.cs + + + ParsedMovieInfo.cs + + + Parser.cs + + + QualityParser.cs + + + SceneChecker.cs + + + Quality.cs + + + QualityModel.cs + + + QualitySource.cs + + + Revision.cs + + + + + + + + + diff --git a/src/ParsingLibrary/PathExtensions.cs b/src/ParsingLibrary/PathExtensions.cs new file mode 100644 index 000000000..e7163dceb --- /dev/null +++ b/src/ParsingLibrary/PathExtensions.cs @@ -0,0 +1,12 @@ +using System.IO; + +namespace NzbDrone.Common.Extensions +{ + public static class PathExtensions + { + public static bool ContainsInvalidPathChars(this string text) + { + return text.IndexOfAny(Path.GetInvalidPathChars()) >= 0; + } + } +}