Fixed: Parsing of multiple languages from Newznab/Torznab indexer releases

Closes #6004
This commit is contained in:
Mark McDowall 2023-09-10 13:51:02 -07:00
parent 5f09f2b25f
commit 2a241294b5
3 changed files with 1959 additions and 7 deletions

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Indexers.Newznab;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
@ -135,5 +136,23 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public async Task should_parse_languages()
{
var recentFeed = ReadAllText(@"Files/Indexers/Newznab/newznab_language.xml");
Mocker.GetMock<IHttpClient>()
.Setup(o => o.ExecuteAsync(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => Task.FromResult(new HttpResponse(r, new HttpHeader(), recentFeed)));
var releases = await Subject.FetchRecent();
releases.Should().HaveCount(100);
releases[0].Languages.Should().BeEquivalentTo(new[] { Language.English, Language.Japanese });
releases[1].Languages.Should().BeEquivalentTo(new[] { Language.English, Language.Spanish });
releases[2].Languages.Should().BeEquivalentTo(new[] { Language.French });
}
}
}

View File

@ -105,22 +105,28 @@ namespace NzbDrone.Core.Indexers.Newznab
protected override List<Language> GetLanguages(XElement item)
{
var languages = TryGetMultipleNewznabAttributes(item, "language");
var languageElements = TryGetMultipleNewznabAttributes(item, "language");
var results = new List<Language>();
// Try to find <language> elements for some indexers that suck at following the rules.
if (languages.Count == 0)
if (languageElements.Count == 0)
{
languages = item.Elements("language").Select(e => e.Value).ToList();
languageElements = item.Elements("language").Select(e => e.Value).ToList();
}
foreach (var language in languages)
foreach (var languageElement in languageElements)
{
var mappedLanguage = IsoLanguages.FindByName(language)?.Language ?? null;
var languages = languageElement.Split(',',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (mappedLanguage != null)
foreach (var language in languages)
{
results.Add(mappedLanguage);
var mappedLanguage = IsoLanguages.FindByName(language)?.Language ?? null;
if (mappedLanguage != null)
{
results.Add(mappedLanguage);
}
}
}