jellyfin/MediaBrowser.LocalMetadata/Parsers/EpisodeXmlParser.cs

272 lines
9.3 KiB
C#
Raw Normal View History

2015-06-29 01:10:45 +00:00
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
2014-02-09 04:52:52 +00:00
using System.Collections.Generic;
using System.Globalization;
2013-02-21 01:33:05 +00:00
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;
2017-05-26 06:48:54 +00:00
2016-10-25 19:02:04 +00:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2016-10-30 07:02:23 +00:00
using MediaBrowser.Model.Xml;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.LocalMetadata.Parsers
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class EpisodeXmlParser
/// </summary>
public class EpisodeXmlParser : BaseItemXmlParser<Episode>
{
2014-02-09 04:52:52 +00:00
private List<LocalImageInfo> _imagesFound;
2015-10-04 03:38:46 +00:00
private readonly IFileSystem _fileSystem;
2014-02-09 04:52:52 +00:00
2016-10-30 07:02:23 +00:00
public EpisodeXmlParser(ILogger logger, IFileSystem fileSystem, IProviderManager providerManager, IXmlReaderSettingsFactory xmlSettings)
: base(logger, providerManager, xmlSettings, fileSystem)
2013-02-21 20:26:35 +00:00
{
2015-10-04 03:38:46 +00:00
_fileSystem = fileSystem;
2013-08-12 19:18:31 +00:00
}
2014-02-21 05:04:11 +00:00
private string _xmlPath;
2015-06-29 01:10:45 +00:00
public void Fetch(MetadataResult<Episode> item,
List<LocalImageInfo> images,
string metadataFile,
CancellationToken cancellationToken)
2013-08-12 19:18:31 +00:00
{
2014-02-09 04:52:52 +00:00
_imagesFound = images;
2014-02-21 05:04:11 +00:00
_xmlPath = metadataFile;
2014-02-09 04:52:52 +00:00
Fetch(item, metadataFile, cancellationToken);
2013-02-21 20:26:35 +00:00
}
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
2013-02-21 01:33:05 +00:00
/// <summary>
/// Fetches the data from XML node.
/// </summary>
/// <param name="reader">The reader.</param>
2015-06-29 01:10:45 +00:00
/// <param name="result">The result.</param>
protected override void FetchDataFromXmlNode(XmlReader reader, MetadataResult<Episode> result)
2013-02-21 01:33:05 +00:00
{
2015-06-29 01:10:45 +00:00
var item = result.Item;
2013-02-21 01:33:05 +00:00
switch (reader.Name)
{
case "Episode":
2013-08-12 19:18:31 +00:00
2013-02-21 01:33:05 +00:00
//MB generated metadata is within an "Episode" node
using (var subTree = reader.ReadSubtree())
{
subTree.MoveToContent();
// Loop through each element
while (subTree.Read())
{
if (subTree.NodeType == XmlNodeType.Element)
{
2015-06-29 01:10:45 +00:00
FetchDataFromXmlNode(subTree, result);
2013-02-21 01:33:05 +00:00
}
}
}
break;
case "filename":
{
var filename = reader.ReadElementContentAsString();
2013-02-21 01:33:05 +00:00
if (!string.IsNullOrWhiteSpace(filename))
{
2014-02-09 04:52:52 +00:00
// Strip off everything but the filename. Some metadata tools like MetaBrowser v1.0 will have an 'episodes' prefix
// even though it's actually using the metadata folder.
filename = Path.GetFileName(filename);
2013-02-21 01:33:05 +00:00
2017-05-04 18:14:45 +00:00
var parentFolder = _fileSystem.GetDirectoryName(_xmlPath);
2014-02-21 05:04:11 +00:00
filename = Path.Combine(parentFolder, filename);
2015-10-04 03:38:46 +00:00
var file = _fileSystem.GetFileInfo(filename);
2014-02-09 04:52:52 +00:00
if (file.Exists)
{
_imagesFound.Add(new LocalImageInfo
{
Type = ImageType.Primary,
FileInfo = file
});
}
2013-02-21 01:33:05 +00:00
}
break;
}
case "SeasonNumber":
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
int num;
if (int.TryParse(number, out num))
{
item.ParentIndexNumber = num;
}
}
break;
}
case "EpisodeNumber":
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
int num;
if (int.TryParse(number, out num))
{
item.IndexNumber = num;
}
}
break;
}
2013-10-25 14:26:22 +00:00
case "EpisodeNumberEnd":
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
int num;
if (int.TryParse(number, out num))
{
item.IndexNumberEnd = num;
}
}
break;
}
case "absolute_number":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
int rval;
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
{
item.AbsoluteEpisodeNumber = rval;
}
}
break;
}
2013-12-08 18:07:45 +00:00
case "DVD_episodenumber":
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
float num;
2013-12-08 18:07:45 +00:00
if (float.TryParse(number, NumberStyles.Any, UsCulture, out num))
2013-12-08 18:07:45 +00:00
{
item.DvdEpisodeNumber = num;
2013-12-08 18:07:45 +00:00
}
}
break;
}
case "DVD_season":
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
float num;
2013-12-08 18:07:45 +00:00
if (float.TryParse(number, NumberStyles.Any, UsCulture, out num))
2013-12-08 18:07:45 +00:00
{
item.DvdSeasonNumber = Convert.ToInt32(num);
2013-12-08 18:07:45 +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
{
int rval;
2013-11-15 21:31:33 +00:00
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out 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))
{
int rval;
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
{
item.AirsAfterSeasonNumber = rval;
}
}
break;
}
case "airsbefore_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
int rval;
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
{
item.AirsBeforeSeasonNumber = rval;
}
}
2013-11-15 21:31:33 +00:00
break;
}
2013-02-21 01:33:05 +00:00
case "EpisodeName":
{
var name = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(name))
{
item.Name = name;
}
break;
}
default:
2015-06-29 01:10:45 +00:00
base.FetchDataFromXmlNode(reader, result);
2013-02-21 01:33:05 +00:00
break;
}
}
}
}