From 8cb8059b2f46cf060ab404687e545e43eeafab46 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 25 Jan 2018 23:07:15 -0500 Subject: [PATCH] Add Skyhook for Searching by Album #179 --- .../Migration/010_album_releases_fix.cs | 6 +- .../MetadataSource/ISearchForNewAlbum.cs | 11 +++ .../SkyHook/Resource/AlbumArtistResource.cs | 13 +++ .../SkyHook/Resource/AlbumResource.cs | 2 + .../MetadataSource/SkyHook/SkyHookProxy.cs | 83 ++++++++++++++++++- src/NzbDrone.Core/NzbDrone.Core.csproj | 3 + 6 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 src/NzbDrone.Core/MetadataSource/ISearchForNewAlbum.cs create mode 100644 src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumArtistResource.cs diff --git a/src/NzbDrone.Core/Datastore/Migration/010_album_releases_fix.cs b/src/NzbDrone.Core/Datastore/Migration/010_album_releases_fix.cs index 0ca24efaa..10b8dd79d 100644 --- a/src/NzbDrone.Core/Datastore/Migration/010_album_releases_fix.cs +++ b/src/NzbDrone.Core/Datastore/Migration/010_album_releases_fix.cs @@ -8,10 +8,8 @@ namespace NzbDrone.Core.Datastore.Migration { protected override void MainDbUpgrade() { - Delete.Column("Releases").FromTable("Albums"); - Delete.Column("CurrentRelease").FromTable("Albums"); - Alter.Table("Albums").AddColumn("Releases").AsString().WithDefaultValue("[]").NotNullable(); - Alter.Table("Albums").AddColumn("CurrentRelease").AsString().WithDefaultValue("").NotNullable(); + Alter.Table("Albums").AlterColumn("Releases").AsString().NotNullable(); + Alter.Table("Albums").AlterColumn("CurrentRelease").AsString().NotNullable(); } } } diff --git a/src/NzbDrone.Core/MetadataSource/ISearchForNewAlbum.cs b/src/NzbDrone.Core/MetadataSource/ISearchForNewAlbum.cs new file mode 100644 index 000000000..803c09cec --- /dev/null +++ b/src/NzbDrone.Core/MetadataSource/ISearchForNewAlbum.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using NzbDrone.Core.Music; + +namespace NzbDrone.Core.MetadataSource +{ + public interface ISearchForNewAlbum + { + List SearchForNewAlbum(string title, string artist, DateTime releaseDate); + } +} diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumArtistResource.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumArtistResource.cs new file mode 100644 index 000000000..11e25dc52 --- /dev/null +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumArtistResource.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.MetadataSource.SkyHook.Resource +{ + public class AlbumArtistResource + { + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumResource.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumResource.cs index 9ad5375a5..a54cdd8d1 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumResource.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/AlbumResource.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook.Resource public AlbumResource() { Media = new List(); + Releases = new List(); } public List Artists { get; set; } // Will always be length of 1 unless a compilation @@ -27,6 +28,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook.Resource public List Tracks { get; set; } public List Releases { get; set; } public string SelectedRelease { get; set; } + public AlbumArtistResource Artist { get; set; } } diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs index b9b809a94..2537e5db1 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs @@ -15,12 +15,13 @@ using NzbDrone.Core.Profiles.Metadata; namespace NzbDrone.Core.MetadataSource.SkyHook { - public class SkyHookProxy : IProvideArtistInfo, ISearchForNewArtist, IProvideAlbumInfo + public class SkyHookProxy : IProvideArtistInfo, ISearchForNewArtist, IProvideAlbumInfo, ISearchForNewAlbum { private readonly IHttpClient _httpClient; private readonly Logger _logger; private readonly IArtistService _artistService; + private readonly IAlbumService _albumService; private readonly IHttpRequestBuilderFactory _requestBuilder; private readonly IConfigService _configService; private readonly IMetadataProfileService _metadataProfileService; @@ -29,7 +30,9 @@ namespace NzbDrone.Core.MetadataSource.SkyHook public SkyHookProxy(IHttpClient httpClient, ILidarrCloudRequestBuilder requestBuilder, - IArtistService artistService, Logger logger, + IArtistService artistService, + IAlbumService albumService, + Logger logger, IConfigService configService, IMetadataProfileService metadataProfileService) { @@ -38,6 +41,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook _metadataProfileService = metadataProfileService; _requestBuilder = requestBuilder.Search; _artistService = artistService; + _albumService = albumService; _logger = logger; } @@ -48,7 +52,8 @@ namespace NzbDrone.Core.MetadataSource.SkyHook SetCustomProvider(); - var metadataProfile = _metadataProfileService.Get(metadataProfileId); + var metadataProfile = _metadataProfileService.Exists(metadataProfileId) ? _metadataProfileService.Get(metadataProfileId) : _metadataProfileService.All().FirstOrDefault(); + var primaryTypes = metadataProfile.PrimaryAlbumTypes.Where(s => s.Allowed).Select(s => s.PrimaryAlbumType.Name); var secondaryTypes = metadataProfile.SecondaryAlbumTypes.Where(s => s.Allowed).Select(s => s.SecondaryAlbumType.Name); @@ -183,6 +188,61 @@ namespace NzbDrone.Core.MetadataSource.SkyHook } } + public List SearchForNewAlbum(string title, string artist, DateTime releaseDate) + { + try + { + var lowerTitle = title.ToLowerInvariant(); + + if (lowerTitle.StartsWith("lidarr:") || lowerTitle.StartsWith("lidarrid:")) + { + var slug = lowerTitle.Split(':')[1].Trim(); + + Guid searchGuid; + + bool isValid = Guid.TryParse(slug, out searchGuid); + + if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || isValid == false) + { + return new List(); + } + + try + { + return new List { GetAlbumInfo(searchGuid.ToString(), null).Item1 }; + } + catch (ArtistNotFoundException) + { + return new List(); + } + } + + SetCustomProvider(); + + var httpRequest = _customerRequestBuilder.Create() + .SetSegment("route", "search") + .AddQueryParam("type", "album") + .AddQueryParam("query", title.ToLower().Trim()) + .AddQueryParam("artist", artist.ToLower().Trim()) + .Build(); + + + + var httpResponse = _httpClient.Get>(httpRequest); + + return httpResponse.Resource.SelectList(MapSearhResult); + } + catch (HttpException) + { + throw new SkyHookException("Search for '{0}' failed. Unable to communicate with LidarrAPI.", title); + } + catch (Exception ex) + { + _logger.Warn(ex, ex.Message); + throw new SkyHookException("Search for '{0}' failed. Invalid response received from LidarrAPI.", title); + } + } + private Artist MapSearhResult(ArtistResource resource) { var artist = _artistService.FindById(resource.Id) ?? MapArtist(resource); @@ -190,6 +250,13 @@ namespace NzbDrone.Core.MetadataSource.SkyHook return artist; } + private Album MapSearhResult(AlbumResource resource) + { + var album = _albumService.FindById(resource.Id) ?? MapAlbum(resource); + + return album; + } + private static Album MapAlbum(AlbumResource resource) { Album album = new Album(); @@ -214,6 +281,16 @@ namespace NzbDrone.Core.MetadataSource.SkyHook album.CurrentRelease = album.Releases.FirstOrDefault(s => s.Id == resource.SelectedRelease); } + if (resource.Artist != null) + { + album.Artist = new Artist + { + ForeignArtistId = resource.Artist.Id, + Name = resource.Artist.Name + }; + } + + return album; } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index dbdc0ba8a..e894a44a4 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -182,6 +182,7 @@ + @@ -731,8 +732,10 @@ + +