From b646787ab6cae179e2e31f05bbdfd9201c7d1a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Fri, 5 Feb 2021 20:02:47 +0100 Subject: [PATCH 1/6] Get IndexNumberEnd from nfo --- .../Parsers/EpisodeNfoParser.cs | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs index 81774b8735..5922b4f44f 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs @@ -35,14 +35,17 @@ namespace MediaBrowser.XbmcMetadata.Parsers { item.ResetPeople(); - var xml = streamReader.ReadToEnd(); + var xmlFile = streamReader.ReadToEnd(); var srch = ""; - var index = xml.IndexOf(srch, StringComparison.OrdinalIgnoreCase); + var index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase); + + var xml = xmlFile; if (index != -1) { - xml = xml.Substring(0, index + srch.Length); + xml = xmlFile.Substring(0, index + srch.Length); + xmlFile = xmlFile.Substring(index + srch.Length); } // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions @@ -73,6 +76,38 @@ namespace MediaBrowser.XbmcMetadata.Parsers catch (XmlException) { } + + while ((index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase)) != -1) + { + xml = xmlFile.Substring(0, index + srch.Length); + xmlFile = xmlFile.Substring(index + srch.Length); + + // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions + try + { + using (var stringReader = new StringReader(xml)) + using (var reader = XmlReader.Create(stringReader, settings)) + { + reader.MoveToContent(); + + if (reader.ReadToDescendant("episode")) + { + var number = reader.ReadElementContentAsString(); + + if (!string.IsNullOrWhiteSpace(number)) + { + if (int.TryParse(number, out var num)) + { + item.Item.IndexNumberEnd = Math.Max(num, item.Item.IndexNumberEnd ?? num); + } + } + } + } + } + catch (XmlException) + { + } + } } } From 094ffafb249e454f8e71279f8aa0693a17990e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Mon, 8 Feb 2021 18:56:06 +0100 Subject: [PATCH 2/6] Comment and simplify code --- .../Parsers/EpisodeNfoParser.cs | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs index 5922b4f44f..3a852384af 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs @@ -51,6 +51,7 @@ namespace MediaBrowser.XbmcMetadata.Parsers // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions try { + // Extract episode details from the firs episodedetails block using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader, settings)) { @@ -72,19 +73,14 @@ namespace MediaBrowser.XbmcMetadata.Parsers } } } - } - catch (XmlException) - { - } - - while ((index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase)) != -1) - { - xml = xmlFile.Substring(0, index + srch.Length); - xmlFile = xmlFile.Substring(index + srch.Length); - // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions - try + // Extract the last episode number from nfo + // This is needed because XBMC metadata uses multiple episodedetails blocks instead of episodenumberend tag + while ((index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase)) != -1) { + xml = xmlFile.Substring(0, index + srch.Length); + xmlFile = xmlFile.Substring(index + srch.Length); + using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader, settings)) { @@ -104,9 +100,9 @@ namespace MediaBrowser.XbmcMetadata.Parsers } } } - catch (XmlException) - { - } + } + catch (XmlException) + { } } } From 351d61a3181c6caf217ec74eed37f972190b624d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Mon, 8 Feb 2021 19:40:17 +0100 Subject: [PATCH 3/6] Add test for multiepisode nfo parsing --- .../Parsers/EpisodeNfoProviderTests.cs | 20 +++++++++++++++++++ .../Test Data/Rising.nfo | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/Jellyfin.XbmcMetadata.Tests/Test Data/Rising.nfo diff --git a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs index 67b4b969a3..7f87163fdf 100644 --- a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs +++ b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs @@ -81,6 +81,26 @@ namespace Jellyfin.XbmcMetadata.Tests.Parsers Assert.Equal(new DateTime(2017, 10, 7, 14, 25, 47), item.DateCreated); } + [Fact] + public void Fetch_Valid_MultiEpisode_Succes() + { + var result = new MetadataResult() + { + Item = new Episode() + }; + + _parser.Fetch(result, "Test Data/Rising.nfo", CancellationToken.None); + + var item = result.Item; + Assert.Equal("Rising (1)", item.Name); + Assert.Equal(1, item.IndexNumber); + Assert.Equal(2, item.IndexNumberEnd); + Assert.Equal(1, item.ParentIndexNumber); + Assert.Equal("A new Stargate team embarks on a dangerous mission to a distant galaxy, where they discover a mythical lost city -- and a deadly new enemy.", item.Overview); + Assert.Equal(new DateTime(2004, 7, 16), item.PremiereDate); + Assert.Equal(2004, item.ProductionYear); + } + [Fact] public void Fetch_WithNullItem_ThrowsArgumentException() { diff --git a/tests/Jellyfin.XbmcMetadata.Tests/Test Data/Rising.nfo b/tests/Jellyfin.XbmcMetadata.Tests/Test Data/Rising.nfo new file mode 100644 index 0000000000..56250c09a8 --- /dev/null +++ b/tests/Jellyfin.XbmcMetadata.Tests/Test Data/Rising.nfo @@ -0,0 +1,20 @@ + + Rising (1) + 1 + 1 + 2004-07-16 + A new Stargate team embarks on a dangerous mission to a distant galaxy, where they discover a mythical lost city -- and a deadly new enemy. + https://artworks.thetvdb.com/banners/episodes/70851/25333.jpg + false + 8.0 + + + Rising (2) + 1 + 2 + 2004-07-16 + Sheppard tries to convince Weir to mount a rescue mission to free Colonel Sumner, Teyla, and the others captured by the Wraith. + https://artworks.thetvdb.com/banners/episodes/70851/25334.jpg + false + 7.9 + From c97edc96eb432940fb7352165c644bab4d6d0dda Mon Sep 17 00:00:00 2001 From: netpok Date: Mon, 8 Feb 2021 21:02:54 +0100 Subject: [PATCH 4/6] Fix typo Co-authored-by: Cody Robibero --- MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs index 3a852384af..aa3be587b4 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs @@ -51,7 +51,7 @@ namespace MediaBrowser.XbmcMetadata.Parsers // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions try { - // Extract episode details from the firs episodedetails block + // Extract episode details from the first episodedetails block using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader, settings)) { From 7ba53548a2ed07cf3b6b4692ddd79df9d85edf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Tue, 9 Feb 2021 07:21:08 +0100 Subject: [PATCH 5/6] Fix typos in nfo tests --- .../Parsers/EpisodeNfoProviderTests.cs | 4 ++-- .../Parsers/MovieNfoParserTests.cs | 2 +- .../Parsers/MusicAlbumNfoProviderTests.cs | 2 +- .../Parsers/MusicArtistNfoParserTests.cs | 2 +- .../Parsers/SeasonNfoProviderTests.cs | 2 +- .../Parsers/SeriesNfoParserTests.cs | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs index 7f87163fdf..3710d52c31 100644 --- a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs +++ b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/EpisodeNfoProviderTests.cs @@ -32,7 +32,7 @@ namespace Jellyfin.XbmcMetadata.Tests.Parsers } [Fact] - public void Fetch_Valid_Succes() + public void Fetch_Valid_Success() { var result = new MetadataResult() { @@ -82,7 +82,7 @@ namespace Jellyfin.XbmcMetadata.Tests.Parsers } [Fact] - public void Fetch_Valid_MultiEpisode_Succes() + public void Fetch_Valid_MultiEpisode_Success() { var result = new MetadataResult() { diff --git a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs index 765464eced..007a41a24a 100644 --- a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs +++ b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs @@ -30,7 +30,7 @@ namespace Jellyfin.XbmcMetadata.Tests.Parsers } [Fact] - public void Fetch_Valid_Succes() + public void Fetch_Valid_Success() { var result = new MetadataResult