2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:56 +00:00
|
|
|
using System.IO;
|
2023-10-10 11:12:09 +00:00
|
|
|
using System.Text;
|
2013-08-12 19:18:31 +00:00
|
|
|
using System.Threading;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Xml;
|
2019-01-13 19:26:56 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2023-10-06 18:53:05 +00:00
|
|
|
using MediaBrowser.Controller.Extensions;
|
2021-02-09 11:42:55 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2019-01-13 19:26:56 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2014-06-30 03:04:50 +00:00
|
|
|
namespace MediaBrowser.XbmcMetadata.Parsers
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2020-02-23 11:11:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Nfo parser for episodes.
|
|
|
|
/// </summary>
|
2014-06-30 03:04:50 +00:00
|
|
|
public class EpisodeNfoParser : BaseNfoParser<Episode>
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2020-02-23 11:11:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="EpisodeNfoParser"/> class.
|
|
|
|
/// </summary>
|
2021-02-09 11:42:55 +00:00
|
|
|
/// <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>
|
2021-02-09 11:42:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-18 17:54:07 +00:00
|
|
|
/// <inheritdoc />
|
2017-06-12 07:02:09 +00:00
|
|
|
protected override void Fetch(MetadataResult<Episode> item, string metadataFile, XmlReaderSettings settings, CancellationToken cancellationToken)
|
|
|
|
{
|
2021-08-15 15:20:07 +00:00
|
|
|
item.ResetPeople();
|
2017-06-12 07:02:09 +00:00
|
|
|
|
2021-08-15 15:20:07 +00:00
|
|
|
var xmlFile = File.ReadAllText(metadataFile);
|
2017-06-12 07:02:09 +00:00
|
|
|
|
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;
|
2017-06-12 07:02:09 +00:00
|
|
|
|
2021-08-15 15:20:07 +00:00
|
|
|
if (index != -1)
|
|
|
|
{
|
|
|
|
xml = xmlFile.Substring(0, index + srch.Length);
|
|
|
|
xmlFile = xmlFile.Substring(index + srch.Length);
|
|
|
|
}
|
2017-06-12 07:02:09 +00:00
|
|
|
|
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)
|
2017-06-12 07:02:09 +00:00
|
|
|
{
|
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)
|
2017-06-12 07:02:09 +00:00
|
|
|
{
|
2021-08-15 15:20:07 +00:00
|
|
|
FetchDataFromXmlNode(reader, item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2017-06-12 07:02:09 +00:00
|
|
|
}
|
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
|
2023-10-10 11:12:09 +00:00
|
|
|
// Retrieves all title and plot tags from the rest of the nfo and concatenates them with the first episode
|
2021-08-15 15:20:07 +00:00
|
|
|
// This is needed because XBMC metadata uses multiple episodedetails blocks instead of episodenumberend tag
|
2023-10-10 11:12:09 +00:00
|
|
|
var name = new StringBuilder(item.Item.Name);
|
|
|
|
var overview = new StringBuilder(item.Item.Overview);
|
2021-08-15 15:20:07 +00:00
|
|
|
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
|
|
|
|
2023-10-10 11:12:09 +00:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2021-02-05 19:02:47 +00:00
|
|
|
{
|
2023-10-10 11:12:09 +00:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "name":
|
|
|
|
case "title":
|
|
|
|
case "localtitle":
|
|
|
|
name.Append(" / ").Append(reader.ReadElementContentAsString());
|
|
|
|
break;
|
|
|
|
case "episode":
|
|
|
|
{
|
|
|
|
if (int.TryParse(reader.ReadElementContentAsString(), out var num))
|
|
|
|
{
|
|
|
|
item.Item.IndexNumberEnd = Math.Max(num, item.Item.IndexNumberEnd ?? num);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "biography":
|
|
|
|
case "plot":
|
|
|
|
case "review":
|
|
|
|
overview.Append(" / ").Append(reader.ReadElementContentAsString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.Read();
|
2021-02-05 19:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-02 15:12:16 +00:00
|
|
|
}
|
2023-10-10 11:12:09 +00:00
|
|
|
|
|
|
|
item.Item.Name = name.ToString();
|
|
|
|
item.Item.Overview = overview.ToString();
|
2021-08-15 15:20:07 +00:00
|
|
|
}
|
|
|
|
catch (XmlException)
|
|
|
|
{
|
2017-06-12 07:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:54:07 +00:00
|
|
|
/// <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)
|
|
|
|
{
|
2014-06-30 03:04:50 +00:00
|
|
|
case "season":
|
2023-10-06 19:35:26 +00:00
|
|
|
if (reader.TryReadInt(out var seasonNumber))
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.ParentIndexNumber = seasonNumber;
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2014-06-30 03:04:50 +00:00
|
|
|
case "episode":
|
2023-10-06 19:35:26 +00:00
|
|
|
if (reader.TryReadInt(out var episodeNumber))
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.IndexNumber = episodeNumber;
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2014-06-30 03:04:50 +00:00
|
|
|
case "episodenumberend":
|
2023-10-06 19:35:26 +00:00
|
|
|
if (reader.TryReadInt(out var episodeNumberEnd))
|
2013-10-25 14:26:22 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.IndexNumberEnd = episodeNumberEnd;
|
2013-10-25 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2013-11-17 15:27:48 +00:00
|
|
|
case "airsbefore_episode":
|
2023-10-06 19:35:26 +00:00
|
|
|
case "displayepisode":
|
|
|
|
if (reader.TryReadInt(out var airsBeforeEpisode))
|
2013-11-15 21:31:33 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.AirsBeforeEpisodeNumber = airsBeforeEpisode;
|
2013-11-17 15:27:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2013-11-17 15:27:48 +00:00
|
|
|
case "airsafter_season":
|
2023-10-10 16:00:45 +00:00
|
|
|
case "displayafterseason":
|
2023-10-06 19:35:26 +00:00
|
|
|
if (reader.TryReadInt(out var airsAfterSeason))
|
2013-11-17 15:27:48 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.AirsAfterSeasonNumber = airsAfterSeason;
|
2013-11-17 15:27:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2013-11-17 15:27:48 +00:00
|
|
|
case "airsbefore_season":
|
2016-06-24 20:07:35 +00:00
|
|
|
case "displayseason":
|
2023-10-06 19:35:26 +00:00
|
|
|
if (reader.TryReadInt(out var airsBeforeSeason))
|
2016-06-24 20:07:35 +00:00
|
|
|
{
|
2023-10-06 19:35:26 +00:00
|
|
|
item.AirsBeforeSeasonNumber = airsBeforeSeason;
|
2016-06-24 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 19:35:26 +00:00
|
|
|
break;
|
2021-01-20 18:47:31 +00:00
|
|
|
case "showtitle":
|
2023-10-06 18:53:05 +00:00
|
|
|
item.SeriesName = reader.ReadNormalizedString();
|
|
|
|
break;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|