From 1937a8e27caa5871fbe348b46e83c9ebfe66d405 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 8 Apr 2018 00:38:12 -0400 Subject: [PATCH] Fixed: Update Emby Notification (#284) --- .../MediaBrowser/MediaBrowser.cs | 10 +-- .../MediaBrowser/MediaBrowserProxy.cs | 67 ++++++++++++++++--- .../MediaBrowser/MediaBrowserService.cs | 7 +- .../MediaBrowser/Model/EmbyMediaFolder.cs | 10 +++ .../Model/EmbyMediaFoldersResponse.cs | 9 +++ .../MediaBrowser/Model/EmbyMediaUpdateInfo.cs | 10 +++ src/NzbDrone.Core/NzbDrone.Core.csproj | 3 + 7 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFolder.cs create mode 100644 src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFoldersResponse.cs create mode 100644 src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaUpdateInfo.cs diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowser.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowser.cs index 3a4af46a6..2b5dc52f6 100644 --- a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowser.cs +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowser.cs @@ -32,6 +32,11 @@ namespace NzbDrone.Core.Notifications.Emby { _mediaBrowserService.Notify(Settings, ALBUM_DOWNLOADED_TITLE_BRANDED, message.Message); } + + if (Settings.UpdateLibrary) + { + _mediaBrowserService.Update(Settings, message.Artist); + } } public override void OnDownload(TrackDownloadMessage message) @@ -40,11 +45,6 @@ namespace NzbDrone.Core.Notifications.Emby { _mediaBrowserService.Notify(Settings, TRACK_DOWNLOADED_TITLE_BRANDED, message.Message); } - - if (Settings.UpdateLibrary) - { - _mediaBrowserService.Update(Settings, message.Artist); - } } public override void OnRename(Artist artist) diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserProxy.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserProxy.cs index 7bc725bab..6d51479b7 100644 --- a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserProxy.cs +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserProxy.cs @@ -1,6 +1,9 @@ +using System.Collections.Generic; +using System.Linq; using NLog; using NzbDrone.Common.Http; using NzbDrone.Common.Serializer; +using NzbDrone.Core.Notifications.MediaBrowser.Model; namespace NzbDrone.Core.Notifications.Emby { @@ -20,22 +23,52 @@ namespace NzbDrone.Core.Notifications.Emby var path = "/Notifications/Admin"; var request = BuildRequest(path, settings); request.Headers.ContentType = "application/json"; + request.Method = HttpMethod.POST; request.SetContent(new - { - Name = title, - Description = message, - ImageUrl = "https://raw.github.com/lidarr/Lidarr/develop/Logo/64.png" - }.ToJson()); + { + Name = title, + Description = message, + ImageUrl = "https://raw.github.com/lidarr/Lidarr/develop/Logo/64.png" + }.ToJson()); ProcessRequest(request, settings); } - public void Update(MediaBrowserSettings settings, string mbId) + public void Update(MediaBrowserSettings settings, List musicCollectionPaths) { - var path = string.Format("/Library/Artist/Updated?tvdbid={0}", mbId); //TODO: Get Emby to add a new Library Route - var request = BuildRequest(path, settings); - request.Headers.Add("Content-Length", "0"); + string path; + HttpRequest request; + + if (musicCollectionPaths.Any()) + { + path = "/Library/Media/Updated"; + request = BuildRequest(path, settings); + request.Headers.ContentType = "application/json"; + + var updateInfo = new List(); + + foreach (var colPath in musicCollectionPaths) + { + updateInfo.Add(new EmbyMediaUpdateInfo + { + Path = colPath, + UpdateType = "Created" + }); + } + + request.SetContent(new + { + Updates = updateInfo + }.ToJson()); + } + else + { + path = "/Library/Refresh"; + request = BuildRequest(path, settings); + } + + request.Method = HttpMethod.POST; ProcessRequest(request, settings); } @@ -44,7 +77,8 @@ namespace NzbDrone.Core.Notifications.Emby { request.Headers.Add("X-MediaBrowser-Token", settings.ApiKey); - var response = _httpClient.Post(request); + var response = _httpClient.Execute(request); + _logger.Trace("Response: {0}", response.Content); CheckForError(response); @@ -55,7 +89,7 @@ namespace NzbDrone.Core.Notifications.Emby private HttpRequest BuildRequest(string path, MediaBrowserSettings settings) { var url = string.Format(@"http://{0}/mediabrowser", settings.Address); - + return new HttpRequestBuilder(url).Resource(path).Build(); } @@ -65,5 +99,16 @@ namespace NzbDrone.Core.Notifications.Emby //TODO: actually check for the error } + + public List GetArtist(MediaBrowserSettings settings) + { + var path = "/Library/MediaFolders"; + var request = BuildRequest(path, settings); + request.Method = HttpMethod.GET; + + var response = ProcessRequest(request, settings); + + return Json.Deserialize(response).Items; + } } } diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserService.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserService.cs index c39b5d1c6..34cae900c 100644 --- a/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserService.cs +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserService.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net; using FluentValidation.Results; using NLog; @@ -32,7 +33,11 @@ namespace NzbDrone.Core.Notifications.Emby public void Update(MediaBrowserSettings settings, Artist artist) { - _proxy.Update(settings, artist.ForeignArtistId); + var folders = _proxy.GetArtist(settings); + + var musicPaths = folders.Select(e => e.CollectionType = "music").ToList(); + + _proxy.Update(settings, musicPaths); } public ValidationFailure Test(MediaBrowserSettings settings) diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFolder.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFolder.cs new file mode 100644 index 000000000..e6a7c3559 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFolder.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Notifications.MediaBrowser.Model +{ + public class EmbyMediaFolder + { + public string Path { get; set; } + public string CollectionType { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFoldersResponse.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFoldersResponse.cs new file mode 100644 index 000000000..dd693001c --- /dev/null +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaFoldersResponse.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Notifications.MediaBrowser.Model +{ + public class EmbyMediaFoldersResponse + { + public List Items { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaUpdateInfo.cs b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaUpdateInfo.cs new file mode 100644 index 000000000..38cd68694 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/MediaBrowser/Model/EmbyMediaUpdateInfo.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Notifications.MediaBrowser.Model +{ + public class EmbyMediaUpdateInfo + { + public string Path { get; set; } + public string UpdateType { get; set; } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 21b2a05f0..4af86d9b7 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -873,6 +873,9 @@ + + +