Added KitsuIO Episode Provider

This commit is contained in:
Pijus Kamandulis 2020-06-21 16:05:32 +03:00
parent 4869ae6d1b
commit 18bd22e8ee
5 changed files with 125 additions and 8 deletions

View File

@ -2,13 +2,13 @@ using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO.ApiClient
{
public partial class ApiSearchResponse
{
public List<Series> Data { get; set; }
public TemperaturesMeta Meta { get; set; }
public TemperaturesLinks Links { get; set; }
public ResponseMeta Meta { get; set; }
public ResponseLinks Links { get; set; }
}
public partial class ApiGetResponse
@ -56,6 +56,12 @@ namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
public string YoutubeVideoId { get; set; }
public ShowTypeEnum ShowType { get; set; }
public bool Nsfw { get; set; }
// Episode specific
public int? Number { get; set; }
public int? SeasonNumber { get; set; }
public DateTime? AirDate { get; set; }
public int? Length { get; set; }
}
public partial class CoverImage
@ -151,14 +157,14 @@ namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
public Uri Related { get; set; }
}
public partial class TemperaturesLinks
public partial class ResponseLinks
{
public Uri First { get; set; }
public Uri Next { get; set; }
public Uri Last { get; set; }
}
public partial class TemperaturesMeta
public partial class ResponseMeta
{
public long? Count { get; set; }
}

View File

@ -6,7 +6,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO.ApiClient
{
internal class KitsuIoApi
{
@ -44,5 +44,32 @@ namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
var response = JsonSerializer.Deserialize<ApiGetResponse>(responseString, _serializerOptions);
return response;
}
public static async Task<ApiSearchResponse> Get_Episodes(string seriesId)
{
var result = new ApiSearchResponse();
long episodeCount = 10;
var step = 10;
for (long offset = 0; offset < episodeCount; offset += step)
{
var queryString = $"?filter[mediaId]={seriesId}&page[limit]={step}&page[offset]={offset}";
var responseString = await _httpClient.GetStringAsync($"{_apiBaseUrl}/episodes{queryString}");
var response = JsonSerializer.Deserialize<ApiSearchResponse>(responseString, _serializerOptions);
episodeCount = response.Meta.Count.Value;
result.Data.AddRange(response.Data);
}
return result;
}
public static async Task<ApiGetResponse> Get_Episode(string episodeId)
{
var filterString = $"/{episodeId}";
var responseString = await _httpClient.GetStringAsync($"{_apiBaseUrl}/episodes{filterString}");
var response = JsonSerializer.Deserialize<ApiGetResponse>(responseString, _serializerOptions);
return response;
}
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Anime.Providers.KitsuIO.ApiClient;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Providers;
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO.Metadata
{
public class KitsuIoEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>
{
private readonly IHttpClient _httpClient;
public string Name => ProviderNames.KitsuIo;
public KitsuIoEpisodeProvider(IHttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{
var id = searchInfo.ProviderIds.GetOrDefault(ProviderNames.KitsuIo);
if (id == null)
{
return new List<RemoteSearchResult>();;
}
var apiResponse = await KitsuIoApi.Get_Episodes(id);
return apiResponse.Data.Select(x => new RemoteSearchResult
{
IndexNumber = x.Attributes.Number,
Name = x.Attributes.Titles.GetTitle,
ParentIndexNumber = x.Attributes.SeasonNumber,
PremiereDate = x.Attributes.AirDate,
ProviderIds = new Dictionary<string, string> {{ProviderNames.KitsuIo, x.Id.ToString()}},
SearchProviderName = Name,
Overview = x.Attributes.Synopsis,
});
}
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Episode>();
var id = info.ProviderIds.GetOrDefault(ProviderNames.KitsuIo);
if (string.IsNullOrEmpty(id))
{
return result;
}
var episodeInfo = await KitsuIoApi.Get_Episode(id);
result.HasMetadata = true;
result.Item = new Episode
{
IndexNumber = info.IndexNumber,
ParentIndexNumber = info.ParentIndexNumber,
Name = episodeInfo.Data.Attributes.Titles.GetTitle,
};
if (episodeInfo.Data.Attributes.Length != null)
{
result.Item.RunTimeTicks = TimeSpan.FromMinutes(episodeInfo.Data.Attributes.Length.Value).Ticks;
}
return result;
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
UserAgent = Constants.UserAgent,
CancellationToken = cancellationToken,
Url = url,
});
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Jellyfin.Plugin.Anime.Providers.KitsuIO.ApiClient;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Providers;
@ -11,7 +12,7 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
namespace Jellyfin.Plugin.Anime.Providers.KitsuIO.Metadata
{
public class KitsuIoSeriesProvider : IRemoteMetadataProvider<MediaBrowser.Controller.Entities.TV.Series, SeriesInfo>, IHasOrder
{
@ -19,7 +20,7 @@ namespace Jellyfin.Plugin.Anime.Providers.KitsuIO
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _paths;
public int Order => -4;
public string Name => "KitsuIO";
public string Name => ProviderNames.KitsuIo;
public KitsuIoSeriesProvider(ILoggerFactory loggerFactory, IApplicationPaths paths, IHttpClient httpClient)
{