2017-09-18 03:20:36 +00:00
|
|
|
using NLog;
|
2017-06-18 02:27:01 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music.Events;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2018-01-18 02:28:47 +00:00
|
|
|
using NzbDrone.Core.MetadataSource;
|
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
|
|
|
using NzbDrone.Core.Exceptions;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Music.Commands;
|
|
|
|
|
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
{
|
|
|
|
public interface IRefreshAlbumService
|
|
|
|
{
|
2018-01-18 02:28:47 +00:00
|
|
|
void RefreshAlbumInfo(Album album);
|
2018-05-28 07:49:34 +00:00
|
|
|
void RefreshAlbumInfo(List<Album> albums, bool forceAlbumRefresh);
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
public class RefreshAlbumService : IRefreshAlbumService, IExecute<RefreshAlbumCommand>
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
|
|
|
private readonly IAlbumService _albumService;
|
2018-01-18 02:28:47 +00:00
|
|
|
private readonly IArtistService _artistService;
|
2018-12-15 00:02:43 +00:00
|
|
|
private readonly IArtistMetadataRepository _artistMetadataRepository;
|
|
|
|
private readonly IReleaseService _releaseService;
|
2018-01-18 02:28:47 +00:00
|
|
|
private readonly IProvideAlbumInfo _albumInfo;
|
2017-06-18 02:27:01 +00:00
|
|
|
private readonly IRefreshTrackService _refreshTrackService;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2018-02-13 01:32:18 +00:00
|
|
|
private readonly ICheckIfAlbumShouldBeRefreshed _checkIfAlbumShouldBeRefreshed;
|
2017-06-18 02:27:01 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
public RefreshAlbumService(IAlbumService albumService,
|
|
|
|
IArtistService artistService,
|
2018-12-15 00:02:43 +00:00
|
|
|
IArtistMetadataRepository artistMetadataRepository,
|
|
|
|
IReleaseService releaseService,
|
2018-01-18 02:28:47 +00:00
|
|
|
IProvideAlbumInfo albumInfo,
|
|
|
|
IRefreshTrackService refreshTrackService,
|
|
|
|
IEventAggregator eventAggregator,
|
2018-02-13 01:32:18 +00:00
|
|
|
ICheckIfAlbumShouldBeRefreshed checkIfAlbumShouldBeRefreshed,
|
2018-01-18 02:28:47 +00:00
|
|
|
Logger logger)
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
|
|
|
_albumService = albumService;
|
2018-01-18 02:28:47 +00:00
|
|
|
_artistService = artistService;
|
2018-12-15 00:02:43 +00:00
|
|
|
_artistMetadataRepository = artistMetadataRepository;
|
|
|
|
_releaseService = releaseService;
|
2018-01-18 02:28:47 +00:00
|
|
|
_albumInfo = albumInfo;
|
2017-06-18 02:27:01 +00:00
|
|
|
_refreshTrackService = refreshTrackService;
|
|
|
|
_eventAggregator = eventAggregator;
|
2018-02-13 01:32:18 +00:00
|
|
|
_checkIfAlbumShouldBeRefreshed = checkIfAlbumShouldBeRefreshed;
|
2017-06-18 02:27:01 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-05-28 07:49:34 +00:00
|
|
|
public void RefreshAlbumInfo(List<Album> albums, bool forceAlbumRefresh)
|
2018-01-28 06:27:33 +00:00
|
|
|
{
|
|
|
|
foreach (var album in albums)
|
|
|
|
{
|
2018-05-28 07:49:34 +00:00
|
|
|
if (forceAlbumRefresh || _checkIfAlbumShouldBeRefreshed.ShouldRefresh(album))
|
2018-02-13 01:32:18 +00:00
|
|
|
{
|
|
|
|
RefreshAlbumInfo(album);
|
|
|
|
}
|
2018-01-28 06:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
public void RefreshAlbumInfo(Album album)
|
|
|
|
{
|
|
|
|
_logger.ProgressInfo("Updating Info for {0}", album.Title);
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
Tuple<string, Album, List<ArtistMetadata>> tuple;
|
2018-01-18 02:28:47 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
tuple = _albumInfo.GetAlbumInfo(album.ForeignAlbumId);
|
2018-01-18 02:28:47 +00:00
|
|
|
}
|
|
|
|
catch (AlbumNotFoundException)
|
|
|
|
{
|
|
|
|
_logger.Error(
|
|
|
|
"Album '{0}' (LidarrAPI {1}) was not found, it may have been removed from Metadata sources.",
|
|
|
|
album.Title, album.ForeignAlbumId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
_artistMetadataRepository.UpsertMany(tuple.Item3);
|
|
|
|
|
|
|
|
var albumInfo = tuple.Item2;
|
2018-01-18 02:28:47 +00:00
|
|
|
|
|
|
|
if (album.ForeignAlbumId != albumInfo.ForeignAlbumId)
|
|
|
|
{
|
|
|
|
_logger.Warn(
|
|
|
|
"Album '{0}' (Album {1}) was replaced with '{2}' (LidarrAPI {3}), because the original was a duplicate.",
|
|
|
|
album.Title, album.ForeignAlbumId, albumInfo.Title, albumInfo.ForeignAlbumId);
|
|
|
|
album.ForeignAlbumId = albumInfo.ForeignAlbumId;
|
|
|
|
}
|
|
|
|
|
|
|
|
album.LastInfoSync = DateTime.UtcNow;
|
|
|
|
album.CleanTitle = albumInfo.CleanTitle;
|
|
|
|
album.Title = albumInfo.Title ?? "Unknown";
|
2018-12-15 00:02:43 +00:00
|
|
|
album.Overview = albumInfo.Overview;
|
2018-07-20 22:33:56 +00:00
|
|
|
album.Disambiguation = albumInfo.Disambiguation;
|
2018-01-18 02:28:47 +00:00
|
|
|
album.AlbumType = albumInfo.AlbumType;
|
|
|
|
album.SecondaryTypes = albumInfo.SecondaryTypes;
|
|
|
|
album.Genres = albumInfo.Genres;
|
|
|
|
album.Images = albumInfo.Images;
|
2018-12-15 00:02:43 +00:00
|
|
|
album.Links = albumInfo.Links;
|
2018-01-18 02:28:47 +00:00
|
|
|
album.ReleaseDate = albumInfo.ReleaseDate;
|
2018-04-08 06:48:34 +00:00
|
|
|
album.Ratings = albumInfo.Ratings;
|
2018-12-15 00:02:43 +00:00
|
|
|
album.AlbumReleases = new List<AlbumRelease>();
|
|
|
|
|
|
|
|
var remoteReleases = albumInfo.AlbumReleases.Value.DistinctBy(m => m.ForeignReleaseId).ToList();
|
2019-01-12 16:56:13 +00:00
|
|
|
|
|
|
|
// Search both ways to make sure we properly deal with releases that have been moved from one album to another
|
|
|
|
// as well as deleting any releases that have been removed from an album.
|
|
|
|
// note that under normal circumstances, a release would be captured by both queries.
|
|
|
|
var existingReleasesByAlbum = _releaseService.GetReleasesByAlbum(album.Id);
|
|
|
|
var existingReleasesById = _releaseService.GetReleasesByForeignReleaseId(remoteReleases.Select(x => x.ForeignReleaseId).ToList());
|
|
|
|
var existingReleases = existingReleasesByAlbum.Union(existingReleasesById).DistinctBy(x => x.Id).ToList();
|
2018-12-15 00:02:43 +00:00
|
|
|
|
|
|
|
var newReleaseList = new List<AlbumRelease>();
|
|
|
|
var updateReleaseList = new List<AlbumRelease>();
|
|
|
|
|
|
|
|
foreach (var release in remoteReleases)
|
|
|
|
{
|
|
|
|
release.AlbumId = album.Id;
|
|
|
|
var releaseToRefresh = existingReleases.SingleOrDefault(r => r.ForeignReleaseId == release.ForeignReleaseId);
|
|
|
|
|
|
|
|
if (releaseToRefresh != null)
|
|
|
|
{
|
|
|
|
existingReleases.Remove(releaseToRefresh);
|
|
|
|
release.Id = releaseToRefresh.Id;
|
|
|
|
release.Monitored = releaseToRefresh.Monitored;
|
|
|
|
updateReleaseList.Add(release);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
release.Monitored = false;
|
|
|
|
newReleaseList.Add(release);
|
|
|
|
}
|
|
|
|
album.AlbumReleases.Value.Add(release);
|
|
|
|
}
|
2018-01-18 02:28:47 +00:00
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
_logger.Debug("{0} Deleting {1}, Updating {2}, Adding {3} releases",
|
|
|
|
album, existingReleases.Count, updateReleaseList.Count, newReleaseList.Count);
|
|
|
|
|
|
|
|
// Delete first to avoid hitting distinct constraints
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseService.DeleteMany(existingReleases);
|
2019-01-12 16:56:13 +00:00
|
|
|
_releaseService.UpdateMany(updateReleaseList);
|
|
|
|
_releaseService.InsertMany(newReleaseList);
|
2018-12-15 00:02:43 +00:00
|
|
|
|
|
|
|
if (album.AlbumReleases.Value.Count(x => x.Monitored) == 0)
|
|
|
|
{
|
|
|
|
var toMonitor = album.AlbumReleases.Value.OrderByDescending(x => x.TrackCount).First();
|
|
|
|
toMonitor.Monitored = true;
|
|
|
|
_releaseService.UpdateMany(new List<AlbumRelease> { toMonitor });
|
|
|
|
}
|
2018-01-18 02:28:47 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
_refreshTrackService.RefreshTrackInfo(album);
|
2018-01-28 06:27:33 +00:00
|
|
|
_albumService.UpdateMany(new List<Album>{album});
|
2018-04-09 03:14:26 +00:00
|
|
|
|
|
|
|
_logger.Debug("Finished album refresh for {0}", album.Title);
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(RefreshAlbumCommand message)
|
|
|
|
{
|
|
|
|
if (message.AlbumId.HasValue)
|
|
|
|
{
|
|
|
|
var album = _albumService.GetAlbum(message.AlbumId.Value);
|
2018-12-15 00:02:43 +00:00
|
|
|
var artist = _artistService.GetArtistByMetadataId(album.ArtistMetadataId);
|
2018-01-18 02:28:47 +00:00
|
|
|
RefreshAlbumInfo(album);
|
|
|
|
_eventAggregator.PublishEvent(new ArtistUpdatedEvent(artist));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|