jellyfin/MediaBrowser.Providers/TV/EpisodeXmlParser.cs

218 lines
7.2 KiB
C#
Raw Normal View History

2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities.TV;
2013-08-12 19:18:31 +00:00
using MediaBrowser.Controller.Persistence;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2013-02-21 20:26:35 +00:00
using MediaBrowser.Model.Logging;
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;
using System.Threading.Tasks;
2013-02-21 01:33:05 +00:00
using System.Xml;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.TV
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class EpisodeXmlParser
/// </summary>
public class EpisodeXmlParser : BaseItemXmlParser<Episode>
{
2013-08-12 19:18:31 +00:00
private readonly IItemRepository _itemRepo;
private Task _chaptersTask = null;
public EpisodeXmlParser(ILogger logger, IItemRepository itemRepo)
2013-02-21 20:26:35 +00:00
: base(logger)
{
2013-08-12 19:18:31 +00:00
_itemRepo = itemRepo;
}
public async Task FetchAsync(Episode item, string metadataFile, CancellationToken cancellationToken)
{
_chaptersTask = null;
Fetch(item, metadataFile, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
if (_chaptersTask != null)
{
await _chaptersTask.ConfigureAwait(false);
}
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>
/// <param name="item">The item.</param>
protected override void FetchDataFromXmlNode(XmlReader reader, Episode item)
{
switch (reader.Name)
{
2013-08-12 19:18:31 +00:00
case "Chapters":
2013-08-30 23:55:17 +00:00
//_chaptersTask = FetchChaptersFromXmlNode(item, reader.ReadSubtree(), _itemRepo, CancellationToken.None);
2013-08-12 19:18:31 +00:00
break;
2013-02-21 01:33:05 +00:00
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)
{
FetchDataFromXmlNode(subTree, item);
}
}
}
break;
case "filename":
{
var filename = reader.ReadElementContentAsString();
2013-02-21 01:33:05 +00:00
if (!string.IsNullOrWhiteSpace(filename))
{
// 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);
var seasonFolder = Path.GetDirectoryName(item.Path);
filename = Path.Combine(seasonFolder, "metadata", filename);
if (File.Exists(filename))
{
item.PrimaryImagePath = filename;
}
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 "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:
base.FetchDataFromXmlNode(reader, item);
break;
}
}
}
}