jellyfin-server/MediaBrowser.Server.Implementations/Library/Validators/ArtistsValidator.cs

88 lines
2.7 KiB
C#
Raw Normal View History

2015-01-17 18:15:09 +00:00
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
{
/// <summary>
/// Class ArtistsValidator
/// </summary>
public class ArtistsValidator
{
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
/// <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="logger">The logger.</param>
2015-01-17 18:15:09 +00:00
public ArtistsValidator(ILibraryManager libraryManager, ILogger logger)
{
_libraryManager = libraryManager;
_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)
{
2015-01-25 06:34:50 +00:00
var allSongs = _libraryManager.RootFolder
.GetRecursiveChildren(i => !i.IsFolder && (i is IHasArtist))
.Cast<IHasArtist>()
.ToList();
var allArtists = allSongs.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
2013-11-21 20:48:26 +00:00
var returnArtists = new List<MusicArtist>(allArtists.Count);
var numComplete = 0;
var numArtists = allArtists.Count;
foreach (var artist in allArtists)
{
2013-09-25 22:36:48 +00:00
cancellationToken.ThrowIfCancellationRequested();
2013-09-25 22:36:48 +00:00
try
{
2013-09-25 22:36:48 +00:00
var artistItem = _libraryManager.GetArtist(artist);
2013-09-25 22:36:48 +00:00
await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false);
2013-09-25 22:36:48 +00:00
returnArtists.Add(artistItem);
}
catch (IOException ex)
{
_logger.ErrorException("Error validating Artist {0}", ex, artist);
}
2013-09-25 22:36:48 +00:00
// Update progress
numComplete++;
double percent = numComplete;
percent /= numArtists;
2013-09-25 22:36:48 +00:00
progress.Report(100 * percent);
}
}
}
}