jellyfin/MediaBrowser.XbmcMetadata/Parsers/EpisodeNfoParser.cs

258 lines
9.7 KiB
C#
Raw Normal View History

using System;
using System.Globalization;
using System.IO;
2013-08-12 19:18:31 +00:00
using System.Threading;
2013-02-21 01:33:05 +00:00
using System.Xml;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.XbmcMetadata.Parsers
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Nfo parser for episodes.
/// </summary>
public class EpisodeNfoParser : BaseNfoParser<Episode>
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Initializes a new instance of the <see cref="EpisodeNfoParser"/> class.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger{BaseNfoParser}"/> interface.</param>
/// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="userDataManager">Instance of the <see cref="IUserDataManager"/> interface.</param>
2021-02-27 21:46:03 +00:00
/// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param>
public EpisodeNfoParser(
ILogger logger,
IConfigurationManager config,
IProviderManager providerManager,
IUserManager userManager,
2021-02-27 21:46:03 +00:00
IUserDataManager userDataManager,
IDirectoryService directoryService)
: base(logger, config, providerManager, userManager, userDataManager, directoryService)
2013-08-12 19:18:31 +00:00
{
2013-02-21 20:26:35 +00:00
}
/// <inheritdoc />
protected override void Fetch(MetadataResult<Episode> item, string metadataFile, XmlReaderSettings settings, CancellationToken cancellationToken)
{
2021-08-15 15:20:07 +00:00
item.ResetPeople();
2021-08-15 15:20:07 +00:00
var xmlFile = File.ReadAllText(metadataFile);
2021-08-15 15:20:07 +00:00
var srch = "</episodedetails>";
var index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
2021-02-05 19:02:47 +00:00
2021-08-15 15:20:07 +00:00
var xml = xmlFile;
2021-08-15 15:20:07 +00:00
if (index != -1)
{
xml = xmlFile.Substring(0, index + srch.Length);
xmlFile = xmlFile.Substring(index + srch.Length);
}
2021-08-15 15:20:07 +00:00
// 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 first episodedetails block
using (var stringReader = new StringReader(xml))
using (var reader = XmlReader.Create(stringReader, settings))
2019-02-02 15:12:16 +00:00
{
2021-08-15 15:20:07 +00:00
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
2021-08-15 15:20:07 +00:00
cancellationToken.ThrowIfCancellationRequested();
2019-02-02 15:12:16 +00:00
2021-08-15 15:20:07 +00:00
if (reader.NodeType == XmlNodeType.Element)
{
2021-08-15 15:20:07 +00:00
FetchDataFromXmlNode(reader, item);
}
else
{
reader.Read();
}
2018-09-12 17:26:21 +00:00
}
2021-08-15 15:20:07 +00:00
}
2021-02-05 19:02:47 +00:00
2021-08-15 15:20:07 +00:00
// 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))
2021-02-05 19:02:47 +00:00
{
2021-08-15 15:20:07 +00:00
reader.MoveToContent();
2021-02-08 17:56:06 +00:00
2021-08-15 15:20:07 +00:00
if (reader.ReadToDescendant("episode") && int.TryParse(reader.ReadElementContentAsString(), out var num))
2021-02-05 19:02:47 +00:00
{
2021-08-15 15:20:07 +00:00
item.Item.IndexNumberEnd = Math.Max(num, item.Item.IndexNumberEnd ?? num);
2021-02-05 19:02:47 +00:00
}
}
2019-02-02 15:12:16 +00:00
}
2021-08-15 15:20:07 +00:00
}
catch (XmlException)
{
}
}
/// <inheritdoc />
2015-08-02 17:31:08 +00:00
protected override void FetchDataFromXmlNode(XmlReader reader, MetadataResult<Episode> itemResult)
2013-02-21 01:33:05 +00:00
{
2015-06-29 01:10:45 +00:00
var item = itemResult.Item;
2013-02-21 01:33:05 +00:00
switch (reader.Name)
{
case "season":
2013-02-21 01:33:05 +00:00
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
2013-02-21 01:33:05 +00:00
{
item.ParentIndexNumber = num;
}
}
2013-02-21 01:33:05 +00:00
break;
}
case "episode":
2013-02-21 01:33:05 +00:00
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
2013-02-21 01:33:05 +00:00
{
item.IndexNumber = num;
}
}
2013-02-21 01:33:05 +00:00
break;
}
case "episodenumberend":
2013-10-25 14:26:22 +00:00
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
2013-10-25 14:26:22 +00:00
{
item.IndexNumberEnd = num;
}
}
2013-10-25 14:26:22 +00:00
break;
}
case "airsbefore_episode":
2013-11-15 21:31:33 +00:00
{
var val = reader.ReadElementContentAsString();
2013-11-15 21:31:33 +00:00
if (!string.IsNullOrWhiteSpace(val))
2013-11-15 21:31:33 +00:00
{
2020-11-18 13:46:14 +00:00
// int.TryParse is local aware, so it can be problematic, force us culture
2021-09-26 14:14:36 +00:00
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
2013-11-15 21:31:33 +00:00
{
item.AirsBeforeEpisodeNumber = rval;
2013-11-15 21:31:33 +00:00
}
}
break;
}
case "airsafter_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
2020-11-18 13:46:14 +00:00
// int.TryParse is local aware, so it can be problematic, force us culture
2021-09-26 14:14:36 +00:00
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsAfterSeasonNumber = rval;
}
}
break;
}
case "airsbefore_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
2020-11-18 13:46:14 +00:00
// int.TryParse is local aware, so it can be problematic, force us culture
2021-09-26 14:14:36 +00:00
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsBeforeSeasonNumber = rval;
}
}
2013-11-15 21:31:33 +00:00
break;
}
2016-06-24 20:07:35 +00:00
case "displayseason":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
2020-11-18 13:46:14 +00:00
// int.TryParse is local aware, so it can be problematic, force us culture
2021-09-26 14:14:36 +00:00
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
2016-06-24 20:07:35 +00:00
{
2016-08-01 18:16:07 +00:00
item.AirsBeforeSeasonNumber = rval;
2016-06-24 20:07:35 +00:00
}
}
break;
}
case "displayepisode":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
2020-11-18 13:46:14 +00:00
// int.TryParse is local aware, so it can be problematic, force us culture
2021-09-26 14:14:36 +00:00
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
2016-06-24 20:07:35 +00:00
{
2016-08-01 18:16:07 +00:00
item.AirsBeforeEpisodeNumber = rval;
2016-06-24 20:07:35 +00:00
}
}
break;
}
2021-01-20 18:47:31 +00:00
case "showtitle":
{
2021-01-23 11:02:22 +00:00
var showtitle = reader.ReadElementContentAsString();
2021-01-20 18:47:31 +00:00
2021-01-23 11:02:22 +00:00
if (!string.IsNullOrWhiteSpace(showtitle))
{
item.SeriesName = showtitle;
}
2021-01-20 18:47:31 +00:00
2021-01-23 11:02:22 +00:00
break;
2021-01-20 18:47:31 +00:00
}
2013-02-21 01:33:05 +00:00
default:
2015-06-29 01:10:45 +00:00
base.FetchDataFromXmlNode(reader, itemResult);
2013-02-21 01:33:05 +00:00
break;
}
}
}
}