2018-02-06 23:08:36 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
2019-03-01 22:26:36 +00:00
|
|
|
using NzbDrone.Core.ImportLists.Exclusions;
|
2018-02-06 23:08:36 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.MetadataSource;
|
|
|
|
using NzbDrone.Core.Music;
|
2020-08-13 20:31:09 +00:00
|
|
|
using NzbDrone.Core.Music.Commands;
|
2018-05-10 23:06:02 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2018-02-06 23:08:36 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.ImportLists
|
|
|
|
{
|
|
|
|
public class ImportListSyncService : IExecute<ImportListSyncCommand>
|
|
|
|
{
|
|
|
|
private readonly IImportListFactory _importListFactory;
|
2019-03-01 22:26:36 +00:00
|
|
|
private readonly IImportListExclusionService _importListExclusionService;
|
2018-02-06 23:08:36 +00:00
|
|
|
private readonly IFetchAndParseImportList _listFetcherAndParser;
|
|
|
|
private readonly ISearchForNewAlbum _albumSearchService;
|
|
|
|
private readonly ISearchForNewArtist _artistSearchService;
|
|
|
|
private readonly IArtistService _artistService;
|
2019-12-16 21:21:32 +00:00
|
|
|
private readonly IAlbumService _albumService;
|
2018-02-06 23:08:36 +00:00
|
|
|
private readonly IAddArtistService _addArtistService;
|
2019-12-16 21:21:32 +00:00
|
|
|
private readonly IAddAlbumService _addAlbumService;
|
2018-02-06 23:08:36 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2020-08-13 20:31:09 +00:00
|
|
|
private readonly IManageCommandQueue _commandQueueManager;
|
2018-02-06 23:08:36 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2019-09-22 21:50:14 +00:00
|
|
|
public ImportListSyncService(IImportListFactory importListFactory,
|
2019-12-16 21:21:32 +00:00
|
|
|
IImportListExclusionService importListExclusionService,
|
|
|
|
IFetchAndParseImportList listFetcherAndParser,
|
|
|
|
ISearchForNewAlbum albumSearchService,
|
|
|
|
ISearchForNewArtist artistSearchService,
|
|
|
|
IArtistService artistService,
|
|
|
|
IAlbumService albumService,
|
|
|
|
IAddArtistService addArtistService,
|
|
|
|
IAddAlbumService addAlbumService,
|
|
|
|
IEventAggregator eventAggregator,
|
2020-08-13 20:31:09 +00:00
|
|
|
IManageCommandQueue commandQueueManager,
|
2019-12-16 21:21:32 +00:00
|
|
|
Logger logger)
|
2018-02-06 23:08:36 +00:00
|
|
|
{
|
|
|
|
_importListFactory = importListFactory;
|
2019-03-01 22:26:36 +00:00
|
|
|
_importListExclusionService = importListExclusionService;
|
2018-02-06 23:08:36 +00:00
|
|
|
_listFetcherAndParser = listFetcherAndParser;
|
|
|
|
_albumSearchService = albumSearchService;
|
|
|
|
_artistSearchService = artistSearchService;
|
|
|
|
_artistService = artistService;
|
2019-12-16 21:21:32 +00:00
|
|
|
_albumService = albumService;
|
2018-02-06 23:08:36 +00:00
|
|
|
_addArtistService = addArtistService;
|
2019-12-16 21:21:32 +00:00
|
|
|
_addAlbumService = addAlbumService;
|
2018-02-06 23:08:36 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2020-08-13 20:31:09 +00:00
|
|
|
_commandQueueManager = commandQueueManager;
|
2018-02-06 23:08:36 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:06:02 +00:00
|
|
|
private List<Album> SyncAll()
|
2018-02-06 23:08:36 +00:00
|
|
|
{
|
|
|
|
_logger.ProgressInfo("Starting Import List Sync");
|
|
|
|
|
|
|
|
var rssReleases = _listFetcherAndParser.Fetch();
|
|
|
|
|
|
|
|
var reports = rssReleases.ToList();
|
2018-05-10 23:06:02 +00:00
|
|
|
|
|
|
|
return ProcessReports(reports);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Album> SyncList(ImportListDefinition definition)
|
|
|
|
{
|
|
|
|
_logger.ProgressInfo(string.Format("Starting Import List Refresh for List {0}", definition.Name));
|
|
|
|
|
|
|
|
var rssReleases = _listFetcherAndParser.FetchSingleList(definition);
|
|
|
|
|
|
|
|
var reports = rssReleases.ToList();
|
|
|
|
|
|
|
|
return ProcessReports(reports);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Album> ProcessReports(List<ImportListItemInfo> reports)
|
|
|
|
{
|
2018-02-06 23:08:36 +00:00
|
|
|
var processed = new List<Album>();
|
|
|
|
var artistsToAdd = new List<Artist>();
|
2019-12-16 21:21:32 +00:00
|
|
|
var albumsToAdd = new List<Album>();
|
2018-02-06 23:08:36 +00:00
|
|
|
|
|
|
|
_logger.ProgressInfo("Processing {0} list items", reports.Count);
|
|
|
|
|
|
|
|
var reportNumber = 1;
|
|
|
|
|
2019-03-01 22:26:36 +00:00
|
|
|
var listExclusions = _importListExclusionService.All();
|
|
|
|
|
2018-02-06 23:08:36 +00:00
|
|
|
foreach (var report in reports)
|
|
|
|
{
|
|
|
|
_logger.ProgressTrace("Processing list item {0}/{1}", reportNumber, reports.Count);
|
|
|
|
|
|
|
|
reportNumber++;
|
|
|
|
|
|
|
|
var importList = _importListFactory.Get(report.ImportListId);
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
if (report.Album.IsNotNullOrWhiteSpace() || report.AlbumMusicBrainzId.IsNotNullOrWhiteSpace())
|
2018-02-06 23:08:36 +00:00
|
|
|
{
|
2019-12-16 21:21:32 +00:00
|
|
|
if (report.AlbumMusicBrainzId.IsNullOrWhiteSpace() || report.ArtistMusicBrainzId.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
MapAlbumReport(report);
|
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
ProcessAlbumReport(importList, report, listExclusions, albumsToAdd);
|
|
|
|
}
|
|
|
|
else if (report.Artist.IsNotNullOrWhiteSpace() || report.ArtistMusicBrainzId.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
if (report.ArtistMusicBrainzId.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
MapArtistReport(report);
|
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
ProcessArtistReport(importList, report, listExclusions, artistsToAdd);
|
2018-02-06 23:08:36 +00:00
|
|
|
}
|
2019-12-16 21:21:32 +00:00
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
2020-08-13 20:31:09 +00:00
|
|
|
var addedArtists = _addArtistService.AddArtists(artistsToAdd, false);
|
2020-08-25 20:16:44 +00:00
|
|
|
var addedAlbums = _addAlbumService.AddAlbums(albumsToAdd, false);
|
2019-10-16 20:04:03 +00:00
|
|
|
|
2020-08-13 20:31:09 +00:00
|
|
|
var message = string.Format($"Import List Sync Completed. Items found: {reports.Count}, Artists added: {addedArtists.Count}, Albums added: {addedAlbums.Count}");
|
2019-10-16 20:04:03 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
_logger.ProgressInfo(message);
|
2019-10-16 20:04:03 +00:00
|
|
|
|
2020-08-13 20:31:09 +00:00
|
|
|
var toRefresh = addedArtists.Select(x => x.Id).Concat(addedAlbums.Select(x => x.Artist.Value.Id)).Distinct().ToList();
|
|
|
|
if (toRefresh.Any())
|
|
|
|
{
|
|
|
|
_commandQueueManager.Push(new BulkRefreshArtistCommand(toRefresh, true));
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
return processed;
|
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
private void MapAlbumReport(ImportListItemInfo report)
|
|
|
|
{
|
|
|
|
var albumQuery = report.AlbumMusicBrainzId.IsNotNullOrWhiteSpace() ? $"lidarr:{report.AlbumMusicBrainzId}" : report.Album;
|
|
|
|
var mappedAlbum = _albumSearchService.SearchForNewAlbum(albumQuery, report.Artist)
|
|
|
|
.FirstOrDefault();
|
2019-09-22 21:50:14 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
// Break if we are looking for an album and cant find it. This will avoid us from adding the artist and possibly getting it wrong.
|
|
|
|
if (mappedAlbum == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-09-22 21:50:14 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
report.AlbumMusicBrainzId = mappedAlbum.ForeignAlbumId;
|
|
|
|
report.Album = mappedAlbum.Title;
|
|
|
|
report.Artist = mappedAlbum.ArtistMetadata?.Value?.Name;
|
|
|
|
report.ArtistMusicBrainzId = mappedAlbum.ArtistMetadata?.Value?.ForeignArtistId;
|
|
|
|
}
|
2019-03-01 22:26:36 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
private void ProcessAlbumReport(ImportListDefinition importList, ImportListItemInfo report, List<ImportListExclusion> listExclusions, List<Album> albumsToAdd)
|
|
|
|
{
|
|
|
|
if (report.AlbumMusicBrainzId == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
// Check to see if album in DB
|
|
|
|
var existingAlbum = _albumService.FindById(report.AlbumMusicBrainzId);
|
|
|
|
|
|
|
|
if (existingAlbum != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("{0} [{1}] Rejected, Album Exists in DB", report.AlbumMusicBrainzId, report.Album);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if album excluded
|
|
|
|
var excludedAlbum = listExclusions.SingleOrDefault(s => s.ForeignId == report.AlbumMusicBrainzId);
|
|
|
|
|
|
|
|
if (excludedAlbum != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("{0} [{1}] Rejected due to list exlcusion", report.AlbumMusicBrainzId, report.Album);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if artist excluded
|
|
|
|
var excludedArtist = listExclusions.SingleOrDefault(s => s.ForeignId == report.ArtistMusicBrainzId);
|
|
|
|
|
|
|
|
if (excludedArtist != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("{0} [{1}] Rejected due to list exlcusion for parent artist", report.AlbumMusicBrainzId, report.Album);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append Album if not already in DB or already on add list
|
|
|
|
if (albumsToAdd.All(s => s.ForeignAlbumId != report.AlbumMusicBrainzId))
|
|
|
|
{
|
|
|
|
var monitored = importList.ShouldMonitor != ImportListMonitorType.None;
|
2019-09-22 21:50:14 +00:00
|
|
|
|
2020-02-28 20:40:12 +00:00
|
|
|
var toAdd = new Album
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
ForeignAlbumId = report.AlbumMusicBrainzId,
|
|
|
|
Monitored = monitored,
|
2020-02-28 21:19:20 +00:00
|
|
|
AnyReleaseOk = true,
|
2020-01-03 12:49:24 +00:00
|
|
|
Artist = new Artist
|
2019-12-16 21:21:32 +00:00
|
|
|
{
|
|
|
|
Monitored = monitored,
|
2020-01-03 12:49:24 +00:00
|
|
|
RootFolderPath = importList.RootFolderPath,
|
|
|
|
QualityProfileId = importList.ProfileId,
|
|
|
|
MetadataProfileId = importList.MetadataProfileId,
|
|
|
|
Tags = importList.Tags,
|
|
|
|
AddOptions = new AddArtistOptions
|
2019-12-16 21:21:32 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
SearchForMissingAlbums = monitored,
|
2019-12-16 21:21:32 +00:00
|
|
|
Monitored = monitored,
|
2020-01-03 12:49:24 +00:00
|
|
|
Monitor = monitored ? MonitorTypes.All : MonitorTypes.None
|
|
|
|
}
|
|
|
|
},
|
2020-08-25 20:16:44 +00:00
|
|
|
AddOptions = new AddAlbumOptions
|
|
|
|
{
|
|
|
|
SearchForNewAlbum = monitored
|
|
|
|
}
|
2020-02-28 20:40:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (importList.ShouldMonitor == ImportListMonitorType.SpecificAlbum)
|
|
|
|
{
|
|
|
|
toAdd.Artist.Value.AddOptions.AlbumsToMonitor.Add(toAdd.ForeignAlbumId);
|
|
|
|
}
|
|
|
|
|
|
|
|
albumsToAdd.Add(toAdd);
|
2019-12-16 21:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void MapArtistReport(ImportListItemInfo report)
|
|
|
|
{
|
|
|
|
var mappedArtist = _artistSearchService.SearchForNewArtist(report.Artist)
|
|
|
|
.FirstOrDefault();
|
|
|
|
report.ArtistMusicBrainzId = mappedArtist?.Metadata.Value?.ForeignArtistId;
|
|
|
|
report.Artist = mappedArtist?.Metadata.Value?.Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ProcessArtistReport(ImportListDefinition importList, ImportListItemInfo report, List<ImportListExclusion> listExclusions, List<Artist> artistsToAdd)
|
|
|
|
{
|
|
|
|
if (report.ArtistMusicBrainzId == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if artist in DB
|
|
|
|
var existingArtist = _artistService.FindById(report.ArtistMusicBrainzId);
|
|
|
|
|
|
|
|
if (existingArtist != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("{0} [{1}] Rejected, Artist Exists in DB", report.ArtistMusicBrainzId, report.Artist);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if artist excluded
|
|
|
|
var excludedArtist = listExclusions.Where(s => s.ForeignId == report.ArtistMusicBrainzId).SingleOrDefault();
|
|
|
|
|
|
|
|
if (excludedArtist != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("{0} [{1}] Rejected due to list exlcusion", report.ArtistMusicBrainzId, report.Artist);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append Artist if not already in DB or already on add list
|
|
|
|
if (artistsToAdd.All(s => s.Metadata.Value.ForeignArtistId != report.ArtistMusicBrainzId))
|
|
|
|
{
|
|
|
|
var monitored = importList.ShouldMonitor != ImportListMonitorType.None;
|
|
|
|
|
|
|
|
artistsToAdd.Add(new Artist
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
Metadata = new ArtistMetadata
|
2018-02-06 23:08:36 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
ForeignArtistId = report.ArtistMusicBrainzId,
|
|
|
|
Name = report.Artist
|
|
|
|
},
|
|
|
|
Monitored = monitored,
|
|
|
|
RootFolderPath = importList.RootFolderPath,
|
|
|
|
QualityProfileId = importList.ProfileId,
|
|
|
|
MetadataProfileId = importList.MetadataProfileId,
|
|
|
|
Tags = importList.Tags,
|
|
|
|
AddOptions = new AddArtistOptions
|
|
|
|
{
|
|
|
|
SearchForMissingAlbums = monitored,
|
2019-07-25 21:22:38 +00:00
|
|
|
Monitored = monitored,
|
2020-01-03 12:49:24 +00:00
|
|
|
Monitor = monitored ? MonitorTypes.All : MonitorTypes.None
|
|
|
|
}
|
|
|
|
});
|
2018-02-06 23:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(ImportListSyncCommand message)
|
|
|
|
{
|
2018-05-10 23:06:02 +00:00
|
|
|
List<Album> processed;
|
|
|
|
|
|
|
|
if (message.DefinitionId.HasValue)
|
|
|
|
{
|
|
|
|
processed = SyncList(_importListFactory.Get(message.DefinitionId.Value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
processed = SyncAll();
|
|
|
|
}
|
2018-02-06 23:08:36 +00:00
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new ImportListSyncCompleteEvent(processed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|