MovieXmlSaver.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Movies;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Security;
  10. using System.Text;
  11. using System.Threading;
  12. namespace MediaBrowser.Providers.Savers
  13. {
  14. /// <summary>
  15. /// Saves movie.xml for movies, trailers and music videos
  16. /// </summary>
  17. public class MovieXmlSaver : IMetadataFileSaver
  18. {
  19. private readonly IItemRepository _itemRepository;
  20. public MovieXmlSaver(IItemRepository itemRepository)
  21. {
  22. _itemRepository = itemRepository;
  23. }
  24. public string Name
  25. {
  26. get
  27. {
  28. return "Media Browser Xml";
  29. }
  30. }
  31. /// <summary>
  32. /// Determines whether [is enabled for] [the specified item].
  33. /// </summary>
  34. /// <param name="item">The item.</param>
  35. /// <param name="updateType">Type of the update.</param>
  36. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  37. public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  38. {
  39. if (!item.SupportsLocalMetadata)
  40. {
  41. return false;
  42. }
  43. var video = item as Video;
  44. // Check parent for null to avoid running this against things like video backdrops
  45. if (video != null && !(item is Episode) && !video.IsOwnedItem)
  46. {
  47. return updateType >= ItemUpdateType.MetadataDownload;
  48. }
  49. return false;
  50. }
  51. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  52. /// <summary>
  53. /// Saves the specified item.
  54. /// </summary>
  55. /// <param name="item">The item.</param>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. /// <returns>Task.</returns>
  58. public void Save(IHasMetadata item, CancellationToken cancellationToken)
  59. {
  60. var video = (Video)item;
  61. var builder = new StringBuilder();
  62. builder.Append("<Title>");
  63. XmlSaverHelpers.AddCommonNodes(video, builder);
  64. var musicVideo = item as MusicVideo;
  65. if (musicVideo != null)
  66. {
  67. if (!string.IsNullOrEmpty(musicVideo.Artist))
  68. {
  69. builder.Append("<Artist>" + SecurityElement.Escape(musicVideo.Artist) + "</Artist>");
  70. }
  71. if (!string.IsNullOrEmpty(musicVideo.Album))
  72. {
  73. builder.Append("<Album>" + SecurityElement.Escape(musicVideo.Album) + "</Album>");
  74. }
  75. }
  76. var movie = item as Movie;
  77. if (movie != null)
  78. {
  79. if (!string.IsNullOrEmpty(movie.TmdbCollectionName))
  80. {
  81. builder.Append("<TmdbCollectionName>" + SecurityElement.Escape(movie.TmdbCollectionName) + "</TmdbCollectionName>");
  82. }
  83. }
  84. XmlSaverHelpers.AddMediaInfo(video, builder, _itemRepository);
  85. builder.Append("</Title>");
  86. var xmlFilePath = GetSavePath(item);
  87. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  88. {
  89. // Deprecated. No longer saving in this field.
  90. "IMDBrating",
  91. // Deprecated. No longer saving in this field.
  92. "Description",
  93. "Artist",
  94. "Album",
  95. "TmdbCollectionName"
  96. });
  97. }
  98. public string GetSavePath(IHasMetadata item)
  99. {
  100. return GetMovieSavePath((Video)item);
  101. }
  102. public static string GetMovieSavePath(Video item)
  103. {
  104. if (item.IsInMixedFolder)
  105. {
  106. return Path.ChangeExtension(item.Path, ".xml");
  107. }
  108. return Path.Combine(item.ContainingFolderPath, "movie.xml");
  109. }
  110. }
  111. }