From e9d0ccd5c0637649a7c68ae316e2886dbc39d0dc Mon Sep 17 00:00:00 2001 From: tidusjar Date: Thu, 31 Mar 2016 13:51:15 +0100 Subject: [PATCH] Started adding the api part for headphones #32 --- .../Music/HeadphonesAlbumSearchResult.cs | 45 +++++++++ .../Music/HeadphonesArtistSearchResult.cs | 37 +++++++ .../PlexRequests.Api.Models.csproj | 2 + PlexRequests.Api/HeadphonesApi.cs | 99 +++++++++++++++++++ PlexRequests.Api/PlexRequests.Api.csproj | 1 + 5 files changed, 184 insertions(+) create mode 100644 PlexRequests.Api.Models/Music/HeadphonesAlbumSearchResult.cs create mode 100644 PlexRequests.Api.Models/Music/HeadphonesArtistSearchResult.cs create mode 100644 PlexRequests.Api/HeadphonesApi.cs diff --git a/PlexRequests.Api.Models/Music/HeadphonesAlbumSearchResult.cs b/PlexRequests.Api.Models/Music/HeadphonesAlbumSearchResult.cs new file mode 100644 index 000000000..8aa4684c6 --- /dev/null +++ b/PlexRequests.Api.Models/Music/HeadphonesAlbumSearchResult.cs @@ -0,0 +1,45 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: HeadphonesAlbumSearchResult.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 +namespace PlexRequests.Api.Models.Music +{ + public class HeadphonesAlbumSearchResult + { + public string rgid { get; set; } + public string albumurl { get; set; } + public string tracks { get; set; } + public string date { get; set; } + public string id { get; set; } // Artist ID + public string rgtype { get; set; } + public string title { get; set; } + public string url { get; set; } + public string country { get; set; } + public string albumid { get; set; } // AlbumId + public int score { get; set; } + public string uniquename { get; set; } + public string formats { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Api.Models/Music/HeadphonesArtistSearchResult.cs b/PlexRequests.Api.Models/Music/HeadphonesArtistSearchResult.cs new file mode 100644 index 000000000..15c574277 --- /dev/null +++ b/PlexRequests.Api.Models/Music/HeadphonesArtistSearchResult.cs @@ -0,0 +1,37 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: HeadphonesSearchResult.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 +namespace PlexRequests.Api.Models.Music +{ + public class HeadphonesArtistSearchResult + { + public string url { get; set; } // MusicBrainz url + public int score { get; set; } // Search Match score? + public string name { get; set; } // Artist Name + public string uniquename { get; set; } // Artist Unique Name + public string id { get; set; } // Artist Unique ID for MusicBrainz + } +} \ No newline at end of file diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index d67958e0d..35def3d76 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -48,6 +48,8 @@ + + diff --git a/PlexRequests.Api/HeadphonesApi.cs b/PlexRequests.Api/HeadphonesApi.cs new file mode 100644 index 000000000..d346ba846 --- /dev/null +++ b/PlexRequests.Api/HeadphonesApi.cs @@ -0,0 +1,99 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: CouchPotatoApi.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 System.Collections.Generic; +using System.Linq; + +using Newtonsoft.Json; + +using NLog; +using PlexRequests.Api.Interfaces; +using PlexRequests.Api.Models.Music; +using PlexRequests.Api.Models.Sonarr; +using PlexRequests.Helpers; + +using RestSharp; + +namespace PlexRequests.Api +{ + public class HeadphonesApi + { + public HeadphonesApi() + { + Api = new ApiRequest(); + } + private ApiRequest Api { get; } + private static Logger Log = LogManager.GetCurrentClassLogger(); + + public List SearchAlbum(string apiKey, Uri baseUrl, string searchTerm) + { + Log.Trace("Searching for album: {0}", searchTerm); + var request = new RestRequest + { + Resource = "/api?cmd=findAlbum&name={searchTerm}", + Method = Method.GET + }; + + request.AddQueryParameter("apikey", apiKey); + request.AddUrlSegment("searchTerm", searchTerm); + + try + { + return Api.ExecuteJson>(request, baseUrl); + } + catch (JsonSerializationException jse) + { + Log.Warn(jse); + return new List(); // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + + public bool AddAlbum(string apiKey, Uri baseUrl, string albumId) + { + Log.Trace("Adding album: {0}", albumId); + var request = new RestRequest + { + Resource = "/api?cmd=addAlbum&id={albumId}", + Method = Method.GET + }; + + request.AddQueryParameter("apikey", apiKey); + request.AddUrlSegment("albumId", albumId); + + try + { + //var result = Api.Execute(request, baseUrl); + return false; + } + catch (JsonSerializationException jse) + { + Log.Warn(jse); + return false; // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + } +} \ No newline at end of file diff --git a/PlexRequests.Api/PlexRequests.Api.csproj b/PlexRequests.Api/PlexRequests.Api.csproj index 6422dfd6f..9484f59a3 100644 --- a/PlexRequests.Api/PlexRequests.Api.csproj +++ b/PlexRequests.Api/PlexRequests.Api.csproj @@ -75,6 +75,7 @@ +