jellyfin-server/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs

723 lines
25 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Xml;
using MediaBrowser.Model.Entities;
using System;
2012-07-12 06:55:27 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace MediaBrowser.Controller.Providers
2012-07-12 06:55:27 +00:00
{
/// <summary>
/// Provides a base class for parsing metadata xml
/// </summary>
public class BaseItemXmlParser<T>
2012-07-12 06:55:27 +00:00
where T : BaseItem, new()
{
/// <summary>
/// Fetches metadata for an item from one xml file
/// </summary>
2012-08-20 21:48:11 +00:00
public void Fetch(T item, string metadataFile)
2012-07-12 06:55:27 +00:00
{
// Use XmlReader for best performance
2012-08-21 16:33:38 +00:00
using (XmlReader reader = XmlReader.Create(metadataFile))
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-19 16:54:41 +00:00
// Loop through each element
2012-08-20 21:48:11 +00:00
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
2012-08-20 21:48:11 +00:00
FetchDataFromXmlNode(reader, item);
}
}
}
2012-07-12 06:55:27 +00:00
}
/// <summary>
/// Fetches metadata from one Xml Element
/// </summary>
2012-08-20 21:48:11 +00:00
protected virtual void FetchDataFromXmlNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
2012-07-12 06:55:27 +00:00
{
// DateCreated
2012-07-12 06:55:27 +00:00
case "Added":
DateTime added;
2012-08-20 21:48:11 +00:00
if (DateTime.TryParse(reader.ReadElementContentAsString() ?? string.Empty, out added))
2012-07-12 06:55:27 +00:00
{
2012-09-04 19:23:15 +00:00
item.DateCreated = added.ToUniversalTime();
2012-07-12 06:55:27 +00:00
}
break;
// DisplayMediaType
2012-07-12 06:55:27 +00:00
case "Type":
{
2012-08-20 21:48:11 +00:00
item.DisplayMediaType = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
switch (item.DisplayMediaType.ToLower())
{
case "blu-ray":
item.DisplayMediaType = VideoType.BluRay.ToString();
break;
case "dvd":
item.DisplayMediaType = VideoType.DVD.ToString();
break;
case "":
item.DisplayMediaType = null;
break;
}
break;
}
// TODO: Do we still need this?
2012-07-12 06:55:27 +00:00
case "banner":
2012-08-20 21:48:11 +00:00
item.BannerImagePath = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "LocalTitle":
2012-08-20 21:48:11 +00:00
item.Name = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "SortTitle":
2012-08-20 21:48:11 +00:00
item.SortName = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "Overview":
case "Description":
2012-08-20 21:48:11 +00:00
item.Overview = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "TagLine":
{
var list = item.Taglines ?? new List<string>();
2012-08-20 21:48:11 +00:00
var tagline = reader.ReadElementContentAsString();
if (!list.Contains(tagline))
{
list.Add(tagline);
}
item.Taglines = list;
break;
}
case "TagLines":
{
2012-08-20 21:48:11 +00:00
FetchFromTaglinesNode(reader.ReadSubtree(), item);
break;
}
2012-07-12 06:55:27 +00:00
case "ContentRating":
case "MPAARating":
2012-08-20 21:48:11 +00:00
item.OfficialRating = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "CustomRating":
2012-08-20 21:48:11 +00:00
item.CustomRating = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
2012-09-02 05:30:42 +00:00
case "CustomPin":
item.CustomPin = reader.ReadElementContentAsString();
break;
case "Runtime":
case "RunningTime":
{
2012-08-20 21:48:11 +00:00
string text = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(text))
{
int runtime;
if (int.TryParse(text.Split(' ')[0], out runtime))
{
item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
}
}
break;
}
2012-07-12 06:55:27 +00:00
case "Genre":
{
var list = item.Genres ?? new List<string>();
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|'));
2012-07-12 06:55:27 +00:00
item.Genres = list;
2012-07-12 06:55:27 +00:00
break;
}
case "AspectRatio":
2012-08-20 21:48:11 +00:00
item.AspectRatio = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "Network":
{
var list = item.Studios ?? new List<string>();
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|'));
2012-07-12 06:55:27 +00:00
item.Studios = list;
2012-07-12 06:55:27 +00:00
break;
}
case "Director":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Director" }))
{
item.AddPerson(p);
}
2012-07-12 06:55:27 +00:00
break;
}
case "Writer":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Writer" }))
{
item.AddPerson(p);
}
2012-07-12 06:55:27 +00:00
break;
}
case "Actors":
case "GuestStars":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Actor" }))
{
item.AddPerson(p);
}
2012-07-12 06:55:27 +00:00
break;
}
case "Trailer":
2012-08-20 21:48:11 +00:00
item.TrailerUrl = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
break;
case "ProductionYear":
{
2012-08-20 21:48:11 +00:00
string val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
2012-07-12 06:55:27 +00:00
{
int ProductionYear;
if (int.TryParse(val, out ProductionYear) && ProductionYear > 1850)
{
item.ProductionYear = ProductionYear;
}
2012-07-12 06:55:27 +00:00
}
break;
}
case "Rating":
case "IMDBrating":
2012-08-20 21:48:11 +00:00
string rating = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(rating))
{
float val;
if (float.TryParse(rating, out val))
{
item.UserRating = val;
}
}
break;
2012-08-04 13:33:23 +00:00
case "FirstAired":
{
2012-08-20 21:48:11 +00:00
string firstAired = reader.ReadElementContentAsString();
2012-08-04 13:33:23 +00:00
if (!string.IsNullOrWhiteSpace(firstAired))
{
DateTime airDate;
if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
{
2012-09-04 19:23:15 +00:00
item.PremiereDate = airDate.ToUniversalTime();
2012-08-04 13:33:23 +00:00
item.ProductionYear = airDate.Year;
}
}
break;
}
case "TMDbId":
2012-08-20 21:48:11 +00:00
string tmdb = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(tmdb))
{
item.SetProviderId(MetadataProviders.Tmdb, tmdb);
}
break;
case "TVcomId":
2012-08-20 21:48:11 +00:00
string TVcomId = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(TVcomId))
{
item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
}
break;
case "IMDB_ID":
case "IMDB":
case "IMDbId":
2012-08-20 21:48:11 +00:00
string IMDbId = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(IMDbId))
{
item.SetProviderId(MetadataProviders.Imdb, IMDbId);
}
break;
case "Genres":
2012-08-20 21:48:11 +00:00
FetchFromGenresNode(reader.ReadSubtree(), item);
break;
case "Persons":
2012-08-20 21:48:11 +00:00
FetchDataFromPersonsNode(reader.ReadSubtree(), item);
break;
case "ParentalRating":
2012-08-20 21:48:11 +00:00
FetchFromParentalRatingNode(reader.ReadSubtree(), item);
break;
case "Studios":
2012-08-20 21:48:11 +00:00
FetchFromStudiosNode(reader.ReadSubtree(), item);
break;
2012-07-12 06:55:27 +00:00
case "MediaInfo":
{
var video = item as Video;
if (video != null)
{
2012-08-20 21:48:11 +00:00
FetchMediaInfo(reader.ReadSubtree(), video);
}
break;
}
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
}
2012-08-20 21:48:11 +00:00
private void FetchMediaInfo(XmlReader reader, Video item)
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Audio":
{
2012-08-20 21:48:11 +00:00
AudioStream stream = FetchMediaInfoAudio(reader.ReadSubtree());
2012-07-12 06:55:27 +00:00
List<AudioStream> streams = item.AudioStreams ?? new List<AudioStream>();
streams.Add(stream);
item.AudioStreams = streams;
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
case "Video":
2012-08-20 21:48:11 +00:00
FetchMediaInfoVideo(reader.ReadSubtree(), item);
break;
2012-07-12 06:55:27 +00:00
case "Subtitle":
2012-08-22 12:56:44 +00:00
{
SubtitleStream stream = FetchMediaInfoSubtitles(reader.ReadSubtree());
List<SubtitleStream> streams = item.Subtitles ?? new List<SubtitleStream>();
2012-08-22 12:56:44 +00:00
streams.Add(stream);
item.Subtitles = streams;
break;
}
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
2012-08-20 21:48:11 +00:00
private AudioStream FetchMediaInfoAudio(XmlReader reader)
2012-07-12 06:55:27 +00:00
{
AudioStream stream = new AudioStream();
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Default":
2012-08-20 21:48:11 +00:00
stream.IsDefault = reader.ReadElementContentAsString() == "True";
break;
2012-08-25 15:03:16 +00:00
case "SamplingRate":
stream.SampleRate = reader.ReadIntSafe();
break;
case "BitRate":
2012-08-20 21:48:11 +00:00
stream.BitRate = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Channels":
2012-08-20 21:48:11 +00:00
stream.Channels = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Language":
2012-08-20 21:48:11 +00:00
stream.Language = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
case "Codec":
2012-08-20 21:48:11 +00:00
stream.Codec = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
return stream;
}
2012-08-20 21:48:11 +00:00
private void FetchMediaInfoVideo(XmlReader reader, Video item)
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Width":
2012-08-20 21:48:11 +00:00
item.Width = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Height":
2012-08-20 21:48:11 +00:00
item.Height = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "BitRate":
2012-08-20 21:48:11 +00:00
item.BitRate = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "FrameRate":
item.FrameRate = reader.ReadFloatSafe();
break;
2012-07-12 06:55:27 +00:00
case "ScanType":
2012-08-20 21:48:11 +00:00
item.ScanType = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
case "Duration":
2012-08-20 21:48:11 +00:00
item.RunTimeTicks = TimeSpan.FromMinutes(reader.ReadIntSafe()).Ticks;
break;
2012-07-12 06:55:27 +00:00
case "DurationSeconds":
2012-08-20 21:48:11 +00:00
int seconds = reader.ReadIntSafe();
if (seconds > 0)
{
item.RunTimeTicks = TimeSpan.FromSeconds(seconds).Ticks;
}
break;
2012-07-12 06:55:27 +00:00
case "Codec":
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
string videoCodec = reader.ReadElementContentAsString();
switch (videoCodec.ToLower())
{
case "sorenson h.263":
item.Codec = "Sorenson H263";
break;
case "h.262":
item.Codec = "MPEG-2 Video";
break;
case "h.264":
item.Codec = "AVC";
break;
default:
item.Codec = videoCodec;
break;
}
break;
2012-07-12 06:55:27 +00:00
}
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
2012-08-22 12:56:44 +00:00
private SubtitleStream FetchMediaInfoSubtitles(XmlReader reader)
2012-07-12 06:55:27 +00:00
{
2012-08-22 12:56:44 +00:00
SubtitleStream stream = new SubtitleStream();
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Language":
2012-08-22 12:56:44 +00:00
stream.Language = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
2012-08-22 12:56:44 +00:00
case "Default":
stream.IsDefault = reader.ReadElementContentAsString() == "True";
break;
case "Forced":
stream.IsForced = reader.ReadElementContentAsString() == "True";
break;
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
2012-08-22 12:56:44 +00:00
return stream;
2012-07-12 06:55:27 +00:00
}
2012-08-20 21:48:11 +00:00
private void FetchFromTaglinesNode(XmlReader reader, T item)
{
var list = item.Taglines ?? new List<string>();
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Tagline":
{
2012-08-20 21:48:11 +00:00
string val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val) && !list.Contains(val))
{
list.Add(val);
}
break;
}
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
}
}
item.Taglines = list;
}
2012-08-20 21:48:11 +00:00
private void FetchFromGenresNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
var list = item.Genres ?? new List<string>();
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Genre":
{
2012-08-20 21:48:11 +00:00
string genre = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
if (!string.IsNullOrWhiteSpace(genre))
{
list.Add(genre);
}
break;
}
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
2012-07-12 06:55:27 +00:00
item.Genres = list;
}
2012-08-20 21:48:11 +00:00
private void FetchDataFromPersonsNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Person":
{
item.AddPerson(GetPersonFromXmlNode(reader.ReadSubtree()));
break;
}
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
2012-08-20 21:48:11 +00:00
private void FetchFromStudiosNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
var list = item.Studios ?? new List<string>();
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Studio":
{
2012-08-20 21:48:11 +00:00
string studio = reader.ReadElementContentAsString();
2012-07-12 06:55:27 +00:00
if (!string.IsNullOrWhiteSpace(studio))
{
list.Add(studio);
}
break;
}
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
item.Studios = list;
}
2012-08-20 21:48:11 +00:00
private void FetchFromParentalRatingNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Value":
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
string ratingString = reader.ReadElementContentAsString();
int rating = 7;
if (!string.IsNullOrWhiteSpace(ratingString))
{
int.TryParse(ratingString, out rating);
}
switch (rating)
{
case -1:
item.OfficialRating = "NR";
break;
case 0:
item.OfficialRating = "UR";
break;
case 1:
item.OfficialRating = "G";
break;
case 3:
item.OfficialRating = "PG";
break;
case 4:
item.OfficialRating = "PG-13";
break;
case 5:
item.OfficialRating = "NC-17";
break;
case 6:
item.OfficialRating = "R";
break;
default:
break;
}
break;
2012-07-12 06:55:27 +00:00
}
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
2012-08-20 21:48:11 +00:00
private PersonInfo GetPersonFromXmlNode(XmlReader reader)
2012-07-12 06:55:27 +00:00
{
2012-07-12 17:09:35 +00:00
PersonInfo person = new PersonInfo();
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
reader.MoveToContent();
2012-08-20 21:48:11 +00:00
while (reader.Read())
2012-07-12 06:55:27 +00:00
{
if (reader.NodeType == XmlNodeType.Element)
2012-07-12 06:55:27 +00:00
{
switch (reader.Name)
{
case "Name":
2012-08-20 21:48:11 +00:00
person.Name = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
case "Type":
2012-08-20 21:48:11 +00:00
person.Type = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
case "Role":
2012-08-20 21:48:11 +00:00
person.Overview = reader.ReadElementContentAsString();
break;
2012-07-12 06:55:27 +00:00
default:
2012-08-20 21:48:11 +00:00
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
2012-07-12 06:55:27 +00:00
return person;
}
protected IEnumerable<string> GetSplitValues(string value, char deliminator)
{
value = (value ?? string.Empty).Trim(deliminator);
return string.IsNullOrWhiteSpace(value) ? new string[] { } : value.Split(deliminator);
2012-07-12 06:55:27 +00:00
}
}
}