2017-09-16 02:49:38 +00:00
using NLog ;
2017-05-07 16:58:24 +00:00
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Instrumentation.Extensions ;
using NzbDrone.Core.Exceptions ;
using NzbDrone.Core.MediaFiles ;
using NzbDrone.Core.Messaging.Commands ;
using NzbDrone.Core.Messaging.Events ;
2017-07-08 12:08:24 +00:00
using NzbDrone.Core.MetadataSource ;
2017-05-07 16:58:24 +00:00
using NzbDrone.Core.Music.Commands ;
using NzbDrone.Core.Music.Events ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
namespace NzbDrone.Core.Music
{
public class RefreshArtistService : IExecute < RefreshArtistCommand >
{
private readonly IProvideArtistInfo _artistInfo ;
private readonly IArtistService _artistService ;
2018-01-28 06:27:33 +00:00
private readonly IAddAlbumService _addAlbumService ;
2018-01-18 02:28:47 +00:00
private readonly IAlbumService _albumService ;
2017-06-18 02:27:01 +00:00
private readonly IRefreshAlbumService _refreshAlbumService ;
2017-07-03 18:39:06 +00:00
private readonly IRefreshTrackService _refreshTrackService ;
2017-05-07 16:58:24 +00:00
private readonly IEventAggregator _eventAggregator ;
private readonly IDiskScanService _diskScanService ;
2017-05-07 22:50:07 +00:00
private readonly ICheckIfArtistShouldBeRefreshed _checkIfArtistShouldBeRefreshed ;
2017-05-07 16:58:24 +00:00
private readonly Logger _logger ;
public RefreshArtistService ( IProvideArtistInfo artistInfo ,
IArtistService artistService ,
2018-01-28 06:27:33 +00:00
IAddAlbumService addAlbumService ,
2018-01-18 02:28:47 +00:00
IAlbumService albumService ,
2017-06-18 02:27:01 +00:00
IRefreshAlbumService refreshAlbumService ,
2017-07-03 18:39:06 +00:00
IRefreshTrackService refreshTrackService ,
2017-05-07 16:58:24 +00:00
IEventAggregator eventAggregator ,
IDiskScanService diskScanService ,
2017-05-07 22:50:07 +00:00
ICheckIfArtistShouldBeRefreshed checkIfArtistShouldBeRefreshed ,
2017-05-07 16:58:24 +00:00
Logger logger )
{
_artistInfo = artistInfo ;
_artistService = artistService ;
2018-01-28 06:27:33 +00:00
_addAlbumService = addAlbumService ;
2018-01-18 02:28:47 +00:00
_albumService = albumService ;
2017-06-18 02:27:01 +00:00
_refreshAlbumService = refreshAlbumService ;
2017-07-03 18:39:06 +00:00
_refreshTrackService = refreshTrackService ;
2017-05-07 16:58:24 +00:00
_eventAggregator = eventAggregator ;
_diskScanService = diskScanService ;
2017-05-07 22:50:07 +00:00
_checkIfArtistShouldBeRefreshed = checkIfArtistShouldBeRefreshed ;
2017-05-07 16:58:24 +00:00
_logger = logger ;
}
private void RefreshArtistInfo ( Artist artist )
{
2017-06-13 02:02:17 +00:00
_logger . ProgressInfo ( "Updating Info for {0}" , artist . Name ) ;
2017-05-07 16:58:24 +00:00
2017-06-18 02:27:01 +00:00
Tuple < Artist , List < Album > > tuple ;
2017-05-07 16:58:24 +00:00
try
{
2017-11-26 03:51:37 +00:00
tuple = _artistInfo . GetArtistInfo ( artist . ForeignArtistId , artist . MetadataProfileId ) ;
2017-05-07 16:58:24 +00:00
}
catch ( ArtistNotFoundException )
{
2017-07-03 18:39:06 +00:00
_logger . Error ( "Artist '{0}' (LidarrAPI {1}) was not found, it may have been removed from Metadata sources." , artist . Name , artist . ForeignArtistId ) ;
2017-05-07 16:58:24 +00:00
return ;
}
var artistInfo = tuple . Item1 ;
2017-06-13 02:02:17 +00:00
if ( artist . ForeignArtistId ! = artistInfo . ForeignArtistId )
2017-05-07 16:58:24 +00:00
{
2017-07-03 18:39:06 +00:00
_logger . Warn ( "Artist '{0}' (Artist {1}) was replaced with '{2}' (LidarrAPI {3}), because the original was a duplicate." , artist . Name , artist . ForeignArtistId , artistInfo . Name , artistInfo . ForeignArtistId ) ;
2017-06-13 02:02:17 +00:00
artist . ForeignArtistId = artistInfo . ForeignArtistId ;
2017-05-07 16:58:24 +00:00
}
2017-06-13 02:02:17 +00:00
artist . Name = artistInfo . Name ;
2017-05-07 16:58:24 +00:00
artist . Overview = artistInfo . Overview ;
artist . Status = artistInfo . Status ;
2017-06-13 02:02:17 +00:00
artist . CleanName = artistInfo . CleanName ;
2017-08-14 02:58:42 +00:00
artist . SortName = artistInfo . SortName ;
2017-05-07 16:58:24 +00:00
artist . LastInfoSync = DateTime . UtcNow ;
artist . Images = artistInfo . Images ;
artist . Genres = artistInfo . Genres ;
2017-09-16 02:49:38 +00:00
artist . Links = artistInfo . Links ;
2018-04-04 03:43:07 +00:00
artist . Ratings = artistInfo . Ratings ;
2017-09-29 03:03:13 +00:00
artist . Disambiguation = artistInfo . Disambiguation ;
artist . ArtistType = artistInfo . ArtistType ;
2017-05-07 16:58:24 +00:00
try
{
artist . Path = new DirectoryInfo ( artist . Path ) . FullName ;
artist . Path = artist . Path . GetActualCasing ( ) ;
}
catch ( Exception e )
{
_logger . Warn ( e , "Couldn't update artist path for " + artist . Path ) ;
}
2018-01-28 06:27:33 +00:00
var remoteAlbums = tuple . Item2 . DistinctBy ( m = > new { m . ForeignAlbumId , m . ReleaseDate } ) . ToList ( ) ;
// Get list of DB current db albums for artist
var existingAlbums = _albumService . GetAlbumsByArtist ( artist . Id ) ;
var newAlbumsList = new List < Album > ( ) ;
var updateAlbumsList = new List < Album > ( ) ;
// Cycle thru albums
foreach ( var album in remoteAlbums )
{
// Check for album in existing albums, if not set properties and add to new list
var albumToRefresh = existingAlbums . FirstOrDefault ( s = > s . ForeignAlbumId = = album . ForeignAlbumId ) ;
if ( albumToRefresh ! = null )
{
existingAlbums . Remove ( albumToRefresh ) ;
updateAlbumsList . Add ( albumToRefresh ) ;
}
else
{
newAlbumsList . Add ( album ) ;
}
}
// Update new albums with artist info and correct monitored status
newAlbumsList = UpdateAlbums ( artist , newAlbumsList ) ;
_artistService . UpdateArtist ( artist ) ;
_addAlbumService . AddAlbums ( newAlbumsList ) ;
_refreshAlbumService . RefreshAlbumInfo ( updateAlbumsList ) ;
_albumService . DeleteMany ( existingAlbums ) ;
2017-05-07 16:58:24 +00:00
2018-03-20 01:56:05 +00:00
_eventAggregator . PublishEvent ( new AlbumInfoRefreshedEvent ( artist , newAlbumsList , updateAlbumsList ) ) ;
2017-06-13 02:02:17 +00:00
_logger . Debug ( "Finished artist refresh for {0}" , artist . Name ) ;
2017-05-07 16:58:24 +00:00
_eventAggregator . PublishEvent ( new ArtistUpdatedEvent ( artist ) ) ;
}
2018-01-28 06:27:33 +00:00
private List < Album > UpdateAlbums ( Artist artist , List < Album > albumsToUpdate )
{
foreach ( var album in albumsToUpdate )
{
album . ArtistId = artist . Id ;
album . ProfileId = artist . ProfileId ;
album . Monitored = artist . Monitored ;
}
return albumsToUpdate ;
}
2017-05-07 16:58:24 +00:00
public void Execute ( RefreshArtistCommand message )
{
_eventAggregator . PublishEvent ( new ArtistRefreshStartingEvent ( message . Trigger = = CommandTrigger . Manual ) ) ;
if ( message . ArtistId . HasValue )
{
var artist = _artistService . GetArtist ( message . ArtistId . Value ) ;
RefreshArtistInfo ( artist ) ;
}
else
{
2017-06-13 02:02:17 +00:00
var allArtists = _artistService . GetAllArtists ( ) . OrderBy ( c = > c . Name ) . ToList ( ) ;
2017-05-07 16:58:24 +00:00
foreach ( var artist in allArtists )
{
2017-05-07 22:50:07 +00:00
if ( message . Trigger = = CommandTrigger . Manual | | _checkIfArtistShouldBeRefreshed . ShouldRefresh ( artist ) )
2017-05-07 16:58:24 +00:00
{
try
{
RefreshArtistInfo ( artist ) ;
}
catch ( Exception e )
{
_logger . Error ( e , "Couldn't refresh info for {0}" , artist ) ;
}
}
else
{
try
{
2017-06-13 02:02:17 +00:00
_logger . Info ( "Skipping refresh of artist: {0}" , artist . Name ) ;
2017-06-19 12:56:42 +00:00
_diskScanService . Scan ( artist ) ;
2017-05-07 16:58:24 +00:00
}
catch ( Exception e )
{
_logger . Error ( e , "Couldn't rescan artist {0}" , artist ) ;
}
}
}
}
}
}
}