|
8 | 8 | using MediaBrowser.Controller.Library; |
9 | 9 | using MediaBrowser.Controller.Persistence; |
10 | 10 | using MediaBrowser.Controller.Providers; |
| 11 | +using MediaBrowser.Controller.Entities.TV; |
11 | 12 | using MediaBrowser.Model.Configuration; |
12 | 13 | using MediaBrowser.Model.Dto; |
13 | 14 | using MediaBrowser.Model.Entities; |
@@ -75,24 +76,65 @@ public static string GetMediaInfoJsonPath(BaseItem item) |
75 | 76 | { |
76 | 77 | var jsonRootFolder = Plugin.Instance.Options.MainPage.MediaInfoJsonRootFolder; |
77 | 78 |
|
78 | | - var relativePath = item.ContainingFolderPath; |
79 | | - if (!string.IsNullOrEmpty(jsonRootFolder) && Path.IsPathRooted(item.ContainingFolderPath)) |
| 79 | + var mediaInfoFileName = GetMediaInfoFileName(item); |
| 80 | + var mediaInfoJsonPath = !string.IsNullOrEmpty(jsonRootFolder) |
| 81 | + ? Path.Combine(jsonRootFolder, mediaInfoFileName) |
| 82 | + : Path.Combine(item.ContainingFolderPath, mediaInfoFileName); |
| 83 | + |
| 84 | + return mediaInfoJsonPath; |
| 85 | + } |
| 86 | + |
| 87 | + private static bool TryGetTmdbId(BaseItem item, out string tmdbId) |
| 88 | + { |
| 89 | + tmdbId = item.GetProviderId(MetadataProviders.Tmdb); |
| 90 | + if (!string.IsNullOrWhiteSpace(tmdbId) && |
| 91 | + !string.Equals(tmdbId, "None", StringComparison.OrdinalIgnoreCase)) |
80 | 92 | { |
81 | | - relativePath = GetRelativePathCompat(Path.GetPathRoot(item.ContainingFolderPath), |
82 | | - item.ContainingFolderPath); |
| 93 | + return true; |
83 | 94 | } |
84 | 95 |
|
85 | | - var mediaInfoJsonPath = !string.IsNullOrEmpty(jsonRootFolder) |
86 | | - ? Path.Combine(jsonRootFolder, relativePath, item.FileNameWithoutExtension + MediaInfoFileExtension) |
87 | | - : Path.Combine(item.ContainingFolderPath, item.FileNameWithoutExtension + MediaInfoFileExtension); |
| 96 | + if (item is Episode episodeWithSeries && Plugin.LibraryManager != null) |
| 97 | + { |
| 98 | + var series = Plugin.LibraryManager.GetItemById(episodeWithSeries.SeriesId); |
| 99 | + tmdbId = series?.GetProviderId(MetadataProviders.Tmdb); |
| 100 | + } |
88 | 101 |
|
89 | | - return mediaInfoJsonPath; |
| 102 | + return !string.IsNullOrWhiteSpace(tmdbId) && |
| 103 | + !string.Equals(tmdbId, "None", StringComparison.OrdinalIgnoreCase); |
| 104 | + } |
| 105 | + |
| 106 | + private static string GetMediaInfoFileName(BaseItem item) |
| 107 | + { |
| 108 | + if (!TryGetTmdbId(item, out var tmdbId)) |
| 109 | + { |
| 110 | + return item.FileNameWithoutExtension + MediaInfoFileExtension; |
| 111 | + } |
| 112 | + |
| 113 | + string episodeSegment = null; |
| 114 | + if (item is Episode episode) |
| 115 | + { |
| 116 | + var seasonNumber = episode.ParentIndexNumber; |
| 117 | + var episodeNumber = episode.IndexNumber; |
| 118 | + if (seasonNumber.HasValue && episodeNumber.HasValue) |
| 119 | + { |
| 120 | + episodeSegment = $"-S{seasonNumber.Value:D2}E{episodeNumber.Value:D2}"; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + var typeSegment = item is Episode || item is Season || item is Series ? "tv" : "movie"; |
| 125 | + return $"[tmdbid={tmdbId};type={typeSegment}]{episodeSegment}{MediaInfoFileExtension}"; |
90 | 126 | } |
91 | 127 |
|
92 | 128 | /// <summary>将媒体条目的 MediaInfo 与章节序列化到 JSON。</summary> |
93 | 129 | private bool SerializeMediaInfo(BaseItem item, IDirectoryService directoryService, bool overwrite, |
94 | 130 | string source) |
95 | 131 | { |
| 132 | + if (!TryGetTmdbId(item, out var tmdbId)) |
| 133 | + { |
| 134 | + this.logger.Info($"{source} 跳过保存 - 无 TMDB ID: {item.Path ?? item.Name ?? item.Id.ToString()}"); |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
96 | 138 | var mediaInfoJsonPath = GetMediaInfoJsonPath(item); |
97 | 139 | var file = directoryService.GetFile(mediaInfoJsonPath); |
98 | 140 |
|
@@ -239,6 +281,10 @@ public async Task<MediaInfoRestoreResult> DeserializeMediaInfo(BaseItem item, ID |
239 | 281 | this.logger.Debug(e.StackTrace); |
240 | 282 | } |
241 | 283 | } |
| 284 | + else |
| 285 | + { |
| 286 | + this.logger.Info($"{source} 未找到 JSON: {mediaInfoJsonPath}"); |
| 287 | + } |
242 | 288 |
|
243 | 289 | return MediaInfoRestoreResult.Failed; |
244 | 290 | } |
@@ -288,12 +334,3 @@ private static string GetRelativePathCompat(string rootPath, string fullPath) |
288 | 334 | } |
289 | 335 | } |
290 | 336 | } |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | | - |
298 | | - |
299 | | - |
|
0 commit comments