From 46bf80dcd10146996e65cf854f886feb55ec263a Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 30 Dec 2014 20:24:35 -0800 Subject: [PATCH] fixed: increased tvdb search limit to 10 --- src/NzbDrone.Common/Http/HttpClient.cs | 3 +- .../MetadataSource/TraktProxy.cs | 320 ------------------ src/NzbDrone.Core/MetadataSource/TvDbProxy.cs | 2 +- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 - 4 files changed, 3 insertions(+), 323 deletions(-) delete mode 100644 src/NzbDrone.Core/MetadataSource/TraktProxy.cs diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index f96cbb299..0af9c3bbd 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -100,7 +100,8 @@ namespace NzbDrone.Common.Http if (!RuntimeInfoBase.IsProduction && (response.StatusCode == HttpStatusCode.Moved || - response.StatusCode == HttpStatusCode.MovedPermanently)) + response.StatusCode == HttpStatusCode.MovedPermanently || + response.StatusCode == HttpStatusCode.Found)) { throw new Exception("Server requested a redirect to [" + response.Headers["Location"] + "]. Update the request URL to avoid this redirect."); } diff --git a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs b/src/NzbDrone.Core/MetadataSource/TraktProxy.cs deleted file mode 100644 index e4f84f4cb..000000000 --- a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs +++ /dev/null @@ -1,320 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using NLog; -using NzbDrone.Common.Extensions; -using NzbDrone.Common.Http; -using NzbDrone.Core.MediaCover; -using NzbDrone.Core.MetadataSource.Trakt; -using NzbDrone.Core.Tv; -using Episode = NzbDrone.Core.Tv.Episode; - -namespace NzbDrone.Core.MetadataSource -{ - public class TraktProxy //: ISearchForNewSeries, IProvideSeriesInfo - { - private readonly Logger _logger; - private readonly IHttpClient _httpClient; - private static readonly Regex CollapseSpaceRegex = new Regex(@"\s+", RegexOptions.Compiled); - private static readonly Regex InvalidSearchCharRegex = new Regex(@"(?:\*|\(|\)|'|!|@|\+)", RegexOptions.Compiled); - private static readonly Regex ExpandCamelCaseRegEx = new Regex(@"(? SearchTrakt(string title) - { - HttpRequest request; - - var lowerTitle = title.ToLowerInvariant(); - - if (lowerTitle.StartsWith("tvdb:") || lowerTitle.StartsWith("tvdbid:") || lowerTitle.StartsWith("slug:")) - { - var slug = lowerTitle.Split(':')[1].Trim(); - - if (slug.IsNullOrWhiteSpace() || slug.Any(char.IsWhiteSpace)) - { - return Enumerable.Empty(); - } - - request = _requestBuilder.Build("/{slug}/extended"); - - request.AddSegment("path", "show"); - request.AddSegment("resource", "summary"); - request.AddSegment("slug", GetSearchTerm(slug)); - - return new List { _httpClient.Get(request).Resource }; - } - - if (lowerTitle.StartsWith("imdb:") || lowerTitle.StartsWith("imdbid:")) - { - var slug = lowerTitle.Split(':')[1].TrimStart('t').Trim(); - - if (slug.IsNullOrWhiteSpace() || !slug.All(char.IsDigit) || slug.Length < 7) - { - return Enumerable.Empty(); - } - - title = "tt" + slug; - } - - request = _requestBuilder.Build(""); - - request.AddSegment("path", "search"); - request.AddSegment("resource", "shows"); - request.UriBuilder.SetQueryParam("query", GetSearchTerm(title)); - request.UriBuilder.SetQueryParam("seasons", true); - - return _httpClient.Get>(request).Resource; - } - - public List SearchForNewSeries(string title) - { - try - { - var series = SearchTrakt(title.Trim()).Select(MapSeries).ToList(); - - series.Sort(new TraktSearchSeriesComparer(title)); - - return series; - } - catch (HttpException) - { - throw new TraktException("Search for '{0}' failed. Unable to communicate with Trakt.", title); - } - catch (Exception ex) - { - _logger.WarnException(ex.Message, ex); - throw new TraktException("Search for '{0}' failed. Invalid response received from Trakt.", title); - } - } - - public Tuple> GetSeriesInfo(int tvdbSeriesId) - { - - var request = _requestBuilder.Build("/{tvdbId}/extended"); - - request.AddSegment("path", "show"); - request.AddSegment("resource", "summary"); - request.AddSegment("tvdbId", tvdbSeriesId.ToString()); - - var response = _httpClient.Get(request).Resource; - - var episodes = response.seasons.SelectMany(c => c.episodes).Select(MapEpisode); - - episodes = RemoveDuplicates(episodes); - - var series = MapSeries(response); - - return new Tuple>(series, episodes.ToList()); - } - - private static IEnumerable RemoveDuplicates(IEnumerable episodes) - { - //http://support.trakt.tv/forums/188762-general/suggestions/4430690-anger-management-duplicate-episode - var episodeGroup = episodes.GroupBy(e => e.SeasonNumber.ToString("0000") + e.EpisodeNumber.ToString("0000")); - return episodeGroup.Select(g => g.First()); - } - - private static Series MapSeries(Show show) - { - var series = new Series(); - series.TvdbId = show.tvdb_id; - series.TvRageId = show.tvrage_id; - series.ImdbId = show.imdb_id; - series.Title = show.title; - series.CleanTitle = Parser.Parser.CleanSeriesTitle(show.title); - series.SortTitle = SeriesTitleNormalizer.Normalize(show.title, show.tvdb_id); - series.Year = GetYear(show.year, show.first_aired); - series.FirstAired = FromIso(show.first_aired_iso); - series.Overview = show.overview; - series.Runtime = show.runtime; - series.Network = show.network; - series.AirTime = show.air_time; - series.TitleSlug = GetTitleSlug(show.url); - series.Status = GetSeriesStatus(show.status, show.ended); - series.Ratings = GetRatings(show.ratings); - series.Genres = show.genres; - series.Certification = show.certification; - series.Actors = GetActors(show.people); - series.Seasons = GetSeasons(show); - - series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Banner, Url = show.images.banner }); - series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Poster, Url = GetPosterThumbnailUrl(show.images.poster) }); - series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Fanart, Url = show.images.fanart }); - - return series; - } - - private static String GetTitleSlug(String url) - { - var slug = url.ToLower().Replace("http://trakt.tv/show/", ""); - - if (slug.StartsWith(".")) - { - slug = "dot" + slug.Substring(1); - } - - return slug; - } - - private static Episode MapEpisode(Trakt.Episode traktEpisode) - { - var episode = new Episode(); - episode.Overview = traktEpisode.overview; - episode.SeasonNumber = traktEpisode.season; - episode.EpisodeNumber = traktEpisode.number; - episode.Title = traktEpisode.title; - episode.AirDate = FromIsoToString(traktEpisode.first_aired_iso); - episode.AirDateUtc = FromIso(traktEpisode.first_aired_iso); - episode.Ratings = GetRatings(traktEpisode.ratings); - - //Don't include series fanart images as episode screenshot - if (!traktEpisode.images.screen.Contains("-940.")) - { - episode.Images.Add(new MediaCover.MediaCover(MediaCoverTypes.Screenshot, traktEpisode.images.screen)); - } - - return episode; - } - - private static string GetPosterThumbnailUrl(string posterUrl) - { - if (posterUrl.Contains("poster-small.jpg") || posterUrl.Contains("poster-dark.jpg")) return posterUrl; - - var extension = Path.GetExtension(posterUrl); - var withoutExtension = posterUrl.Substring(0, posterUrl.Length - extension.Length); - return withoutExtension + "-300" + extension; - } - - private static SeriesStatusType GetSeriesStatus(string status, bool? ended) - { - if (string.IsNullOrWhiteSpace(status)) - { - if (ended.HasValue && ended.Value) - { - return SeriesStatusType.Ended; - } - - return SeriesStatusType.Continuing; - } - if (status.Equals("Ended", StringComparison.InvariantCultureIgnoreCase)) return SeriesStatusType.Ended; - return SeriesStatusType.Continuing; - } - - private static DateTime? FromIso(string iso) - { - DateTime result; - - if (!DateTime.TryParse(iso, out result)) - return null; - - return result.ToUniversalTime(); - } - - private static string FromIsoToString(string iso) - { - if (String.IsNullOrWhiteSpace(iso)) return null; - - var match = Regex.Match(iso, @"^\d{4}\W\d{2}\W\d{2}"); - - if (!match.Success) return null; - - return match.Captures[0].Value; - } - - private static string GetSearchTerm(string phrase) - { - phrase = phrase.RemoveAccent(); - phrase = InvalidSearchCharRegex.Replace(phrase, ""); - -// if (!phrase.Any(char.IsWhiteSpace) && phrase.Any(char.IsUpper) && phrase.Any(char.IsLower) && phrase.Length > 4) -// { -// phrase = ExpandCamelCaseRegEx.Replace(phrase, " "); -// } - - phrase = CollapseSpaceRegex.Replace(phrase, " ").Trim(); - phrase = phrase.Trim('-'); - - phrase = System.Web.HttpUtility.UrlEncode(phrase.ToLower()); - - return phrase; - } - - private static int GetYear(int year, int firstAired) - { - if (year > 1969) return year; - - if (firstAired == 0) return DateTime.Today.Year; - - return year; - } - - private static Tv.Ratings GetRatings(Trakt.Ratings ratings) - { - return new Tv.Ratings - { - Percentage = ratings.percentage, - Votes = ratings.votes, - Loved = ratings.loved, - Hated = ratings.hated - }; - } - - private static List GetActors(People people) - { - if (people == null) - { - return new List(); - } - - return GetActors(people.actors).ToList(); - } - - private static IEnumerable GetActors(IEnumerable trakcActors) - { - foreach (var traktActor in trakcActors) - { - var actor = new Tv.Actor - { - Name = traktActor.name, - Character = traktActor.character, - }; - - actor.Images.Add(new MediaCover.MediaCover(MediaCoverTypes.Headshot, traktActor.images.headshot)); - - yield return actor; - } - } - - private static List GetSeasons(Show show) - { - var seasons = new List(); - - foreach (var traktSeason in show.seasons.OrderByDescending(s => s.season)) - { - var season = new Tv.Season - { - SeasonNumber = traktSeason.season - }; - - if (traktSeason.images != null) - { - season.Images.Add(new MediaCover.MediaCover(MediaCoverTypes.Poster, traktSeason.images.poster)); - } - - seasons.Add(season); - } - - return seasons; - } - } -} diff --git a/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs b/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs index 26a94d006..904dece74 100644 --- a/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs @@ -45,7 +45,7 @@ namespace NzbDrone.Core.MetadataSource return new[] { _tvdb.GetShow(tvdbId) }; } - return _tvdb.Search(GetSearchTerm(lowerTitle)); + return _tvdb.Search(GetSearchTerm(lowerTitle), 10); } public List SearchForNewSeries(string title) diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 4dff656ff..7762523ee 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -631,7 +631,6 @@ -