Refactored tests

This commit is contained in:
PingWin committed 2021-05-22 16:02:01 +00:00
1 parent b7a0f2c0ee
commit 35ce5536be
38 files changed
+782 -1480

No files matched your search

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public abstract class BaseImageProvider : IRemoteImageProvider
{
private readonly IHttpClientFactory _httpClientFactory;
protected BaseImageProvider(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public abstract string Name { get; }
public abstract bool Supports(BaseItem item);
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
return await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
public abstract Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken);
public abstract IEnumerable<ImageType> GetSupportedImages(BaseItem item);
}
}
@@ -0,0 +1,19 @@
using System.Net.Http;
using Jellyfin.Plugin.Kinopoisk.Api;
using Jellyfin.Plugin.Kinopoisk.Api.Model;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class MovieProvider : VideoBaseProvider<Movie, MovieInfo>
{
public MovieProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<MovieProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiProxy, logger, httpClientFactory)
{
}
protected override Movie ConvertResponseToItem(FilmDetails apiResponse)
=> apiResponse.ToMovie();
}
}
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk.Api;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class PersonImageProvider : BaseImageProvider
{
private readonly KinopoiskApiProxy _kinopoiskApiProxy;
private readonly ILogger<PersonImageProvider> _logger;
public PersonImageProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<PersonImageProvider> logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
_kinopoiskApiProxy = kinopoiskApiProxy ?? throw new System.ArgumentNullException(nameof(kinopoiskApiProxy));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}
public override string Name => Utils.ProviderName;
public override bool Supports(BaseItem item)
=> item is Person;
public override Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}
public override IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
yield return ImageType.Primary;
}
}
}
@@ -0,0 +1,19 @@
using System.Net.Http;
using Jellyfin.Plugin.Kinopoisk.Api;
using Jellyfin.Plugin.Kinopoisk.Api.Model;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class SeriesProvider : VideoBaseProvider<Series, SeriesInfo>
{
public SeriesProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<SeriesProvider> logger, IHttpClientFactory httpClientFactory) : base(kinopoiskApiProxy, logger, httpClientFactory)
{
}
protected override Series ConvertResponseToItem(FilmDetails apiResponse)
=> apiResponse.ToSeries();
}
}
@@ -0,0 +1,81 @@
using System.Collections.Generic;
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 MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public abstract class VideoBaseProvider<TItemType, TLookupInfoType> : IRemoteMetadataProvider<TItemType, TLookupInfoType>
where TItemType : BaseItem, IHasLookupInfo<TLookupInfoType>
where TLookupInfoType : ItemLookupInfo, new()
{
private readonly ILogger _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly KinopoiskApiProxy _apiProxy;
public string Name => Utils.ProviderName;
public VideoBaseProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger logger, IHttpClientFactory 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));
}
protected abstract TItemType ConvertResponseToItem(FilmDetails apiResponse);
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
return await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
public async Task<MetadataResult<TItemType>> GetMetadata(TLookupInfoType info, CancellationToken cancellationToken)
{
var result = new MetadataResult<TItemType>()
{
QueriedById = true,
Provider = Utils.ProviderName,
ResultLanguage = Utils.ProviderMetadataLanguage
};
if (!Utils.TryGetKinopoiskId(info, _logger, out var kinopoiskId))
return result;
var film = await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken);
result.Item = ConvertResponseToItem(film);
if (result.Item != null)
result.HasMetadata = true;
cancellationToken.ThrowIfCancellationRequested();
var staff = await _apiProxy.GetStaff(kinopoiskId, cancellationToken);
foreach (var item in staff.ToPersonInfos())
result.AddPerson(item);
return result;
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(TLookupInfoType searchInfo, CancellationToken cancellationToken)
{
if (Utils.TryGetKinopoiskId(searchInfo, _logger, out var kinopoiskId))
{
var singleResult = (await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken)).ToRemoteSearchResult();
return Enumerable.Repeat(singleResult, 1);
}
else
{
return (await _apiProxy.SearchByKeyword(searchInfo.Name, cancellationToken: cancellationToken)).ToRemoteSearchResults();
}
}
}
}
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Kinopoisk.Api;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
{
public class VideoImageProvider : BaseImageProvider
{
private readonly ILogger<VideoImageProvider> _logger;
private readonly KinopoiskApiProxy _apiProxy;
public override string Name => Utils.ProviderName;
public VideoImageProvider(KinopoiskApiProxy kinopoiskApiProxy, ILogger<VideoImageProvider> logger, IHttpClientFactory httpClientFactory)
: base(httpClientFactory)
{
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
this._apiProxy = kinopoiskApiProxy ?? throw new ArgumentNullException(nameof(kinopoiskApiProxy));
}
public override bool Supports(BaseItem item)
=> item is Movie || item is Series;
public override IEnumerable<ImageType> GetSupportedImages(BaseItem item)
=> new ImageType[]
{
ImageType.Primary,
ImageType.Backdrop
};
public override async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
if (!Utils.TryGetKinopoiskId(item, _logger, out var kinopoiskId))
return Enumerable.Empty<RemoteImageInfo>();
var film = await _apiProxy.GetSingleFilmImages(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;
}
}
}