From acb7d33d09307986bca94c1ec79da5e4b435f758 Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Sun, 7 May 2017 14:55:49 -0500 Subject: [PATCH] Implemented track lookup into skyhook. --- .../Cloud/SonarrCloudRequestBuilder.cs | 3 +- .../SkyHook/Resource/ArtistResource.cs | 9 ++++ .../SkyHook/Resource/TrackInfoResource.cs | 25 +++++++++++ .../MetadataSource/SkyHook/SkyHookProxy.cs | 41 ++++++++++++++++++- src/NzbDrone.Core/Music/Album.cs | 1 + src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackInfoResource.cs diff --git a/src/NzbDrone.Common/Cloud/SonarrCloudRequestBuilder.cs b/src/NzbDrone.Common/Cloud/SonarrCloudRequestBuilder.cs index 0349dcf8f..9efdda47c 100644 --- a/src/NzbDrone.Common/Cloud/SonarrCloudRequestBuilder.cs +++ b/src/NzbDrone.Common/Cloud/SonarrCloudRequestBuilder.cs @@ -17,7 +17,8 @@ namespace NzbDrone.Common.Cloud Services = new HttpRequestBuilder("http://services.lidarr.tv/v1/") .CreateFactory(); - Search = new HttpRequestBuilder("https://api.spotify.com/v1/{route}/") // TODO: maybe use {version} + Search = new HttpRequestBuilder("https://api.spotify.com/{version}/{route}/") // TODO: maybe use {version} + .SetSegment("version", "v1") .CreateFactory(); InternalSearch = new HttpRequestBuilder("https://itunes.apple.com/WebObjects/MZStore.woa/wa/{route}") //viewArtist or search diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/ArtistResource.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/ArtistResource.cs index 2dd663383..8d98d6890 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/ArtistResource.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/ArtistResource.cs @@ -26,6 +26,15 @@ namespace NzbDrone.Core.MetadataSource.SkyHook.Resource public List Items { get; set; } } + public class TrackResultResource + { + public TrackResultResource() + { + + } + + public List Items { get; set; } + } public class ArtistResource { public ArtistResource() diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackInfoResource.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackInfoResource.cs new file mode 100644 index 000000000..2f905637d --- /dev/null +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackInfoResource.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.MetadataSource.SkyHook.Resource +{ + public class TrackInfoResource + { + public TrackInfoResource() + { + + } + + public int DiscNumber { get; set; } + public int DurationMs { get; set; } + public string Href { get; set; } + public string Id { get; set; } + public string Name { get; set; } + public int TrackNumber { get; set; } + public bool Explicit { get; set; } + public List Artists { get; set; } + + } +} diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs index 0bb74c548..bf47348b1 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs @@ -114,7 +114,6 @@ namespace NzbDrone.Core.MetadataSource.SkyHook artist.ArtistName = httpResponse.Resource.Name; artist.SpotifyId = httpResponse.Resource.Id; artist.Genres = httpResponse.Resource.Genres; - //Artist artist = MapArtists(httpResponse.Resource)[0]; artist = MapAlbums(artist); @@ -149,6 +148,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook album.AlbumId = albumResource.Id; album.Title = albumResource.Name; album.ArtworkUrl = albumResource.Images[0].Url; + album.Tracks = MapTracksToAlbum(album); albums.Add(album); } @@ -157,7 +157,44 @@ namespace NzbDrone.Core.MetadataSource.SkyHook artist.Albums = albums; return artist; } - + + private List MapTracksToAlbum(Album album) + { + var httpRequest = _requestBuilder.Create() + .SetSegment("route", "albums/" + album.AlbumId + "/tracks") + .Build(); + + httpRequest.AllowAutoRedirect = true; + httpRequest.SuppressHttpError = true; + + var httpResponse = _httpClient.Get(httpRequest); + + if (httpResponse.HasHttpError) + { + throw new HttpException(httpRequest, httpResponse); + } + + List tracks = new List(); + foreach(var trackResource in httpResponse.Resource.Items) + { + Track track = new Track(); + track.AlbumId = album.AlbumId; + //track.Album = album; // This will cause infinite loop when trying to serialize. + // TODO: Implement more track mapping + //track.Artist = trackResource.Artists + //track.ArtistId = album. + track.Explict = trackResource.Explicit; + track.Compilation = trackResource.Artists.Count > 1; + track.TrackNumber = trackResource.TrackNumber; + track.TrackExplicitName = trackResource.Name; + track.TrackCensoredName = trackResource.Name; + tracks.Add(track); + } + + return tracks; + } + + public List SearchForNewArtist(string title) { try diff --git a/src/NzbDrone.Core/Music/Album.cs b/src/NzbDrone.Core/Music/Album.cs index 43aaecb14..6d18a766b 100644 --- a/src/NzbDrone.Core/Music/Album.cs +++ b/src/NzbDrone.Core/Music/Album.cs @@ -18,6 +18,7 @@ namespace NzbDrone.Core.Music public string Title { get; set; } // NOTE: This should be CollectionName in API public int Year { get; set; } public int TrackCount { get; set; } + public List Tracks { get; set; } public int DiscCount { get; set; } public bool Monitored { get; set; } public List Images { get; set; } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 7f5a97c01..d263b8013 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -824,6 +824,7 @@ +