2013-09-10 18:56:00 +00: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.Generic;
|
|
|
|
|
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 02:08:18 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-09-10 18:56:00 +00: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 02:08:18 +00:00
|
|
|
|
public ArtistsValidator(ILibraryManager libraryManager, IUserManager userManager, ILogger logger)
|
2013-09-10 18:56:00 +00: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-27 12:24:28 +00:00
|
|
|
|
var allItems = _libraryManager.RootFolder.GetRecursiveChildren();
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-20 00:53:18 +00:00
|
|
|
|
var allSongs = allItems.OfType<Audio>().ToList();
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
innerProgress.RegisterAction(pct => progress.Report(pct * .8));
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
2013-09-10 18:56:00 +00:00
|
|
|
|
var allArtists = await GetAllArtists(allSongs, cancellationToken, innerProgress).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
progress.Report(80);
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
2013-09-19 00:37:01 +00:00
|
|
|
|
var userLibraries = _userManager.Users
|
2013-09-20 00:53:18 +00:00
|
|
|
|
.Select(i => new Tuple<Guid, List<IHasArtist>>(i.Id, i.RootFolder.GetRecursiveChildren(i).OfType<IHasArtist>().ToList()))
|
|
|
|
|
.ToList();
|
2013-09-19 00:37:01 +00:00
|
|
|
|
|
|
|
|
|
var numArtists = allArtists.Count;
|
|
|
|
|
|
2013-09-10 18:56:00 +00:00
|
|
|
|
foreach (var artist in allArtists)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
2013-11-21 20:48:26 +00:00
|
|
|
|
// Only do this for artists accessed by name. Folder-based artists use ArtistInfoFromSongsProvider
|
|
|
|
|
if (artist.IsAccessedByName && !artist.LockedFields.Contains(MetadataFields.Genres))
|
2013-09-10 18:56:00 +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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate counts of items
|
2013-09-11 17:54:59 +00:00
|
|
|
|
//SetItemCounts(artist, null, allItems.OfType<IHasArtist>());
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-19 00:37:01 +00:00
|
|
|
|
foreach (var lib in userLibraries)
|
2013-09-10 18:56:00 +00:00
|
|
|
|
{
|
2013-09-19 00:37:01 +00:00
|
|
|
|
SetItemCounts(artist, lib.Item1, lib.Item2);
|
2013-09-10 18:56:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
2013-09-19 00:37:01 +00:00
|
|
|
|
percent /= numArtists;
|
2013-09-10 18:56:00 +00: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>
|
2013-11-21 20:48:26 +00:00
|
|
|
|
private void SetItemCounts(MusicArtist artist, Guid? userId, IEnumerable<IHasArtist> allItems)
|
2013-09-10 18:56:00 +00:00
|
|
|
|
{
|
|
|
|
|
var name = artist.Name;
|
|
|
|
|
|
|
|
|
|
var items = allItems
|
|
|
|
|
.Where(i => i.HasArtist(name))
|
2013-09-20 00:53:18 +00:00
|
|
|
|
.ToList();
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
|
|
|
|
var counts = new ItemByNameCounts
|
|
|
|
|
{
|
2013-09-20 00:53:18 +00:00
|
|
|
|
TotalCount = items.Count,
|
2013-09-10 18:56:00 +00: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>
|
|
|
|
|
/// 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-11-21 20:48:26 +00:00
|
|
|
|
private async Task<List<MusicArtist>> GetAllArtists(IEnumerable<Audio> allSongs, CancellationToken cancellationToken, IProgress<double> progress)
|
2013-09-10 18:56:00 +00:00
|
|
|
|
{
|
2013-11-21 20:48:26 +00:00
|
|
|
|
var allArtists = _libraryManager.GetAllArtists(allSongs)
|
2013-09-20 00:53:18 +00:00
|
|
|
|
.ToList();
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-11-21 20:48:26 +00:00
|
|
|
|
var returnArtists = new List<MusicArtist>(allArtists.Count);
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
2013-09-20 00:53:18 +00:00
|
|
|
|
var numArtists = allArtists.Count;
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
|
|
|
|
foreach (var artist in allArtists)
|
|
|
|
|
{
|
2013-09-25 22:36:48 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-25 22:36:48 +00:00
|
|
|
|
try
|
2013-09-10 18:56:00 +00:00
|
|
|
|
{
|
2013-09-25 22:36:48 +00:00
|
|
|
|
var artistItem = _libraryManager.GetArtist(artist);
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
2013-09-25 22:36:48 +00:00
|
|
|
|
await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-25 22:36:48 +00:00
|
|
|
|
returnArtists.Add(artistItem);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error validating Artist {0}", ex, artist);
|
|
|
|
|
}
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-25 22:36:48 +00:00
|
|
|
|
// Update progress
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numArtists;
|
2013-09-10 18:56:00 +00:00
|
|
|
|
|
2013-09-25 22:36:48 +00:00
|
|
|
|
progress.Report(100 * percent);
|
2013-09-10 18:56:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-19 00:37:01 +00:00
|
|
|
|
return returnArtists;
|
2013-09-10 18:56:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|