|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
|
using NzbDrone.Core.Notifications.Plex.Models;
|
|
|
|
|
using NzbDrone.Core.Rest;
|
|
|
|
|
using RestSharp;
|
|
|
|
|
|
|
|
|
@ -14,8 +16,12 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
{
|
|
|
|
|
public interface IPlexServerProxy
|
|
|
|
|
{
|
|
|
|
|
List<PlexDirectory> GetTvSections(PlexServerSettings settings);
|
|
|
|
|
List<PlexSection> GetTvSections(PlexServerSettings settings);
|
|
|
|
|
void Update(int sectionId, PlexServerSettings settings);
|
|
|
|
|
void UpdateSeries(int metadataId, PlexServerSettings settings);
|
|
|
|
|
string Version(PlexServerSettings settings);
|
|
|
|
|
List<PlexPreference> Preferences(PlexServerSettings settings);
|
|
|
|
|
int? GetMetadataId(int sectionId, int tvdbId, string language, PlexServerSettings settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PlexServerProxy : IPlexServerProxy
|
|
|
|
@ -29,16 +35,14 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PlexDirectory> GetTvSections(PlexServerSettings settings)
|
|
|
|
|
public List<PlexSection> GetTvSections(PlexServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var request = GetPlexServerRequest("library/sections", Method.GET, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Sections response: {0}", response.Content);
|
|
|
|
|
|
|
|
|
|
CheckForError(response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
return Json.Deserialize<PlexMediaContainer>(response.Content)
|
|
|
|
|
.Directories
|
|
|
|
@ -51,11 +55,68 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
var resource = String.Format("library/sections/{0}/refresh", sectionId);
|
|
|
|
|
var request = GetPlexServerRequest(resource, Method.GET, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Update response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateSeries(int metadataId, PlexServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var resource = String.Format("library/metadata/{0}/refresh", metadataId);
|
|
|
|
|
var request = GetPlexServerRequest(resource, Method.PUT, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Update Series response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Version(PlexServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var request = GetPlexServerRequest("identity", Method.GET, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
CheckForError(response.Content);
|
|
|
|
|
_logger.Debug("Update response: {0}", response.Content);
|
|
|
|
|
_logger.Trace("Version response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
return Json.Deserialize<PlexIdentity>(response.Content).Version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PlexPreference> Preferences(PlexServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var request = GetPlexServerRequest(":/prefs", Method.GET, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Preferences response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
return Json.Deserialize<PlexPreferences>(response.Content).Preferences;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? GetMetadataId(int sectionId, int tvdbId, string language, PlexServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var guid = String.Format("com.plexapp.agents.thetvdb://{0}?lang={1}", tvdbId, language);
|
|
|
|
|
var resource = String.Format("library/sections/{0}/all?guid={1}", sectionId, System.Web.HttpUtility.UrlEncode(guid));
|
|
|
|
|
var request = GetPlexServerRequest(resource, Method.GET, settings);
|
|
|
|
|
var client = GetPlexServerClient(settings);
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Sections response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
var item = Json.Deserialize<PlexSectionResponse>(response.Content)
|
|
|
|
|
.Items
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String Authenticate(string username, string password)
|
|
|
|
@ -65,8 +126,8 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
CheckForError(response.Content);
|
|
|
|
|
_logger.Debug("Authentication Response: {0}", response.Content);
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
var user = Json.Deserialize<PlexUser>(JObject.Parse(response.Content).SelectToken("user").ToString());
|
|
|
|
|
|
|
|
|
@ -109,7 +170,7 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
|
|
|
|
|
if (!settings.Username.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
request.AddParameter("X-Plex-Token", GetAuthenticationToken(settings.Username, settings.Password));
|
|
|
|
|
request.AddParameter("X-Plex-Token", GetAuthenticationToken(settings.Username, settings.Password), ParameterType.HttpHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
@ -120,14 +181,23 @@ namespace NzbDrone.Core.Notifications.Plex
|
|
|
|
|
return _authCache.Get(username, () => Authenticate(username, password));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckForError(string response)
|
|
|
|
|
private void CheckForError(IRestResponse response)
|
|
|
|
|
{
|
|
|
|
|
var error = Json.Deserialize<PlexError>(response);
|
|
|
|
|
_logger.Trace("Checking for error");
|
|
|
|
|
|
|
|
|
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
|
|
|
|
throw new PlexException("Unauthorized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var error = Json.Deserialize<PlexError>(response.Content);
|
|
|
|
|
|
|
|
|
|
if (error != null && !error.Error.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
throw new PlexException(error.Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Trace("No error detected");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|