2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
2015-01-09 01:43:51 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2015-05-07 15:43:52 +00:00
|
|
|
using System.Net;
|
2015-01-09 01:43:51 +00:00
|
|
|
using NLog;
|
2016-02-28 15:41:22 +00:00
|
|
|
using NzbDrone.Common.Cloud;
|
2015-01-09 01:43:51 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Common.Http;
|
2015-08-12 18:45:44 +00:00
|
|
|
using NzbDrone.Core.Exceptions;
|
2015-01-09 01:43:51 +00:00
|
|
|
using NzbDrone.Core.MediaCover;
|
|
|
|
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
|
|
|
|
using NzbDrone.Core.Tv;
|
2017-04-28 22:05:35 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using Newtonsoft.Json;
|
2015-01-09 01:43:51 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.MetadataSource.SkyHook
|
|
|
|
{
|
2017-08-24 01:39:27 +00:00
|
|
|
public class SkyHookProxy : IProvideSeriesInfo, IProvideArtistInfo, ISearchForNewArtist
|
2015-01-09 01:43:51 +00:00
|
|
|
{
|
|
|
|
private readonly IHttpClient _httpClient;
|
2015-05-07 15:43:52 +00:00
|
|
|
private readonly Logger _logger;
|
2015-01-09 01:43:51 +00:00
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
private readonly IArtistService _artistService;
|
2016-02-28 15:41:22 +00:00
|
|
|
private readonly IHttpRequestBuilderFactory _requestBuilder;
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
public SkyHookProxy(IHttpClient httpClient, ILidarrCloudRequestBuilder requestBuilder, IArtistService artistService, Logger logger)
|
2015-01-09 01:43:51 +00:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
2017-09-04 02:20:56 +00:00
|
|
|
_requestBuilder = requestBuilder.Search;
|
|
|
|
_artistService = artistService;
|
2015-05-07 15:43:52 +00:00
|
|
|
_logger = logger;
|
2015-01-09 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tuple<Series, List<Episode>> GetSeriesInfo(int tvdbSeriesId)
|
|
|
|
{
|
2017-08-24 01:39:27 +00:00
|
|
|
throw new NotImplementedException();
|
2017-05-07 19:32:13 +00:00
|
|
|
}
|
2017-04-28 22:05:35 +00:00
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
public Tuple<Artist, List<Album>> GetArtistInfo(string foreignArtistId)
|
2017-05-07 19:32:13 +00:00
|
|
|
{
|
2017-04-28 22:05:35 +00:00
|
|
|
|
2017-06-19 13:06:37 +00:00
|
|
|
_logger.Debug("Getting Artist with LidarrAPI.MetadataID of {0}", foreignArtistId);
|
2017-05-05 17:57:58 +00:00
|
|
|
|
2017-05-07 19:32:13 +00:00
|
|
|
// We need to perform a direct lookup of the artist
|
|
|
|
var httpRequest = _requestBuilder.Create()
|
2017-07-03 18:39:06 +00:00
|
|
|
.SetSegment("route", "artists/" + foreignArtistId)
|
2017-08-14 17:26:35 +00:00
|
|
|
.Build();
|
2017-05-05 18:35:28 +00:00
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-04-30 21:54:01 +00:00
|
|
|
|
2017-05-07 19:32:13 +00:00
|
|
|
httpRequest.AllowAutoRedirect = true;
|
|
|
|
httpRequest.SuppressHttpError = true;
|
2017-04-30 21:54:01 +00:00
|
|
|
|
2017-06-03 01:25:34 +00:00
|
|
|
var httpResponse = _httpClient.Get<ArtistResource>(httpRequest);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-04-30 21:54:01 +00:00
|
|
|
|
|
|
|
if (httpResponse.HasHttpError)
|
|
|
|
{
|
|
|
|
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
{
|
2017-06-18 02:27:01 +00:00
|
|
|
throw new ArtistNotFoundException(foreignArtistId);
|
2017-04-30 21:54:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-07 19:32:13 +00:00
|
|
|
throw new HttpException(httpRequest, httpResponse);
|
2017-05-05 18:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
var albums = httpResponse.Resource.Albums.Select(MapAlbum);
|
|
|
|
var artist = MapArtist(httpResponse.Resource);
|
2017-05-07 19:32:13 +00:00
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
return new Tuple<Artist, List<Album>>(artist, albums.ToList());
|
2017-05-07 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:05:35 +00:00
|
|
|
public List<Artist> SearchForNewArtist(string title)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var lowerTitle = title.ToLowerInvariant();
|
|
|
|
Console.WriteLine("Searching for " + lowerTitle);
|
|
|
|
|
2017-07-08 12:08:24 +00:00
|
|
|
if (lowerTitle.StartsWith("lidarr:") || lowerTitle.StartsWith("lidarrid:"))
|
2017-04-30 13:54:37 +00:00
|
|
|
{
|
|
|
|
var slug = lowerTitle.Split(':')[1].Trim();
|
|
|
|
|
2017-05-07 19:32:13 +00:00
|
|
|
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
|
2017-04-30 13:54:37 +00:00
|
|
|
{
|
|
|
|
return new List<Artist>();
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:54:01 +00:00
|
|
|
try
|
|
|
|
{
|
2017-05-07 19:32:13 +00:00
|
|
|
return new List<Artist> { GetArtistInfo(slug).Item1 };
|
2017-04-30 21:54:01 +00:00
|
|
|
}
|
|
|
|
catch (ArtistNotFoundException)
|
|
|
|
{
|
|
|
|
return new List<Artist>();
|
|
|
|
}
|
2017-04-30 13:54:37 +00:00
|
|
|
}
|
2015-05-07 15:43:52 +00:00
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
var httpRequest = _requestBuilder.Create()
|
2017-04-30 21:54:01 +00:00
|
|
|
.SetSegment("route", "search")
|
2017-06-22 01:30:45 +00:00
|
|
|
.AddQueryParam("type", "artist")
|
2017-06-03 01:25:34 +00:00
|
|
|
.AddQueryParam("query", title.ToLower().Trim())
|
2017-04-28 22:05:35 +00:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
2015-05-07 15:43:52 +00:00
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
var httpResponse = _httpClient.Get<List<ArtistResource>>(httpRequest);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
return httpResponse.Resource.SelectList(MapSearhResult);
|
2015-05-07 15:43:52 +00:00
|
|
|
}
|
2015-08-12 18:45:44 +00:00
|
|
|
catch (HttpException)
|
2015-05-07 15:43:52 +00:00
|
|
|
{
|
|
|
|
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with SkyHook.", title);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Warn(ex, ex.Message);
|
2015-05-07 15:43:52 +00:00
|
|
|
throw new SkyHookException("Search for '{0}' failed. Invalid response received from SkyHook.", title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
private Artist MapSearhResult(ArtistResource resource)
|
|
|
|
{
|
|
|
|
var artist = _artistService.FindById(resource.Id);
|
|
|
|
|
|
|
|
if (artist == null)
|
|
|
|
{
|
|
|
|
artist = MapArtist(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
private static Album MapAlbum(AlbumResource resource)
|
2017-05-05 17:57:58 +00:00
|
|
|
{
|
2017-06-13 02:02:17 +00:00
|
|
|
Album album = new Album();
|
2017-06-18 02:27:01 +00:00
|
|
|
album.Title = resource.Title;
|
|
|
|
album.ForeignAlbumId = resource.Id;
|
|
|
|
album.ReleaseDate = resource.ReleaseDate;
|
|
|
|
album.CleanTitle = Parser.Parser.CleanArtistTitle(album.Title);
|
|
|
|
album.AlbumType = resource.Type;
|
2017-08-14 17:26:35 +00:00
|
|
|
album.Images = resource.Images.Select(MapImage).ToList();
|
2017-06-18 02:27:01 +00:00
|
|
|
|
|
|
|
var tracks = resource.Tracks.Select(MapTrack);
|
|
|
|
album.Tracks = tracks.ToList();
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-05-05 17:57:58 +00:00
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
return album;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Track MapTrack(TrackResource resource)
|
|
|
|
{
|
|
|
|
Track track = new Track();
|
2017-06-18 02:27:01 +00:00
|
|
|
track.Title = resource.TrackName;
|
|
|
|
track.ForeignTrackId = resource.Id;
|
|
|
|
track.TrackNumber = resource.TrackNumber;
|
2017-08-14 02:58:42 +00:00
|
|
|
track.Duration = resource.DurationMs;
|
2017-06-13 02:02:17 +00:00
|
|
|
return track;
|
2017-05-05 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
private static Artist MapArtist(ArtistResource resource)
|
2017-04-30 21:54:01 +00:00
|
|
|
{
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
Artist artist = new Artist();
|
|
|
|
|
|
|
|
artist.Name = resource.ArtistName;
|
2017-06-19 13:06:37 +00:00
|
|
|
artist.ForeignArtistId = resource.Id;
|
2017-06-13 02:02:17 +00:00
|
|
|
artist.Genres = resource.Genres;
|
|
|
|
artist.Overview = resource.Overview;
|
|
|
|
artist.NameSlug = Parser.Parser.CleanArtistTitle(artist.Name);
|
|
|
|
artist.CleanName = Parser.Parser.CleanArtistTitle(artist.Name);
|
2017-09-04 02:20:56 +00:00
|
|
|
artist.SortName = SeriesTitleNormalizer.Normalize(artist.Name, 0);
|
2017-08-14 17:26:35 +00:00
|
|
|
artist.Images = resource.Images.Select(MapImage).ToList();
|
2017-09-04 02:20:56 +00:00
|
|
|
artist.Status = ArtistStatusType.Continuing; // TODO: Remove HACK when we get from Metadata
|
|
|
|
artist.Ratings = MapRatings(null); // TODO: Remove HACK when we get from Metadata
|
2017-04-30 21:54:01 +00:00
|
|
|
|
2017-06-13 02:02:17 +00:00
|
|
|
return artist;
|
2017-04-30 21:54:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 01:43:51 +00:00
|
|
|
private static Actor MapActors(ActorResource arg)
|
|
|
|
{
|
|
|
|
var newActor = new Actor
|
|
|
|
{
|
|
|
|
Name = arg.Name,
|
2015-05-07 15:43:52 +00:00
|
|
|
Character = arg.Character
|
2015-01-09 01:43:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (arg.Image != null)
|
|
|
|
{
|
|
|
|
newActor.Images = new List<MediaCover.MediaCover>
|
|
|
|
{
|
|
|
|
new MediaCover.MediaCover(MediaCoverTypes.Headshot, arg.Image)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newActor;
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
private static ArtistStatusType MapArtistStatus(string status)
|
2015-01-09 01:43:51 +00:00
|
|
|
{
|
|
|
|
if (status.Equals("ended", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2017-09-04 02:20:56 +00:00
|
|
|
return ArtistStatusType.Ended;
|
2015-01-09 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
return ArtistStatusType.Continuing;
|
2015-01-09 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 13:17:49 +00:00
|
|
|
private static Core.Music.Ratings MapRatings(RatingResource rating)
|
2015-01-09 01:43:51 +00:00
|
|
|
{
|
|
|
|
if (rating == null)
|
|
|
|
{
|
2017-06-25 13:17:49 +00:00
|
|
|
return new Core.Music.Ratings();
|
2015-01-09 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 13:17:49 +00:00
|
|
|
return new Core.Music.Ratings
|
2015-01-09 01:43:51 +00:00
|
|
|
{
|
|
|
|
Votes = rating.Count,
|
|
|
|
Value = rating.Value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MediaCover.MediaCover MapImage(ImageResource arg)
|
|
|
|
{
|
|
|
|
return new MediaCover.MediaCover
|
|
|
|
{
|
|
|
|
Url = arg.Url,
|
|
|
|
CoverType = MapCoverType(arg.CoverType)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MediaCoverTypes MapCoverType(string coverType)
|
|
|
|
{
|
|
|
|
switch (coverType.ToLower())
|
|
|
|
{
|
|
|
|
case "poster":
|
|
|
|
return MediaCoverTypes.Poster;
|
|
|
|
case "banner":
|
|
|
|
return MediaCoverTypes.Banner;
|
|
|
|
case "fanart":
|
|
|
|
return MediaCoverTypes.Fanart;
|
2017-08-14 17:26:35 +00:00
|
|
|
case "cover":
|
|
|
|
return MediaCoverTypes.Cover;
|
|
|
|
case "disc":
|
|
|
|
return MediaCoverTypes.Disc;
|
2015-01-09 01:43:51 +00:00
|
|
|
default:
|
|
|
|
return MediaCoverTypes.Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|