mirror of
https://github.com/Radarr/Radarr
synced 2025-01-04 06:23:32 +00:00
Fixed: Don't crap on bad searches
This commit is contained in:
parent
5d2ac0b86b
commit
fcb31ac51b
1 changed files with 105 additions and 89 deletions
|
@ -86,6 +86,7 @@ public Movie GetMovieInfo(int TmdbId, Profile profile = null, bool hasPreDBEntry
|
||||||
request.SuppressHttpError = true;
|
request.SuppressHttpError = true;
|
||||||
|
|
||||||
var response = _httpClient.Get<MovieResourceRoot>(request);
|
var response = _httpClient.Get<MovieResourceRoot>(request);
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||||
{
|
{
|
||||||
throw new MovieNotFoundException("Movie not found.");
|
throw new MovieNotFoundException("Movie not found.");
|
||||||
|
@ -305,14 +306,17 @@ public Movie GetMovieInfo(string imdbId)
|
||||||
// request.SuppressHttpError = true;
|
// request.SuppressHttpError = true;
|
||||||
|
|
||||||
var response = _httpClient.Get<FindRoot>(request);
|
var response = _httpClient.Get<FindRoot>(request);
|
||||||
if (response.StatusCode != HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
throw new HttpException(request, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.Headers.ContentType != HttpAccept.JsonCharset.Value)
|
if (response.HasHttpError)
|
||||||
{
|
{
|
||||||
throw new HttpException(request, response);
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||||
|
{
|
||||||
|
throw new MovieNotFoundException(imdbId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new HttpException(request, response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The dude abides, so should us, Lets be nice to TMDb
|
// The dude abides, so should us, Lets be nice to TMDb
|
||||||
|
@ -328,9 +332,9 @@ public Movie GetMovieInfo(string imdbId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var resources = response.Resource;
|
var resource = response.Resource.movie_results.FirstOrDefault();
|
||||||
|
|
||||||
return resources.movie_results.SelectList(MapMovie).FirstOrDefault();
|
return MapMovie(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Movie> DiscoverNewMovies(string action)
|
public List<Movie> DiscoverNewMovies(string action)
|
||||||
|
@ -381,102 +385,114 @@ private string StripTrailingTheFromTitle(string title)
|
||||||
|
|
||||||
public List<Movie> SearchForNewMovie(string title)
|
public List<Movie> SearchForNewMovie(string title)
|
||||||
{
|
{
|
||||||
var lowerTitle = title.ToLower();
|
try
|
||||||
|
|
||||||
lowerTitle = lowerTitle.Replace(".", "");
|
|
||||||
|
|
||||||
var parserResult = Parser.Parser.ParseMovieTitle(title, true, true);
|
|
||||||
|
|
||||||
var yearTerm = "";
|
|
||||||
|
|
||||||
if (parserResult != null && parserResult.MovieTitle != title)
|
|
||||||
{
|
{
|
||||||
//Parser found something interesting!
|
var lowerTitle = title.ToLower();
|
||||||
lowerTitle = parserResult.MovieTitle.ToLower().Replace(".", " "); //TODO Update so not every period gets replaced (e.g. R.I.P.D.)
|
|
||||||
if (parserResult.Year > 1800)
|
lowerTitle = lowerTitle.Replace(".", "");
|
||||||
|
|
||||||
|
var parserResult = Parser.Parser.ParseMovieTitle(title, true, true);
|
||||||
|
|
||||||
|
var yearTerm = "";
|
||||||
|
|
||||||
|
if (parserResult != null && parserResult.MovieTitle != title)
|
||||||
{
|
{
|
||||||
yearTerm = parserResult.Year.ToString();
|
//Parser found something interesting!
|
||||||
|
lowerTitle = parserResult.MovieTitle.ToLower().Replace(".", " "); //TODO Update so not every period gets replaced (e.g. R.I.P.D.)
|
||||||
|
if (parserResult.Year > 1800)
|
||||||
|
{
|
||||||
|
yearTerm = parserResult.Year.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parserResult.ImdbId.IsNotNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new List<Movie> { GetMovieInfo(parserResult.ImdbId) };
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return new List<Movie>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parserResult.ImdbId.IsNotNullOrWhiteSpace())
|
lowerTitle = StripTrailingTheFromTitle(lowerTitle);
|
||||||
|
|
||||||
|
if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:"))
|
||||||
{
|
{
|
||||||
try
|
var slug = lowerTitle.Split(':')[1].Trim();
|
||||||
{
|
|
||||||
return new List<Movie> { GetMovieInfo(parserResult.ImdbId) };
|
string imdbid = slug;
|
||||||
}
|
|
||||||
catch (Exception)
|
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
|
||||||
{
|
{
|
||||||
return new List<Movie>();
|
return new List<Movie>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new List<Movie> { GetMovieInfo(imdbid) };
|
||||||
|
}
|
||||||
|
catch (MovieNotFoundException)
|
||||||
|
{
|
||||||
|
return new List<Movie>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (lowerTitle.StartsWith("tmdb:") || lowerTitle.StartsWith("tmdbid:"))
|
||||||
|
{
|
||||||
|
var slug = lowerTitle.Split(':')[1].Trim();
|
||||||
|
|
||||||
|
int tmdbid = -1;
|
||||||
|
|
||||||
|
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || !(int.TryParse(slug, out tmdbid)))
|
||||||
|
{
|
||||||
|
return new List<Movie>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new List<Movie> { GetMovieInfo(tmdbid) };
|
||||||
|
}
|
||||||
|
catch (MovieNotFoundException)
|
||||||
|
{
|
||||||
|
return new List<Movie>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var searchTerm = lowerTitle.Replace("_", "+").Replace(" ", "+").Replace(".", "+");
|
||||||
|
|
||||||
|
var firstChar = searchTerm.First();
|
||||||
|
|
||||||
|
var request = _movieBuilder.Create()
|
||||||
|
.SetSegment("route", "search")
|
||||||
|
.SetSegment("id", "movie")
|
||||||
|
.SetSegment("secondaryRoute", "")
|
||||||
|
.AddQueryParam("query", searchTerm)
|
||||||
|
.AddQueryParam("year", yearTerm)
|
||||||
|
.AddQueryParam("include_adult", false)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
request.AllowAutoRedirect = true;
|
||||||
|
request.SuppressHttpError = true;
|
||||||
|
|
||||||
|
var response = _httpClient.Get<MovieSearchRoot>(request);
|
||||||
|
|
||||||
|
var movieResults = response.Resource.results;
|
||||||
|
|
||||||
|
return movieResults.SelectList(MapMovie);
|
||||||
}
|
}
|
||||||
|
catch (HttpException)
|
||||||
lowerTitle = StripTrailingTheFromTitle(lowerTitle);
|
|
||||||
|
|
||||||
if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:"))
|
|
||||||
{
|
{
|
||||||
var slug = lowerTitle.Split(':')[1].Trim();
|
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with TMDb.", title);
|
||||||
|
|
||||||
string imdbid = slug;
|
|
||||||
|
|
||||||
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
|
|
||||||
{
|
|
||||||
return new List<Movie>();
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return new List<Movie> { GetMovieInfo(imdbid) };
|
|
||||||
}
|
|
||||||
catch (MovieNotFoundException)
|
|
||||||
{
|
|
||||||
return new List<Movie>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
if (lowerTitle.StartsWith("tmdb:") || lowerTitle.StartsWith("tmdbid:"))
|
|
||||||
{
|
{
|
||||||
var slug = lowerTitle.Split(':')[1].Trim();
|
_logger.Warn(ex, ex.Message);
|
||||||
|
throw new SkyHookException("Search for '{0}' failed. Invalid response received from TMDb.", title);
|
||||||
int tmdbid = -1;
|
|
||||||
|
|
||||||
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || !(int.TryParse(slug, out tmdbid)))
|
|
||||||
{
|
|
||||||
return new List<Movie>();
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return new List<Movie> { GetMovieInfo(tmdbid) };
|
|
||||||
}
|
|
||||||
catch (MovieNotFoundException)
|
|
||||||
{
|
|
||||||
return new List<Movie>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchTerm = lowerTitle.Replace("_", "+").Replace(" ", "+").Replace(".", "+");
|
|
||||||
|
|
||||||
var firstChar = searchTerm.First();
|
|
||||||
|
|
||||||
var request = _movieBuilder.Create()
|
|
||||||
.SetSegment("route", "search")
|
|
||||||
.SetSegment("id", "movie")
|
|
||||||
.SetSegment("secondaryRoute", "")
|
|
||||||
.AddQueryParam("query", searchTerm)
|
|
||||||
.AddQueryParam("year", yearTerm)
|
|
||||||
.AddQueryParam("include_adult", false)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
request.AllowAutoRedirect = true;
|
|
||||||
request.SuppressHttpError = true;
|
|
||||||
|
|
||||||
var response = _httpClient.Get<MovieSearchRoot>(request);
|
|
||||||
|
|
||||||
var movieResults = response.Resource.results;
|
|
||||||
|
|
||||||
return movieResults.SelectList(MapMovie);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Movie MapMovie(MovieResult result)
|
public Movie MapMovie(MovieResult result)
|
||||||
|
|
Loading…
Reference in a new issue