AlbumNfoSaver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Xml;
  12. using MediaBrowser.Controller.IO;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Xml;
  15. namespace MediaBrowser.XbmcMetadata.Savers
  16. {
  17. public class AlbumNfoSaver : BaseNfoSaver
  18. {
  19. protected override string GetLocalSavePath(IHasMetadata item)
  20. {
  21. return Path.Combine(item.Path, "album.nfo");
  22. }
  23. protected override string GetRootElementName(IHasMetadata item)
  24. {
  25. return "album";
  26. }
  27. public override bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  28. {
  29. if (!item.SupportsLocalMetadata)
  30. {
  31. return false;
  32. }
  33. return item is MusicAlbum && updateType >= MinimumUpdateType;
  34. }
  35. protected override void WriteCustomElements(IHasMetadata item, XmlWriter writer)
  36. {
  37. var album = (MusicAlbum)item;
  38. foreach (var artist in album.Artists)
  39. {
  40. writer.WriteElementString("artist", artist);
  41. }
  42. foreach (var artist in album.AlbumArtists)
  43. {
  44. writer.WriteElementString("albumartist", artist);
  45. }
  46. AddTracks(album.Tracks, writer);
  47. }
  48. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  49. private void AddTracks(IEnumerable<BaseItem> tracks, XmlWriter writer)
  50. {
  51. foreach (var track in tracks.OrderBy(i => i.ParentIndexNumber ?? 0).ThenBy(i => i.IndexNumber ?? 0))
  52. {
  53. writer.WriteStartElement("track");
  54. if (track.IndexNumber.HasValue)
  55. {
  56. writer.WriteElementString("position", track.IndexNumber.Value.ToString(UsCulture));
  57. }
  58. if (!string.IsNullOrEmpty(track.Name))
  59. {
  60. writer.WriteElementString("title", track.Name);
  61. }
  62. if (track.RunTimeTicks.HasValue)
  63. {
  64. var time = TimeSpan.FromTicks(track.RunTimeTicks.Value).ToString(@"mm\:ss");
  65. writer.WriteElementString("duration", time);
  66. }
  67. writer.WriteEndElement();
  68. }
  69. }
  70. protected override List<string> GetTagsUsed(IHasMetadata item)
  71. {
  72. var list = base.GetTagsUsed(item);
  73. list.AddRange(new string[]
  74. {
  75. "track",
  76. "artist",
  77. "albumartist"
  78. });
  79. return list;
  80. }
  81. public AlbumNfoSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataManager, ILogger logger, IXmlReaderSettingsFactory xmlReaderSettingsFactory) : base(fileSystem, configurationManager, libraryManager, userManager, userDataManager, logger, xmlReaderSettingsFactory)
  82. {
  83. }
  84. }
  85. }