Huge refactoring

Added person info provider
Added person image provider
Added trailers fetching
This commit is contained in:
PingWin committed 2021-05-22 22:21:08 +00:00
1 parent 35ce5536be
commit fce88168e8
43 files changed
+4381 -540

No files matched your search

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@@ -33,5 +34,37 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
public abstract IEnumerable<ImageType> GetSupportedImages(BaseItem item);
protected async Task<IEnumerable<RemoteImageInfo>> FilterEmptyImages(IEnumerable<RemoteImageInfo> images)
{
using var httpClient = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }, true);
var res = await Task.WhenAll(images.Select(i => NullIfEmptyImage(i, httpClient)));
return res.Where(i => i != null);
}
protected async Task<RemoteImageInfo> NullIfEmptyImage(RemoteImageInfo info, HttpClient httpClient)
{
if (info == null)
return null;
var currentUrl = info.Url;
while (!currentUrl.Contains("no-poster"))
{
var response = await httpClient.SendAsync(
new HttpRequestMessage(HttpMethod.Get, currentUrl), HttpCompletionOption.ResponseHeadersRead);
if ((int)response.StatusCode <= 299)
return info;
else if (response.Headers.Location != null)
{
currentUrl = response.Headers.Location.ToString();
continue;
}
else
throw new InvalidOperationException("Not expected answer");
}
return null;
}
}
}
@@ -0,0 +1,25 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Providers;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public abstract class BaseMetadataProvider : IMetadataProvider
{
protected IHttpClientFactory _httpClientFactory;
protected BaseMetadataProvider(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new System.ArgumentNullException(nameof(httpClientFactory));
}
public string Name => Utils.ProviderName;
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
return await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}
@@ -1,6 +1,5 @@
using System.Net.Http;
using Jellyfin.Plugin.Kinopoisk.Api;
using Jellyfin.Plugin.Kinopoisk.Api.Model;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
@@ -9,11 +8,11 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class MovieProvider : VideoBaseProvider<Movie, MovieInfo>
{
public MovieProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<MovieProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiProxy, logger, httpClientFactory)
public MovieProvider(IKinopoiskApiClient kinopoiskApiClient, ILogger<MovieProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiClient, logger, httpClientFactory)
{
}
protected override Movie ConvertResponseToItem(FilmDetails apiResponse)
protected override Movie ConvertResponseToItem(Film apiResponse)
=> apiResponse.ToMovie();
}
}
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk.Api;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
@@ -12,13 +13,13 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class PersonImageProvider : BaseImageProvider
{
private readonly KinopoiskApiProxy _kinopoiskApiProxy;
private readonly IKinopoiskApiClient _apiClient;
private readonly ILogger<PersonImageProvider> _logger;
public PersonImageProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<PersonImageProvider> logger, IHttpClientFactory httpClientFactory)
public PersonImageProvider(IKinopoiskApiClient kinopoiskApiClient, ILogger<PersonImageProvider> logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
_kinopoiskApiProxy = kinopoiskApiProxy ?? throw new System.ArgumentNullException(nameof(kinopoiskApiProxy));
_apiClient = kinopoiskApiClient ?? throw new System.ArgumentNullException(nameof(kinopoiskApiClient));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}
@@ -27,9 +28,15 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
public override bool Supports(BaseItem item)
=> item is Person;
public override Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
public override async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
if (!Utils.TryGetKinopoiskId(item, _logger, out var kinopoiskId))
return Enumerable.Empty<RemoteImageInfo>();
var person = await _apiClient.GetPerson(kinopoiskId, cancellationToken);
var res = new[] { person.ToRemoteImageInfo() };
return await FilterEmptyImages(res);
}
public override IEnumerable<ImageType> GetSupportedImages(BaseItem item)
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk;
using Jellyfin.Plugin.Kinopoisk.MetadataProviders;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace k.MetadataProviders
{
public class PersonProvider : BaseMetadataProvider, IRemoteMetadataProvider<Person, PersonLookupInfo>
{
private readonly IKinopoiskApiClient _apiClient;
private readonly ILogger<PersonProvider> _logger;
public PersonProvider(IKinopoiskApiClient apiClient, ILogger<PersonProvider> logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
_apiClient = apiClient ?? throw new System.ArgumentNullException(nameof(apiClient));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}
public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Person>()
{
QueriedById = true,
Provider = Utils.ProviderName,
ResultLanguage = Utils.ProviderMetadataLanguage
};
if (!Utils.TryGetKinopoiskId(info, _logger, out var kinopoiskId))
return result;
var person = await _apiClient.GetPerson(kinopoiskId, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
result.Item = person.ToPerson();
if (result.Item != null)
result.HasMetadata = true;
return result;
}
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationToken cancellationToken)
=> Task.FromResult(Enumerable.Empty<RemoteSearchResult>()); // Not supported
}
}
@@ -1,6 +1,5 @@
using System.Net.Http;
using Jellyfin.Plugin.Kinopoisk.Api;
using Jellyfin.Plugin.Kinopoisk.Api.Model;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
@@ -9,11 +8,11 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class SeriesProvider : VideoBaseProvider<Series, SeriesInfo>
{
public SeriesProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<SeriesProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiProxy, logger, httpClientFactory)
public SeriesProvider(IKinopoiskApiClient kinopoiskApiClient, ILogger<SeriesProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiClient, logger, httpClientFactory)
{
}
protected override Series ConvertResponseToItem(FilmDetails apiResponse)
protected override Series ConvertResponseToItem(Film apiResponse)
=> apiResponse.ToSeries();
}
}
@@ -3,9 +3,7 @@ using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk.Api;
using Jellyfin.Plugin.Kinopoisk.Api.Model;
using MediaBrowser.Common.Net;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Providers;
@@ -13,29 +11,21 @@ using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public abstract class VideoBaseProvider<TItemType, TLookupInfoType> : IRemoteMetadataProvider<TItemType, TLookupInfoType>
public abstract class VideoBaseProvider<TItemType, TLookupInfoType> : BaseMetadataProvider, IRemoteMetadataProvider<TItemType, TLookupInfoType>
where TItemType : BaseItem, IHasLookupInfo<TLookupInfoType>
where TLookupInfoType : ItemLookupInfo, new()
{
private readonly ILogger _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly KinopoiskApiProxy _apiProxy;
private readonly IKinopoiskApiClient _apiClient;
public string Name => Utils.ProviderName;
public VideoBaseProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger logger, IHttpClientFactory httpClientFactory)
public VideoBaseProvider(IKinopoiskApiClient kinopoiskApiClient, ILogger logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
this._logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
this._httpClientFactory = httpClientFactory ?? throw new System.ArgumentNullException(nameof(httpClientFactory));
this._apiProxy = kinopoiskApiProxy ?? throw new System.ArgumentNullException(nameof(kinopoiskApiProxy));
this._apiClient = kinopoiskApiClient ?? throw new System.ArgumentNullException(nameof(kinopoiskApiClient));
}
protected abstract TItemType ConvertResponseToItem(FilmDetails apiResponse);
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
return await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
protected abstract TItemType ConvertResponseToItem(Film apiResponse);
public async Task<MetadataResult<TItemType>> GetMetadata(TLookupInfoType info, CancellationToken cancellationToken)
{
@@ -49,19 +39,27 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
if (!Utils.TryGetKinopoiskId(info, _logger, out var kinopoiskId))
return result;
var film = await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken);
var film = await _apiClient.GetSingleFilm(kinopoiskId, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
result.Item = ConvertResponseToItem(film);
if (result.Item != null)
result.HasMetadata = true;
cancellationToken.ThrowIfCancellationRequested();
var staff = await _apiClient.GetStaff(kinopoiskId, cancellationToken);
var staff = await _apiProxy.GetStaff(kinopoiskId, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
foreach (var item in staff.ToPersonInfos())
result.AddPerson(item);
var trailers = await _apiClient.GetTrailers(kinopoiskId, cancellationToken);
var remoteTrailers = trailers.ToMediaUrls();
if (remoteTrailers is not null)
result.Item.RemoteTrailers = remoteTrailers;
return result;
}
@@ -69,12 +67,12 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
if (Utils.TryGetKinopoiskId(searchInfo, _logger, out var kinopoiskId))
{
var singleResult = (await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken)).ToRemoteSearchResult();
var singleResult = (await _apiClient.GetSingleFilm(kinopoiskId, cancellationToken)).ToRemoteSearchResult();
return Enumerable.Repeat(singleResult, 1);
}
else
{
return (await _apiProxy.SearchByKeyword(searchInfo.Name, cancellationToken: cancellationToken)).ToRemoteSearchResults();
return (await _apiClient.SearchByKeyword(searchInfo.Name, cancellationToken: cancellationToken)).ToRemoteSearchResults();
}
}
}
@@ -4,7 +4,7 @@ using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk.Api;
using KinopoiskUnofficialInfo.ApiClient;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
@@ -17,15 +17,15 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
public class VideoImageProvider : BaseImageProvider
{
private readonly ILogger<VideoImageProvider> _logger;
private readonly KinopoiskApiProxy _apiProxy;
private readonly IKinopoiskApiClient _apiClient;
public override string Name => Utils.ProviderName;
public VideoImageProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<VideoImageProvider> logger, IHttpClientFactory httpClientFactory)
public VideoImageProvider(IKinopoiskApiClient kinopoiskApiClient, ILogger<VideoImageProvider> logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
this._apiProxy = kinopoiskApiProxy ?? throw new ArgumentNullException(nameof(kinopoiskApiProxy));
this._apiClient = kinopoiskApiClient ?? throw new ArgumentNullException(nameof(kinopoiskApiClient));
}
public override bool Supports(BaseItem item)
@@ -43,40 +43,9 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
if (!Utils.TryGetKinopoiskId(item, _logger, out var kinopoiskId))
return Enumerable.Empty<RemoteImageInfo>();
var film = await _apiProxy.GetSingleFilmImages(kinopoiskId, cancellationToken);
var film = await _apiClient.GetSingleFilm(kinopoiskId, cancellationToken);
return await FilterEmptyImages(film.ToRemoteImageInfos());
}
private async Task<IEnumerable<RemoteImageInfo>> FilterEmptyImages(IEnumerable<RemoteImageInfo> images)
{
using var httpClient = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }, true);
var res = await Task.WhenAll(images.Select(i => NullIfEmptyImage(i, httpClient)));
return res.Where(i => i != null);
}
private async Task<RemoteImageInfo> NullIfEmptyImage(RemoteImageInfo info, HttpClient httpClient)
{
var currentUrl = info.Url;
while (!currentUrl.Contains("no-poster"))
{
var response = await httpClient.SendAsync(
new HttpRequestMessage(HttpMethod.Get, currentUrl), HttpCompletionOption.ResponseHeadersRead);
if ((int)response.StatusCode <= 299)
return info;
else if (response.Headers.Location != null)
{
currentUrl = response.Headers.Location.ToString();
continue;
}
else
throw new InvalidOperationException("Not expected answer");
}
return null;
}
}
}