2013-04-18 02:33:17 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-04-05 08:24:23 +02:00
|
|
|
|
using System.IO;
|
2013-03-31 22:25:39 +02:00
|
|
|
|
using System.Linq;
|
2013-05-29 02:15:42 +02:00
|
|
|
|
using NzbDrone.Common;
|
2013-03-31 22:25:39 +02:00
|
|
|
|
using NzbDrone.Core.MediaCover;
|
|
|
|
|
using NzbDrone.Core.MetadataSource.Trakt;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
using RestSharp;
|
|
|
|
|
using NzbDrone.Common.EnsureThat;
|
|
|
|
|
using Episode = NzbDrone.Core.Tv.Episode;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MetadataSource
|
|
|
|
|
{
|
|
|
|
|
public class TraktProxy : ISearchForNewSeries, IProvideSeriesInfo, IProvideEpisodeInfo
|
|
|
|
|
{
|
|
|
|
|
public List<Series> SearchForNewSeries(string title)
|
|
|
|
|
{
|
|
|
|
|
var client = BuildClient("search", "shows");
|
2013-05-30 05:26:47 +02:00
|
|
|
|
var restRequest = new RestRequest(title.ToSearchTerm());
|
2013-03-31 22:25:39 +02:00
|
|
|
|
var response = client.Execute<List<Show>>(restRequest);
|
|
|
|
|
|
|
|
|
|
return response.Data.Select(MapSeries).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Series GetSeriesInfo(int tvDbSeriesId)
|
|
|
|
|
{
|
|
|
|
|
var client = BuildClient("show", "summary");
|
|
|
|
|
var restRequest = new RestRequest(tvDbSeriesId.ToString());
|
|
|
|
|
var response = client.Execute<Show>(restRequest);
|
|
|
|
|
|
|
|
|
|
return MapSeries(response.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<Episode> GetEpisodeInfo(int tvDbSeriesId)
|
|
|
|
|
{
|
|
|
|
|
var client = BuildClient("show", "summary");
|
|
|
|
|
var restRequest = new RestRequest(tvDbSeriesId.ToString() + "/extended");
|
|
|
|
|
var response = client.Execute<Show>(restRequest);
|
|
|
|
|
|
|
|
|
|
return response.Data.seasons.SelectMany(c => c.episodes).Select(MapEpisode).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IRestClient BuildClient(string resource, string method)
|
|
|
|
|
{
|
2013-04-16 02:08:06 +02:00
|
|
|
|
return new RestClient(string.Format("http://api.trakt.tv/{0}/{1}.json/bc3c2c460f22cbb01c264022b540e191", resource, method));
|
2013-03-31 22:25:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Series MapSeries(Show show)
|
|
|
|
|
{
|
|
|
|
|
var series = new Series();
|
2013-04-17 08:55:39 +02:00
|
|
|
|
series.TvdbId = show.tvdb_id;
|
2013-03-31 22:25:39 +02:00
|
|
|
|
series.TvRageId = show.tvrage_id;
|
2013-04-01 01:40:19 +02:00
|
|
|
|
series.ImdbId = show.imdb_id;
|
2013-03-31 22:25:39 +02:00
|
|
|
|
series.Title = show.title;
|
2013-05-14 05:21:37 +02:00
|
|
|
|
series.FirstAired = FromIso(show.first_aired_iso);
|
2013-03-31 22:25:39 +02:00
|
|
|
|
series.Overview = show.overview;
|
|
|
|
|
series.Runtime = show.runtime;
|
|
|
|
|
series.Network = show.network;
|
2013-05-13 03:32:21 +02:00
|
|
|
|
series.AirTime = show.air_time_utc;
|
2013-03-31 23:45:16 +02:00
|
|
|
|
series.TitleSlug = show.url.ToLower().Replace("http://trakt.tv/show/", "");
|
2013-04-18 02:33:17 +02:00
|
|
|
|
series.Status = GetSeriesStatus(show.status);
|
2013-03-31 22:25:39 +02:00
|
|
|
|
|
|
|
|
|
series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Banner, Url = show.images.banner });
|
2013-04-05 08:24:23 +02:00
|
|
|
|
series.Images.Add(new MediaCover.MediaCover { CoverType = MediaCoverTypes.Poster, Url = GetPosterThumbnailUrl(show.images.poster) });
|
2013-03-31 22:25:39 +02: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-05-14 05:21:37 +02:00
|
|
|
|
episode.AirDate = FromIso(traktEpisode.first_aired_iso);
|
2013-03-31 22:25:39 +02:00
|
|
|
|
|
|
|
|
|
return episode;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-05 08:24:23 +02:00
|
|
|
|
private static string GetPosterThumbnailUrl(string posterUrl)
|
|
|
|
|
{
|
2013-05-13 03:32:21 +02:00
|
|
|
|
if (posterUrl.Contains("poster-small.jpg")) return posterUrl;
|
2013-04-06 02:12:50 +02:00
|
|
|
|
|
2013-04-05 08:24:23 +02:00
|
|
|
|
var extension = Path.GetExtension(posterUrl);
|
|
|
|
|
var withoutExtension = posterUrl.Substring(0, posterUrl.Length - extension.Length);
|
2013-05-29 06:10:23 +02:00
|
|
|
|
return withoutExtension + "-300" + extension;
|
2013-04-05 08:24:23 +02:00
|
|
|
|
}
|
2013-04-18 02:33:17 +02: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 03:32:21 +02:00
|
|
|
|
|
2013-05-14 05:21:37 +02:00
|
|
|
|
private static DateTime? FromEpoch(long ticks)
|
2013-05-13 03:32:21 +02:00
|
|
|
|
{
|
|
|
|
|
if (ticks == 0) return null;
|
|
|
|
|
|
2013-05-14 05:21:37 +02:00
|
|
|
|
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(ticks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DateTime? FromIso(string iso)
|
|
|
|
|
{
|
|
|
|
|
DateTime result;
|
|
|
|
|
|
|
|
|
|
if (!DateTime.TryParse(iso, out result))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return result.ToUniversalTime();
|
2013-05-13 03:32:21 +02:00
|
|
|
|
}
|
2013-03-31 22:25:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|