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