using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
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
{
///
/// Class ArtistsValidator
///
public class ArtistsValidator
{
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// The _logger
///
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The library manager.
/// The logger.
public ArtistsValidator(ILibraryManager libraryManager, ILogger logger)
{
_libraryManager = libraryManager;
_logger = logger;
}
///
/// Runs the specified progress.
///
/// The progress.
/// The cancellation token.
/// Task.
public async Task Run(IProgress progress, CancellationToken cancellationToken)
{
var allItems = _libraryManager.RootFolder.GetRecursiveChildren();
var allSongs = allItems.Where(i => !i.IsFolder).OfType().ToList();
var allArtists = allSongs.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
var returnArtists = new List(allArtists.Count);
var numComplete = 0;
var numArtists = allArtists.Count;
foreach (var artist in allArtists)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var artistItem = _libraryManager.GetArtist(artist);
await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false);
returnArtists.Add(artistItem);
}
catch (IOException ex)
{
_logger.ErrorException("Error validating Artist {0}", ex, artist);
}
// Update progress
numComplete++;
double percent = numComplete;
percent /= numArtists;
progress.Report(100 * percent);
}
}
}
}