using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Persistence; 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 { private static readonly Dictionary CommonTags = new[] { "Added", "AspectRatio", "AudioDbAlbumId", "AudioDbArtistId", "AwardSummary", "BirthDate", "Budget", "certification", "Chapters", "ContentRating", "CustomRating", "CriticRating", "CriticRatingSummary", "DeathDate", "DisplayOrder", "EndDate", "Genres", "Genre", "GamesDbId", "IMDB_ID", "IMDB", "IMDbId", "Language", "LocalTitle", "LockData", "LockedFields", "Format3D", "Metascore", "MPAARating", "MusicBrainzArtistId", "MusicBrainzAlbumArtistId", "MusicBrainzAlbumId", "MusicBrainzReleaseGroupId", // Old - not used anymore "MusicbrainzId", "Overview", "Persons", "PlotKeywords", "PremiereDate", "ProductionYear", "Rating", "Revenue", "RottenTomatoesId", "RunningTime", "Runtime", "SortTitle", "Studios", "Tags", "TagLine", "Taglines", "TMDbCollectionId", "TMDbId", "Trailer", "Trailers", "TVcomId", "TvDbId", "Type", "TVRageId", "VoteCount", "Website", "Zap2ItId" }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); /// /// 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, List xmlTagsUsed) { if (File.Exists(path)) { var position = xml.ToString().LastIndexOf(" /// Gets the custom tags. /// /// The path. /// The XML tags used. /// System.String. private static string GetCustomTags(string path, List xmlTagsUsed) { var settings = new XmlReaderSettings { CheckCharacters = false, IgnoreProcessingInstructions = true, IgnoreComments = true, ValidationType = ValidationType.None }; var builder = new StringBuilder(); using (var streamReader = new StreamReader(path, Encoding.UTF8)) { // Use XmlReader for best performance using (var reader = XmlReader.Create(streamReader, settings)) { reader.MoveToContent(); // Loop through each element while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { var name = reader.Name; if (!CommonTags.ContainsKey(name) && !xmlTagsUsed.Contains(name, StringComparer.OrdinalIgnoreCase)) { builder.AppendLine(reader.ReadOuterXml()); } else { reader.Skip(); } } } } } return builder.ToString(); } /// /// 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.ToLocalTime().ToString("G")) + ""); builder.Append("" + item.DontFetchMeta.ToString().ToLower() + ""); if (item.LockedFields.Count > 0) { builder.Append("" + string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray()) + ""); } if (!string.IsNullOrEmpty(item.DisplayMediaType)) { builder.Append("" + SecurityElement.Escape(item.DisplayMediaType) + ""); } var hasCriticRating = item as IHasCriticRating; if (hasCriticRating != null) { if (hasCriticRating.CriticRating.HasValue) { builder.Append("" + SecurityElement.Escape(hasCriticRating.CriticRating.Value.ToString(UsCulture)) + ""); } if (!string.IsNullOrEmpty(hasCriticRating.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) { if (item is Person) { builder.Append("" + SecurityElement.Escape(item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd")) + ""); } else if (!(item is Episode)) { builder.Append("" + SecurityElement.Escape(item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd")) + ""); } } if (item.EndDate.HasValue) { if (item is Person) { builder.Append("" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + ""); } else if (!(item is Episode)) { builder.Append("" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + ""); } } var hasTrailers = item as IHasTrailers; if (hasTrailers != null) { if (hasTrailers.RemoteTrailers.Count > 0) { builder.Append(""); foreach (var trailer in hasTrailers.RemoteTrailers) { builder.Append("" + SecurityElement.Escape(trailer.Url) + ""); } builder.Append(""); } } var hasDisplayOrder = item as IHasDisplayOrder; if (hasDisplayOrder != null && !string.IsNullOrEmpty(hasDisplayOrder.DisplayOrder)) { builder.Append("" + SecurityElement.Escape(hasDisplayOrder.DisplayOrder) + ""); } var hasMetascore = item as IHasMetascore; if (hasMetascore != null && hasMetascore.Metascore.HasValue) { builder.Append("" + SecurityElement.Escape(hasMetascore.Metascore.Value.ToString(UsCulture)) + ""); } var hasAwards = item as IHasAwards; if (hasAwards != null && !string.IsNullOrEmpty(hasAwards.AwardSummary)) { builder.Append("" + SecurityElement.Escape(hasAwards.AwardSummary) + ""); } var hasBudget = item as IHasBudget; if (hasBudget != null) { if (hasBudget.Budget.HasValue) { builder.Append("" + SecurityElement.Escape(hasBudget.Budget.Value.ToString(UsCulture)) + ""); } if (hasBudget.Revenue.HasValue) { builder.Append("" + SecurityElement.Escape(hasBudget.Revenue.Value.ToString(UsCulture)) + ""); } } if (item.CommunityRating.HasValue) { builder.Append("" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + ""); } if (item.VoteCount.HasValue) { builder.Append("" + SecurityElement.Escape(item.VoteCount.Value.ToString(UsCulture)) + ""); } if (item.ProductionYear.HasValue && !(item is Person)) { builder.Append("" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + ""); } if (!string.IsNullOrEmpty(item.HomePageUrl)) { builder.Append("" + SecurityElement.Escape(item.HomePageUrl) + ""); } var hasAspectRatio = item as IHasAspectRatio; if (hasAspectRatio != null) { if (!string.IsNullOrEmpty(hasAspectRatio.AspectRatio)) { builder.Append("" + SecurityElement.Escape(hasAspectRatio.AspectRatio) + ""); } } var hasLanguage = item as IHasPreferredMetadataLanguage; if (hasLanguage != null) { if (!string.IsNullOrEmpty(hasLanguage.PreferredMetadataLanguage)) { builder.Append("" + SecurityElement.Escape(hasLanguage.PreferredMetadataLanguage) + ""); } } // Use original runtime here, actual file runtime later in MediaInfo var runTimeTicks = 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) + ""); } if (!(item is Series)) { var tvdb = item.GetProviderId(MetadataProviders.Tvdb); if (!string.IsNullOrEmpty(tvdb)) { builder.Append("" + SecurityElement.Escape(tvdb) + ""); } } var externalId = item.GetProviderId(MetadataProviders.Tvcom); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.RottenTomatoes); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.Zap2It); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbum); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbumArtist); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.MusicBrainzArtist); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.Gamesdb); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.TmdbCollection); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.AudioDbArtist); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.AudioDbAlbum); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } externalId = item.GetProviderId(MetadataProviders.TvRage); if (!string.IsNullOrEmpty(externalId)) { builder.Append("" + SecurityElement.Escape(externalId) + ""); } var hasTagline = item as IHasTaglines; if (hasTagline != null) { if (hasTagline.Taglines.Count > 0) { builder.Append(""); foreach (var tagline in hasTagline.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(""); } var hasTags = item as IHasTags; if (hasTags != null) { if (hasTags.Tags.Count > 0) { builder.Append(""); foreach (var tag in hasTags.Tags) { builder.Append("" + SecurityElement.Escape(tag) + ""); } builder.Append(""); } } var hasKeywords = item as IHasKeywords; if (hasKeywords != null) { if (hasKeywords.Keywords.Count > 0) { builder.Append(""); foreach (var tag in hasKeywords.Keywords) { 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) + ""); if (person.SortOrder.HasValue) { builder.Append("" + SecurityElement.Escape(person.SortOrder.Value.ToString(UsCulture)) + ""); } builder.Append(""); } builder.Append(""); } } public static void AddChapters(Video item, StringBuilder builder, IItemRepository repository) { var chapters = repository.GetChapters(item.Id); builder.Append(""); foreach (var chapter in chapters) { builder.Append(""); builder.Append("" + SecurityElement.Escape(chapter.Name) + ""); var time = TimeSpan.FromTicks(chapter.StartPositionTicks); var ms = Convert.ToInt64(time.TotalMilliseconds); builder.Append("" + SecurityElement.Escape(ms.ToString(UsCulture)) + ""); builder.Append(""); } builder.Append(""); } /// /// Appends the media info. /// /// public static void AddMediaInfo(T item, StringBuilder builder, IItemRepository itemRepository) where T : BaseItem, IHasMediaStreams { var video = item as Video; if (video != null && video.Video3DFormat.HasValue) { switch (video.Video3DFormat.Value) { case Video3DFormat.FullSideBySide: builder.Append("FSBS"); break; case Video3DFormat.FullTopAndBottom: builder.Append("FTAB"); break; case Video3DFormat.HalfSideBySide: builder.Append("HSBS"); break; case Video3DFormat.HalfTopAndBottom: builder.Append("HTAB"); break; } } } } }