Radarr/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs

211 lines
7.7 KiB
C#

using System;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.History;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies.Events;
namespace NzbDrone.Core.Download.History
{
public interface IDownloadHistoryService
{
bool DownloadAlreadyImported(string downloadId);
DownloadHistory GetLatestDownloadHistoryItem(string downloadId);
}
public class DownloadHistoryService : IDownloadHistoryService,
IHandle<MovieGrabbedEvent>,
IHandle<MovieImportedEvent>,
IHandle<DownloadCompletedEvent>,
IHandle<DownloadFailedEvent>,
IHandle<DownloadIgnoredEvent>,
IHandle<MovieDeletedEvent>
{
private readonly IDownloadHistoryRepository _repository;
private readonly IHistoryService _historyService;
public DownloadHistoryService(IDownloadHistoryRepository repository, IHistoryService historyService)
{
_repository = repository;
_historyService = historyService;
}
public bool DownloadAlreadyImported(string downloadId)
{
var events = _repository.FindByDownloadId(downloadId);
// Events are ordered by date descending, if a grabbed event comes before an imported event then it was never imported
// or grabbed again after importing and should be reprocessed.
foreach (var e in events)
{
if (e.EventType == DownloadHistoryEventType.DownloadGrabbed)
{
return false;
}
if (e.EventType == DownloadHistoryEventType.DownloadImported)
{
return true;
}
}
return false;
}
public DownloadHistory GetLatestDownloadHistoryItem(string downloadId)
{
var events = _repository.FindByDownloadId(downloadId);
// Events are ordered by date descending. We'll return the most recent expected event.
foreach (var e in events)
{
if (e.EventType == DownloadHistoryEventType.DownloadGrabbed)
{
return e;
}
if (e.EventType == DownloadHistoryEventType.DownloadImported)
{
return e;
}
if (e.EventType == DownloadHistoryEventType.DownloadFailed)
{
return e;
}
}
return null;
}
public void Handle(MovieGrabbedEvent message)
{
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.DownloadGrabbed,
MovieId = message.Movie.Movie.Id,
DownloadId = message.DownloadId,
SourceTitle = message.Movie.Release.Title,
Date = DateTime.UtcNow,
Protocol = message.Movie.Release.DownloadProtocol,
IndexerId = message.Movie.Release.IndexerId,
DownloadClientId = message.DownloadClientId,
Release = message.Movie.Release
};
history.Data.Add("Indexer", message.Movie.Release.Indexer);
history.Data.Add("DownloadClient", message.DownloadClient);
history.Data.Add("DownloadClientName", message.DownloadClientName);
history.Data.Add("CustomFormatScore", message.Movie.CustomFormatScore.ToString());
_repository.Insert(history);
}
public void Handle(MovieImportedEvent message)
{
if (!message.NewDownload)
{
return;
}
var downloadId = message.DownloadId;
// Try to find the downloadId if the user used manual import (from wanted: missing) or the
// API to import and downloadId wasn't provided.
if (downloadId.IsNullOrWhiteSpace())
{
downloadId = _historyService.FindDownloadId(message);
}
if (downloadId.IsNullOrWhiteSpace())
{
return;
}
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.FileImported,
MovieId = message.MovieInfo.Movie.Id,
DownloadId = downloadId,
SourceTitle = message.MovieInfo.Path,
Date = DateTime.UtcNow,
Protocol = message.DownloadClientInfo.Protocol,
DownloadClientId = message.DownloadClientInfo.Id
};
history.Data.Add("DownloadClient", message.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", message.DownloadClientInfo.Name);
_repository.Insert(history);
}
public void Handle(DownloadCompletedEvent message)
{
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.DownloadImported,
MovieId = message.TrackedDownload.RemoteMovie.Movie.Id,
DownloadId = message.TrackedDownload.DownloadItem.DownloadId,
SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(),
Date = DateTime.UtcNow,
Protocol = message.TrackedDownload.Protocol,
DownloadClientId = message.TrackedDownload.DownloadClient
};
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
_repository.Insert(history);
}
public void Handle(DownloadFailedEvent message)
{
// Don't track failed download for an unknown download
if (message.TrackedDownload == null)
{
return;
}
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.DownloadFailed,
MovieId = message.MovieId,
DownloadId = message.DownloadId,
SourceTitle = message.SourceTitle,
Date = DateTime.UtcNow,
Protocol = message.TrackedDownload.Protocol,
DownloadClientId = message.TrackedDownload.DownloadClient
};
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
_repository.Insert(history);
}
public void Handle(DownloadIgnoredEvent message)
{
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.DownloadIgnored,
MovieId = message.MovieId,
DownloadId = message.DownloadId,
SourceTitle = message.SourceTitle,
Date = DateTime.UtcNow,
Protocol = message.DownloadClientInfo.Protocol,
DownloadClientId = message.DownloadClientInfo.Id
};
history.Data.Add("DownloadClient", message.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", message.DownloadClientInfo.Name);
_repository.Insert(history);
}
public void Handle(MovieDeletedEvent message)
{
_repository.DeleteByMovieId(message.Movie.Id);
}
}
}