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" }); var position = xml.ToString().LastIndexOf(" /// 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) + ""); } 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(""); } if (!string.IsNullOrEmpty(item.DisplayMediaType)) { builder.Append("" + SecurityElement.Escape(item.DisplayMediaType) + ""); } 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) { builder.Append("" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + ""); } 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) + ""); } if (item.RunTimeTicks.HasValue) { var timespan = TimeSpan.FromTicks(item.RunTimeTicks.Value); builder.Append("" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + ""); builder.Append("" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + ""); } 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(""); } 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.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(""); } builder.Append("" + SecurityElement.Escape(item.DateCreated.ToString(UsCulture)) + ""); } /// /// 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(""); } } }