PlaylistItemsProvider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.Playlists;
  13. using MediaBrowser.Controller.Providers;
  14. using Microsoft.Extensions.Logging;
  15. using PlaylistsNET.Content;
  16. namespace MediaBrowser.Providers.Playlists
  17. {
  18. public class PlaylistItemsProvider : ICustomMetadataProvider<Playlist>,
  19. IHasOrder,
  20. IForcedProvider,
  21. IPreRefreshProvider,
  22. IHasItemChangeMonitor
  23. {
  24. private readonly ILogger<PlaylistItemsProvider> _logger;
  25. public PlaylistItemsProvider(ILogger<PlaylistItemsProvider> logger)
  26. {
  27. _logger = logger;
  28. }
  29. public string Name => "Playlist Reader";
  30. // Run last
  31. public int Order => 100;
  32. public Task<ItemUpdateType> FetchAsync(Playlist item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  33. {
  34. var path = item.Path;
  35. if (!Playlist.IsPlaylistFile(path))
  36. {
  37. return Task.FromResult(ItemUpdateType.None);
  38. }
  39. var extension = Path.GetExtension(path);
  40. if (!Playlist.SupportedExtensions.Contains(extension ?? string.Empty, StringComparison.OrdinalIgnoreCase))
  41. {
  42. return Task.FromResult(ItemUpdateType.None);
  43. }
  44. var items = GetItems(path, extension).ToArray();
  45. item.LinkedChildren = items;
  46. return Task.FromResult(ItemUpdateType.MetadataImport);
  47. }
  48. private IEnumerable<LinkedChild> GetItems(string path, string extension)
  49. {
  50. using (var stream = File.OpenRead(path))
  51. {
  52. if (string.Equals(".wpl", extension, StringComparison.OrdinalIgnoreCase))
  53. {
  54. return GetWplItems(stream, path);
  55. }
  56. if (string.Equals(".zpl", extension, StringComparison.OrdinalIgnoreCase))
  57. {
  58. return GetZplItems(stream, path);
  59. }
  60. if (string.Equals(".m3u", extension, StringComparison.OrdinalIgnoreCase))
  61. {
  62. return GetM3uItems(stream, path);
  63. }
  64. if (string.Equals(".m3u8", extension, StringComparison.OrdinalIgnoreCase))
  65. {
  66. return GetM3u8Items(stream, path);
  67. }
  68. if (string.Equals(".pls", extension, StringComparison.OrdinalIgnoreCase))
  69. {
  70. return GetPlsItems(stream, path);
  71. }
  72. }
  73. return Enumerable.Empty<LinkedChild>();
  74. }
  75. private IEnumerable<LinkedChild> GetPlsItems(Stream stream, string path)
  76. {
  77. var content = new PlsContent();
  78. var playlist = content.GetFromStream(stream);
  79. return playlist.PlaylistEntries.Select(i => new LinkedChild
  80. {
  81. Path = GetPlaylistItemPath(i.Path, path),
  82. Type = LinkedChildType.Manual
  83. });
  84. }
  85. private IEnumerable<LinkedChild> GetM3u8Items(Stream stream, string path)
  86. {
  87. var content = new M3uContent();
  88. var playlist = content.GetFromStream(stream);
  89. return playlist.PlaylistEntries.Select(i => new LinkedChild
  90. {
  91. Path = GetPlaylistItemPath(i.Path, path),
  92. Type = LinkedChildType.Manual
  93. });
  94. }
  95. private IEnumerable<LinkedChild> GetM3uItems(Stream stream, string path)
  96. {
  97. var content = new M3uContent();
  98. var playlist = content.GetFromStream(stream);
  99. return playlist.PlaylistEntries.Select(i => new LinkedChild
  100. {
  101. Path = GetPlaylistItemPath(i.Path, path),
  102. Type = LinkedChildType.Manual
  103. });
  104. }
  105. private IEnumerable<LinkedChild> GetZplItems(Stream stream, string path)
  106. {
  107. var content = new ZplContent();
  108. var playlist = content.GetFromStream(stream);
  109. return playlist.PlaylistEntries.Select(i => new LinkedChild
  110. {
  111. Path = GetPlaylistItemPath(i.Path, path),
  112. Type = LinkedChildType.Manual
  113. });
  114. }
  115. private IEnumerable<LinkedChild> GetWplItems(Stream stream, string path)
  116. {
  117. var content = new WplContent();
  118. var playlist = content.GetFromStream(stream);
  119. return playlist.PlaylistEntries.Select(i => new LinkedChild
  120. {
  121. Path = GetPlaylistItemPath(i.Path, path),
  122. Type = LinkedChildType.Manual
  123. });
  124. }
  125. private string GetPlaylistItemPath(string itemPath, string containingPlaylistFolder)
  126. {
  127. if (!File.Exists(itemPath))
  128. {
  129. var path = Path.Combine(Path.GetDirectoryName(containingPlaylistFolder), itemPath);
  130. if (File.Exists(path))
  131. {
  132. return path;
  133. }
  134. }
  135. return itemPath;
  136. }
  137. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  138. {
  139. var path = item.Path;
  140. if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol)
  141. {
  142. var file = directoryService.GetFile(path);
  143. if (file is not null && file.LastWriteTimeUtc != item.DateModified)
  144. {
  145. _logger.LogDebug("Refreshing {Path} due to date modified timestamp change.", path);
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. }
  152. }