| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Jellyfin.Data.Enums; |
| | 5 | | using MediaBrowser.Controller.Configuration; |
| | 6 | | using MediaBrowser.Controller.Entities; |
| | 7 | | using MediaBrowser.Controller.Entities.Audio; |
| | 8 | | using MediaBrowser.Controller.Library; |
| | 9 | | using MediaBrowser.Controller.Providers; |
| | 10 | | using MediaBrowser.Model.Entities; |
| | 11 | | using MediaBrowser.Model.IO; |
| | 12 | | using MediaBrowser.Providers.Manager; |
| | 13 | | using Microsoft.Extensions.Logging; |
| | 14 | |
|
| | 15 | | namespace MediaBrowser.Providers.Music |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The album metadata service. |
| | 19 | | /// </summary> |
| | 20 | | public class AlbumMetadataService : MetadataService<MusicAlbum, AlbumInfo> |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="AlbumMetadataService"/> class. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/>.</param> |
| | 26 | | /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param> |
| | 27 | | /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param> |
| | 28 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | 29 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 30 | | public AlbumMetadataService( |
| | 31 | | IServerConfigurationManager serverConfigurationManager, |
| | 32 | | ILogger<AlbumMetadataService> logger, |
| | 33 | | IProviderManager providerManager, |
| | 34 | | IFileSystem fileSystem, |
| | 35 | | ILibraryManager libraryManager) |
| 21 | 36 | | : base(serverConfigurationManager, logger, providerManager, fileSystem, libraryManager) |
| | 37 | | { |
| 21 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| 0 | 41 | | protected override bool EnableUpdatingPremiereDateFromChildren => true; |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| 0 | 44 | | protected override bool EnableUpdatingGenresFromChildren => true; |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| 0 | 47 | | protected override bool EnableUpdatingStudiosFromChildren => true; |
| | 48 | |
|
| | 49 | | /// <inheritdoc /> |
| | 50 | | protected override IReadOnlyList<BaseItem> GetChildrenForMetadataUpdates(MusicAlbum item) |
| 0 | 51 | | => item.GetRecursiveChildren(i => i is Audio); |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| | 54 | | protected override ItemUpdateType UpdateMetadataFromChildren(MusicAlbum item, IReadOnlyList<BaseItem> children, |
| | 55 | | { |
| 0 | 56 | | var updateType = base.UpdateMetadataFromChildren(item, children, isFullRefresh, currentUpdateType); |
| | 57 | |
|
| | 58 | | // don't update user-changeable metadata for locked items |
| 0 | 59 | | if (item.IsLocked) |
| | 60 | | { |
| 0 | 61 | | return updateType; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (isFullRefresh || currentUpdateType > ItemUpdateType.None) |
| | 65 | | { |
| 0 | 66 | | if (!item.LockedFields.Contains(MetadataField.Name)) |
| | 67 | | { |
| 0 | 68 | | var name = children.Select(i => i.Album).FirstOrDefault(i => !string.IsNullOrEmpty(i)); |
| | 69 | |
|
| 0 | 70 | | if (!string.IsNullOrEmpty(name) |
| 0 | 71 | | && !string.Equals(item.Name, name, StringComparison.Ordinal)) |
| | 72 | | { |
| 0 | 73 | | item.Name = name; |
| 0 | 74 | | updateType |= ItemUpdateType.MetadataEdit; |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | var songs = children.Cast<Audio>().ToArray(); |
| | 79 | |
|
| 0 | 80 | | updateType |= SetArtistsFromSongs(item, songs); |
| 0 | 81 | | updateType |= SetAlbumArtistFromSongs(item, songs); |
| 0 | 82 | | updateType |= SetAlbumFromSongs(item, songs); |
| 0 | 83 | | updateType |= SetPeople(item); |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return updateType; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private ItemUpdateType SetAlbumArtistFromSongs(MusicAlbum item, IReadOnlyList<Audio> songs) |
| | 90 | | { |
| 0 | 91 | | var updateType = ItemUpdateType.None; |
| | 92 | |
|
| 0 | 93 | | var albumArtists = songs |
| 0 | 94 | | .SelectMany(i => i.AlbumArtists) |
| 0 | 95 | | .GroupBy(i => i) |
| 0 | 96 | | .OrderByDescending(g => g.Count()) |
| 0 | 97 | | .Select(g => g.Key) |
| 0 | 98 | | .ToArray(); |
| | 99 | |
|
| 0 | 100 | | updateType |= SetProviderIdFromSongs(item, songs, MetadataProvider.MusicBrainzAlbumArtist); |
| | 101 | |
|
| 0 | 102 | | if (!item.AlbumArtists.SequenceEqual(albumArtists, StringComparer.OrdinalIgnoreCase)) |
| | 103 | | { |
| 0 | 104 | | item.AlbumArtists = albumArtists; |
| 0 | 105 | | updateType |= ItemUpdateType.MetadataEdit; |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | return updateType; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | private ItemUpdateType SetArtistsFromSongs(MusicAlbum item, IReadOnlyList<Audio> songs) |
| | 112 | | { |
| 0 | 113 | | var updateType = ItemUpdateType.None; |
| | 114 | |
|
| 0 | 115 | | var artists = songs |
| 0 | 116 | | .SelectMany(i => i.Artists) |
| 0 | 117 | | .GroupBy(i => i) |
| 0 | 118 | | .OrderByDescending(g => g.Count()) |
| 0 | 119 | | .Select(g => g.Key) |
| 0 | 120 | | .ToArray(); |
| | 121 | |
|
| 0 | 122 | | if (!item.Artists.SequenceEqual(artists, StringComparer.OrdinalIgnoreCase)) |
| | 123 | | { |
| 0 | 124 | | item.Artists = artists; |
| 0 | 125 | | updateType |= ItemUpdateType.MetadataEdit; |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | return updateType; |
| | 129 | | } |
| | 130 | |
|
| | 131 | | private ItemUpdateType SetAlbumFromSongs(MusicAlbum item, IReadOnlyList<Audio> songs) |
| | 132 | | { |
| 0 | 133 | | var updateType = ItemUpdateType.None; |
| | 134 | |
|
| 0 | 135 | | updateType |= SetProviderIdFromSongs(item, songs, MetadataProvider.MusicBrainzAlbum); |
| 0 | 136 | | updateType |= SetProviderIdFromSongs(item, songs, MetadataProvider.MusicBrainzReleaseGroup); |
| | 137 | |
|
| 0 | 138 | | return updateType; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private ItemUpdateType SetProviderIdFromSongs(BaseItem item, IReadOnlyList<Audio> songs, MetadataProvider provid |
| | 142 | | { |
| 0 | 143 | | var ids = songs |
| 0 | 144 | | .Select(i => i.GetProviderId(provider)) |
| 0 | 145 | | .GroupBy(i => i) |
| 0 | 146 | | .OrderByDescending(g => g.Count()) |
| 0 | 147 | | .Select(g => g.Key) |
| 0 | 148 | | .ToArray(); |
| | 149 | |
|
| 0 | 150 | | var id = item.GetProviderId(provider); |
| 0 | 151 | | if (ids.Length != 0) |
| | 152 | | { |
| 0 | 153 | | var firstId = ids[0]; |
| 0 | 154 | | if (!string.IsNullOrEmpty(firstId) |
| 0 | 155 | | && (string.IsNullOrEmpty(id) |
| 0 | 156 | | || !id.Equals(firstId, StringComparison.OrdinalIgnoreCase))) |
| | 157 | | { |
| 0 | 158 | | item.SetProviderId(provider, firstId); |
| 0 | 159 | | return ItemUpdateType.MetadataEdit; |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | return ItemUpdateType.None; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | private void SetProviderId(MusicAlbum sourceItem, MusicAlbum targetItem, MetadataProvider provider) |
| | 167 | | { |
| 0 | 168 | | var source = sourceItem.GetProviderId(provider); |
| 0 | 169 | | var target = targetItem.GetProviderId(provider); |
| 0 | 170 | | if (!string.IsNullOrEmpty(source) |
| 0 | 171 | | && (string.IsNullOrEmpty(target) |
| 0 | 172 | | || !target.Equals(source, StringComparison.Ordinal))) |
| | 173 | | { |
| 0 | 174 | | targetItem.SetProviderId(provider, source); |
| | 175 | | } |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | private ItemUpdateType SetPeople(MusicAlbum item) |
| | 179 | | { |
| 0 | 180 | | var updateType = ItemUpdateType.None; |
| | 181 | |
|
| 0 | 182 | | if (item.AlbumArtists.Any() || item.Artists.Any()) |
| | 183 | | { |
| 0 | 184 | | var people = new List<PersonInfo>(); |
| | 185 | |
|
| 0 | 186 | | foreach (var albumArtist in item.AlbumArtists) |
| | 187 | | { |
| 0 | 188 | | PeopleHelper.AddPerson(people, new PersonInfo |
| 0 | 189 | | { |
| 0 | 190 | | Name = albumArtist.Trim(), |
| 0 | 191 | | Type = PersonKind.AlbumArtist |
| 0 | 192 | | }); |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | foreach (var artist in item.Artists) |
| | 196 | | { |
| 0 | 197 | | PeopleHelper.AddPerson(people, new PersonInfo |
| 0 | 198 | | { |
| 0 | 199 | | Name = artist.Trim(), |
| 0 | 200 | | Type = PersonKind.Artist |
| 0 | 201 | | }); |
| | 202 | | } |
| | 203 | |
|
| 0 | 204 | | LibraryManager.UpdatePeople(item, people); |
| 0 | 205 | | updateType |= ItemUpdateType.MetadataEdit; |
| | 206 | | } |
| | 207 | |
|
| 0 | 208 | | return updateType; |
| | 209 | | } |
| | 210 | |
|
| | 211 | | /// <inheritdoc /> |
| | 212 | | protected override void MergeData( |
| | 213 | | MetadataResult<MusicAlbum> source, |
| | 214 | | MetadataResult<MusicAlbum> target, |
| | 215 | | MetadataField[] lockedFields, |
| | 216 | | bool replaceData, |
| | 217 | | bool mergeMetadataSettings) |
| | 218 | | { |
| 0 | 219 | | base.MergeData(source, target, lockedFields, replaceData, mergeMetadataSettings); |
| | 220 | |
|
| 0 | 221 | | var sourceItem = source.Item; |
| 0 | 222 | | var targetItem = target.Item; |
| | 223 | |
|
| 0 | 224 | | if (replaceData || targetItem.Artists.Count == 0) |
| | 225 | | { |
| 0 | 226 | | targetItem.Artists = sourceItem.Artists; |
| | 227 | | } |
| | 228 | | else |
| | 229 | | { |
| 0 | 230 | | targetItem.Artists = targetItem.Artists.Concat(sourceItem.Artists).Distinct().ToArray(); |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | if (replaceData || string.IsNullOrEmpty(targetItem.GetProviderId(MetadataProvider.MusicBrainzAlbumArtist))) |
| | 234 | | { |
| 0 | 235 | | SetProviderId(sourceItem, targetItem, MetadataProvider.MusicBrainzAlbumArtist); |
| | 236 | | } |
| | 237 | |
|
| 0 | 238 | | if (replaceData || string.IsNullOrEmpty(targetItem.GetProviderId(MetadataProvider.MusicBrainzAlbum))) |
| | 239 | | { |
| 0 | 240 | | SetProviderId(sourceItem, targetItem, MetadataProvider.MusicBrainzAlbum); |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | if (replaceData || string.IsNullOrEmpty(targetItem.GetProviderId(MetadataProvider.MusicBrainzReleaseGroup))) |
| | 244 | | { |
| 0 | 245 | | SetProviderId(sourceItem, targetItem, MetadataProvider.MusicBrainzReleaseGroup); |
| | 246 | | } |
| 0 | 247 | | } |
| | 248 | | } |
| | 249 | | } |