2013-09-10 20:56:00 +02:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library.Validators
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ArtistsValidator
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ArtistsValidator
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
2013-09-17 04:08:18 +02:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="ArtistsPostScanTask" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-09-17 04:08:18 +02:00
|
|
|
|
public ArtistsValidator(ILibraryManager libraryManager, IUserManager userManager, ILogger logger)
|
2013-09-10 20:56:00 +02:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2013-09-18 04:43:34 +02:00
|
|
|
|
var allItems = _libraryManager.RootFolder.RecursiveChildren.ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
2013-09-18 04:43:34 +02:00
|
|
|
|
var allMusicArtists = allItems.OfType<MusicArtist>().ToArray();
|
|
|
|
|
var allSongs = allItems.OfType<Audio>().ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
innerProgress.RegisterAction(pct => progress.Report(pct * .8));
|
2013-09-17 04:08:18 +02:00
|
|
|
|
|
2013-09-10 20:56:00 +02:00
|
|
|
|
var allArtists = await GetAllArtists(allSongs, cancellationToken, innerProgress).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
progress.Report(80);
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
var userLibraries = _userManager.Users
|
2013-09-18 04:43:34 +02:00
|
|
|
|
.Select(i => new Tuple<Guid, IHasArtist[]>(i.Id, i.RootFolder.GetRecursiveChildren(i).OfType<IHasArtist>().ToArray()))
|
|
|
|
|
.ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
foreach (var artist in allArtists)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-09-17 04:08:18 +02:00
|
|
|
|
|
2013-09-10 20:56:00 +02:00
|
|
|
|
artist.ValidateImages();
|
|
|
|
|
artist.ValidateBackdrops();
|
|
|
|
|
|
|
|
|
|
var musicArtist = FindMusicArtist(artist, allMusicArtists);
|
|
|
|
|
|
|
|
|
|
if (musicArtist != null)
|
|
|
|
|
{
|
|
|
|
|
MergeImages(musicArtist.Images, artist.Images);
|
|
|
|
|
|
|
|
|
|
// Merge backdrops
|
|
|
|
|
var backdrops = musicArtist.BackdropImagePaths.ToList();
|
|
|
|
|
backdrops.InsertRange(0, artist.BackdropImagePaths);
|
|
|
|
|
artist.BackdropImagePaths = backdrops.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!artist.LockedFields.Contains(MetadataFields.Genres))
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var artist1 = artist;
|
|
|
|
|
|
|
|
|
|
artist.Genres = allSongs.Where(i => i.HasArtist(artist1.Name))
|
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate counts of items
|
2013-09-11 19:54:59 +02:00
|
|
|
|
//SetItemCounts(artist, null, allItems.OfType<IHasArtist>());
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
foreach (var lib in userLibraries)
|
|
|
|
|
{
|
|
|
|
|
SetItemCounts(artist, lib.Item1, lib.Item2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
2013-09-18 04:43:34 +02:00
|
|
|
|
percent /= allArtists.Length;
|
2013-09-10 20:56:00 +02:00
|
|
|
|
percent *= 20;
|
|
|
|
|
|
|
|
|
|
progress.Report(80 + percent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the item counts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="artist">The artist.</param>
|
|
|
|
|
/// <param name="userId">The user id.</param>
|
|
|
|
|
/// <param name="allItems">All items.</param>
|
|
|
|
|
private void SetItemCounts(Artist artist, Guid? userId, IEnumerable<IHasArtist> allItems)
|
|
|
|
|
{
|
|
|
|
|
var name = artist.Name;
|
|
|
|
|
|
|
|
|
|
var items = allItems
|
|
|
|
|
.Where(i => i.HasArtist(name))
|
2013-09-18 04:43:34 +02:00
|
|
|
|
.ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
var counts = new ItemByNameCounts
|
|
|
|
|
{
|
2013-09-18 04:43:34 +02:00
|
|
|
|
TotalCount = items.Length,
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
SongCount = items.OfType<Audio>().Count(),
|
|
|
|
|
|
|
|
|
|
AlbumCount = items.OfType<MusicAlbum>().Count(),
|
|
|
|
|
|
|
|
|
|
MusicVideoCount = items.OfType<MusicVideo>().Count()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (userId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
artist.UserItemCounts[userId.Value] = counts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Merges the images.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The source.</param>
|
|
|
|
|
/// <param name="target">The target.</param>
|
|
|
|
|
private void MergeImages(Dictionary<ImageType, string> source, Dictionary<ImageType, string> target)
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in source.Keys
|
2013-09-18 04:43:34 +02:00
|
|
|
|
.ToArray()
|
2013-09-10 20:56:00 +02:00
|
|
|
|
.Where(k => !target.ContainsKey(k)))
|
|
|
|
|
{
|
|
|
|
|
string path;
|
|
|
|
|
|
|
|
|
|
if (source.TryGetValue(key, out path))
|
|
|
|
|
{
|
|
|
|
|
target[key] = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all artists.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="allSongs">All songs.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <returns>Task{Artist[]}.</returns>
|
2013-09-18 04:43:34 +02:00
|
|
|
|
private async Task<Artist[]> GetAllArtists(IEnumerable<Audio> allSongs, CancellationToken cancellationToken, IProgress<double> progress)
|
2013-09-10 20:56:00 +02:00
|
|
|
|
{
|
|
|
|
|
var allArtists = allSongs
|
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(i.AlbumArtist))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.AlbumArtist);
|
|
|
|
|
}
|
|
|
|
|
list.AddRange(i.Artists);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
})
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
2013-09-18 04:43:34 +02:00
|
|
|
|
.ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
2013-09-17 04:44:06 +02:00
|
|
|
|
const int maxTasks = 3;
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
|
|
|
|
|
|
var returnArtists = new ConcurrentBag<Artist>();
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var artist in allArtists)
|
|
|
|
|
{
|
|
|
|
|
if (tasks.Count > maxTasks)
|
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
tasks.Clear();
|
|
|
|
|
|
|
|
|
|
// Safe cancellation point, when there are no pending tasks
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid accessing the foreach variable within the closure
|
|
|
|
|
var currentArtist = artist;
|
|
|
|
|
|
|
|
|
|
tasks.Add(Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 04:08:18 +02:00
|
|
|
|
var artistItem = _libraryManager.GetArtist(currentArtist);
|
|
|
|
|
|
|
|
|
|
await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
returnArtists.Add(artistItem);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error validating Artist {0}", ex, currentArtist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update progress
|
|
|
|
|
lock (progress)
|
|
|
|
|
{
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
2013-09-18 04:43:34 +02:00
|
|
|
|
percent /= allArtists.Length;
|
2013-09-10 20:56:00 +02:00
|
|
|
|
|
|
|
|
|
progress.Report(100 * percent);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
2013-09-18 04:43:34 +02:00
|
|
|
|
return returnArtists.ToArray();
|
2013-09-10 20:56:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|