using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Xml;
namespace MediaBrowser.Providers.Savers
{
///
/// Class XmlHelpers
///
public static class XmlSaverHelpers
{
///
/// The us culture
///
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
///
/// Saves the specified XML.
///
/// The XML.
/// The path.
/// The XML tags used.
public static void Save(StringBuilder xml, string path, IEnumerable xmlTagsUsed)
{
if (File.Exists(path))
{
var tags = xmlTagsUsed.ToList();
tags.AddRange(new[]
{
"MediaInfo",
"ContentRating",
"MPAARating",
"certification",
"Persons",
"Type",
"Overview",
"CustomRating",
"LocalTitle",
"SortTitle",
"PremiereDate",
"Budget",
"Revenue",
"Rating",
"ProductionYear",
"Website",
"AspectRatio",
"Language",
"RunningTime",
"Runtime",
"TagLine",
"TagLines",
"IMDB_ID",
"IMDB",
"IMDbId",
"TMDbId",
"TVcomId",
"RottenTomatoesId",
"MusicbrainzId",
"CollectionNumber",
"Genres",
"Studios",
"Tags",
"Added",
"LockData",
"Trailer"
});
var position = xml.ToString().LastIndexOf("", StringComparison.OrdinalIgnoreCase);
xml.Insert(position, GetCustomTags(path, tags));
}
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml.ToString());
//Add the new node to the document.
xmlDocument.InsertBefore(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xmlDocument.DocumentElement);
var parentPath = Path.GetDirectoryName(path);
if (!Directory.Exists(parentPath))
{
Directory.CreateDirectory(parentPath);
}
var wasHidden = false;
var file = new FileInfo(path);
// This will fail if the file is hidden
if (file.Exists)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
file.Attributes &= ~FileAttributes.Hidden;
wasHidden = true;
}
}
using (var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var streamWriter = new StreamWriter(filestream, Encoding.UTF8))
{
xmlDocument.Save(streamWriter);
}
}
if (wasHidden)
{
file.Refresh();
// Add back the attribute
file.Attributes |= FileAttributes.Hidden;
}
}
///
/// Gets the custom tags.
///
/// The path.
/// The XML tags used.
/// System.String.
private static string GetCustomTags(string path, ICollection xmlTagsUsed)
{
var doc = new XmlDocument();
doc.Load(path);
var nodes = doc.DocumentElement.ChildNodes.Cast()
.Where(i => !xmlTagsUsed.Contains(i.Name))
.Select(i => i.OuterXml)
.ToArray();
return string.Join(Environment.NewLine, nodes);
}
///
/// Adds the common nodes.
///
/// The item.
/// The builder.
public static void AddCommonNodes(BaseItem item, StringBuilder builder)
{
if (!string.IsNullOrEmpty(item.OfficialRating))
{
builder.Append("" + SecurityElement.Escape(item.OfficialRating) + "");
builder.Append("" + SecurityElement.Escape(item.OfficialRating) + "");
builder.Append("" + SecurityElement.Escape(item.OfficialRating) + "");
}
builder.Append("" + SecurityElement.Escape(item.DateCreated.ToString(UsCulture)) + "");
builder.Append("" + item.DontFetchMeta.ToString().ToLower() + "");
if (!string.IsNullOrEmpty(item.DisplayMediaType))
{
builder.Append("" + SecurityElement.Escape(item.DisplayMediaType) + "");
}
if (item.CriticRating.HasValue)
{
builder.Append("" + SecurityElement.Escape(item.CriticRating.Value.ToString(UsCulture)) + "");
}
if (!string.IsNullOrEmpty(item.CriticRatingSummary))
{
builder.Append("");
}
if (!string.IsNullOrEmpty(item.Overview))
{
builder.Append("");
}
if (!string.IsNullOrEmpty(item.CustomRating))
{
builder.Append("" + SecurityElement.Escape(item.CustomRating) + "");
}
if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
{
builder.Append("" + SecurityElement.Escape(item.Name) + "");
}
if (!string.IsNullOrEmpty(item.ForcedSortName))
{
builder.Append("" + SecurityElement.Escape(item.ForcedSortName) + "");
}
if (item.PremiereDate.HasValue && !(item is Episode))
{
builder.Append("" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "");
}
if (item.RemoteTrailers.Count > 0)
{
builder.Append("" + SecurityElement.Escape(item.RemoteTrailers[0].Url) + "");
}
if (item.Budget.HasValue)
{
builder.Append("" + SecurityElement.Escape(item.Budget.Value.ToString(UsCulture)) + "");
}
if (item.Revenue.HasValue)
{
builder.Append("" + SecurityElement.Escape(item.Revenue.Value.ToString(UsCulture)) + "");
}
if (item.CommunityRating.HasValue)
{
builder.Append("" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "");
}
if (item.ProductionYear.HasValue)
{
builder.Append("" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "");
}
if (!string.IsNullOrEmpty(item.HomePageUrl))
{
builder.Append("" + SecurityElement.Escape(item.HomePageUrl) + "");
}
if (!string.IsNullOrEmpty(item.AspectRatio))
{
builder.Append("" + SecurityElement.Escape(item.AspectRatio) + "");
}
if (!string.IsNullOrEmpty(item.Language))
{
builder.Append("" + SecurityElement.Escape(item.Language) + "");
}
// Use original runtime here, actual file runtime later in MediaInfo
var runTimeTicks = item.OriginalRunTimeTicks ?? item.RunTimeTicks;
if (runTimeTicks.HasValue)
{
var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
builder.Append("" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "");
builder.Append("" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "");
}
var imdb = item.GetProviderId(MetadataProviders.Imdb);
if (!string.IsNullOrEmpty(imdb))
{
builder.Append("" + SecurityElement.Escape(imdb) + "");
builder.Append("" + SecurityElement.Escape(imdb) + "");
builder.Append("" + SecurityElement.Escape(imdb) + "");
}
var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
if (!string.IsNullOrEmpty(tmdb))
{
builder.Append("" + SecurityElement.Escape(tmdb) + "");
}
var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
if (!string.IsNullOrEmpty(tvcom))
{
builder.Append("" + SecurityElement.Escape(tvcom) + "");
}
var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
if (!string.IsNullOrEmpty(rt))
{
builder.Append("" + SecurityElement.Escape(rt) + "");
}
var mbz = item.GetProviderId(MetadataProviders.Musicbrainz);
if (!string.IsNullOrEmpty(mbz))
{
builder.Append("" + SecurityElement.Escape(mbz) + "");
}
var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
if (!string.IsNullOrEmpty(tmdbCollection))
{
builder.Append("" + SecurityElement.Escape(tmdbCollection) + "");
}
if (item.Taglines.Count > 0)
{
builder.Append("" + SecurityElement.Escape(item.Taglines[0]) + "");
builder.Append("");
foreach (var tagline in item.Taglines)
{
builder.Append("" + SecurityElement.Escape(tagline) + "");
}
builder.Append("");
}
if (item.Genres.Count > 0)
{
builder.Append("");
foreach (var genre in item.Genres)
{
builder.Append("" + SecurityElement.Escape(genre) + "");
}
builder.Append("");
}
if (item.Studios.Count > 0)
{
builder.Append("");
foreach (var studio in item.Studios)
{
builder.Append("" + SecurityElement.Escape(studio) + "");
}
builder.Append("");
}
if (item.Tags.Count > 0)
{
builder.Append("");
foreach (var tag in item.Tags)
{
builder.Append("" + SecurityElement.Escape(tag) + "");
}
builder.Append("");
}
if (item.People.Count > 0)
{
builder.Append("");
foreach (var person in item.People)
{
builder.Append("");
builder.Append("" + SecurityElement.Escape(person.Name) + "");
builder.Append("" + SecurityElement.Escape(person.Type) + "");
builder.Append("" + SecurityElement.Escape(person.Role) + "");
builder.Append("");
}
builder.Append("");
}
}
///
/// Appends the media info.
///
///
/// The item.
/// The builder.
public static void AppendMediaInfo(T item, StringBuilder builder)
where T : BaseItem, IHasMediaStreams
{
builder.Append("");
foreach (var stream in item.MediaStreams)
{
if (stream.Type == MediaStreamType.Video)
{
builder.Append("");
}
else if (stream.Type == MediaStreamType.Audio)
{
builder.Append("");
}
else if (stream.Type == MediaStreamType.Subtitle)
{
builder.Append("");
if (!string.IsNullOrEmpty(stream.Language))
{
builder.Append("" + SecurityElement.Escape(stream.Language) + "");
}
builder.Append("" + SecurityElement.Escape(stream.IsDefault.ToString()) + "");
builder.Append("" + SecurityElement.Escape(stream.IsForced.ToString()) + "");
builder.Append("");
}
}
builder.Append("");
}
}
}