You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Controller/Xml/BaseItemXmlParser.cs

722 lines
25 KiB

13 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Xml
{
/// <summary>
/// Provides a base class for parsing metadata xml
/// </summary>
public class BaseItemXmlParser<T>
13 years ago
where T : BaseItem, new()
{
/// <summary>
/// Fetches metadata for an item from one xml file
/// </summary>
public void Fetch(T item, string metadataFile)
13 years ago
{
// Use XmlReader for best performance
using (XmlReader reader = XmlReader.Create(metadataFile))
13 years ago
{
reader.MoveToContent();
// Loop through each element
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
FetchDataFromXmlNode(reader, item);
}
}
}
13 years ago
}
/// <summary>
/// Fetches metadata from one Xml Element
/// </summary>
protected virtual void FetchDataFromXmlNode(XmlReader reader, T item)
13 years ago
{
switch (reader.Name)
13 years ago
{
// DateCreated
13 years ago
case "Added":
DateTime added;
if (DateTime.TryParse(reader.ReadElementContentAsString() ?? string.Empty, out added))
13 years ago
{
item.DateCreated = added;
}
break;
// DisplayMediaType
13 years ago
case "Type":
{
item.DisplayMediaType = reader.ReadElementContentAsString();
13 years ago
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?
13 years ago
case "banner":
item.BannerImagePath = reader.ReadElementContentAsString();
13 years ago
break;
case "LocalTitle":
item.Name = reader.ReadElementContentAsString();
13 years ago
break;
case "SortTitle":
item.SortName = reader.ReadElementContentAsString();
13 years ago
break;
case "Overview":
case "Description":
item.Overview = reader.ReadElementContentAsString();
13 years ago
break;
case "TagLine":
{
var list = item.Taglines ?? new List<string>();
var tagline = reader.ReadElementContentAsString();
if (!list.Contains(tagline))
{
list.Add(tagline);
}
item.Taglines = list;
break;
}
case "TagLines":
{
FetchFromTaglinesNode(reader.ReadSubtree(), item);
break;
}
13 years ago
case "ContentRating":
case "MPAARating":
item.OfficialRating = reader.ReadElementContentAsString();
13 years ago
break;
case "CustomRating":
item.CustomRating = reader.ReadElementContentAsString();
13 years ago
break;
case "CustomPin":
item.CustomPin = reader.ReadElementContentAsString();
break;
case "Runtime":
case "RunningTime":
{
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;
}
13 years ago
case "Genre":
{
var list = item.Genres ?? new List<string>();
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|'));
13 years ago
item.Genres = list;
13 years ago
break;
}
case "AspectRatio":
item.AspectRatio = reader.ReadElementContentAsString();
13 years ago
break;
case "Network":
{
var list = item.Studios ?? new List<string>();
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|'));
13 years ago
item.Studios = list;
13 years ago
break;
}
case "Director":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Director" }))
{
item.AddPerson(p);
}
13 years ago
break;
}
case "Writer":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Writer" }))
{
item.AddPerson(p);
}
13 years ago
break;
}
case "Actors":
case "GuestStars":
{
foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Actor" }))
{
item.AddPerson(p);
}
13 years ago
break;
}
case "Trailer":
item.TrailerUrl = reader.ReadElementContentAsString();
13 years ago
break;
case "ProductionYear":
{
string val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
13 years ago
{
int ProductionYear;
if (int.TryParse(val, out ProductionYear) && ProductionYear > 1850)
{
item.ProductionYear = ProductionYear;
}
13 years ago
}
break;
}
case "Rating":
case "IMDBrating":
string rating = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(rating))
{
float val;
if (float.TryParse(rating, out val))
{
item.UserRating = val;
}
}
break;
case "FirstAired":
{
string firstAired = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(firstAired))
{
DateTime airDate;
if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
{
item.PremiereDate = airDate;
item.ProductionYear = airDate.Year;
}
}
break;
}
case "TMDbId":
string tmdb = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(tmdb))
{
item.SetProviderId(MetadataProviders.Tmdb, tmdb);
}
break;
case "TVcomId":
string TVcomId = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(TVcomId))
{
item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
}
break;
case "IMDB_ID":
case "IMDB":
case "IMDbId":
string IMDbId = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(IMDbId))
{
item.SetProviderId(MetadataProviders.Imdb, IMDbId);
}
break;
case "Genres":
FetchFromGenresNode(reader.ReadSubtree(), item);
break;
case "Persons":
FetchDataFromPersonsNode(reader.ReadSubtree(), item);
break;
case "ParentalRating":
FetchFromParentalRatingNode(reader.ReadSubtree(), item);
break;
case "Studios":
FetchFromStudiosNode(reader.ReadSubtree(), item);
break;
13 years ago
case "MediaInfo":
{
var video = item as Video;
if (video != null)
{
FetchMediaInfo(reader.ReadSubtree(), video);
}
break;
}
13 years ago
default:
reader.Skip();
13 years ago
break;
}
}
private void FetchMediaInfo(XmlReader reader, Video item)
13 years ago
{
reader.MoveToContent();
13 years ago
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Audio":
{
AudioStream stream = FetchMediaInfoAudio(reader.ReadSubtree());
13 years ago
List<AudioStream> streams = item.AudioStreams ?? new List<AudioStream>();
streams.Add(stream);
item.AudioStreams = streams;
13 years ago
break;
}
13 years ago
case "Video":
FetchMediaInfoVideo(reader.ReadSubtree(), item);
break;
13 years ago
case "Subtitle":
{
SubtitleStream stream = FetchMediaInfoSubtitles(reader.ReadSubtree());
List<SubtitleStream> streams = item.Subtitles ?? new List<SubtitleStream>();
streams.Add(stream);
item.Subtitles = streams;
break;
}
13 years ago
default:
reader.Skip();
break;
}
13 years ago
}
}
}
private AudioStream FetchMediaInfoAudio(XmlReader reader)
13 years ago
{
AudioStream stream = new AudioStream();
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Default":
stream.IsDefault = reader.ReadElementContentAsString() == "True";
break;
case "SamplingRate":
stream.SampleRate = reader.ReadIntSafe();
break;
case "BitRate":
stream.BitRate = reader.ReadIntSafe();
break;
13 years ago
case "Channels":
stream.Channels = reader.ReadIntSafe();
break;
13 years ago
case "Language":
stream.Language = reader.ReadElementContentAsString();
break;
13 years ago
case "Codec":
stream.Codec = reader.ReadElementContentAsString();
break;
13 years ago
default:
reader.Skip();
13 years ago
break;
}
13 years ago
}
}
return stream;
}
private void FetchMediaInfoVideo(XmlReader reader, Video item)
13 years ago
{
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Width":
item.Width = reader.ReadIntSafe();
break;
13 years ago
case "Height":
item.Height = reader.ReadIntSafe();
break;
13 years ago
case "BitRate":
item.BitRate = reader.ReadIntSafe();
break;
13 years ago
case "FrameRate":
item.FrameRate = reader.ReadFloatSafe();
break;
13 years ago
case "ScanType":
item.ScanType = reader.ReadElementContentAsString();
break;
13 years ago
case "Duration":
item.RunTimeTicks = TimeSpan.FromMinutes(reader.ReadIntSafe()).Ticks;
break;
13 years ago
case "DurationSeconds":
int seconds = reader.ReadIntSafe();
if (seconds > 0)
{
item.RunTimeTicks = TimeSpan.FromSeconds(seconds).Ticks;
}
break;
13 years ago
case "Codec":
13 years ago
{
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;
13 years ago
}
default:
reader.Skip();
13 years ago
break;
}
13 years ago
}
}
}
private SubtitleStream FetchMediaInfoSubtitles(XmlReader reader)
13 years ago
{
SubtitleStream stream = new SubtitleStream();
reader.MoveToContent();
13 years ago
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Language":
stream.Language = reader.ReadElementContentAsString();
break;
13 years ago
case "Default":
stream.IsDefault = reader.ReadElementContentAsString() == "True";
break;
case "Forced":
stream.IsForced = reader.ReadElementContentAsString() == "True";
break;
13 years ago
default:
reader.Skip();
break;
}
13 years ago
}
}
return stream;
13 years ago
}
private void FetchFromTaglinesNode(XmlReader reader, T item)
{
var list = item.Taglines ?? new List<string>();
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Tagline":
{
string val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val) && !list.Contains(val))
{
list.Add(val);
}
break;
}
default:
reader.Skip();
break;
}
}
}
item.Taglines = list;
}
private void FetchFromGenresNode(XmlReader reader, T item)
13 years ago
{
var list = item.Genres ?? new List<string>();
13 years ago
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Genre":
{
string genre = reader.ReadElementContentAsString();
13 years ago
if (!string.IsNullOrWhiteSpace(genre))
{
list.Add(genre);
}
break;
}
13 years ago
default:
reader.Skip();
break;
}
13 years ago
}
}
13 years ago
item.Genres = list;
}
private void FetchDataFromPersonsNode(XmlReader reader, T item)
13 years ago
{
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Person":
{
item.AddPerson(GetPersonFromXmlNode(reader.ReadSubtree()));
break;
}
13 years ago
default:
reader.Skip();
13 years ago
break;
}
13 years ago
}
}
}
private void FetchFromStudiosNode(XmlReader reader, T item)
13 years ago
{
var list = item.Studios ?? new List<string>();
13 years ago
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Studio":
{
string studio = reader.ReadElementContentAsString();
13 years ago
if (!string.IsNullOrWhiteSpace(studio))
{
list.Add(studio);
}
break;
}
13 years ago
default:
reader.Skip();
break;
}
13 years ago
}
}
item.Studios = list;
}
private void FetchFromParentalRatingNode(XmlReader reader, T item)
13 years ago
{
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Value":
13 years ago
{
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;
13 years ago
}
default:
reader.Skip();
break;
}
13 years ago
}
}
}
private PersonInfo GetPersonFromXmlNode(XmlReader reader)
13 years ago
{
PersonInfo person = new PersonInfo();
13 years ago
reader.MoveToContent();
while (reader.Read())
13 years ago
{
if (reader.NodeType == XmlNodeType.Element)
13 years ago
{
switch (reader.Name)
{
case "Name":
person.Name = reader.ReadElementContentAsString();
break;
13 years ago
case "Type":
person.Type = reader.ReadElementContentAsString();
break;
13 years ago
case "Role":
person.Overview = reader.ReadElementContentAsString();
break;
13 years ago
default:
reader.Skip();
break;
}
13 years ago
}
}
13 years ago
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);
13 years ago
}
}
}