2017-10-29 18:19:30 +00:00
|
|
|
using System;
|
2013-10-13 02:46:36 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2013-10-13 02:46:36 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2019-11-04 20:44:33 +00:00
|
|
|
using NzbDrone.Core.HealthCheck;
|
2013-10-13 02:46:36 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2014-01-18 11:44:36 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
2014-09-10 00:02:55 +00:00
|
|
|
using NzbDrone.Core.ThingiProvider;
|
2013-10-13 02:46:36 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications
|
|
|
|
{
|
|
|
|
public class NotificationService
|
2019-12-22 21:24:11 +00:00
|
|
|
: IHandle<MovieRenamedEvent>,
|
2017-01-24 19:17:24 +00:00
|
|
|
IHandle<MovieGrabbedEvent>,
|
2019-11-04 20:44:33 +00:00
|
|
|
IHandle<MovieDownloadedEvent>,
|
|
|
|
IHandle<HealthCheckFailedEvent>
|
2013-10-13 02:46:36 +00:00
|
|
|
{
|
|
|
|
private readonly INotificationFactory _notificationFactory;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public NotificationService(INotificationFactory notificationFactory, Logger logger)
|
|
|
|
{
|
|
|
|
_notificationFactory = notificationFactory;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:17:24 +00:00
|
|
|
private string GetMessage(Movie movie, QualityModel quality)
|
|
|
|
{
|
|
|
|
var qualityString = quality.Quality.ToString();
|
2019-12-22 22:08:53 +00:00
|
|
|
var imdbUrl = "https://www.imdb.com/title/" + movie.ImdbId + "/";
|
2017-01-24 19:17:24 +00:00
|
|
|
|
|
|
|
if (quality.Revision.Version > 1)
|
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
qualityString += " Proper";
|
2017-01-24 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 12:47:22 +00:00
|
|
|
return string.Format("{0} ({1}) [{2}] {3}",
|
2017-01-24 19:17:24 +00:00
|
|
|
movie.Title,
|
2017-01-28 14:42:37 +00:00
|
|
|
movie.Year,
|
2017-05-15 12:47:22 +00:00
|
|
|
qualityString,
|
2019-12-22 22:08:53 +00:00
|
|
|
imdbUrl);
|
2017-01-24 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool ShouldHandleMovie(ProviderDefinition definition, Movie movie)
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
if (definition.Tags.Empty())
|
2017-01-24 19:17:24 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("No tags set for this notification.");
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
if (definition.Tags.Intersect(movie.Tags).Any())
|
2017-01-24 19:17:24 +00:00
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
_logger.Debug("Notification and movie have one or more intersecting tags.");
|
2017-01-24 19:17:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: this message could be more clear
|
2018-11-23 07:03:32 +00:00
|
|
|
_logger.Debug("{0} does not have any intersecting tags with {1}. Notification will not be sent", definition.Name, movie.Title);
|
2017-01-24 19:17:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-04 20:44:33 +00:00
|
|
|
private bool ShouldHandleHealthFailure(HealthCheck.HealthCheck healthCheck, bool includeWarnings)
|
|
|
|
{
|
|
|
|
if (healthCheck.Type == HealthCheckResult.Error)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (healthCheck.Type == HealthCheckResult.Warning && includeWarnings)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:17:24 +00:00
|
|
|
public void Handle(MovieGrabbedEvent message)
|
|
|
|
{
|
|
|
|
var grabMessage = new GrabMessage
|
|
|
|
{
|
|
|
|
Message = GetMessage(message.Movie.Movie, message.Movie.ParsedMovieInfo.Quality),
|
|
|
|
Quality = message.Movie.ParsedMovieInfo.Quality,
|
|
|
|
Movie = message.Movie.Movie,
|
2019-05-02 10:52:33 +00:00
|
|
|
RemoteMovie = message.Movie,
|
|
|
|
DownloadClient = message.DownloadClient,
|
|
|
|
DownloadId = message.DownloadId
|
2017-01-24 19:17:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var notification in _notificationFactory.OnGrabEnabled())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
if (!ShouldHandleMovie(notification.Definition, message.Movie.Movie))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:17:24 +00:00
|
|
|
notification.OnGrab(grabMessage);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-04-13 03:25:58 +00:00
|
|
|
_logger.Error(ex, "Unable to send OnGrab notification to {0}", notification.Definition.Name);
|
2017-01-24 19:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieDownloadedEvent message)
|
|
|
|
{
|
|
|
|
var downloadMessage = new DownloadMessage();
|
2017-01-28 19:32:57 +00:00
|
|
|
downloadMessage.Message = GetMessage(message.Movie.Movie, message.Movie.Quality);
|
2017-01-26 20:46:55 +00:00
|
|
|
downloadMessage.MovieFile = message.MovieFile;
|
2017-01-24 19:17:24 +00:00
|
|
|
downloadMessage.Movie = message.Movie.Movie;
|
|
|
|
downloadMessage.OldMovieFiles = message.OldFiles;
|
|
|
|
downloadMessage.SourcePath = message.Movie.Path;
|
2017-10-29 18:19:30 +00:00
|
|
|
downloadMessage.DownloadId = message.DownloadId;
|
2017-01-24 19:17:24 +00:00
|
|
|
|
|
|
|
foreach (var notification in _notificationFactory.OnDownloadEnabled())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (ShouldHandleMovie(notification.Definition, message.Movie.Movie))
|
|
|
|
{
|
|
|
|
if (downloadMessage.OldMovieFiles.Empty() || ((NotificationDefinition)notification.Definition).OnUpgrade)
|
|
|
|
{
|
|
|
|
notification.OnDownload(downloadMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Warn(ex, "Unable to send OnDownload notification to: " + notification.Definition.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieRenamedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var notification in _notificationFactory.OnRenameEnabled())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (ShouldHandleMovie(notification.Definition, message.Movie))
|
|
|
|
{
|
|
|
|
notification.OnMovieRename(message.Movie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Warn(ex, "Unable to send OnRename notification to: " + notification.Definition.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 20:44:33 +00:00
|
|
|
|
|
|
|
public void Handle(HealthCheckFailedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var notification in _notificationFactory.OnHealthIssueEnabled())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (ShouldHandleHealthFailure(message.HealthCheck, ((NotificationDefinition)notification.Definition).IncludeHealthWarnings))
|
|
|
|
{
|
|
|
|
notification.OnHealthIssue(message.HealthCheck);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Warn(ex, "Unable to send OnHealthIssue notification to: " + notification.Definition.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-13 02:46:36 +00:00
|
|
|
}
|
|
|
|
}
|