MovieXmlSaver.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using MediaBrowser.Providers.Movies;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Security;
  12. using System.Text;
  13. using System.Threading;
  14. namespace MediaBrowser.Providers.Savers
  15. {
  16. /// <summary>
  17. /// Saves movie.xml for movies, trailers and music videos
  18. /// </summary>
  19. public class MovieXmlSaver : IMetadataSaver
  20. {
  21. private readonly IServerConfigurationManager _config;
  22. private readonly IItemRepository _itemRepository;
  23. public MovieXmlSaver(IServerConfigurationManager config, IItemRepository itemRepository)
  24. {
  25. _config = config;
  26. _itemRepository = itemRepository;
  27. }
  28. /// <summary>
  29. /// Determines whether [is enabled for] [the specified item].
  30. /// </summary>
  31. /// <param name="item">The item.</param>
  32. /// <param name="updateType">Type of the update.</param>
  33. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  34. public bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
  35. {
  36. var wasMetadataEdited = (updateType & ItemUpdateType.MetadataEdit) == ItemUpdateType.MetadataEdit;
  37. var wasMetadataDownloaded = (updateType & ItemUpdateType.MetadataDownload) == ItemUpdateType.MetadataDownload;
  38. // If new metadata has been downloaded and save local is on
  39. if (_config.Configuration.SaveLocalMeta && (wasMetadataEdited || wasMetadataDownloaded))
  40. {
  41. var trailer = item as Trailer;
  42. // Don't support local trailers
  43. if (trailer != null)
  44. {
  45. return !trailer.IsLocalTrailer;
  46. }
  47. return item is Movie || item is MusicVideo || item is AdultVideo;
  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(BaseItem item, CancellationToken cancellationToken)
  59. {
  60. var builder = new StringBuilder();
  61. builder.Append("<Title>");
  62. XmlSaverHelpers.AddCommonNodes(item, builder);
  63. if (item.CommunityRating.HasValue)
  64. {
  65. builder.Append("<IMDBrating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</IMDBrating>");
  66. }
  67. if (!string.IsNullOrEmpty(item.Overview))
  68. {
  69. builder.Append("<Description><![CDATA[" + item.Overview + "]]></Description>");
  70. }
  71. var musicVideo = item as MusicVideo;
  72. if (musicVideo != null)
  73. {
  74. if (!string.IsNullOrEmpty(musicVideo.Artist))
  75. {
  76. builder.Append("<Artist>" + SecurityElement.Escape(musicVideo.Artist) + "</Artist>");
  77. }
  78. if (!string.IsNullOrEmpty(musicVideo.Album))
  79. {
  80. builder.Append("<Album>" + SecurityElement.Escape(musicVideo.Album) + "</Album>");
  81. }
  82. }
  83. var movie = item as Movie;
  84. if (movie != null)
  85. {
  86. if (!string.IsNullOrEmpty(movie.TmdbCollectionName))
  87. {
  88. builder.Append("<TmdbCollectionName>" + SecurityElement.Escape(movie.TmdbCollectionName) + "</TmdbCollectionName>");
  89. }
  90. }
  91. var video = (Video)item;
  92. XmlSaverHelpers.AddMediaInfo(video, builder, _itemRepository);
  93. builder.Append("</Title>");
  94. var xmlFilePath = GetSavePath(item);
  95. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  96. {
  97. "IMDBrating",
  98. "Description",
  99. "Artist",
  100. "Album",
  101. "TmdbCollectionName"
  102. });
  103. // Set last refreshed so that the provider doesn't trigger after the file save
  104. MovieProviderFromXml.Current.SetLastRefreshed(item, DateTime.UtcNow);
  105. }
  106. public string GetSavePath(BaseItem item)
  107. {
  108. return GetMovieSavePath(item);
  109. }
  110. public static string GetMovieSavePath(BaseItem item)
  111. {
  112. if (item.IsInMixedFolder)
  113. {
  114. return Path.ChangeExtension(item.Path, ".xml");
  115. }
  116. return Path.Combine(item.MetaLocation, "movie.xml");
  117. }
  118. }
  119. }