using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Ombi.Api.TvMaze.Models; using Ombi.Helpers; namespace Ombi.Api.TvMaze { public class TvMazeApi : ITvMazeApi { public TvMazeApi(ILogger logger) { Api = new Ombi.Api.Api(); Logger = logger; //Mapper = mapper; } private string Uri = "http://api.tvmaze.com"; private Api Api { get; } private ILogger Logger { get; } public async Task> Search(string searchTerm) { var request = new Request("search/shows", Uri, HttpMethod.Get); request.AddQueryString("q", searchTerm); request.ContentHeaders.Add(new KeyValuePair("Content-Type","application/json")); return await Api.Request>(request); } public async Task ShowLookup(int showId) { var request = new Request($"shows/{showId}", Uri, HttpMethod.Get); request.AddContentHeader("Content-Type", "application/json"); return await Api.Request(request); } public async Task> EpisodeLookup(int showId) { var request = new Request($"shows/{showId}/episodes", Uri, HttpMethod.Get); request.AddContentHeader("Content-Type", "application/json"); return await Api.Request>(request); } public async Task ShowLookupByTheTvDbId(int theTvDbId) { var request = new Request($"lookup/shows?thetvdb={theTvDbId}", Uri, HttpMethod.Get); request.AddContentHeader("Content-Type", "application/json"); try { var obj = await Api.Request(request); var episodes = await EpisodeLookup(obj.id); foreach (var e in episodes) { // Check if the season exists var currentSeason = obj.Season.FirstOrDefault(x => x.SeasonNumber == e.season); if (currentSeason == null) { // Create the season obj.Season.Add(new TvMazeCustomSeason { SeasonNumber = e.season, EpisodeNumber = new List {e.number} }); } else { // Just add a new episode into that season currentSeason.EpisodeNumber.Add(e.number); } } return obj; } catch (Exception e) { Logger.LogError(LoggingEvents.ApiException, e, "Exception when calling ShowLookupByTheTvDbId with id:{0}",theTvDbId); return null; } } public async Task> GetSeasons(int id) { var request = new Request($"shows/{id}/seasons", Uri, HttpMethod.Get); request.AddContentHeader("Content-Type", "application/json"); return await Api.Request>(request); } } }