XmlSaverHelpers.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Controller.Persistence;
  4. using MediaBrowser.Model.Entities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Security;
  11. using System.Text;
  12. using System.Xml;
  13. namespace MediaBrowser.Providers.Savers
  14. {
  15. /// <summary>
  16. /// Class XmlHelpers
  17. /// </summary>
  18. public static class XmlSaverHelpers
  19. {
  20. private static readonly Dictionary<string, string> CommonTags = new[] {
  21. "Added",
  22. "AspectRatio",
  23. "AwardSummary",
  24. "BirthDate",
  25. "Budget",
  26. "certification",
  27. "Chapters",
  28. "ContentRating",
  29. "CustomRating",
  30. "CriticRating",
  31. "CriticRatingSummary",
  32. "DeathDate",
  33. "DisplayOrder",
  34. "EndDate",
  35. "Genres",
  36. "Genre",
  37. "GamesDbId",
  38. "IMDB_ID",
  39. "IMDB",
  40. "IMDbId",
  41. "Language",
  42. "LocalTitle",
  43. "LockData",
  44. "LockedFields",
  45. "Format3D",
  46. "Metascore",
  47. "MPAARating",
  48. "MusicBrainzArtistId",
  49. "MusicBrainzAlbumArtistId",
  50. "MusicBrainzAlbumId",
  51. "MusicBrainzReleaseGroupId",
  52. "Overview",
  53. "Persons",
  54. "PlotKeywords",
  55. "PremiereDate",
  56. "ProductionYear",
  57. "Rating",
  58. "Revenue",
  59. "RottenTomatoesId",
  60. "RunningTime",
  61. "Runtime",
  62. "SortTitle",
  63. "Studios",
  64. "Tags",
  65. "TagLine",
  66. "Taglines",
  67. "TMDbCollectionId",
  68. "TMDbId",
  69. "Trailer",
  70. "Trailers",
  71. "TVcomId",
  72. "TvDbId",
  73. "Type",
  74. "VoteCount",
  75. "Website",
  76. "Zap2ItId"
  77. }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  78. /// <summary>
  79. /// The us culture
  80. /// </summary>
  81. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  82. /// <summary>
  83. /// Saves the specified XML.
  84. /// </summary>
  85. /// <param name="xml">The XML.</param>
  86. /// <param name="path">The path.</param>
  87. /// <param name="xmlTagsUsed">The XML tags used.</param>
  88. public static void Save(StringBuilder xml, string path, List<string> xmlTagsUsed)
  89. {
  90. if (File.Exists(path))
  91. {
  92. var position = xml.ToString().LastIndexOf("</", StringComparison.OrdinalIgnoreCase);
  93. xml.Insert(position, GetCustomTags(path, xmlTagsUsed));
  94. }
  95. var xmlDocument = new XmlDocument();
  96. xmlDocument.LoadXml(xml.ToString());
  97. //Add the new node to the document.
  98. xmlDocument.InsertBefore(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xmlDocument.DocumentElement);
  99. var parentPath = Path.GetDirectoryName(path);
  100. Directory.CreateDirectory(parentPath);
  101. var wasHidden = false;
  102. var file = new FileInfo(path);
  103. // This will fail if the file is hidden
  104. if (file.Exists)
  105. {
  106. if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  107. {
  108. file.Attributes &= ~FileAttributes.Hidden;
  109. wasHidden = true;
  110. }
  111. }
  112. using (var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  113. {
  114. using (var streamWriter = new StreamWriter(filestream, Encoding.UTF8))
  115. {
  116. xmlDocument.Save(streamWriter);
  117. }
  118. }
  119. if (wasHidden)
  120. {
  121. file.Refresh();
  122. // Add back the attribute
  123. file.Attributes |= FileAttributes.Hidden;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets the custom tags.
  128. /// </summary>
  129. /// <param name="path">The path.</param>
  130. /// <param name="xmlTagsUsed">The XML tags used.</param>
  131. /// <returns>System.String.</returns>
  132. private static string GetCustomTags(string path, List<string> xmlTagsUsed)
  133. {
  134. var settings = new XmlReaderSettings
  135. {
  136. CheckCharacters = false,
  137. IgnoreProcessingInstructions = true,
  138. IgnoreComments = true,
  139. ValidationType = ValidationType.None
  140. };
  141. var builder = new StringBuilder();
  142. using (var streamReader = new StreamReader(path, Encoding.UTF8))
  143. {
  144. // Use XmlReader for best performance
  145. using (var reader = XmlReader.Create(streamReader, settings))
  146. {
  147. reader.MoveToContent();
  148. // Loop through each element
  149. while (reader.Read())
  150. {
  151. if (reader.NodeType == XmlNodeType.Element)
  152. {
  153. var name = reader.Name;
  154. if (!CommonTags.ContainsKey(name) && !xmlTagsUsed.Contains(name, StringComparer.OrdinalIgnoreCase))
  155. {
  156. builder.AppendLine(reader.ReadOuterXml());
  157. }
  158. else
  159. {
  160. reader.Skip();
  161. }
  162. }
  163. }
  164. }
  165. }
  166. return builder.ToString();
  167. }
  168. /// <summary>
  169. /// Adds the common nodes.
  170. /// </summary>
  171. /// <param name="item">The item.</param>
  172. /// <param name="builder">The builder.</param>
  173. public static void AddCommonNodes(BaseItem item, StringBuilder builder)
  174. {
  175. if (!string.IsNullOrEmpty(item.OfficialRating))
  176. {
  177. builder.Append("<ContentRating>" + SecurityElement.Escape(item.OfficialRating) + "</ContentRating>");
  178. builder.Append("<MPAARating>" + SecurityElement.Escape(item.OfficialRating) + "</MPAARating>");
  179. builder.Append("<certification>" + SecurityElement.Escape(item.OfficialRating) + "</certification>");
  180. }
  181. builder.Append("<Added>" + SecurityElement.Escape(item.DateCreated.ToLocalTime().ToString("G")) + "</Added>");
  182. builder.Append("<LockData>" + item.DontFetchMeta.ToString().ToLower() + "</LockData>");
  183. if (item.LockedFields.Count > 0)
  184. {
  185. builder.Append("<LockedFields>" + string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray()) + "</LockedFields>");
  186. }
  187. if (!string.IsNullOrEmpty(item.DisplayMediaType))
  188. {
  189. builder.Append("<Type>" + SecurityElement.Escape(item.DisplayMediaType) + "</Type>");
  190. }
  191. var hasCriticRating = item as IHasCriticRating;
  192. if (hasCriticRating != null)
  193. {
  194. if (hasCriticRating.CriticRating.HasValue)
  195. {
  196. builder.Append("<CriticRating>" + SecurityElement.Escape(hasCriticRating.CriticRating.Value.ToString(UsCulture)) + "</CriticRating>");
  197. }
  198. if (!string.IsNullOrEmpty(hasCriticRating.CriticRatingSummary))
  199. {
  200. builder.Append("<CriticRatingSummary><![CDATA[" + hasCriticRating.CriticRatingSummary + "]]></CriticRatingSummary>");
  201. }
  202. }
  203. if (!string.IsNullOrEmpty(item.Overview))
  204. {
  205. builder.Append("<Overview><![CDATA[" + item.Overview + "]]></Overview>");
  206. }
  207. if (!string.IsNullOrEmpty(item.CustomRating))
  208. {
  209. builder.Append("<CustomRating>" + SecurityElement.Escape(item.CustomRating) + "</CustomRating>");
  210. }
  211. if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
  212. {
  213. builder.Append("<LocalTitle>" + SecurityElement.Escape(item.Name) + "</LocalTitle>");
  214. }
  215. if (!string.IsNullOrEmpty(item.ForcedSortName))
  216. {
  217. builder.Append("<SortTitle>" + SecurityElement.Escape(item.ForcedSortName) + "</SortTitle>");
  218. }
  219. if (item.PremiereDate.HasValue)
  220. {
  221. if (item is Person)
  222. {
  223. builder.Append("<BirthDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd")) + "</BirthDate>");
  224. }
  225. else if (!(item is Episode))
  226. {
  227. builder.Append("<PremiereDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToLocalTime().ToString("yyyy-MM-dd")) + "</PremiereDate>");
  228. }
  229. }
  230. if (item.EndDate.HasValue)
  231. {
  232. if (item is Person)
  233. {
  234. builder.Append("<DeathDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</DeathDate>");
  235. }
  236. else if (!(item is Episode))
  237. {
  238. builder.Append("<EndDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</EndDate>");
  239. }
  240. }
  241. var hasTrailers = item as IHasTrailers;
  242. if (hasTrailers != null)
  243. {
  244. if (hasTrailers.RemoteTrailers.Count > 0)
  245. {
  246. builder.Append("<Trailers>");
  247. foreach (var trailer in hasTrailers.RemoteTrailers)
  248. {
  249. builder.Append("<Trailer>" + SecurityElement.Escape(trailer.Url) + "</Trailer>");
  250. }
  251. builder.Append("</Trailers>");
  252. }
  253. }
  254. var hasDisplayOrder = item as IHasDisplayOrder;
  255. if (hasDisplayOrder != null && !string.IsNullOrEmpty(hasDisplayOrder.DisplayOrder))
  256. {
  257. builder.Append("<DisplayOrder>" + SecurityElement.Escape(hasDisplayOrder.DisplayOrder) + "</DisplayOrder>");
  258. }
  259. var hasMetascore = item as IHasMetascore;
  260. if (hasMetascore != null && hasMetascore.Metascore.HasValue)
  261. {
  262. builder.Append("<Metascore>" + SecurityElement.Escape(hasMetascore.Metascore.Value.ToString(UsCulture)) + "</Metascore>");
  263. }
  264. var hasAwards = item as IHasAwards;
  265. if (hasAwards != null && !string.IsNullOrEmpty(hasAwards.AwardSummary))
  266. {
  267. builder.Append("<AwardSummary>" + SecurityElement.Escape(hasAwards.AwardSummary) + "</AwardSummary>");
  268. }
  269. var hasBudget = item as IHasBudget;
  270. if (hasBudget != null)
  271. {
  272. if (hasBudget.Budget.HasValue)
  273. {
  274. builder.Append("<Budget>" + SecurityElement.Escape(hasBudget.Budget.Value.ToString(UsCulture)) + "</Budget>");
  275. }
  276. if (hasBudget.Revenue.HasValue)
  277. {
  278. builder.Append("<Revenue>" + SecurityElement.Escape(hasBudget.Revenue.Value.ToString(UsCulture)) + "</Revenue>");
  279. }
  280. }
  281. if (item.CommunityRating.HasValue)
  282. {
  283. builder.Append("<Rating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</Rating>");
  284. }
  285. if (item.VoteCount.HasValue)
  286. {
  287. builder.Append("<VoteCount>" + SecurityElement.Escape(item.VoteCount.Value.ToString(UsCulture)) + "</VoteCount>");
  288. }
  289. if (item.ProductionYear.HasValue && !(item is Person))
  290. {
  291. builder.Append("<ProductionYear>" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "</ProductionYear>");
  292. }
  293. if (!string.IsNullOrEmpty(item.HomePageUrl))
  294. {
  295. builder.Append("<Website>" + SecurityElement.Escape(item.HomePageUrl) + "</Website>");
  296. }
  297. var hasAspectRatio = item as IHasAspectRatio;
  298. if (hasAspectRatio != null)
  299. {
  300. if (!string.IsNullOrEmpty(hasAspectRatio.AspectRatio))
  301. {
  302. builder.Append("<AspectRatio>" + SecurityElement.Escape(hasAspectRatio.AspectRatio) + "</AspectRatio>");
  303. }
  304. }
  305. var hasLanguage = item as IHasPreferredMetadataLanguage;
  306. if (hasLanguage != null)
  307. {
  308. if (!string.IsNullOrEmpty(hasLanguage.PreferredMetadataLanguage))
  309. {
  310. builder.Append("<Language>" + SecurityElement.Escape(hasLanguage.PreferredMetadataLanguage) + "</Language>");
  311. }
  312. }
  313. // Use original runtime here, actual file runtime later in MediaInfo
  314. var runTimeTicks = item.RunTimeTicks;
  315. if (runTimeTicks.HasValue)
  316. {
  317. var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
  318. builder.Append("<RunningTime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</RunningTime>");
  319. builder.Append("<Runtime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Runtime>");
  320. }
  321. var imdb = item.GetProviderId(MetadataProviders.Imdb);
  322. if (!string.IsNullOrEmpty(imdb))
  323. {
  324. builder.Append("<IMDB_ID>" + SecurityElement.Escape(imdb) + "</IMDB_ID>");
  325. builder.Append("<IMDB>" + SecurityElement.Escape(imdb) + "</IMDB>");
  326. builder.Append("<IMDbId>" + SecurityElement.Escape(imdb) + "</IMDbId>");
  327. }
  328. var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
  329. if (!string.IsNullOrEmpty(tmdb))
  330. {
  331. builder.Append("<TMDbId>" + SecurityElement.Escape(tmdb) + "</TMDbId>");
  332. }
  333. if (!(item is Series))
  334. {
  335. var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
  336. if (!string.IsNullOrEmpty(tvdb))
  337. {
  338. builder.Append("<TvDbId>" + SecurityElement.Escape(tvdb) + "</TvDbId>");
  339. }
  340. }
  341. var externalId = item.GetProviderId(MetadataProviders.Tvcom);
  342. if (!string.IsNullOrEmpty(externalId))
  343. {
  344. builder.Append("<TVcomId>" + SecurityElement.Escape(externalId) + "</TVcomId>");
  345. }
  346. externalId = item.GetProviderId(MetadataProviders.RottenTomatoes);
  347. if (!string.IsNullOrEmpty(externalId))
  348. {
  349. builder.Append("<RottenTomatoesId>" + SecurityElement.Escape(externalId) + "</RottenTomatoesId>");
  350. }
  351. externalId = item.GetProviderId(MetadataProviders.Zap2It);
  352. if (!string.IsNullOrEmpty(externalId))
  353. {
  354. builder.Append("<Zap2ItId>" + SecurityElement.Escape(externalId) + "</Zap2ItId>");
  355. }
  356. externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  357. if (!string.IsNullOrEmpty(externalId))
  358. {
  359. builder.Append("<MusicBrainzAlbumId>" + SecurityElement.Escape(externalId) + "</MusicBrainzAlbumId>");
  360. }
  361. externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbumArtist);
  362. if (!string.IsNullOrEmpty(externalId))
  363. {
  364. builder.Append("<MusicBrainzAlbumArtistId>" + SecurityElement.Escape(externalId) + "</MusicBrainzAlbumArtistId>");
  365. }
  366. externalId = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  367. if (!string.IsNullOrEmpty(externalId))
  368. {
  369. builder.Append("<MusicBrainzArtistId>" + SecurityElement.Escape(externalId) + "</MusicBrainzArtistId>");
  370. }
  371. externalId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  372. if (!string.IsNullOrEmpty(externalId))
  373. {
  374. builder.Append("<MusicBrainzReleaseGroupId>" + SecurityElement.Escape(externalId) + "</MusicBrainzReleaseGroupId>");
  375. }
  376. externalId = item.GetProviderId(MetadataProviders.Gamesdb);
  377. if (!string.IsNullOrEmpty(externalId))
  378. {
  379. builder.Append("<GamesDbId>" + SecurityElement.Escape(externalId) + "</GamesDbId>");
  380. }
  381. externalId = item.GetProviderId(MetadataProviders.TmdbCollection);
  382. if (!string.IsNullOrEmpty(externalId))
  383. {
  384. builder.Append("<TMDbCollectionId>" + SecurityElement.Escape(externalId) + "</TMDbCollectionId>");
  385. }
  386. externalId = item.GetProviderId(MetadataProviders.AudioDbArtist);
  387. if (!string.IsNullOrEmpty(externalId))
  388. {
  389. builder.Append("<AudioDbArtistId>" + SecurityElement.Escape(externalId) + "</AudioDbArtistId>");
  390. }
  391. externalId = item.GetProviderId(MetadataProviders.TvRage);
  392. if (!string.IsNullOrEmpty(externalId))
  393. {
  394. builder.Append("<TvRageId>" + SecurityElement.Escape(externalId) + "</TvRageId>");
  395. }
  396. var hasTagline = item as IHasTaglines;
  397. if (hasTagline != null)
  398. {
  399. if (hasTagline.Taglines.Count > 0)
  400. {
  401. builder.Append("<Taglines>");
  402. foreach (var tagline in hasTagline.Taglines)
  403. {
  404. builder.Append("<Tagline>" + SecurityElement.Escape(tagline) + "</Tagline>");
  405. }
  406. builder.Append("</Taglines>");
  407. }
  408. }
  409. if (item.Genres.Count > 0)
  410. {
  411. builder.Append("<Genres>");
  412. foreach (var genre in item.Genres)
  413. {
  414. builder.Append("<Genre>" + SecurityElement.Escape(genre) + "</Genre>");
  415. }
  416. builder.Append("</Genres>");
  417. }
  418. if (item.Studios.Count > 0)
  419. {
  420. builder.Append("<Studios>");
  421. foreach (var studio in item.Studios)
  422. {
  423. builder.Append("<Studio>" + SecurityElement.Escape(studio) + "</Studio>");
  424. }
  425. builder.Append("</Studios>");
  426. }
  427. var hasTags = item as IHasTags;
  428. if (hasTags != null)
  429. {
  430. if (hasTags.Tags.Count > 0)
  431. {
  432. builder.Append("<Tags>");
  433. foreach (var tag in hasTags.Tags)
  434. {
  435. builder.Append("<Tag>" + SecurityElement.Escape(tag) + "</Tag>");
  436. }
  437. builder.Append("</Tags>");
  438. }
  439. }
  440. var hasKeywords = item as IHasKeywords;
  441. if (hasKeywords != null)
  442. {
  443. if (hasKeywords.Keywords.Count > 0)
  444. {
  445. builder.Append("<PlotKeywords>");
  446. foreach (var tag in hasKeywords.Keywords)
  447. {
  448. builder.Append("<PlotKeyword>" + SecurityElement.Escape(tag) + "</PlotKeyword>");
  449. }
  450. builder.Append("</PlotKeywords>");
  451. }
  452. }
  453. if (item.People.Count > 0)
  454. {
  455. builder.Append("<Persons>");
  456. foreach (var person in item.People)
  457. {
  458. builder.Append("<Person>");
  459. builder.Append("<Name>" + SecurityElement.Escape(person.Name) + "</Name>");
  460. builder.Append("<Type>" + SecurityElement.Escape(person.Type) + "</Type>");
  461. builder.Append("<Role>" + SecurityElement.Escape(person.Role) + "</Role>");
  462. if (person.SortOrder.HasValue)
  463. {
  464. builder.Append("<SortOrder>" + SecurityElement.Escape(person.SortOrder.Value.ToString(UsCulture)) + "</SortOrder>");
  465. }
  466. builder.Append("</Person>");
  467. }
  468. builder.Append("</Persons>");
  469. }
  470. }
  471. public static void AddChapters(Video item, StringBuilder builder, IItemRepository repository)
  472. {
  473. var chapters = repository.GetChapters(item.Id);
  474. builder.Append("<Chapters>");
  475. foreach (var chapter in chapters)
  476. {
  477. builder.Append("<Chapter>");
  478. builder.Append("<Name>" + SecurityElement.Escape(chapter.Name) + "</Name>");
  479. var time = TimeSpan.FromTicks(chapter.StartPositionTicks);
  480. var ms = Convert.ToInt64(time.TotalMilliseconds);
  481. builder.Append("<StartPositionMs>" + SecurityElement.Escape(ms.ToString(UsCulture)) + "</StartPositionMs>");
  482. builder.Append("</Chapter>");
  483. }
  484. builder.Append("</Chapters>");
  485. }
  486. /// <summary>
  487. /// Appends the media info.
  488. /// </summary>
  489. /// <typeparam name="T"></typeparam>
  490. public static void AddMediaInfo<T>(T item, StringBuilder builder, IItemRepository itemRepository)
  491. where T : BaseItem, IHasMediaStreams
  492. {
  493. var video = item as Video;
  494. if (video != null && video.Video3DFormat.HasValue)
  495. {
  496. switch (video.Video3DFormat.Value)
  497. {
  498. case Video3DFormat.FullSideBySide:
  499. builder.Append("<Format3D>FSBS</Format3D>");
  500. break;
  501. case Video3DFormat.FullTopAndBottom:
  502. builder.Append("<Format3D>FTAB</Format3D>");
  503. break;
  504. case Video3DFormat.HalfSideBySide:
  505. builder.Append("<Format3D>HSBS</Format3D>");
  506. break;
  507. case Video3DFormat.HalfTopAndBottom:
  508. builder.Append("<Format3D>HTAB</Format3D>");
  509. break;
  510. }
  511. }
  512. }
  513. }
  514. }