mirror of
https://github.com/lidarr/Lidarr
synced 2025-01-03 13:34:54 +00:00
New: Sync Lists on Add and Update (#342)
This commit is contained in:
parent
ad6e651090
commit
5a8e79eec2
7 changed files with 139 additions and 6 deletions
|
@ -12,6 +12,7 @@ namespace NzbDrone.Core.ImportLists
|
|||
public interface IFetchAndParseImportList
|
||||
{
|
||||
List<ImportListItemInfo> Fetch();
|
||||
List<ImportListItemInfo> FetchSingleList(ImportListDefinition definition);
|
||||
}
|
||||
|
||||
public class FetchAndParseImportListService : IFetchAndParseImportList
|
||||
|
@ -76,5 +77,51 @@ public List<ImportListItemInfo> Fetch()
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<ImportListItemInfo> FetchSingleList(ImportListDefinition definition)
|
||||
{
|
||||
var result = new List<ImportListItemInfo>();
|
||||
|
||||
var importList = _importListFactory.GetInstance(definition);
|
||||
|
||||
if (importList == null || !definition.EnableAutomaticAdd)
|
||||
{
|
||||
_logger.Warn("No available import lists. check your configuration.");
|
||||
return result;
|
||||
}
|
||||
|
||||
var taskList = new List<Task>();
|
||||
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
|
||||
|
||||
var importListLocal = importList;
|
||||
|
||||
var task = taskFactory.StartNew(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var importListReports = importListLocal.Fetch();
|
||||
|
||||
lock (result)
|
||||
{
|
||||
_logger.Debug("Found {0} from {1}", importListReports.Count, importList.Name);
|
||||
|
||||
result.AddRange(importListReports);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Error during Import List Sync");
|
||||
}
|
||||
}).LogExceptions();
|
||||
|
||||
taskList.Add(task);
|
||||
|
||||
|
||||
Task.WaitAll(taskList.ToArray());
|
||||
|
||||
result = result.DistinctBy(r => new { r.Artist, r.Album }).ToList();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,19 @@ namespace NzbDrone.Core.ImportLists
|
|||
{
|
||||
public class ImportListSyncCommand : Command
|
||||
{
|
||||
public int? DefinitionId { get; set; }
|
||||
|
||||
public ImportListSyncCommand()
|
||||
{
|
||||
}
|
||||
|
||||
public ImportListSyncCommand(int? definition)
|
||||
{
|
||||
DefinitionId = definition;
|
||||
}
|
||||
|
||||
public override bool SendUpdatesToClient => true;
|
||||
|
||||
public override bool UpdateScheduledTask => !DefinitionId.HasValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.MetadataSource;
|
||||
using NzbDrone.Core.Music;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.ImportLists
|
||||
{
|
||||
|
@ -44,13 +45,32 @@ public ImportListSyncService(IImportListStatusService importListStatusService,
|
|||
}
|
||||
|
||||
|
||||
private List<Album> Sync()
|
||||
private List<Album> SyncAll()
|
||||
{
|
||||
_logger.ProgressInfo("Starting Import List Sync");
|
||||
|
||||
var rssReleases = _listFetcherAndParser.Fetch();
|
||||
|
||||
var reports = rssReleases.ToList();
|
||||
|
||||
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)
|
||||
{
|
||||
var processed = new List<Album>();
|
||||
var artistsToAdd = new List<Artist>();
|
||||
|
||||
|
@ -128,7 +148,16 @@ private List<Album> Sync()
|
|||
|
||||
public void Execute(ImportListSyncCommand message)
|
||||
{
|
||||
var processed = Sync();
|
||||
List<Album> processed;
|
||||
|
||||
if (message.DefinitionId.HasValue)
|
||||
{
|
||||
processed = SyncList(_importListFactory.Get(message.DefinitionId.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
processed = SyncAll();
|
||||
}
|
||||
|
||||
_eventAggregator.PublishEvent(new ImportListSyncCompleteEvent(processed));
|
||||
}
|
||||
|
|
26
src/NzbDrone.Core/ImportLists/ImportListUpdatedHandler.cs
Normal file
26
src/NzbDrone.Core/ImportLists/ImportListUpdatedHandler.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using NzbDrone.Core.Messaging.Commands;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.ThingiProvider.Events;
|
||||
|
||||
namespace NzbDrone.Core.ImportLists
|
||||
{
|
||||
public class ImportListUpdatedHandler : IHandle<ProviderUpdatedEvent<IImportList>>, IHandle<ProviderAddedEvent<IImportList>>
|
||||
{
|
||||
private readonly IManageCommandQueue _commandQueueManager;
|
||||
|
||||
public ImportListUpdatedHandler(IManageCommandQueue commandQueueManager)
|
||||
{
|
||||
_commandQueueManager = commandQueueManager;
|
||||
}
|
||||
|
||||
public void Handle(ProviderUpdatedEvent<IImportList> message)
|
||||
{
|
||||
_commandQueueManager.Push(new ImportListSyncCommand(message.Definition.Id));
|
||||
}
|
||||
|
||||
public void Handle(ProviderAddedEvent<IImportList> message)
|
||||
{
|
||||
_commandQueueManager.Push(new ImportListSyncCommand(message.Definition.Id));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -547,6 +547,7 @@
|
|||
<Compile Include="ImportLists\ImportListBase.cs" />
|
||||
<Compile Include="ImportLists\ImportListPageableRequestChain.cs" />
|
||||
<Compile Include="ImportLists\ImportListPageableRequest.cs" />
|
||||
<Compile Include="ImportLists\ImportListUpdatedHandler.cs" />
|
||||
<Compile Include="ImportLists\IProcessImportListResponse.cs" />
|
||||
<Compile Include="ImportLists\ImportListSyncService.cs" />
|
||||
<Compile Include="ImportLists\ImportListSyncCompleteEvent.cs" />
|
||||
|
@ -1111,6 +1112,7 @@
|
|||
<Compile Include="ThingiProvider\ConfigContractNotFoundException.cs" />
|
||||
<Compile Include="ThingiProvider\Events\ProviderDeletedEvent.cs" />
|
||||
<Compile Include="ThingiProvider\Events\ProviderStatusChangedEvent.cs" />
|
||||
<Compile Include="ThingiProvider\Events\ProviderAddedEvent.cs" />
|
||||
<Compile Include="ThingiProvider\Events\ProviderUpdatedEvent.cs" />
|
||||
<Compile Include="ThingiProvider\IProvider.cs" />
|
||||
<Compile Include="ThingiProvider\IProviderConfig.cs" />
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
using NzbDrone.Common.Messaging;
|
||||
|
||||
namespace NzbDrone.Core.ThingiProvider.Events
|
||||
{
|
||||
public class ProviderAddedEvent<TProvider> : IEvent
|
||||
{
|
||||
public ProviderDefinition Definition { get; private set; }
|
||||
|
||||
public ProviderAddedEvent(ProviderDefinition definition)
|
||||
{
|
||||
Definition = definition;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -98,7 +98,9 @@ public TProviderDefinition Get(int id)
|
|||
|
||||
public virtual TProviderDefinition Create(TProviderDefinition definition)
|
||||
{
|
||||
return _providerRepository.Insert(definition);
|
||||
var addedDefinition = _providerRepository.Insert(definition);
|
||||
_eventAggregator.PublishEvent(new ProviderAddedEvent<TProvider>(definition));
|
||||
return addedDefinition;
|
||||
}
|
||||
|
||||
public virtual void Update(TProviderDefinition definition)
|
||||
|
|
Loading…
Reference in a new issue