From a85979c2f69c5a8ceabb06cff6474ab0131ccd7b Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 7 Feb 2021 21:32:58 +0100 Subject: [PATCH] New: Added Hindi language closes #4275 --- .../ParserTests/LanguageParserFixture.cs | 8 ++ .../155_add_arabic_and_hindi_languages.cs | 125 ++++++++++++++++++ src/NzbDrone.Core/Languages/Language.cs | 12 +- src/NzbDrone.Core/Parser/IsoLanguages.cs | 3 +- src/NzbDrone.Core/Parser/LanguageParser.cs | 9 +- src/NzbDrone.Core/Sonarr.Core.csproj | 8 +- 6 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/155_add_arabic_and_hindi_languages.cs diff --git a/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs b/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs index 8bff8b768..1bed671ae 100644 --- a/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs @@ -239,6 +239,14 @@ namespace NzbDrone.Core.Test.ParserTests result.Language.Id.Should().Be(Language.Arabic.Id); } + [TestCase("The Shadow Series S01 E01-08 WebRip Dual Audio [Hindi 5.1 + English 5.1] 720p x264 AAC ESub")] + [TestCase("The Final Sonarr (2020) S04 Complete 720p NF WEBRip [Hindi+English] Dual audio")] + public void should_parse_language_hindi(string postTitle) + { + var result = Parser.Parser.ParseTitle(postTitle); + result.Language.Id.Should().Be(Language.Hindi.Id); + } + [TestCase("Title.the.Russian.Series.S01E07.Cold.Action.HDTV.XviD-Droned")] [TestCase("Title.the.Russian.Series.S01E07E08.Cold.Action.HDTV.XviD-Droned")] [TestCase("Title.the.Russian.Series.S01.1080p.WEBRip.DDP5.1.x264-Drone")] diff --git a/src/NzbDrone.Core/Datastore/Migration/155_add_arabic_and_hindi_languages.cs b/src/NzbDrone.Core/Datastore/Migration/155_add_arabic_and_hindi_languages.cs new file mode 100644 index 000000000..7b603caa6 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/155_add_arabic_and_hindi_languages.cs @@ -0,0 +1,125 @@ +using System.Collections.Generic; +using System.Data; +using System.Linq; +using FluentMigrator; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration.Framework; +using NzbDrone.Core.Languages; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(155)] + public class add_arabic_and_hindi_languages : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(ConvertProfile); + } + + private void ConvertProfile(IDbConnection conn, IDbTransaction tran) + { + var updater = new LanguageProfileUpdater128(conn, tran); + + updater.AppendMissing(); + + updater.Commit(); + } + } + + public class LanguageProfile128 : ModelBase + { + public string Name { get; set; } + public List Languages { get; set; } + public bool UpgradeAllowed { get; set; } + public Language Cutoff { get; set; } + } + + public class LanguageProfileItem128 + { + public int Language { get; set; } + public bool Allowed { get; set; } + } + + + public class LanguageProfileUpdater128 + { + private readonly IDbConnection _connection; + private readonly IDbTransaction _transaction; + + private List _profiles; + private HashSet _changedProfiles = new HashSet(); + + public LanguageProfileUpdater128(IDbConnection conn, IDbTransaction tran) + { + _connection = conn; + _transaction = tran; + + _profiles = GetProfiles(); + } + + public void Commit() + { + foreach (var profile in _changedProfiles) + { + using (var updateProfileCmd = _connection.CreateCommand()) + { + updateProfileCmd.Transaction = _transaction; + updateProfileCmd.CommandText = "UPDATE LanguageProfiles SET Languages = ? WHERE Id = ?"; + updateProfileCmd.AddParameter(profile.Languages.ToJson()); + updateProfileCmd.AddParameter(profile.Id); + + updateProfileCmd.ExecuteNonQuery(); + } + } + + _changedProfiles.Clear(); + } + + public void AppendMissing() + { + foreach (var profile in _profiles) + { + var hash = new HashSet(profile.Languages.Select(v => v.Language)); + + var missing = Language.All.Where(l => !hash.Contains(l.Id)) + .OrderByDescending(l => l.Name) + .ToList(); + + if (missing.Any()) + { + profile.Languages.InsertRange(0, missing.Select(l => new LanguageProfileItem128 { Language = l.Id, Allowed = false })); + + _changedProfiles.Add(profile); + } + } + } + + private List GetProfiles() + { + var profiles = new List(); + + using (var getProfilesCmd = _connection.CreateCommand()) + { + getProfilesCmd.Transaction = _transaction; + getProfilesCmd.CommandText = @"SELECT Id, Name, Languages, UpgradeAllowed, Cutoff FROM LanguageProfiles"; + + using (var profileReader = getProfilesCmd.ExecuteReader()) + { + while (profileReader.Read()) + { + profiles.Add(new LanguageProfile128 + { + Id = profileReader.GetInt32(0), + Name = profileReader.GetString(1), + Languages = Json.Deserialize>(profileReader.GetString(2)), + UpgradeAllowed = profileReader.GetBoolean(3), + Cutoff = Language.FindById(profileReader.GetInt32(4)) + }); + } + } + } + + return profiles; + } + } +} diff --git a/src/NzbDrone.Core/Languages/Language.cs b/src/NzbDrone.Core/Languages/Language.cs index 8acbe471d..92ac0ec29 100644 --- a/src/NzbDrone.Core/Languages/Language.cs +++ b/src/NzbDrone.Core/Languages/Language.cs @@ -65,7 +65,7 @@ namespace NzbDrone.Core.Languages public static Language Dutch { get { return new Language(7, "Dutch"); } } public static Language Japanese { get { return new Language(8, "Japanese"); } } public static Language Icelandic { get { return new Language(9, "Icelandic"); } } - public static Language Chinese { get { return new Language(10, "Chinese"); } } + public static Language Chinese { get { return new Language(10, "Chinese"); } } public static Language Russian { get { return new Language(11, "Russian"); } } public static Language Polish { get { return new Language(12, "Polish"); } } public static Language Vietnamese { get { return new Language(13, "Vietnamese"); } } @@ -82,6 +82,7 @@ namespace NzbDrone.Core.Languages public static Language Lithuanian { get { return new Language(24, "Lithuanian"); } } public static Language Czech { get { return new Language(25, "Czech"); } } public static Language Arabic { get { return new Language(26, "Arabic"); } } + public static Language Hindi { get { return new Language(27, "Hindi"); } } public static List All @@ -116,18 +117,19 @@ namespace NzbDrone.Core.Languages Hebrew, Lithuanian, Czech, - Arabic + Arabic, + Hindi }; } } + private static readonly Dictionary Lookup = All.ToDictionary(v => v.Id); + public static Language FindById(int id) { if (id == 0) return Unknown; - Language language = All.FirstOrDefault(v => v.Id == id); - - if (language == null) + if (!Lookup.TryGetValue(id, out var language)) { throw new ArgumentException("ID does not match a known language", nameof(id)); } diff --git a/src/NzbDrone.Core/Parser/IsoLanguages.cs b/src/NzbDrone.Core/Parser/IsoLanguages.cs index f9c32cdcc..661a931c4 100644 --- a/src/NzbDrone.Core/Parser/IsoLanguages.cs +++ b/src/NzbDrone.Core/Parser/IsoLanguages.cs @@ -34,7 +34,8 @@ namespace NzbDrone.Core.Parser new IsoLanguage("he", "heb", Language.Hebrew), new IsoLanguage("lt", "lit", Language.Lithuanian), new IsoLanguage("cs", "ces", Language.Czech), - new IsoLanguage("ar", "ara", Language.Arabic) + new IsoLanguage("ar", "ara", Language.Arabic), + new IsoLanguage("hi", "hin", Language.Hindi) }; public static IsoLanguage Find(string isoCode) diff --git a/src/NzbDrone.Core/Parser/LanguageParser.cs b/src/NzbDrone.Core/Parser/LanguageParser.cs index 84ab52561..3c4cab389 100644 --- a/src/NzbDrone.Core/Parser/LanguageParser.cs +++ b/src/NzbDrone.Core/Parser/LanguageParser.cs @@ -36,9 +36,6 @@ namespace NzbDrone.Core.Parser var lowerTitle = title.ToLower(); - if (lowerTitle.Contains("english")) - return Language.English; - if (lowerTitle.Contains("french")) return Language.French; @@ -96,6 +93,9 @@ namespace NzbDrone.Core.Parser if (lowerTitle.Contains("arabic")) return Language.Arabic; + if (lowerTitle.Contains("hindi")) + return Language.Hindi; + var regexLanguage = RegexLanguage(title); if (regexLanguage != Language.Unknown) @@ -103,6 +103,9 @@ namespace NzbDrone.Core.Parser return regexLanguage; } + if (lowerTitle.Contains("english")) + return Language.English; + return defaultToEnglish ? Language.English : Language.Unknown; } diff --git a/src/NzbDrone.Core/Sonarr.Core.csproj b/src/NzbDrone.Core/Sonarr.Core.csproj index bfe710d36..5e2c4d970 100644 --- a/src/NzbDrone.Core/Sonarr.Core.csproj +++ b/src/NzbDrone.Core/Sonarr.Core.csproj @@ -21,13 +21,7 @@ - - - ..\Libraries\Growl.Connector.dll - - - ..\Libraries\Growl.CoreLibrary.dll - + ..\Libraries\Sqlite\System.Data.SQLite.dll