From 55f296b9a40e256551787c227dc93b9decb006b3 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 18 Mar 2016 14:39:04 +0000 Subject: [PATCH] Added TVMaze to the search. #21 --- .../PlexRequests.Api.Models.csproj | 1 + PlexRequests.Api.Models/Tv/TvMazeSearch.cs | 97 +++++++++++++++++++ PlexRequests.Api/PlexRequests.Api.csproj | 2 + PlexRequests.Api/TvMazeApi.cs | 60 ++++++++++++ PlexRequests.Api/TvMazeBase.cs | 33 +++++++ .../Models/SearchTvShowViewModel.cs | 2 - PlexRequests.UI/Modules/SearchModule.cs | 44 ++++----- PlexRequests.UI/Views/Search/Index.cshtml | 2 +- 8 files changed, 214 insertions(+), 27 deletions(-) create mode 100644 PlexRequests.Api.Models/Tv/TvMazeSearch.cs create mode 100644 PlexRequests.Api/TvMazeApi.cs create mode 100644 PlexRequests.Api/TvMazeBase.cs diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index 402d1192a..fee0b033c 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -60,6 +60,7 @@ + diff --git a/PlexRequests.Api.Models/Tv/TvMazeSearch.cs b/PlexRequests.Api.Models/Tv/TvMazeSearch.cs new file mode 100644 index 000000000..399154c57 --- /dev/null +++ b/PlexRequests.Api.Models/Tv/TvMazeSearch.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlexRequests.Api.Models.Tv +{ + public class Schedule + { + public string time { get; set; } + public List days { get; set; } + } + + public class Rating + { + public double? average { get; set; } + } + + public class Country + { + public string name { get; set; } + public string code { get; set; } + public string timezone { get; set; } + } + + public class Network + { + public int id { get; set; } + public string name { get; set; } + public Country country { get; set; } + } + + public class Externals + { + public int? tvrage { get; set; } + public int? thetvdb { get; set; } + public string imdb { get; set; } + } + + public class Image + { + public string medium { get; set; } + public string original { get; set; } + } + + public class Self + { + public string href { get; set; } + } + + public class Previousepisode + { + public string href { get; set; } + } + + public class Nextepisode + { + public string href { get; set; } + } + + public class Links + { + public Self self { get; set; } + public Previousepisode previousepisode { get; set; } + public Nextepisode nextepisode { get; set; } + } + + public class Show + { + public int id { get; set; } + public string url { get; set; } + public string name { get; set; } + public string type { get; set; } + public string language { get; set; } + public List genres { get; set; } + public string status { get; set; } + public int? runtime { get; set; } + public string premiered { get; set; } + public Schedule schedule { get; set; } + public Rating rating { get; set; } + public int weight { get; set; } + public Network network { get; set; } + public object webChannel { get; set; } + public Externals externals { get; set; } + public Image image { get; set; } + public string summary { get; set; } + public int updated { get; set; } + public Links _links { get; set; } + } + + public class TvMazeSearch + { + public double score { get; set; } + public Show show { get; set; } + } +} diff --git a/PlexRequests.Api/PlexRequests.Api.csproj b/PlexRequests.Api/PlexRequests.Api.csproj index aea64364e..5157e905c 100644 --- a/PlexRequests.Api/PlexRequests.Api.csproj +++ b/PlexRequests.Api/PlexRequests.Api.csproj @@ -81,6 +81,8 @@ + + diff --git a/PlexRequests.Api/TvMazeApi.cs b/PlexRequests.Api/TvMazeApi.cs new file mode 100644 index 000000000..6af8c51a2 --- /dev/null +++ b/PlexRequests.Api/TvMazeApi.cs @@ -0,0 +1,60 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: TvMazeApi.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Collections.Generic; + +using NLog; + +using PlexRequests.Api.Models.Tv; + +using RestSharp; + +namespace PlexRequests.Api +{ + public class TvMazeApi : TvMazeBase + { + public TvMazeApi() + { + Api = new ApiRequest(); + } + private ApiRequest Api { get; } + private static Logger Log = LogManager.GetCurrentClassLogger(); + public List Search(string searchTerm) + { + var request = new RestRequest + { + Method = Method.GET, + Resource = "search/shows?q={searchTerm}" + }; + request.AddUrlSegment("searchTerm", searchTerm); + request.AddHeader("Content-Type", "application/json"); + + return Api.Execute>(request, new Uri(Uri)); + } + + } +} \ No newline at end of file diff --git a/PlexRequests.Api/TvMazeBase.cs b/PlexRequests.Api/TvMazeBase.cs new file mode 100644 index 000000000..1526603bc --- /dev/null +++ b/PlexRequests.Api/TvMazeBase.cs @@ -0,0 +1,33 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: TvMazeBase.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace PlexRequests.Api +{ + public class TvMazeBase + { + public string Uri = "http://api.tvmaze.com"; + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Models/SearchTvShowViewModel.cs b/PlexRequests.UI/Models/SearchTvShowViewModel.cs index 8e31193fc..ee6de42d1 100644 --- a/PlexRequests.UI/Models/SearchTvShowViewModel.cs +++ b/PlexRequests.UI/Models/SearchTvShowViewModel.cs @@ -48,8 +48,6 @@ public class SearchTvShowViewModel public string AirsTime { get; set; } public string Rating { get; set; } public string ImdbId { get; set; } - public string Zap2ItId { get; set; } - public string Added { get; set; } public int SiteRating { get; set; } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 6556f2e3c..e39adca21 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -26,6 +26,9 @@ #endregion using System; using System.Collections.Generic; +using System.Globalization; +using System.Linq; + using Nancy; using Nancy.Responses.Negotiation; @@ -104,41 +107,34 @@ private Response SearchMovie(string searchTerm) private Response SearchTvShow(string searchTerm) { Log.Trace("Searching for TV Show {0}", searchTerm); - var tvShow = TvApi.SearchTv(searchTerm, AuthToken); - - if (tvShow?.data == null) + //var tvShow = TvApi.SearchTv(searchTerm, AuthToken); + var tvShow = new TvMazeApi().Search(searchTerm); + if (!tvShow.Any()) { Log.Trace("TV Show data is null"); return Response.AsJson(""); } var model = new List(); - foreach (var t in tvShow.data) + foreach (var t in tvShow) { model.Add(new SearchTvShowViewModel { - Added = t.added, - AirsDayOfWeek = t.airsDayOfWeek, - AirsTime = t.airsTime, - Aliases = t.aliases, // We are constructing the banner with the id: // http://thetvdb.com/banners/_cache/posters/ID-1.jpg - Banner = t.id.ToString(), - FirstAired = t.firstAired, - Genre = t.genre, - Id = t.id, - ImdbId = t.imdbId, - LastUpdated = t.lastUpdated, - Network = t.network, - NetworkId = t.networkId, - Overview = t.overview, - Rating = t.rating, - Runtime = t.runtime, - SeriesId = t.id, - SeriesName = t.seriesName, - SiteRating = t.siteRating, - Status = t.status, - Zap2ItId = t.zap2itId + Banner = t.show.image?.medium, + FirstAired = t.show.premiered, + Id = t.show.id, + ImdbId = t.show.externals?.imdb, + Network = t.show.network.name, + NetworkId = t.show.network.id.ToString(), + Overview = t.show.summary, + Rating = t.score.ToString(CultureInfo.CurrentUICulture), + Runtime = t.show.runtime.ToString(), + SeriesId = t.show.id, + SeriesName = t.show.name, + + Status = t.show.status, }); } diff --git a/PlexRequests.UI/Views/Search/Index.cshtml b/PlexRequests.UI/Views/Search/Index.cshtml index 8c3c491f1..309435839 100644 --- a/PlexRequests.UI/Views/Search/Index.cshtml +++ b/PlexRequests.UI/Views/Search/Index.cshtml @@ -69,7 +69,7 @@ {{/if_eq}} {{#if_eq type "tv"}} {{#if posterPath}} - poster + poster {{/if}} {{/if_eq}}