From 65087642e118e549bf84a1176530f4922a992261 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Fri, 4 Mar 2016 20:48:56 +0000 Subject: [PATCH] Fixed the issue when sending movies to CouchPotato. --- PlexRequests.Api/CouchPotatoApi.cs | 4 +- .../SettingModels/CouchPotatoSettings.cs | 15 +++ .../PlexRequests.Helpers.Tests.csproj | 96 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++ PlexRequests.Helpers.Tests/UriHelperTests.cs | 90 +++++++++++++++++ PlexRequests.Helpers.Tests/packages.config | 4 + .../PlexRequests.Helpers.csproj | 1 + PlexRequests.Helpers/UriHelper.cs | 93 ++++++++++++++++++ PlexRequests.UI/Content/requests.js | 2 +- PlexRequests.UI/Modules/AdminModule.cs | 1 - PlexRequests.UI/Modules/SearchModule.cs | 7 +- PlexRequests.sln | 6 ++ 12 files changed, 350 insertions(+), 5 deletions(-) create mode 100644 PlexRequests.Helpers.Tests/PlexRequests.Helpers.Tests.csproj create mode 100644 PlexRequests.Helpers.Tests/Properties/AssemblyInfo.cs create mode 100644 PlexRequests.Helpers.Tests/UriHelperTests.cs create mode 100644 PlexRequests.Helpers.Tests/packages.config create mode 100644 PlexRequests.Helpers/UriHelper.cs diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index c4e0f5213..4366f9bb1 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -43,7 +43,7 @@ namespace PlexRequests.Api private ApiRequest Api { get; set; } private static Logger Log = LogManager.GetCurrentClassLogger(); - public bool AddMovie(string imdbid, string apiKey, string title, string baseUrl) + public bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl) { var request = new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}" }; @@ -51,7 +51,7 @@ namespace PlexRequests.Api request.AddUrlSegment("imdbid", imdbid); request.AddUrlSegment("title", title); - var obj = Api.ExecuteJson(request, new Uri(baseUrl)); + var obj = Api.ExecuteJson(request, baseUrl); Log.Trace("CP movie Add result count {0}", obj.Count); if (obj.Count > 0) diff --git a/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs b/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs index 9ff8a3117..d577e1e1a 100644 --- a/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs +++ b/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs @@ -24,6 +24,11 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion + +using System; +using Newtonsoft.Json; +using PlexRequests.Helpers; + namespace PlexRequests.Core.SettingModels { public class CouchPotatoSettings : Settings @@ -32,5 +37,15 @@ namespace PlexRequests.Core.SettingModels public int Port { get; set; } public string ApiKey { get; set; } public bool Enabled { get; set; } + + [JsonIgnore] + public Uri FullUri + { + get + { + var formatted = Ip.ReturnUri(Port); + return formatted; + } + } } } \ No newline at end of file diff --git a/PlexRequests.Helpers.Tests/PlexRequests.Helpers.Tests.csproj b/PlexRequests.Helpers.Tests/PlexRequests.Helpers.Tests.csproj new file mode 100644 index 000000000..6ca05c099 --- /dev/null +++ b/PlexRequests.Helpers.Tests/PlexRequests.Helpers.Tests.csproj @@ -0,0 +1,96 @@ + + + + Debug + AnyCPU + {0E6395D3-B074-49E8-898D-0EB99E507E0E} + Library + Properties + PlexRequests.Helpers.Tests + PlexRequests.Helpers.Tests + v4.6 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NUnit.3.0.1\lib\net45\nunit.framework.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + {1252336d-42a3-482a-804c-836e60173dfa} + PlexRequests.Helpers + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/PlexRequests.Helpers.Tests/Properties/AssemblyInfo.cs b/PlexRequests.Helpers.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..66ce11c94 --- /dev/null +++ b/PlexRequests.Helpers.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PlexRequests.Helpers.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PlexRequests.Helpers.Tests")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("0e6395d3-b074-49e8-898d-0eb99e507e0e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PlexRequests.Helpers.Tests/UriHelperTests.cs b/PlexRequests.Helpers.Tests/UriHelperTests.cs new file mode 100644 index 000000000..c872b6f65 --- /dev/null +++ b/PlexRequests.Helpers.Tests/UriHelperTests.cs @@ -0,0 +1,90 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: UriHelperTests.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using NUnit.Framework; + +namespace PlexRequests.Helpers.Tests +{ + [TestFixture] + public class UriHelperTests + { + [TestCaseSource(nameof(UriData))] + public void CreateUri(string uri, Uri expected) + { + var result = uri.ReturnUri(); + + Assert.That(result, Is.EqualTo(expected)); + } + + [TestCaseSource(nameof(UriDataWithPort))] + public void CreateUri(string uri, int port, Uri expected) + { + var result = uri.ReturnUri(port); + + Assert.That(result, Is.EqualTo(expected)); + } + + static readonly object[] UriData = + { + new object[] { "google.com", new Uri("http://google.com/"), }, + new object[] { "http://google.com", new Uri("http://google.com/"), }, + new object[] { "https://google.com", new Uri("https://google.com/"), }, + new object[] { "192.168.1.1", new Uri("http://192.168.1.1")}, + new object[] { "0.0.0.0:5533", new Uri("http://0.0.0.0:5533")}, + new object[] {"www.google.com", new Uri("http://www.google.com/")}, + new object[] {"http://www.google.com/", new Uri("http://www.google.com/") }, + new object[] {"https://www.google.com", new Uri("https://www.google.com/") }, + new object[] {"www.google.com:443", new Uri("http://www.google.com:443/") }, + new object[] {"https://www.google.com:443", new Uri("https://www.google.com:443/") }, + new object[] {"http://www.google.com:443/id=2", new Uri("http://www.google.com:443/id=2") }, + new object[] {"www.google.com:4438/id=22", new Uri("http://www.google.com:4438/id=22") } + }; + + static readonly object[] UriDataWithPort = +{ + new object[] { "google.com", new Uri("http://google.com/"), }, + new object[] { "http://google.com", new Uri("http://google.com/"), }, + new object[] { "https://google.com", new Uri("https://google.com/"), }, + new object[] { "192.168.1.1", new Uri("http://192.168.1.1")}, + new object[] { "0.0.0.0:5533", new Uri("http://0.0.0.0:5533")}, + new object[] {"www.google.com", new Uri("http://www.google.com/")}, + new object[] {"http://www.google.com/", new Uri("http://www.google.com/") }, + new object[] {"https://www.google.com", new Uri("https://www.google.com/") }, + new object[] {"www.google.com:443", new Uri("http://www.google.com:443/") }, + new object[] {"https://www.google.com:443", new Uri("https://www.google.com:443/") }, + new object[] {"http://www.google.com:443/id=2", new Uri("http://www.google.com:443/id=2") }, + new object[] {"www.google.com", 80, new Uri("http://www.google.com:80/"), }, + new object[] {"www.google.com", 443, new Uri("http://www.google.com:443/") }, + new object[] {"http://www.google.com", 443, new Uri("http://www.google.com:443/") }, + new object[] {"https://www.google.com", 443, new Uri("https://www.google.com:443/") }, + new object[] {"http://www.google.com/id=2", 443, new Uri("http://www.google.com:443/id=2") }, + new object[] {"http://www.google.com/id=2", 443, new Uri("http://www.google.com:443/id=2") }, + new object[] {"https://www.google.com/id=2", 443, new Uri("https://www.google.com:443/id=2") }, + }; + } +} \ No newline at end of file diff --git a/PlexRequests.Helpers.Tests/packages.config b/PlexRequests.Helpers.Tests/packages.config new file mode 100644 index 000000000..c56ed9641 --- /dev/null +++ b/PlexRequests.Helpers.Tests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/PlexRequests.Helpers/PlexRequests.Helpers.csproj b/PlexRequests.Helpers/PlexRequests.Helpers.csproj index ee34ef920..1e7744f75 100644 --- a/PlexRequests.Helpers/PlexRequests.Helpers.csproj +++ b/PlexRequests.Helpers/PlexRequests.Helpers.csproj @@ -54,6 +54,7 @@ + diff --git a/PlexRequests.Helpers/UriHelper.cs b/PlexRequests.Helpers/UriHelper.cs new file mode 100644 index 000000000..c267aae41 --- /dev/null +++ b/PlexRequests.Helpers/UriHelper.cs @@ -0,0 +1,93 @@ +using System; + +namespace PlexRequests.Helpers +{ + public static class UriHelper + { + + public static Uri ReturnUri(this string val) + { + try + { + var uri = new UriBuilder(); + + if (val.StartsWith("http://", StringComparison.Ordinal)) + { + uri = new UriBuilder(val); + } + else if (val.StartsWith("https://", StringComparison.Ordinal)) + { + uri = new UriBuilder(val); + } + else if (val.Contains(":")) + { + var split = val.Split(':', '/'); + int port; + int.TryParse(split[1], out port); + + uri = split.Length == 3 + ? new UriBuilder(Uri.UriSchemeHttp, split[0], port, "/" + split[2]) + : new UriBuilder(Uri.UriSchemeHttp, split[0], port); + } + else + { + uri = new UriBuilder(Uri.UriSchemeHttp, val); + } + + return uri.Uri; + } + catch (Exception exception) + { + throw new Exception(exception.Message, exception); + } + } + + + + /// + /// Returns the URI. + /// + /// The value. + /// The port. + /// + /// + public static Uri ReturnUri(this string val, int port) + { + try + { + var uri = new UriBuilder(); + + if (val.StartsWith("http://", StringComparison.Ordinal)) + { + var split = val.Split('/'); + if (split.Length >= 4) + { + uri = new UriBuilder(Uri.UriSchemeHttp, split[2], port, "/" + split[3]); + } + else + uri = new UriBuilder(new Uri(string.Format("{0}:{1}", val, port))); + } + else if (val.StartsWith("https://", StringComparison.Ordinal)) + { + var split = val.Split('/'); + if (split.Length >= 4) + { + uri = new UriBuilder(Uri.UriSchemeHttps, split[2], port, "/" + split[3]); + } + else + uri = new UriBuilder(Uri.UriSchemeHttps, split[2], port); + } + else + { + uri = new UriBuilder(Uri.UriSchemeHttp, val, port); + } + + return uri.Uri; + } + catch (Exception exception) + { + throw new Exception(exception.Message, exception); + } + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Content/requests.js b/PlexRequests.UI/Content/requests.js index 671a29e72..f52df5d3f 100644 --- a/PlexRequests.UI/Content/requests.js +++ b/PlexRequests.UI/Content/requests.js @@ -71,7 +71,7 @@ function buildRequestContext(result, type) { var context = { posterPath: result.posterPath, - id: result.tmdbid, + id: result.providerId, title: result.title, overview: result.overview, year: result.releaseYear, diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 60c79ee5f..a22169096 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -175,7 +175,6 @@ namespace PlexRequests.UI.Modules private Response SaveCouchPotato() { var couchPotatoSettings = this.Bind(); - CpService.SaveSettings(couchPotatoSettings); return Context.GetRedirect("~/admin/couchpotato"); diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 38bc1d338..d645aec84 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -149,6 +149,11 @@ namespace PlexRequests.UI.Modules } Log.Trace("movie with id {0} doesnt exists", movieId); var settings = CpService.GetSettings(); + if (settings.ApiKey == null) + { + Log.Warn("CP apiKey is null"); + return Response.AsJson(new { Result = false, Message = "CouchPotato is not yet configured, If you are the Admin, please log in." }); + } Log.Trace("Settings: "); Log.Trace(settings.DumpJson); @@ -173,7 +178,7 @@ namespace PlexRequests.UI.Modules var cp = new CouchPotatoApi(); Log.Trace("Adding movie to CP"); - var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.Ip); + var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.FullUri); Log.Trace("Adding movie to CP result {0}", result); if (result) { diff --git a/PlexRequests.sln b/PlexRequests.sln index 57d56dcc4..2f492d98b 100644 --- a/PlexRequests.sln +++ b/PlexRequests.sln @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.UI.Tests", "Pl EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.Core.Tests", "PlexRequests.Core.Tests\PlexRequests.Core.Tests.csproj", "{FCFECD5D-47F6-454D-8692-E27A921BE655}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.Helpers.Tests", "PlexRequests.Helpers.Tests\PlexRequests.Helpers.Tests.csproj", "{0E6395D3-B074-49E8-898D-0EB99E507E0E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -65,6 +67,10 @@ Global {FCFECD5D-47F6-454D-8692-E27A921BE655}.Debug|Any CPU.Build.0 = Debug|Any CPU {FCFECD5D-47F6-454D-8692-E27A921BE655}.Release|Any CPU.ActiveCfg = Release|Any CPU {FCFECD5D-47F6-454D-8692-E27A921BE655}.Release|Any CPU.Build.0 = Release|Any CPU + {0E6395D3-B074-49E8-898D-0EB99E507E0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E6395D3-B074-49E8-898D-0EB99E507E0E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E6395D3-B074-49E8-898D-0EB99E507E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E6395D3-B074-49E8-898D-0EB99E507E0E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE