Radarr/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs

671 lines
23 KiB
C#
Raw Normal View History

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;
using NLog;
using NzbDrone.Common.Cloud;
2015-01-09 01:43:51 +00:00
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
2020-04-03 00:57:36 +00:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Exceptions;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.Languages;
2015-01-09 01:43:51 +00:00
using NzbDrone.Core.MediaCover;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
using NzbDrone.Core.Movies;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.Movies.AlternativeTitles;
2022-03-08 02:03:00 +00:00
using NzbDrone.Core.Movies.Collections;
using NzbDrone.Core.Movies.Credits;
2020-05-26 01:55:10 +00:00
using NzbDrone.Core.Movies.Translations;
using NzbDrone.Core.Parser;
2015-01-09 01:43:51 +00:00
namespace NzbDrone.Core.MetadataSource.SkyHook
{
2020-05-10 01:49:09 +00:00
public class SkyHookProxy : IProvideMovieInfo, ISearchForNewMovie
2015-01-09 01:43:51 +00:00
{
private readonly IHttpClient _httpClient;
2015-05-07 15:43:52 +00:00
private readonly Logger _logger;
2019-12-22 21:24:11 +00:00
2020-05-10 01:49:09 +00:00
private readonly IHttpRequestBuilderFactory _radarrMetadata;
2020-04-03 00:57:36 +00:00
private readonly IConfigService _configService;
private readonly IMovieService _movieService;
2022-03-20 15:55:47 +00:00
private readonly IMovieMetadataService _movieMetadataService;
2020-05-26 01:55:10 +00:00
private readonly IMovieTranslationService _movieTranslationService;
2019-12-22 22:08:53 +00:00
public SkyHookProxy(IHttpClient httpClient,
IRadarrCloudRequestBuilder requestBuilder,
2020-04-03 00:57:36 +00:00
IConfigService configService,
2019-12-22 22:08:53 +00:00
IMovieService movieService,
2022-03-20 15:55:47 +00:00
IMovieMetadataService movieMetadataService,
2020-05-26 01:55:10 +00:00
IMovieTranslationService movieTranslationService,
2019-12-22 22:08:53 +00:00
Logger logger)
2015-01-09 01:43:51 +00:00
{
_httpClient = httpClient;
2020-05-10 01:49:09 +00:00
_radarrMetadata = requestBuilder.RadarrMetadata;
_configService = configService;
_movieService = movieService;
2022-03-20 15:55:47 +00:00
_movieMetadataService = movieMetadataService;
2020-05-26 01:55:10 +00:00
_movieTranslationService = movieTranslationService;
2015-05-07 15:43:52 +00:00
_logger = logger;
2015-01-09 01:43:51 +00:00
}
2019-12-22 22:08:53 +00:00
public HashSet<int> GetChangedMovies(DateTime startTime)
2019-12-15 03:30:44 +00:00
{
// Round down to the hour to ensure we cover gap and don't kill cache every call
var cacheAdjustedStart = startTime.AddMinutes(-15);
var startDate = cacheAdjustedStart.Date.AddHours(cacheAdjustedStart.Hour).ToString("s");
2019-12-15 05:35:42 +00:00
var request = _radarrMetadata.Create()
.SetSegment("route", "movie/changed")
.AddQueryParam("since", startDate)
2019-12-15 03:30:44 +00:00
.Build();
request.AllowAutoRedirect = true;
request.SuppressHttpError = true;
var response = _httpClient.Get<List<int>>(request);
2019-12-15 03:30:44 +00:00
return new HashSet<int>(response.Resource);
2019-12-15 03:30:44 +00:00
}
2022-03-20 15:55:47 +00:00
public Tuple<MovieMetadata, List<Credit>> GetMovieInfo(int tmdbId)
{
2020-05-10 01:49:09 +00:00
var httpRequest = _radarrMetadata.Create()
.SetSegment("route", "movie")
.Resource(tmdbId.ToString())
.Build();
2020-05-10 01:49:09 +00:00
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<MovieResource>(httpRequest);
2019-12-17 02:44:59 +00:00
2020-05-10 01:49:09 +00:00
if (httpResponse.HasHttpError)
{
2020-05-10 01:49:09 +00:00
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
throw new MovieNotFoundException(tmdbId);
}
else
{
throw new HttpException(httpRequest, httpResponse);
}
}
2019-12-22 22:08:53 +00:00
2020-05-10 01:49:09 +00:00
var credits = new List<Credit>();
credits.AddRange(httpResponse.Resource.Credits.Cast.Select(MapCast));
credits.AddRange(httpResponse.Resource.Credits.Crew.Select(MapCrew));
2020-05-10 01:49:09 +00:00
var movie = MapMovie(httpResponse.Resource);
2022-03-20 15:55:47 +00:00
return new Tuple<MovieMetadata, List<Credit>>(movie, credits.ToList());
2020-05-10 01:49:09 +00:00
}
2022-03-08 02:03:00 +00:00
public MovieCollection GetCollectionInfo(int tmdbId)
{
var httpRequest = _radarrMetadata.Create()
.SetSegment("route", "movie/collection")
.Resource(tmdbId.ToString())
.Build();
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Get<CollectionResource>(httpRequest);
if (httpResponse.HasHttpError)
{
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
throw new MovieNotFoundException(tmdbId);
}
else
{
throw new HttpException(httpRequest, httpResponse);
}
}
var collection = MapCollection(httpResponse.Resource);
return collection;
}
2022-03-20 15:55:47 +00:00
public List<MovieMetadata> GetBulkMovieInfo(List<int> tmdbIds)
{
var httpRequest = _radarrMetadata.Create()
.SetSegment("route", "movie/bulk")
.Build();
httpRequest.Headers.ContentType = "application/json";
httpRequest.SetContent(tmdbIds.ToJson());
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
var httpResponse = _httpClient.Post<List<MovieResource>>(httpRequest);
if (httpResponse.HasHttpError || httpResponse.Resource.Count == 0)
{
throw new HttpException(httpRequest, httpResponse);
}
var movies = httpResponse.Resource.Select(MapMovie).ToList();
return movies;
}
2022-03-20 15:55:47 +00:00
public MovieMetadata GetMovieByImdbId(string imdbId)
2020-05-10 01:49:09 +00:00
{
imdbId = Parser.Parser.NormalizeImdbId(imdbId);
if (imdbId == null)
{
return null;
}
2020-05-10 01:49:09 +00:00
var httpRequest = _radarrMetadata.Create()
.SetSegment("route", "movie/imdb")
.Resource(imdbId.ToString())
.Build();
2020-05-10 01:49:09 +00:00
httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true;
2020-05-10 01:49:09 +00:00
var httpResponse = _httpClient.Get<List<MovieResource>>(httpRequest);
2020-05-10 01:49:09 +00:00
if (httpResponse.HasHttpError)
{
2020-05-10 01:49:09 +00:00
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
2020-05-10 01:49:09 +00:00
throw new MovieNotFoundException(imdbId);
}
2020-05-10 01:49:09 +00:00
else
{
2020-05-10 01:49:09 +00:00
throw new HttpException(httpRequest, httpResponse);
}
}
2020-05-10 01:49:09 +00:00
var movie = httpResponse.Resource.SelectList(MapMovie).FirstOrDefault();
2020-05-10 01:49:09 +00:00
return movie;
}
2022-03-20 15:55:47 +00:00
public MovieMetadata MapMovie(MovieResource resource)
2020-05-10 01:49:09 +00:00
{
2022-03-20 15:55:47 +00:00
var movie = new MovieMetadata();
2020-05-10 01:49:09 +00:00
var altTitles = new List<AlternativeTitle>();
2020-05-10 01:49:09 +00:00
movie.TmdbId = resource.TmdbId;
movie.ImdbId = resource.ImdbId;
movie.Title = resource.Title;
2020-05-26 01:55:10 +00:00
movie.OriginalTitle = resource.OriginalTitle;
movie.CleanTitle = resource.Title.CleanMovieTitle();
2020-05-10 01:49:09 +00:00
movie.SortTitle = Parser.Parser.NormalizeTitle(resource.Title);
2022-03-20 15:55:47 +00:00
movie.CleanOriginalTitle = resource.OriginalTitle.CleanMovieTitle();
2020-05-10 01:49:09 +00:00
movie.Overview = resource.Overview;
2020-05-10 01:49:09 +00:00
movie.AlternativeTitles.AddRange(resource.AlternativeTitles.Select(MapAlternativeTitle));
2020-05-26 01:55:10 +00:00
movie.Translations.AddRange(resource.Translations.Select(MapTranslation));
movie.OriginalLanguage = IsoLanguages.Find(resource.OriginalLanguage.ToLower())?.Language ?? Language.English;
2020-05-10 01:49:09 +00:00
movie.Website = resource.Homepage;
movie.InCinemas = resource.InCinema;
movie.PhysicalRelease = resource.PhysicalRelease;
2020-05-26 01:55:10 +00:00
movie.DigitalRelease = resource.DigitalRelease;
2020-05-10 01:49:09 +00:00
movie.Year = resource.Year;
2020-04-05 07:30:03 +00:00
2022-11-20 18:27:45 +00:00
// If the premier differs from the TMDB year, use it as a secondary year.
2020-05-10 01:49:09 +00:00
if (resource.Premier.HasValue && resource.Premier?.Year != movie.Year)
2020-04-05 07:30:03 +00:00
{
2020-05-10 01:49:09 +00:00
movie.SecondaryYear = resource.Premier?.Year;
2020-04-05 07:30:03 +00:00
}
2020-05-10 01:49:09 +00:00
if (resource.Runtime != null)
{
2020-05-10 01:49:09 +00:00
movie.Runtime = resource.Runtime.Value;
}
2022-03-20 15:55:47 +00:00
if (resource.Popularity != null)
{
movie.Popularity = resource.Popularity.Value;
}
2020-05-10 01:49:09 +00:00
var certificationCountry = _configService.CertificationCountry.ToString();
movie.Certification = resource.Certifications.FirstOrDefault(m => m.Country == certificationCountry)?.Certification;
2020-10-13 04:14:15 +00:00
movie.Ratings = MapRatings(resource.MovieRatings) ?? new Ratings();
2022-03-20 15:55:47 +00:00
movie.TmdbId = resource.TmdbId;
2020-05-10 01:49:09 +00:00
movie.Genres = resource.Genres;
2022-03-20 15:55:47 +00:00
movie.Images = resource.Images.Select(MapImage).ToList();
2022-11-20 18:27:45 +00:00
// movie.Genres = resource.Genres;
movie.Recommendations = resource.Recommendations?.Select(r => r.TmdbId).ToList() ?? new List<int>();
2020-05-10 01:49:09 +00:00
2022-11-20 18:27:45 +00:00
// Workaround due to metadata change until cache cleans up
2020-10-13 04:14:15 +00:00
if (movie.Ratings.Tmdb == null)
{
var tmdbRating = resource.Ratings.FirstOrDefault();
movie.Ratings.Tmdb = new RatingChild
{
Votes = tmdbRating.Count,
Value = tmdbRating.Value
};
}
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
var now = DateTime.Now;
2019-12-22 22:08:53 +00:00
2020-05-26 01:55:10 +00:00
movie.Status = MovieStatusType.Announced;
if (resource.InCinema.HasValue && now > resource.InCinema)
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
{
2020-05-26 01:55:10 +00:00
movie.Status = MovieStatusType.InCinemas;
2019-12-22 22:08:53 +00:00
2020-05-26 01:55:10 +00:00
if (!resource.PhysicalRelease.HasValue && !resource.DigitalRelease.HasValue && now > resource.InCinema.Value.AddDays(90))
2019-12-22 22:08:53 +00:00
{
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
movie.Status = MovieStatusType.Released;
2019-12-22 22:08:53 +00:00
}
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
}
2019-12-22 22:08:53 +00:00
2020-05-26 01:55:10 +00:00
if (resource.PhysicalRelease.HasValue && now >= resource.PhysicalRelease)
{
movie.Status = MovieStatusType.Released;
}
2019-12-22 22:08:53 +00:00
2020-05-26 01:55:10 +00:00
if (resource.DigitalRelease.HasValue && now >= resource.DigitalRelease)
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
{
movie.Status = MovieStatusType.Released;
}
2020-05-10 01:49:09 +00:00
movie.YouTubeTrailerId = resource.YoutubeTrailerId;
movie.Studio = resource.Studio;
if (resource.Collection != null)
{
2022-03-08 02:03:00 +00:00
movie.CollectionTmdbId = resource.Collection.TmdbId;
movie.CollectionTitle = resource.Collection.Name;
}
2020-05-10 01:49:09 +00:00
return movie;
}
private string StripTrailingTheFromTitle(string title)
{
if (title.EndsWith(",the"))
{
2020-05-10 01:49:09 +00:00
title = title.Substring(0, title.Length - 4);
}
2020-05-10 01:49:09 +00:00
else if (title.EndsWith(", the"))
{
2020-05-10 01:49:09 +00:00
title = title.Substring(0, title.Length - 5);
}
2020-05-10 01:49:09 +00:00
return title;
}
2022-03-20 15:55:47 +00:00
public MovieMetadata MapMovieToTmdbMovie(MovieMetadata movie)
{
2020-05-10 01:49:09 +00:00
try
{
var newMovie = movie;
2020-05-10 01:49:09 +00:00
if (movie.TmdbId > 0)
2019-12-17 02:44:59 +00:00
{
2022-03-20 15:55:47 +00:00
newMovie = _movieMetadataService.FindByTmdbId(movie.TmdbId);
2022-03-20 15:55:47 +00:00
if (newMovie != null)
{
2022-03-20 15:55:47 +00:00
return newMovie;
}
2022-03-20 15:55:47 +00:00
newMovie = GetMovieInfo(movie.TmdbId).Item1;
2019-12-17 02:44:59 +00:00
}
2020-05-10 01:49:09 +00:00
else if (movie.ImdbId.IsNotNullOrWhiteSpace())
2019-12-17 02:44:59 +00:00
{
newMovie = _movieMetadataService.FindByImdbId(Parser.Parser.NormalizeImdbId(movie.ImdbId));
if (newMovie != null)
{
return newMovie;
}
2020-05-10 01:49:09 +00:00
newMovie = GetMovieByImdbId(movie.ImdbId);
2019-12-17 02:44:59 +00:00
}
2020-05-10 01:49:09 +00:00
else
2019-07-10 03:14:53 +00:00
{
2020-05-10 01:49:09 +00:00
var yearStr = "";
if (movie.Year > 1900)
{
yearStr = $" {movie.Year}";
}
var newMovieObject = SearchForNewMovie(movie.Title + yearStr).FirstOrDefault();
if (newMovieObject == null)
{
newMovie = null;
}
else
{
newMovie = newMovieObject.MovieMetadata;
}
2020-05-10 01:49:09 +00:00
}
2020-05-10 01:49:09 +00:00
if (newMovie == null)
{
2020-05-10 01:49:09 +00:00
_logger.Warn("Couldn't map movie {0} to a movie on The Movie DB. It will not be added :(", movie.Title);
return null;
}
2020-05-10 01:49:09 +00:00
return newMovie;
2019-12-22 22:08:53 +00:00
}
2020-05-10 01:49:09 +00:00
catch (Exception ex)
{
2020-05-10 01:49:09 +00:00
_logger.Warn(ex, "Couldn't map movie {0} to a movie on The Movie DB. It will not be added :(", movie.Title);
return null;
}
}
public List<Movie> SearchForNewMovie(string title)
{
2019-12-17 02:44:59 +00:00
try
{
var lowerTitle = title.ToLower();
2019-12-17 02:44:59 +00:00
lowerTitle = lowerTitle.Replace(".", "");
var parserTitle = lowerTitle;
var parserResult = Parser.Parser.ParseMovieTitle(title, true);
2019-12-17 02:44:59 +00:00
var yearTerm = "";
if (parserResult != null && parserResult.PrimaryMovieTitle != title)
{
2022-11-20 18:27:45 +00:00
// Parser found something interesting!
parserTitle = parserResult.PrimaryMovieTitle.ToLower().Replace(".", " "); // TODO Update so not every period gets replaced (e.g. R.I.P.D.)
2019-12-17 02:44:59 +00:00
if (parserResult.Year > 1800)
{
2019-12-17 02:44:59 +00:00
yearTerm = parserResult.Year.ToString();
}
2019-12-17 02:44:59 +00:00
if (parserResult.ImdbId.IsNotNullOrWhiteSpace())
{
2019-12-17 02:44:59 +00:00
try
{
var movieLookup = GetMovieByImdbId(parserResult.ImdbId);
2022-03-20 15:55:47 +00:00
return movieLookup == null ? new List<Movie>() : new List<Movie> { _movieService.FindByTmdbId(movieLookup.TmdbId) ?? new Movie { MovieMetadata = movieLookup } };
2019-12-17 02:44:59 +00:00
}
catch (Exception)
{
return new List<Movie>();
}
}
if (parserResult.TmdbId > 0)
{
try
{
var movieLookup = GetMovieInfo(parserResult.TmdbId).Item1;
2022-03-20 15:55:47 +00:00
return movieLookup == null ? new List<Movie>() : new List<Movie> { _movieService.FindByTmdbId(movieLookup.TmdbId) ?? new Movie { MovieMetadata = movieLookup } };
}
catch (Exception)
{
return new List<Movie>();
}
}
}
parserTitle = StripTrailingTheFromTitle(parserTitle);
2019-12-17 02:44:59 +00:00
if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:"))
{
var slug = lowerTitle.Split(':')[1].Trim();
2019-12-17 02:44:59 +00:00
string imdbid = slug;
2019-12-17 02:44:59 +00:00
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
{
return new List<Movie>();
}
2015-01-09 01:43:51 +00:00
2019-12-17 02:44:59 +00:00
try
{
var movieLookup = GetMovieByImdbId(imdbid);
2022-03-20 15:55:47 +00:00
return movieLookup == null ? new List<Movie>() : new List<Movie> { _movieService.FindByTmdbId(movieLookup.TmdbId) ?? new Movie { MovieMetadata = movieLookup } };
2019-12-17 02:44:59 +00:00
}
catch (MovieNotFoundException)
{
return new List<Movie>();
}
}
2019-12-17 02:44:59 +00:00
if (lowerTitle.StartsWith("tmdb:") || lowerTitle.StartsWith("tmdbid:"))
{
var slug = lowerTitle.Split(':')[1].Trim();
2019-12-17 02:44:59 +00:00
int tmdbid = -1;
2019-12-22 22:08:53 +00:00
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || !int.TryParse(slug, out tmdbid))
2019-12-17 02:44:59 +00:00
{
return new List<Movie>();
}
2019-12-17 02:44:59 +00:00
try
{
var movieLookup = GetMovieInfo(tmdbid).Item1;
2022-03-20 15:55:47 +00:00
return movieLookup == null ? new List<Movie>() : new List<Movie> { _movieService.FindByTmdbId(movieLookup.TmdbId) ?? new Movie { MovieMetadata = movieLookup } };
2019-12-17 02:44:59 +00:00
}
catch (MovieNotFoundException)
{
return new List<Movie>();
}
}
var searchTerm = parserTitle.Replace("_", "+").Replace(" ", "+").Replace(".", "+");
2019-12-17 02:44:59 +00:00
var firstChar = searchTerm.First();
2020-05-10 01:49:09 +00:00
var request = _radarrMetadata.Create()
2019-12-17 02:44:59 +00:00
.SetSegment("route", "search")
2020-05-10 01:49:09 +00:00
.AddQueryParam("q", searchTerm)
2019-12-17 02:44:59 +00:00
.AddQueryParam("year", yearTerm)
.Build();
2019-12-17 02:44:59 +00:00
request.AllowAutoRedirect = true;
request.SuppressHttpError = true;
2020-05-10 01:49:09 +00:00
var httpResponse = _httpClient.Get<List<MovieResource>>(request);
2020-05-10 01:49:09 +00:00
return httpResponse.Resource.SelectList(MapSearchResult);
2019-12-17 02:44:59 +00:00
}
catch (HttpException ex)
2019-12-17 02:44:59 +00:00
{
_logger.Warn(ex);
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with TMDb.", ex, title);
}
catch (WebException ex)
{
_logger.Warn(ex);
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with TMDb.", ex, title, ex.Message);
2019-12-17 02:44:59 +00:00
}
catch (Exception ex)
{
_logger.Warn(ex);
throw new SkyHookException("Search for '{0}' failed. Invalid response received from TMDb.", ex, title);
2019-12-17 02:44:59 +00:00
}
2015-01-09 01:43:51 +00:00
}
2020-05-10 01:49:09 +00:00
private Movie MapSearchResult(MovieResource result)
{
2020-05-10 01:49:09 +00:00
var movie = _movieService.FindByTmdbId(result.TmdbId);
if (movie == null)
{
2022-03-20 15:55:47 +00:00
movie = new Movie { MovieMetadata = MapMovie(result) };
}
2020-05-26 01:55:10 +00:00
else
{
movie.MovieMetadata.Value.Translations = _movieTranslationService.GetAllTranslationsForMovieMetadata(movie.MovieMetadataId);
2020-05-26 01:55:10 +00:00
}
return movie;
}
2022-03-08 02:03:00 +00:00
private MovieCollection MapCollection(CollectionResource arg)
{
var collection = new MovieCollection
{
TmdbId = arg.TmdbId,
Title = arg.Name,
Overview = arg.Overview,
CleanTitle = arg.Name.CleanMovieTitle(),
SortTitle = Parser.Parser.NormalizeTitle(arg.Name),
Images = arg.Images?.Select(MapImage).ToList() ?? new List<MediaCover.MediaCover>(),
Movies = arg.Parts?.Select(x => MapMovie(x)).ToList() ?? new List<MovieMetadata>()
};
return collection;
}
private static Credit MapCast(CastResource arg)
2015-01-09 01:43:51 +00:00
{
var newActor = new Credit
2015-01-09 01:43:51 +00:00
{
Name = arg.Name,
Character = arg.Character,
Order = arg.Order,
2020-05-10 01:49:09 +00:00
CreditTmdbId = arg.CreditId,
PersonTmdbId = arg.TmdbId,
Type = CreditType.Cast,
Images = arg.Images.Select(MapImage).ToList()
2015-01-09 01:43:51 +00:00
};
return newActor;
}
private static Credit MapCrew(CrewResource arg)
2015-01-09 01:43:51 +00:00
{
var newActor = new Credit
2015-01-09 01:43:51 +00:00
{
Name = arg.Name,
Department = arg.Department,
Job = arg.Job,
2020-05-10 01:49:09 +00:00
CreditTmdbId = arg.CreditId,
PersonTmdbId = arg.TmdbId,
Type = CreditType.Crew,
Images = arg.Images.Select(MapImage).ToList()
};
2015-01-09 01:43:51 +00:00
2020-05-10 01:49:09 +00:00
return newActor;
}
private static AlternativeTitle MapAlternativeTitle(AlternativeTitleResource arg)
{
var newAlternativeTitle = new AlternativeTitle
2015-01-09 01:43:51 +00:00
{
2020-05-10 01:49:09 +00:00
Title = arg.Title,
SourceType = SourceType.TMDB,
CleanTitle = arg.Title.CleanMovieTitle(),
2020-05-10 01:49:09 +00:00
Language = IsoLanguages.Find(arg.Language.ToLower())?.Language ?? Language.English
};
2020-05-10 01:49:09 +00:00
return newAlternativeTitle;
2015-01-09 01:43:51 +00:00
}
2020-05-26 01:55:10 +00:00
private static MovieTranslation MapTranslation(TranslationResource arg)
{
var newAlternativeTitle = new MovieTranslation
{
Title = arg.Title,
Overview = arg.Overview,
CleanTitle = arg.Title.CleanMovieTitle(),
2020-05-26 01:55:10 +00:00
Language = IsoLanguages.Find(arg.Language.ToLower())?.Language
};
return newAlternativeTitle;
}
2020-10-13 04:14:15 +00:00
private static Ratings MapRatings(RatingResource ratings)
{
2020-10-13 04:14:15 +00:00
if (ratings == null)
{
2020-05-10 01:49:09 +00:00
return new Ratings();
}
2020-10-13 04:14:15 +00:00
var mappedRatings = new Ratings();
if (ratings.Tmdb != null)
2020-05-10 01:49:09 +00:00
{
2020-10-13 04:14:15 +00:00
mappedRatings.Tmdb = new RatingChild
{
Type = (RatingType)Enum.Parse(typeof(RatingType), ratings.Tmdb.Type),
Value = ratings.Tmdb.Value,
Votes = ratings.Tmdb.Count
};
}
if (ratings.Imdb != null)
{
mappedRatings.Imdb = new RatingChild
{
Type = (RatingType)Enum.Parse(typeof(RatingType), ratings.Imdb.Type),
Value = ratings.Imdb.Value,
Votes = ratings.Imdb.Count
};
}
if (ratings.Metacritic != null)
{
mappedRatings.Metacritic = new RatingChild
{
Type = (RatingType)Enum.Parse(typeof(RatingType), ratings.Metacritic.Type),
Value = ratings.Metacritic.Value,
Votes = ratings.Metacritic.Count
};
}
if (ratings.RottenTomatoes != null)
{
mappedRatings.RottenTomatoes = new RatingChild
{
Type = (RatingType)Enum.Parse(typeof(RatingType), ratings.RottenTomatoes.Type),
Value = ratings.RottenTomatoes.Value,
Votes = ratings.RottenTomatoes.Count
};
}
return mappedRatings;
}
2020-05-10 01:49:09 +00:00
private static MediaCover.MediaCover MapImage(ImageResource arg)
{
2020-05-10 01:49:09 +00:00
return new MediaCover.MediaCover
2019-12-22 21:24:10 +00:00
{
2020-05-10 01:49:09 +00:00
Url = arg.Url,
CoverType = MapCoverType(arg.CoverType)
};
}
2019-12-22 21:24:10 +00:00
2020-05-10 01:49:09 +00:00
private static MediaCoverTypes MapCoverType(string coverType)
{
switch (coverType.ToLower())
{
case "poster":
return MediaCoverTypes.Poster;
case "headshot":
return MediaCoverTypes.Headshot;
case "fanart":
return MediaCoverTypes.Fanart;
default:
return MediaCoverTypes.Unknown;
2019-12-22 21:24:10 +00:00
}
}
2015-01-09 01:43:51 +00:00
}
}