2013-05-27 16:53:10 +00:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Music
|
2013-05-27 16:53:10 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ArtistsPostScanTask
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ArtistsPostScanTask : ILibraryPostScanTask
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="ArtistsPostScanTask"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
public ArtistsPostScanTask(ILibraryManager libraryManager)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the specified progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var allItems = _libraryManager.RootFolder.RecursiveChildren.ToList();
|
|
|
|
|
|
|
|
|
|
var allArtists = await GetAllArtists(allItems).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
progress.Report(10);
|
|
|
|
|
|
|
|
|
|
var allMusicArtists = allItems.OfType<MusicArtist>().ToList();
|
2013-06-11 01:28:41 +00:00
|
|
|
|
var allSongs = allItems.OfType<Audio>().ToList();
|
2013-05-27 16:53:10 +00:00
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var artist in allArtists)
|
|
|
|
|
{
|
|
|
|
|
var musicArtist = FindMusicArtist(artist, allMusicArtists);
|
|
|
|
|
|
|
|
|
|
if (musicArtist != null)
|
|
|
|
|
{
|
2013-08-14 14:05:55 +00:00
|
|
|
|
MergeImages(musicArtist.Images, artist.Images);
|
2013-05-27 16:53:10 +00:00
|
|
|
|
|
2013-08-14 14:05:55 +00:00
|
|
|
|
// Merge backdrops
|
|
|
|
|
var backdrops = musicArtist.BackdropImagePaths.ToList();
|
|
|
|
|
backdrops.InsertRange(0, artist.BackdropImagePaths);
|
|
|
|
|
artist.BackdropImagePaths = backdrops.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
2013-06-11 01:28:41 +00:00
|
|
|
|
}
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
|
|
|
|
if (!artist.LockedFields.Contains(MetadataFields.Genres))
|
2013-06-11 01:28:41 +00:00
|
|
|
|
{
|
2013-09-06 15:23:20 +00:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var artist1 = artist;
|
|
|
|
|
|
|
|
|
|
artist.Genres = allSongs.Where(i => i.HasArtist(artist1.Name))
|
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
2013-05-27 16:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= allArtists.Length;
|
|
|
|
|
percent *= 5;
|
|
|
|
|
|
|
|
|
|
progress.Report(10 + percent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
innerProgress.RegisterAction(pct => progress.Report(15 + pct * .85));
|
|
|
|
|
|
|
|
|
|
await _libraryManager.ValidateArtists(cancellationToken, innerProgress).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:05:55 +00:00
|
|
|
|
private void MergeImages(Dictionary<ImageType, string> source, Dictionary<ImageType, string> target)
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in source.Keys
|
|
|
|
|
.ToList()
|
|
|
|
|
.Where(k => !target.ContainsKey(k)))
|
|
|
|
|
{
|
|
|
|
|
string path;
|
|
|
|
|
|
|
|
|
|
if (source.TryGetValue(key, out path))
|
|
|
|
|
{
|
|
|
|
|
target[key] = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-27 16:53:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all artists.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="allItems">All items.</param>
|
|
|
|
|
/// <returns>Task{Artist[]}.</returns>
|
|
|
|
|
private Task<Artist[]> GetAllArtists(IEnumerable<BaseItem> allItems)
|
|
|
|
|
{
|
|
|
|
|
var itemsList = allItems.OfType<Audio>().ToList();
|
|
|
|
|
|
|
|
|
|
var tasks = itemsList
|
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(i.AlbumArtist))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.AlbumArtist);
|
|
|
|
|
}
|
2013-09-05 19:00:50 +00:00
|
|
|
|
list.AddRange(i.Artists);
|
2013-05-27 16:53:10 +00:00
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
})
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.Select(i => _libraryManager.GetArtist(i));
|
|
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the music artist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="artist">The artist.</param>
|
|
|
|
|
/// <param name="allMusicArtists">All music artists.</param>
|
|
|
|
|
/// <returns>MusicArtist.</returns>
|
|
|
|
|
private static MusicArtist FindMusicArtist(Artist artist, IEnumerable<MusicArtist> allMusicArtists)
|
|
|
|
|
{
|
|
|
|
|
var musicBrainzId = artist.GetProviderId(MetadataProviders.Musicbrainz);
|
|
|
|
|
|
|
|
|
|
return allMusicArtists.FirstOrDefault(i =>
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId) && string.Equals(musicBrainzId, i.GetProviderId(MetadataProviders.Musicbrainz), StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Compare(i.Name, artist.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|