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 ;
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 ,
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 ;
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-06-13 02:02:17 +00:00
tuple = _artistInfo . GetArtistInfo ( artist . ForeignArtistId ) ;
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 ;
artist . NameSlug = artistInfo . NameSlug ;
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 ;
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 ) ;
}
2017-07-03 18:39:06 +00:00
//artist.Albums = UpdateAlbums(artist, artistInfo); # We don't need this since we don't store albums in artist table.
2017-05-07 16:58:24 +00:00
_artistService . UpdateArtist ( artist ) ;
2017-07-03 18:39:06 +00:00
2017-06-18 02:27:01 +00:00
_refreshAlbumService . RefreshAlbumInfo ( artist , tuple . Item2 ) ;
2017-07-03 18:39:06 +00:00
foreach ( var album in tuple . Item2 )
{
_refreshTrackService . RefreshTrackInfo ( album , album . Tracks ) ;
}
2017-05-07 16:58:24 +00:00
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 ) ) ;
}
private List < Album > UpdateAlbums ( Artist artist , Artist artistInfo )
{
2017-06-18 02:27:01 +00:00
var albums = artistInfo . Albums . DistinctBy ( s = > s . ForeignAlbumId ) . ToList ( ) ;
2017-05-07 16:58:24 +00:00
foreach ( var album in albums )
{
2017-06-18 02:27:01 +00:00
var existingAlbum = artist . Albums . FirstOrDefault ( s = > s . ForeignAlbumId = = album . ForeignAlbumId ) ;
2017-05-07 16:58:24 +00:00
//Todo: Should this should use the previous season's monitored state?
if ( existingAlbum = = null )
{
//if (album.SeasonNumber == 0)
//{
// album.Monitored = false;
// continue;
//}
2017-06-13 02:02:17 +00:00
_logger . Debug ( "New album ({0}) for artist: [{1}] {2}, setting monitored to true" , album . Title , artist . ForeignArtistId , artist . Name ) ;
2017-05-07 16:58:24 +00:00
album . Monitored = true ;
}
else
{
album . Monitored = existingAlbum . Monitored ;
}
}
return albums ;
}
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 ) ;
}
}
}
}
}
}
}