From 45fb3ad3761a9134b724b14eb3c239a04c102ae6 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 1 Apr 2016 12:39:13 +0100 Subject: [PATCH] A bit more work on #32 started working on requesting it. The DB is a bit of an issue... --- PlexRequests.Api.Interfaces/IHeadphonesApi.cs | 35 ++++++++++ .../IMusicBrainzApi.cs | 37 +++++++++++ .../PlexRequests.Api.Interfaces.csproj | 2 + .../Music/MusicBrainzReleaseInfo.cs | 66 +++++++++++++++++++ .../PlexRequests.Api.Models.csproj | 1 + PlexRequests.Api/HeadphonesApi.cs | 33 ++-------- PlexRequests.Api/MusicBrainzApi.cs | 26 +++++++- PlexRequests.Store/RequestedModel.cs | 1 + PlexRequests.UI/Bootstrapper.cs | 2 + PlexRequests.UI/Modules/SearchModule.cs | 49 ++++++++++---- 10 files changed, 209 insertions(+), 43 deletions(-) create mode 100644 PlexRequests.Api.Interfaces/IHeadphonesApi.cs create mode 100644 PlexRequests.Api.Interfaces/IMusicBrainzApi.cs create mode 100644 PlexRequests.Api.Models/Music/MusicBrainzReleaseInfo.cs diff --git a/PlexRequests.Api.Interfaces/IHeadphonesApi.cs b/PlexRequests.Api.Interfaces/IHeadphonesApi.cs new file mode 100644 index 000000000..2dee51d4f --- /dev/null +++ b/PlexRequests.Api.Interfaces/IHeadphonesApi.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IHeadphonesApi.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; + +namespace PlexRequests.Api.Interfaces +{ + public interface IHeadphonesApi + { + bool AddAlbum(string apiKey, Uri baseUrl, string albumId); + } +} \ No newline at end of file diff --git a/PlexRequests.Api.Interfaces/IMusicBrainzApi.cs b/PlexRequests.Api.Interfaces/IMusicBrainzApi.cs new file mode 100644 index 000000000..011c430e7 --- /dev/null +++ b/PlexRequests.Api.Interfaces/IMusicBrainzApi.cs @@ -0,0 +1,37 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IMusicBrainzApi.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 PlexRequests.Api.Models.Music; + +namespace PlexRequests.Api.Interfaces +{ + public interface IMusicBrainzApi + { + MusicBrainzSearchResults SearchAlbum(string searchTerm); + MusicBrainzCoverArt GetCoverArt(string releaseId); + MusicBrainzReleaseInfo GetAlbum(string releaseId); + } +} \ No newline at end of file diff --git a/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj b/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj index 7522c8156..f27825298 100644 --- a/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj +++ b/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj @@ -47,6 +47,8 @@ + + diff --git a/PlexRequests.Api.Models/Music/MusicBrainzReleaseInfo.cs b/PlexRequests.Api.Models/Music/MusicBrainzReleaseInfo.cs new file mode 100644 index 000000000..974703cda --- /dev/null +++ b/PlexRequests.Api.Models/Music/MusicBrainzReleaseInfo.cs @@ -0,0 +1,66 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MusicBrainzReleaseInfo.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.Collections.Generic; + +using Newtonsoft.Json; + +namespace PlexRequests.Api.Models.Music +{ + public class CoverArtArchive + { + public int count { get; set; } + public bool back { get; set; } + public bool artwork { get; set; } + public bool front { get; set; } + public bool darkened { get; set; } + } + + + public class MusicBrainzReleaseInfo + { + public string date { get; set; } + public string status { get; set; } + public string asin { get; set; } + public string title { get; set; } + public string quality { get; set; } + public string country { get; set; } + public string packaging { get; set; } + + [JsonProperty(PropertyName = "text-representation")] + public TextRepresentation TextRepresentation { get; set; } + + [JsonProperty(PropertyName = "cover-art-archive")] + public CoverArtArchive CoverArtArchive { get; set; } + public string barcode { get; set; } + public string disambiguation { get; set; } + + [JsonProperty(PropertyName = "release-events")] + public List ReleaseRvents { get; set; } + public string id { get; set; } + } + +} \ 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 978171662..e24c0e31f 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -51,6 +51,7 @@ + diff --git a/PlexRequests.Api/HeadphonesApi.cs b/PlexRequests.Api/HeadphonesApi.cs index d346ba846..ed0dac9c6 100644 --- a/PlexRequests.Api/HeadphonesApi.cs +++ b/PlexRequests.Api/HeadphonesApi.cs @@ -1,7 +1,7 @@ #region Copyright // /************************************************************************ // Copyright (c) 2016 Jamie Rees -// File: CouchPotatoApi.cs +// File: HeadphonesApi.cs // Created By: Jamie Rees // // Permission is hereby granted, free of charge, to any person obtaining @@ -26,51 +26,26 @@ #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 class HeadphonesApi : IHeadphonesApi { 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". - } - } + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); public bool AddAlbum(string apiKey, Uri baseUrl, string albumId) { diff --git a/PlexRequests.Api/MusicBrainzApi.cs b/PlexRequests.Api/MusicBrainzApi.cs index 8331e8a0d..62b0771be 100644 --- a/PlexRequests.Api/MusicBrainzApi.cs +++ b/PlexRequests.Api/MusicBrainzApi.cs @@ -30,15 +30,16 @@ using Newtonsoft.Json; using NLog; +using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models.Music; using RestSharp; namespace PlexRequests.Api { - public class MusicBrainsApi + public class MusicBrainzApi : IMusicBrainzApi { - public MusicBrainsApi() + public MusicBrainzApi() { Api = new ApiRequest(); } @@ -67,6 +68,27 @@ namespace PlexRequests.Api } } + public MusicBrainzReleaseInfo GetAlbum(string releaseId) + { + Log.Trace("Getting album: {0}", releaseId); + var request = new RestRequest + { + Resource = "release/{albumId}?fmt=json", + Method = Method.GET + }; + request.AddUrlSegment("albumId", releaseId); + + try + { + return Api.Execute(request, BaseUri); + } + catch (JsonSerializationException jse) + { + Log.Warn(jse); + return new MusicBrainzReleaseInfo(); // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + public MusicBrainzCoverArt GetCoverArt(string releaseId) { Log.Trace("Getting cover art for release: {0}", releaseId); diff --git a/PlexRequests.Store/RequestedModel.cs b/PlexRequests.Store/RequestedModel.cs index dcceeb1ab..ffb80db8b 100644 --- a/PlexRequests.Store/RequestedModel.cs +++ b/PlexRequests.Store/RequestedModel.cs @@ -38,6 +38,7 @@ namespace PlexRequests.Store public int[] SeasonList { get; set; } public int SeasonCount { get; set; } public string SeasonsRequested { get; set; } + public string MusicBrainzId { get; set; } public List RequestedUsers { get; set; } [JsonIgnore] diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index dd1daaec0..a4e13f18b 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -95,6 +95,8 @@ namespace PlexRequests.UI container.Register(); container.Register(); container.Register(); + container.Register(); + container.Register(); // NotificationService container.Register().AsSingleton(); diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index ed18a89a3..4d97a2cce 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -55,7 +55,7 @@ namespace PlexRequests.UI.Modules ISettingsService prSettings, IAvailabilityChecker checker, IRequestService request, ISonarrApi sonarrApi, ISettingsService sonarrSettings, ISettingsService sickRageService, ICouchPotatoApi cpApi, ISickRageApi srApi, - INotificationService notify) : base("search") + INotificationService notify, IMusicBrainzApi mbApi, IHeadphonesApi hpApi, ISettingsService hpService) : base("search") { CpService = cpSettings; PrService = prSettings; @@ -70,6 +70,10 @@ namespace PlexRequests.UI.Modules SickRageService = sickRageService; SickrageApi = srApi; NotificationService = notify; + MusicBrainzApi = mbApi; + HeadphonesApi = hpApi; + HeadphonesService = hpService; + Get["/"] = parameters => RequestLoad(); @@ -96,9 +100,11 @@ namespace PlexRequests.UI.Modules private ISettingsService PrService { get; } private ISettingsService SonarrService { get; } private ISettingsService SickRageService { get; } + private ISettingsService HeadphonesService { get; } private IAvailabilityChecker Checker { get; } + private IMusicBrainzApi MusicBrainzApi { get; } + private IHeadphonesApi HeadphonesApi { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); - private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50); private Negotiator RequestLoad() { @@ -157,20 +163,12 @@ namespace PlexRequests.UI.Modules private Response SearchMusic(string searchTerm) { - var api = new MusicBrainsApi(); - var albums = api.SearchAlbum(searchTerm); + var albums = MusicBrainzApi.SearchAlbum(searchTerm); var releases = albums.releases ?? new List(); var model = new List(); foreach (var a in releases) { - var coverArt = api.GetCoverArt(a.id); - var firstImage = coverArt?.images?.FirstOrDefault(); - var img = string.Empty; - - if (firstImage != null) - { - img = firstImage.thumbnails?.small ?? firstImage.image; - } + var img = GetMusicBrainzCoverArt(a.id); model.Add(new SearchMusicViewModel { Title = a.title, @@ -478,9 +476,36 @@ namespace PlexRequests.UI.Modules private Response RequestAlbum(string releaseId) { + var settings = HeadphonesService.GetSettings(); + + var albumInfo = MusicBrainzApi.GetAlbum(releaseId); + var img = GetMusicBrainzCoverArt(albumInfo.id); + var model = new RequestedModel + { + Title = albumInfo.title, + MusicBrainzId = albumInfo.id, + Overview = albumInfo.disambiguation, + PosterPath = img, + Type = RequestType.Album + }; + // TODO need to send to Headphones return Response.AsJson(""); } + + private string GetMusicBrainzCoverArt(string id) + { + var coverArt = MusicBrainzApi.GetCoverArt(id); + var firstImage = coverArt?.images?.FirstOrDefault(); + var img = string.Empty; + + if (firstImage != null) + { + img = firstImage.thumbnails?.small ?? firstImage.image; + } + + return img; + } } } \ No newline at end of file