2013-04-18 03:33:17 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-04-05 09:24:23 +03:00
|
|
|
using System.IO;
|
2013-03-31 23:25:39 +03:00
|
|
|
using System.Linq;
|
2013-09-09 09:40:24 +03:00
|
|
|
using System.Net;
|
2013-07-30 02:52:54 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2013-09-09 09:40:24 +03:00
|
|
|
using NLog;
|
2013-05-29 03:15:42 +03:00
|
|
|
using NzbDrone.Common;
|
2013-03-31 23:25:39 +03:00
|
|
|
using NzbDrone.Core.MediaCover;
|
|
|
|
using NzbDrone.Core.MetadataSource.Trakt;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using RestSharp;
|
|
|
|
using Episode = NzbDrone.Core.Tv.Episode;
|
2013-08-18 03:17:37 +03:00
|
|
|
using NzbDrone.Core.Rest;
|
2013-03-31 23:25:39 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.MetadataSource
|
|
|
|
{
|
2013-06-02 22:29:00 +03:00
|
|
|
public class TraktProxy : ISearchForNewSeries, IProvideSeriesInfo
|
2013-03-31 23:25:39 +03:00
|
|
|
{
|
2013-09-09 09:40:24 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
private static readonly Regex InvalidSearchCharRegex = new Regex(@"[^a-zA-Z0-9\s-\.]", RegexOptions.Compiled);
|
2013-03-31 23:25:39 +03:00
|
|
|
|
2013-09-09 09:40:24 +03:00
|
|
|
public TraktProxy(Logger logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2013-03-31 23:25:39 +03:00
|
|
|
}
|
|
|
|
|
2013-09-09 09:40:24 +03:00
|
|
|
public List<Series> SearchForNewSeries(string title)
|
2013-08-18 04:52:56 +03:00
|
|
|
{
|
2013-09-09 09:40:24 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var client = BuildClient("search", "shows");
|
2013-09-10 08:22:38 +03:00
|
|
|
var restRequest = new RestRequest(GetSearchTerm(title) +"/30/seasons");
|
|
|
|
var response = client.ExecuteAndValidate<List<Show>>(restRequest);
|
2013-08-18 04:52:56 +03:00
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
return response.Select(MapSeries).ToList();
|
2013-09-09 09:40:24 +03:00
|
|
|
}
|
|
|
|
catch (WebException ex)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2013-08-18 04:52:56 +03:00
|
|
|
}
|
|
|
|
|
2013-06-02 22:29:00 +03:00
|
|
|
public Tuple<Series, List<Episode>> GetSeriesInfo(int tvDbSeriesId)
|
2013-03-31 23:25:39 +03:00
|
|
|
{
|
|
|
|
var client = BuildClient("show", "summary");
|
|
|
|
var restRequest = new RestRequest(tvDbSeriesId.ToString() + "/extended");
|
2013-08-18 03:17:37 +03:00
|
|
|
var response = client.ExecuteAndValidate<Show>(restRequest);
|
2013-03-31 23:25:39 +03:00
|
|
|
|
2013-08-18 03:17:37 +03:00
|
|
|
var episodes = response.seasons.SelectMany(c => c.episodes).Select(MapEpisode).ToList();
|
|
|
|
var series = MapSeries(response);
|
2013-06-02 22:29:00 +03:00
|
|
|
|
|
|
|
return new Tuple<Series, List<Episode>>(series, episodes);
|
2013-03-31 23:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static IRestClient BuildClient(string resource, string method)
|
|
|
|
{
|
2013-04-16 03:08:06 +03:00
|
|
|
return new RestClient(string.Format("http://api.trakt.tv/{0}/{1}.json/bc3c2c460f22cbb01c264022b540e191", resource, method));
|
2013-03-31 23:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Series MapSeries(Show show)
|
|
|
|
{
|
|
|
|
var series = new Series();
|
2013-04-17 09:55:39 +03:00
|
|
|
series.TvdbId = show.tvdb_id;
|
2013-03-31 23:25:39 +03:00
|
|
|
series.TvRageId = show.tvrage_id;
|
2013-04-01 02:40:19 +03:00
|
|
|
series.ImdbId = show.imdb_id;
|
2013-09-03 09:18:54 +03:00
|
|
|
series.Title = show.title;
|
|
|
|
series.CleanTitle = Parser.Parser.CleanSeriesTitle(show.title);
|
2013-09-27 06:40:36 +03:00
|
|
|
series.Year = GetYear(show.year, show.first_aired);
|
2013-09-03 09:18:54 +03:00
|
|
|
series.FirstAired = FromIso(show.first_aired_iso);
|
|
|
|
series.Overview = show.overview;
|
|
|
|
series.Runtime = show.runtime;
|
|
|
|
series.Network = show.network;
|
|
|
|
series.AirTime = show.air_time_utc;
|
|
|
|
series.TitleSlug = show.url.ToLower().Replace("http://trakt.tv/show/", "");
|
|
|
|
series.Status = GetSeriesStatus(show.status);
|
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
series.Seasons = show.seasons.Select(s => new Tv.Season
|
|
|
|
{
|
|
|
|
SeasonNumber = s.season
|
2013-09-10 09:22:54 +03:00
|
|
|
}).OrderByDescending(s => s.SeasonNumber).ToList();
|
2013-03-31 23:25:39 +03:00
|
|
|
|
|
|
|
series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Banner, Url = show.images.banner });
|
2013-04-05 09:24:23 +03:00
|
|
|
series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Poster, Url = GetPosterThumbnailUrl(show.images.poster) });
|
2013-03-31 23:25:39 +03:00
|
|
|
series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Fanart, Url = show.images.fanart });
|
|
|
|
return series;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Episode MapEpisode(Trakt.Episode traktEpisode)
|
|
|
|
{
|
|
|
|
var episode = new Episode();
|
|
|
|
episode.Overview = traktEpisode.overview;
|
|
|
|
episode.SeasonNumber = traktEpisode.season;
|
|
|
|
episode.EpisodeNumber = traktEpisode.episode;
|
|
|
|
episode.EpisodeNumber = traktEpisode.number;
|
|
|
|
episode.TvDbEpisodeId = traktEpisode.tvdb_id;
|
|
|
|
episode.Title = traktEpisode.title;
|
2013-07-26 06:25:24 +03:00
|
|
|
episode.AirDate = FromIsoToString(traktEpisode.first_aired_iso);
|
2013-07-24 08:28:06 +03:00
|
|
|
episode.AirDateUtc = FromIso(traktEpisode.first_aired_iso);
|
2013-03-31 23:25:39 +03:00
|
|
|
|
|
|
|
return episode;
|
|
|
|
}
|
|
|
|
|
2013-04-05 09:24:23 +03:00
|
|
|
private static string GetPosterThumbnailUrl(string posterUrl)
|
|
|
|
{
|
2013-05-13 04:32:21 +03:00
|
|
|
if (posterUrl.Contains("poster-small.jpg")) return posterUrl;
|
2013-04-06 03:12:50 +03:00
|
|
|
|
2013-04-05 09:24:23 +03:00
|
|
|
var extension = Path.GetExtension(posterUrl);
|
|
|
|
var withoutExtension = posterUrl.Substring(0, posterUrl.Length - extension.Length);
|
2013-05-29 07:10:23 +03:00
|
|
|
return withoutExtension + "-300" + extension;
|
2013-04-05 09:24:23 +03:00
|
|
|
}
|
2013-04-18 03:33:17 +03:00
|
|
|
|
|
|
|
private static SeriesStatusType GetSeriesStatus(string status)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(status)) return SeriesStatusType.Continuing;
|
|
|
|
if (status.Equals("Ended", StringComparison.InvariantCultureIgnoreCase)) return SeriesStatusType.Ended;
|
|
|
|
return SeriesStatusType.Continuing;
|
|
|
|
}
|
2013-05-13 04:32:21 +03:00
|
|
|
|
2013-05-14 06:21:37 +03:00
|
|
|
private static DateTime? FromEpoch(long ticks)
|
2013-05-13 04:32:21 +03:00
|
|
|
{
|
|
|
|
if (ticks == 0) return null;
|
|
|
|
|
2013-07-26 06:25:24 +03:00
|
|
|
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).AddSeconds(ticks);
|
2013-05-14 06:21:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static DateTime? FromIso(string iso)
|
|
|
|
{
|
|
|
|
DateTime result;
|
|
|
|
|
|
|
|
if (!DateTime.TryParse(iso, out result))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return result.ToUniversalTime();
|
2013-05-13 04:32:21 +03:00
|
|
|
}
|
2013-07-26 06:25:24 +03:00
|
|
|
|
|
|
|
private static string FromIsoToString(string iso)
|
|
|
|
{
|
2013-07-30 02:52:54 +03:00
|
|
|
if (String.IsNullOrWhiteSpace(iso)) return null;
|
2013-07-26 06:25:24 +03:00
|
|
|
|
2013-07-30 02:52:54 +03:00
|
|
|
var match = Regex.Match(iso, @"^\d{4}\W\d{2}\W\d{2}");
|
|
|
|
|
|
|
|
if (!match.Success) return null;
|
2013-07-26 06:25:24 +03:00
|
|
|
|
2013-07-30 02:52:54 +03:00
|
|
|
return match.Captures[0].Value;
|
2013-07-26 06:25:24 +03:00
|
|
|
}
|
2013-08-07 09:16:28 +03:00
|
|
|
|
2013-09-09 09:40:24 +03:00
|
|
|
private static string GetSearchTerm(string phrase)
|
|
|
|
{
|
|
|
|
phrase = phrase.RemoveAccent().ToLower();
|
|
|
|
phrase = phrase.Replace("&", "and");
|
|
|
|
phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty);
|
|
|
|
phrase = phrase.CleanSpaces().Replace(" ", "+");
|
2013-08-18 03:17:37 +03:00
|
|
|
|
2013-09-09 09:40:24 +03:00
|
|
|
return phrase;
|
|
|
|
}
|
2013-09-27 06:40:36 +03:00
|
|
|
|
|
|
|
private static int GetYear(int year, int firstAired)
|
|
|
|
{
|
|
|
|
if (year > 1969) return year;
|
|
|
|
|
|
|
|
if (firstAired == 0) return DateTime.Today.Year;
|
|
|
|
|
|
|
|
return year;
|
|
|
|
}
|
2013-03-31 23:25:39 +03:00
|
|
|
}
|
|
|
|
}
|