From 0a657302f79eb83c0c28d79999a1d0192f39f350 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 23 Nov 2016 16:38:57 -0800 Subject: [PATCH] New: Support for Plex Media Server 1.3.0's new JSON responses --- .../Plex/Models/PlexPreferences.cs | 8 ++- .../Notifications/Plex/Models/PlexResponse.cs | 7 +++ .../Notifications/Plex/Models/PlexSection.cs | 28 +++++++--- .../Plex/Models/PlexSectionItem.cs | 5 ++ .../Notifications/Plex/PlexServerProxy.cs | 51 ++++++++++++++++--- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 src/NzbDrone.Core/Notifications/Plex/Models/PlexResponse.cs diff --git a/src/NzbDrone.Core/Notifications/Plex/Models/PlexPreferences.cs b/src/NzbDrone.Core/Notifications/Plex/Models/PlexPreferences.cs index 39a7040b7..1cea5ef58 100644 --- a/src/NzbDrone.Core/Notifications/Plex/Models/PlexPreferences.cs +++ b/src/NzbDrone.Core/Notifications/Plex/Models/PlexPreferences.cs @@ -5,7 +5,7 @@ namespace NzbDrone.Core.Notifications.Plex.Models { public class PlexPreferences { - [JsonProperty("_children")] + [JsonProperty("Setting")] public List Preferences { get; set; } } @@ -15,4 +15,10 @@ public class PlexPreference public string Type { get; set; } public string Value { get; set; } } + + public class PlexPreferencesLegacy + { + [JsonProperty("_children")] + public List Preferences { get; set; } + } } diff --git a/src/NzbDrone.Core/Notifications/Plex/Models/PlexResponse.cs b/src/NzbDrone.Core/Notifications/Plex/Models/PlexResponse.cs new file mode 100644 index 000000000..7d2214f54 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Plex/Models/PlexResponse.cs @@ -0,0 +1,7 @@ +namespace NzbDrone.Core.Notifications.Plex.Models +{ + public class PlexResponse + { + public T MediaContainer { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Plex/Models/PlexSection.cs b/src/NzbDrone.Core/Notifications/Plex/Models/PlexSection.cs index 11decb6ff..87b5e9faa 100644 --- a/src/NzbDrone.Core/Notifications/Plex/Models/PlexSection.cs +++ b/src/NzbDrone.Core/Notifications/Plex/Models/PlexSection.cs @@ -3,11 +3,10 @@ namespace NzbDrone.Core.Notifications.Plex.Models { - public class PlexSectionDetails + public class PlexSectionLocation { public int Id { get; set; } public string Path { get; set; } - public string Language { get; set; } } public class PlexSection @@ -18,13 +17,30 @@ public class PlexSection public string Type { get; set; } public string Language { get; set; } - [JsonProperty("_children")] - public List Sections { get; set; } + public PlexSectionLocation Location { get; set; } } - public class PlexMediaContainer + public class PlexSectionsContainer + { + [JsonProperty("Metadata")] + public List Sections { get; set; } + } + + public class PlexSectionLegacy + { + [JsonProperty("key")] + public int Id { get; set; } + + public string Type { get; set; } + public string Language { get; set; } + + [JsonProperty("_children")] + public List Locations { get; set; } + } + + public class PlexMediaContainerLegacy { [JsonProperty("_children")] - public List Directories { get; set; } + public List Sections { get; set; } } } diff --git a/src/NzbDrone.Core/Notifications/Plex/Models/PlexSectionItem.cs b/src/NzbDrone.Core/Notifications/Plex/Models/PlexSectionItem.cs index 7758c2d0a..009baed7b 100644 --- a/src/NzbDrone.Core/Notifications/Plex/Models/PlexSectionItem.cs +++ b/src/NzbDrone.Core/Notifications/Plex/Models/PlexSectionItem.cs @@ -12,6 +12,11 @@ public class PlexSectionItem } public class PlexSectionResponse + { + public List Items { get; set; } + } + + public class PlexSectionResponseLegacy { [JsonProperty("_children")] public List Items { get; set; } diff --git a/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs b/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs index de35ec79d..cc1011b8d 100644 --- a/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs +++ b/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs @@ -44,8 +44,24 @@ public List GetTvSections(PlexServerSettings settings) _logger.Trace("Sections response: {0}", response.Content); CheckForError(response, settings); - return Json.Deserialize(response.Content) - .Directories + if (response.Content.Contains("_children")) + { + return Json.Deserialize(response.Content) + .Sections + .Where(d => d.Type == "show") + .Select(s => new PlexSection + { + Id = s.Id, + Language = s.Language, + Location = s.Locations.FirstOrDefault(), + Type = s.Type + }) + .ToList(); + } + + return Json.Deserialize>(response.Content) + .MediaContainer + .Sections .Where(d => d.Type == "show") .ToList(); } @@ -81,6 +97,11 @@ public string Version(PlexServerSettings settings) _logger.Trace("Version response: {0}", response.Content); CheckForError(response, settings); + if (response.Content.Contains("MediaContainer")) + { + return Json.Deserialize>(response.Content).MediaContainer.Version; + } + return Json.Deserialize(response.Content).Version; } @@ -93,7 +114,12 @@ public List Preferences(PlexServerSettings settings) _logger.Trace("Preferences response: {0}", response.Content); CheckForError(response, settings); - return Json.Deserialize(response.Content).Preferences; + if (response.Content.Contains("MediaContainer")) + { + return Json.Deserialize>(response.Content).MediaContainer.Preferences; + } + + return Json.Deserialize(response.Content).Preferences; } public int? GetMetadataId(int sectionId, int tvdbId, string language, PlexServerSettings settings) @@ -107,8 +133,19 @@ public List Preferences(PlexServerSettings settings) _logger.Trace("Sections response: {0}", response.Content); CheckForError(response, settings); - var items = Json.Deserialize(response.Content) - .Items; + List items; + + if (response.Content.Contains("_children")) + { + items = Json.Deserialize(response.Content) + .Items; + } + + else + { + items = Json.Deserialize(response.Content) + .Items; + } if (items == null || items.Empty()) { @@ -203,7 +240,9 @@ private void CheckForError(IRestResponse response, PlexServerSettings settings) throw new PlexAuthenticationException("Unauthorized - Username or password is incorrect"); } - var error = Json.Deserialize(response.Content); + var error = response.Content.Contains("MediaContainer") ? + Json.Deserialize>(response.Content).MediaContainer : + Json.Deserialize(response.Content); if (error != null && !error.Error.IsNullOrWhiteSpace()) { diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index d427f229e..4564e0df8 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -822,6 +822,7 @@ +