Fixed: Don't crap on bad searches

pull/3962/head
Qstick 5 years ago
parent 5d2ac0b86b
commit fcb31ac51b

@ -86,6 +86,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
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 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
// 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 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
} }
} }
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 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
public List<Movie> SearchForNewMovie(string title) public List<Movie> SearchForNewMovie(string title)
{ {
var lowerTitle = title.ToLower(); try
{
var lowerTitle = title.ToLower();
lowerTitle = lowerTitle.Replace(".", ""); lowerTitle = lowerTitle.Replace(".", "");
var parserResult = Parser.Parser.ParseMovieTitle(title, true, true); var parserResult = Parser.Parser.ParseMovieTitle(title, true, true);
var yearTerm = ""; var yearTerm = "";
if (parserResult != null && parserResult.MovieTitle != title) if (parserResult != null && parserResult.MovieTitle != title)
{
//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(); //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)
if (parserResult.ImdbId.IsNotNullOrWhiteSpace())
{
try
{ {
return new List<Movie> { GetMovieInfo(parserResult.ImdbId) }; yearTerm = parserResult.Year.ToString();
} }
catch (Exception)
if (parserResult.ImdbId.IsNotNullOrWhiteSpace())
{ {
return new List<Movie>(); try
} {
return new List<Movie> { GetMovieInfo(parserResult.ImdbId) };
}
catch (Exception)
{
return new List<Movie>();
}
}
} }
}
lowerTitle = StripTrailingTheFromTitle(lowerTitle); lowerTitle = StripTrailingTheFromTitle(lowerTitle);
if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:")) if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:"))
{ {
var slug = lowerTitle.Split(':')[1].Trim(); var slug = lowerTitle.Split(':')[1].Trim();
string imdbid = slug; string imdbid = slug;
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace)) if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace))
{ {
return new List<Movie>(); return new List<Movie>();
} }
try try
{ {
return new List<Movie> { GetMovieInfo(imdbid) }; return new List<Movie> { GetMovieInfo(imdbid) };
} }
catch (MovieNotFoundException) catch (MovieNotFoundException)
{ {
return new List<Movie>(); return new List<Movie>();
}
} }
}
if (lowerTitle.StartsWith("tmdb:") || lowerTitle.StartsWith("tmdbid:")) if (lowerTitle.StartsWith("tmdb:") || lowerTitle.StartsWith("tmdbid:"))
{ {
var slug = lowerTitle.Split(':')[1].Trim(); var slug = lowerTitle.Split(':')[1].Trim();
int tmdbid = -1; int tmdbid = -1;
if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || !(int.TryParse(slug, out tmdbid))) if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace) || !(int.TryParse(slug, out tmdbid)))
{ {
return new List<Movie>(); return new List<Movie>();
} }
try try
{ {
return new List<Movie> { GetMovieInfo(tmdbid) }; return new List<Movie> { GetMovieInfo(tmdbid) };
} }
catch (MovieNotFoundException) catch (MovieNotFoundException)
{ {
return new List<Movie>(); return new List<Movie>();
}
} }
}
var searchTerm = lowerTitle.Replace("_", "+").Replace(" ", "+").Replace(".", "+"); var searchTerm = lowerTitle.Replace("_", "+").Replace(" ", "+").Replace(".", "+");
var firstChar = searchTerm.First(); var firstChar = searchTerm.First();
var request = _movieBuilder.Create() var request = _movieBuilder.Create()
.SetSegment("route", "search") .SetSegment("route", "search")
.SetSegment("id", "movie") .SetSegment("id", "movie")
.SetSegment("secondaryRoute", "") .SetSegment("secondaryRoute", "")
.AddQueryParam("query", searchTerm) .AddQueryParam("query", searchTerm)
.AddQueryParam("year", yearTerm) .AddQueryParam("year", yearTerm)
.AddQueryParam("include_adult", false) .AddQueryParam("include_adult", false)
.Build(); .Build();
request.AllowAutoRedirect = true; request.AllowAutoRedirect = true;
request.SuppressHttpError = true; request.SuppressHttpError = true;
var response = _httpClient.Get<MovieSearchRoot>(request); var response = _httpClient.Get<MovieSearchRoot>(request);
var movieResults = response.Resource.results; var movieResults = response.Resource.results;
return movieResults.SelectList(MapMovie); return movieResults.SelectList(MapMovie);
}
catch (HttpException)
{
throw new SkyHookException("Search for '{0}' failed. Unable to communicate with TMDb.", title);
}
catch (Exception ex)
{
_logger.Warn(ex, ex.Message);
throw new SkyHookException("Search for '{0}' failed. Invalid response received from TMDb.", title);
}
} }
public Movie MapMovie(MovieResult result) public Movie MapMovie(MovieResult result)

Loading…
Cancel
Save