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

775 lines
27 KiB
C#
Raw Normal View History

2012-07-12 06:55:27 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2012-07-12 06:55:27 +00:00
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>
2012-07-12 06:55:27 +00:00
where T : BaseItem, new()
{
/// <summary>
/// Fetches metadata for an item from one xml file
/// </summary>
public async Task Fetch(T item, string metadataFile)
2012-07-12 06:55:27 +00:00
{
// Use XmlReader for best performance
using (XmlReader reader = XmlReader.Create(metadataFile, new XmlReaderSettings() { Async = true }))
2012-07-12 06:55:27 +00:00
{
await reader.MoveToContentAsync();
2012-08-19 16:54:41 +00:00
// Loop through each element
while (await reader.ReadAsync())
{
if (reader.NodeType == XmlNodeType.Element)
{
FetchDataFromXmlNode(reader, item);
}
}
}
2012-07-12 06:55:27 +00:00
// If dates weren't supplied in metadata, use values from the xml file
if (item.DateCreated == DateTime.MinValue)
{
item.DateCreated = File.GetCreationTime(metadataFile);
}
2012-07-12 06:55:27 +00:00
if (item.DateModified == DateTime.MinValue)
{
item.DateModified = File.GetLastWriteTime(metadataFile);
}
2012-07-12 06:55:27 +00:00
}
/// <summary>
/// Fetches metadata from one Xml Element
/// </summary>
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;
if (DateTime.TryParse(reader.ReadString() ?? string.Empty, out added))
2012-07-12 06:55:27 +00:00
{
item.DateCreated = added;
}
break;
// DisplayMediaType
2012-07-12 06:55:27 +00:00
case "Type":
{
item.DisplayMediaType = reader.ReadString();
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":
item.BannerImagePath = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "LocalTitle":
item.Name = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "SortTitle":
item.SortName = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "Overview":
case "Description":
item.Overview = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "TagLine":
{
var list = (item.Taglines ?? new string[] { }).ToList();
var tagline = reader.ReadString();
if (!list.Contains(tagline))
{
list.Add(tagline);
}
item.Taglines = list;
break;
}
case "TagLines":
{
FetchFromTaglinesNode(reader.ReadSubtree(), item);
break;
}
2012-07-12 06:55:27 +00:00
case "ContentRating":
case "MPAARating":
item.OfficialRating = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "CustomRating":
item.CustomRating = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "Runtime":
case "RunningTime":
{
string text = reader.ReadString();
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 genres = (item.Genres ?? new string[] { }).ToList();
genres.AddRange(GetSplitValues(reader.ReadString(), '|'));
2012-07-12 06:55:27 +00:00
item.Genres = genres;
break;
}
case "AspectRatio":
item.AspectRatio = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "Network":
{
var studios = (item.Studios ?? new string[] { }).ToList();
studios.AddRange(GetSplitValues(reader.ReadString(), '|'));
2012-07-12 06:55:27 +00:00
item.Studios = studios;
break;
}
case "Director":
{
var list = (item.People ?? new PersonInfo[] { }).ToList();
list.AddRange(GetSplitValues(reader.ReadString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Director" }));
2012-07-12 06:55:27 +00:00
item.People = list;
break;
}
case "Writer":
{
2012-07-12 17:09:35 +00:00
var list = (item.People ?? new PersonInfo[] { }).ToList();
list.AddRange(GetSplitValues(reader.ReadString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Writer" }));
2012-07-12 06:55:27 +00:00
item.People = list;
break;
}
case "Actors":
case "GuestStars":
{
2012-07-12 17:09:35 +00:00
var list = (item.People ?? new PersonInfo[] { }).ToList();
list.AddRange(GetSplitValues(reader.ReadString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Actor" }));
2012-07-12 06:55:27 +00:00
item.People = list;
break;
}
case "Trailer":
item.TrailerUrl = reader.ReadString();
2012-07-12 06:55:27 +00:00
break;
case "ProductionYear":
{
string val = reader.ReadString();
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":
string rating = reader.ReadString();
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":
{
string firstAired = reader.ReadString();
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.ReadString();
if (!string.IsNullOrWhiteSpace(tmdb))
{
item.SetProviderId(MetadataProviders.Tmdb, tmdb);
}
break;
case "TVcomId":
string TVcomId = reader.ReadString();
if (!string.IsNullOrWhiteSpace(TVcomId))
{
item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
}
break;
case "IMDB_ID":
case "IMDB":
case "IMDbId":
string IMDbId = reader.ReadString();
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;
2012-07-12 06:55:27 +00:00
case "MediaInfo":
FetchMediaInfo(reader.ReadSubtree(), item);
2012-07-12 06:55:27 +00:00
break;
default:
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
}
private void FetchMediaInfo(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
var video = item as Video;
2012-07-12 06:55:27 +00:00
if (video != null)
2012-07-12 06:55:27 +00:00
{
FetchMediaInfo(reader, video);
2012-07-12 06:55:27 +00:00
}
}
private void FetchMediaInfo(XmlReader reader, Video item)
2012-07-12 06:55:27 +00:00
{
reader.MoveToContent();
2012-07-12 06:55:27 +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":
{
AudioStream stream = FetchMediaInfoAudio(reader.ReadSubtree());
2012-07-12 06:55:27 +00:00
List<AudioStream> streams = (item.AudioStreams ?? new AudioStream[] { }).ToList();
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":
FetchMediaInfoVideo(reader.ReadSubtree(), item);
break;
2012-07-12 06:55:27 +00:00
case "Subtitle":
FetchMediaInfoSubtitles(reader.ReadSubtree(), item);
break;
2012-07-12 06:55:27 +00:00
default:
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
private AudioStream FetchMediaInfoAudio(XmlReader reader)
2012-07-12 06:55:27 +00:00
{
AudioStream stream = new AudioStream();
reader.MoveToContent();
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":
stream.IsDefault = reader.ReadString() == "True";
break;
case "Forced":
stream.IsForced = reader.ReadString() == "True";
break;
case "BitRate":
stream.BitRate = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Channels":
stream.Channels = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Language":
stream.Language = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
case "Codec":
2012-07-12 06:55:27 +00:00
{
string codec = reader.ReadString();
switch (codec.ToLower())
{
case "dts-es":
case "dts-es matrix":
case "dts-es discrete":
stream.Format = "DTS";
stream.Profile = "ES";
break;
case "dts-hd hra":
case "dts-hd high resolution":
stream.Format = "DTS";
stream.Profile = "HRA";
break;
case "dts ma":
case "dts-hd ma":
case "dts-hd master":
stream.Format = "DTS";
stream.Profile = "MA";
break;
case "dolby digital":
case "dolby digital surround ex":
case "dolby surround":
stream.Format = "AC-3";
break;
case "dolby digital plus":
stream.Format = "E-AC-3";
break;
case "dolby truehd":
stream.Format = "AC-3";
stream.Profile = "TrueHD";
break;
case "mp2":
stream.Format = "MPEG Audio";
stream.Profile = "Layer 2";
break;
case "other":
break;
default:
stream.Format = codec;
break;
}
break;
2012-07-12 06:55:27 +00:00
}
default:
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
return stream;
}
private void FetchMediaInfoVideo(XmlReader reader, Video item)
2012-07-12 06:55:27 +00:00
{
reader.MoveToContent();
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":
item.Width = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "Height":
item.Height = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "BitRate":
item.BitRate = reader.ReadIntSafe();
break;
2012-07-12 06:55:27 +00:00
case "FrameRate":
item.FrameRate = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
case "ScanType":
item.ScanType = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
case "Duration":
item.RunTimeTicks = TimeSpan.FromMinutes(reader.ReadIntSafe()).Ticks;
break;
2012-07-12 06:55:27 +00:00
case "DurationSeconds":
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
{
string videoCodec = reader.ReadString();
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:
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
}
private void FetchMediaInfoSubtitles(XmlReader reader, Video item)
2012-07-12 06:55:27 +00:00
{
List<string> list = (item.Subtitles ?? new string[] { }).ToList();
reader.MoveToContent();
2012-07-12 06:55:27 +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":
{
string genre = reader.ReadString();
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:
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
item.Subtitles = list;
2012-07-12 06:55:27 +00:00
}
private void FetchFromTaglinesNode(XmlReader reader, T item)
{
List<string> list = (item.Taglines ?? new string[] { }).ToList();
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Tagline":
{
string val = reader.ReadString();
if (!string.IsNullOrWhiteSpace(val))
{
list.Add(val);
}
break;
}
default:
reader.Skip();
break;
}
}
}
item.Taglines = list;
}
private void FetchFromGenresNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
List<string> list = (item.Genres ?? new string[] { }).ToList();
reader.MoveToContent();
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":
{
string genre = reader.ReadString();
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:
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
2012-07-12 06:55:27 +00:00
item.Genres = list;
}
private void FetchDataFromPersonsNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
2012-07-12 17:09:35 +00:00
List<PersonInfo> list = (item.People ?? new PersonInfo[] { }).ToList();
2012-07-12 06:55:27 +00:00
reader.MoveToContent();
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":
{
list.Add(GetPersonFromXmlNode(reader.ReadSubtree()));
break;
}
2012-07-12 06:55:27 +00:00
default:
reader.Skip();
2012-07-12 06:55:27 +00:00
break;
}
2012-07-12 06:55:27 +00:00
}
}
item.People = list;
}
private void FetchFromStudiosNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
List<string> list = (item.Studios ?? new string[] { }).ToList();
reader.MoveToContent();
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":
{
string studio = reader.ReadString();
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:
reader.Skip();
break;
}
2012-07-12 06:55:27 +00:00
}
}
item.Studios = list;
}
private void FetchFromParentalRatingNode(XmlReader reader, T item)
2012-07-12 06:55:27 +00:00
{
reader.MoveToContent();
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
{
string ratingString = reader.ReadString();
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:
reader.Skip();
break;
}
2012-07-12 06:55:27 +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
reader.MoveToContent();
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":
person.Name = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
case "Type":
person.Type = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
case "Role":
person.Overview = reader.ReadString();
break;
2012-07-12 06:55:27 +00:00
default:
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
}
}
}