From cc0a284660f139d5f47b27a2c389973e5e888587 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 7 May 2024 17:45:28 -0700 Subject: [PATCH] New: Add series tags to Webhook and Notifiarr events --- .../CustomScript/CustomScript.cs | 23 +++++++++---- .../Notifications/Notifiarr/Notifiarr.cs | 5 +-- .../Notifications/Webhook/Webhook.cs | 5 +-- .../Notifications/Webhook/WebhookBase.cs | 34 +++++++++++++------ .../Notifications/Webhook/WebhookSeries.cs | 5 ++- src/NzbDrone.Core/Tags/TagRepository.cs | 7 ++++ 6 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index ec2974b54..6b6f3a086 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -74,7 +74,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_Release_EpisodeCount", remoteEpisode.Episodes.Count.ToString()); environmentVariables.Add("Sonarr_Release_SeasonNumber", remoteEpisode.Episodes.First().SeasonNumber.ToString()); environmentVariables.Add("Sonarr_Release_EpisodeNumbers", string.Join(",", remoteEpisode.Episodes.Select(e => e.EpisodeNumber))); @@ -121,7 +121,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); environmentVariables.Add("Sonarr_EpisodeFile_EpisodeCount", episodeFile.Episodes.Value.Count.ToString()); environmentVariables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); @@ -186,7 +186,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_EpisodeFile_Ids", string.Join(",", renamedFiles.Select(e => e.EpisodeFile.Id))); environmentVariables.Add("Sonarr_EpisodeFile_RelativePaths", string.Join("|", renamedFiles.Select(e => e.EpisodeFile.RelativePath))); environmentVariables.Add("Sonarr_EpisodeFile_Paths", string.Join("|", renamedFiles.Select(e => e.EpisodeFile.Path))); @@ -218,7 +218,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); environmentVariables.Add("Sonarr_EpisodeFile_EpisodeCount", episodeFile.Episodes.Value.Count.ToString()); environmentVariables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); @@ -257,7 +257,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); ExecuteScript(environmentVariables); } @@ -281,7 +281,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_Series_DeletedFiles", deleteMessage.DeletedFiles.ToString()); ExecuteScript(environmentVariables); @@ -350,7 +350,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Series_Year", series.Year.ToString()); environmentVariables.Add("Sonarr_Series_OriginalLanguage", IsoLanguages.Get(series.OriginalLanguage).ThreeLetterCode); environmentVariables.Add("Sonarr_Series_Genres", string.Join("|", series.Genres)); - environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", series.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Sonarr_Series_Tags", string.Join("|", GetTagLabels(series))); environmentVariables.Add("Sonarr_Download_Client", message.DownloadClientInfo?.Name ?? string.Empty); environmentVariables.Add("Sonarr_Download_Client_Type", message.DownloadClientInfo?.Type ?? string.Empty); environmentVariables.Add("Sonarr_Download_Id", message.DownloadId ?? string.Empty); @@ -411,5 +411,14 @@ namespace NzbDrone.Core.Notifications.CustomScript { return possibleParent.IsParentPath(path); } + + private List GetTagLabels(Series series) + { + return _tagRepository.GetTags(series.Tags) + .Select(s => s.Label) + .Where(l => l.IsNotNullOrWhiteSpace()) + .OrderBy(l => l) + .ToList(); + } } } diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs index cc3c9b7b7..96251eb32 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs @@ -5,6 +5,7 @@ using NzbDrone.Core.Configuration; using NzbDrone.Core.Localization; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Notifications.Webhook; +using NzbDrone.Core.Tags; using NzbDrone.Core.Tv; using NzbDrone.Core.Validation; @@ -14,8 +15,8 @@ namespace NzbDrone.Core.Notifications.Notifiarr { private readonly INotifiarrProxy _proxy; - public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService) - : base(configFileProvider, configService, localizationService) + public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService, ITagRepository tagRepository) + : base(configFileProvider, configService, localizationService, tagRepository) { _proxy = proxy; } diff --git a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs index b7cf6379d..7d0e84478 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs @@ -4,6 +4,7 @@ using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.Localization; using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.Tags; using NzbDrone.Core.Tv; using NzbDrone.Core.Validation; @@ -13,8 +14,8 @@ namespace NzbDrone.Core.Notifications.Webhook { private readonly IWebhookProxy _proxy; - public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService) - : base(configFileProvider, configService, localizationService) + public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService, ITagRepository tagRepository) + : base(configFileProvider, configService, localizationService, tagRepository) { _proxy = proxy; } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs index d114a88db..df2d29355 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.Localization; using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.Tags; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; @@ -15,12 +17,14 @@ namespace NzbDrone.Core.Notifications.Webhook private readonly IConfigFileProvider _configFileProvider; private readonly IConfigService _configService; protected readonly ILocalizationService _localizationService; + private readonly ITagRepository _tagRepository; - protected WebhookBase(IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService) + protected WebhookBase(IConfigFileProvider configFileProvider, IConfigService configService, ILocalizationService localizationService, ITagRepository tagRepository) { _configFileProvider = configFileProvider; _configService = configService; _localizationService = localizationService; + _tagRepository = tagRepository; } protected WebhookGrabPayload BuildOnGrabPayload(GrabMessage message) @@ -33,7 +37,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.Grab, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(message.Series), + Series = new WebhookSeries(message.Series, GetTagLabels(message.Series)), Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)), Release = new WebhookRelease(quality, remoteEpisode), DownloadClient = message.DownloadClientName, @@ -52,7 +56,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.Download, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(message.Series), + Series = new WebhookSeries(message.Series, GetTagLabels(message.Series)), Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), EpisodeFile = new WebhookEpisodeFile(episodeFile), Release = new WebhookGrabbedRelease(message.Release), @@ -82,7 +86,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.EpisodeFileDelete, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(deleteMessage.Series), + Series = new WebhookSeries(deleteMessage.Series, GetTagLabels(deleteMessage.Series)), Episodes = deleteMessage.EpisodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), EpisodeFile = new WebhookEpisodeFile(deleteMessage.EpisodeFile), DeleteReason = deleteMessage.Reason @@ -96,7 +100,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.SeriesAdd, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(addMessage.Series), + Series = new WebhookSeries(addMessage.Series, GetTagLabels(addMessage.Series)), }; } @@ -107,7 +111,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.SeriesDelete, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(deleteMessage.Series), + Series = new WebhookSeries(deleteMessage.Series, GetTagLabels(deleteMessage.Series)), DeletedFiles = deleteMessage.DeletedFiles }; } @@ -119,7 +123,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.Rename, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(series), + Series = new WebhookSeries(series, GetTagLabels(series)), RenamedEpisodeFiles = renamedFiles.ConvertAll(x => new WebhookRenamedEpisodeFile(x)) }; } @@ -172,7 +176,7 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.ManualInteractionRequired, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(message.Series), + Series = new WebhookSeries(message.Series, GetTagLabels(message.Series)), Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)), DownloadInfo = new WebhookDownloadClientItem(quality, message.TrackedDownload.DownloadItem), DownloadClient = message.DownloadClientInfo?.Name, @@ -192,12 +196,13 @@ namespace NzbDrone.Core.Notifications.Webhook EventType = WebhookEventType.Test, InstanceName = _configFileProvider.InstanceName, ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries() + Series = new WebhookSeries { Id = 1, Title = "Test Title", Path = "C:\\testpath", - TvdbId = 1234 + TvdbId = 1234, + Tags = new List { "test-tag" } }, Episodes = new List() { @@ -211,5 +216,14 @@ namespace NzbDrone.Core.Notifications.Webhook } }; } + + private List GetTagLabels(Series series) + { + return _tagRepository.GetTags(series.Tags) + .Select(s => s.Label) + .Where(l => l.IsNotNullOrWhiteSpace()) + .OrderBy(l => l) + .ToList(); + } } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs index c3662ee11..57200fb40 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Notifications.Webhook @@ -13,12 +14,13 @@ namespace NzbDrone.Core.Notifications.Webhook public string ImdbId { get; set; } public SeriesTypes Type { get; set; } public int Year { get; set; } + public List Tags { get; set; } public WebhookSeries() { } - public WebhookSeries(Series series) + public WebhookSeries(Series series, List tags) { Id = series.Id; Title = series.Title; @@ -29,6 +31,7 @@ namespace NzbDrone.Core.Notifications.Webhook ImdbId = series.ImdbId; Type = series.SeriesType; Year = series.Year; + Tags = tags; } } } diff --git a/src/NzbDrone.Core/Tags/TagRepository.cs b/src/NzbDrone.Core/Tags/TagRepository.cs index e24315675..39927d963 100644 --- a/src/NzbDrone.Core/Tags/TagRepository.cs +++ b/src/NzbDrone.Core/Tags/TagRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; @@ -9,6 +10,7 @@ namespace NzbDrone.Core.Tags { Tag GetByLabel(string label); Tag FindByLabel(string label); + List GetTags(HashSet tagIds); } public class TagRepository : BasicRepository, ITagRepository @@ -34,5 +36,10 @@ namespace NzbDrone.Core.Tags { return Query(c => c.Label == label).SingleOrDefault(); } + + public List GetTags(HashSet tagIds) + { + return Query(t => tagIds.Contains(t.Id)); + } } }