feat: fetch cover, logo and stills from Kinopoisk
The plugin only returned the single poster (posterUrl). The API also exposes
coverUrl (wide), logoUrl and /films/{id}/images (stills). Map them to
Backdrop/Logo/Thumb and deduplicate URL availability checks.
This commit is contained in:
1 parent
6ab8025c05
commit
91555d3872
11 files changed
+192
-15
No files matched your search
@@ -142,29 +142,69 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
}
|
||||
|
||||
public static IEnumerable<RemoteImageInfo> ToRemoteImageInfos(this Film src)
|
||||
=> src.ToRemoteImageInfos(null);
|
||||
|
||||
/// <summary>
|
||||
/// Poster + cover + logo + stills from /films/{id}/images.
|
||||
/// Jellyfin needs more than one Primary/Backdrop to fill the hero and the
|
||||
/// "choose image" dialog, so expose every artwork the API returns.
|
||||
/// </summary>
|
||||
public static IEnumerable<RemoteImageInfo> ToRemoteImageInfos(this Film src, FilmImagesResponse images)
|
||||
{
|
||||
var res = Enumerable.Empty<RemoteImageInfo>();
|
||||
var res = new List<RemoteImageInfo>();
|
||||
if (src is null)
|
||||
return res;
|
||||
|
||||
if (src?.PosterUrl != null)
|
||||
if (!string.IsNullOrEmpty(src.PosterUrl))
|
||||
{
|
||||
var mainPoster = new RemoteImageInfo(){
|
||||
res.Add(new RemoteImageInfo(){
|
||||
Type = ImageType.Primary,
|
||||
Url = src.PosterUrl,
|
||||
Language = Constants.ProviderMetadataLanguage,
|
||||
ProviderName = Constants.ProviderName
|
||||
};
|
||||
res = res.Concat(Enumerable.Repeat(mainPoster, 1));
|
||||
});
|
||||
}
|
||||
|
||||
// if (src.Images != null)
|
||||
// {
|
||||
// if (src.Images.Posters != null)
|
||||
// res = res.Concat(src.Images.Posters.ToRemoteImageInfos(ImageType.Primary));
|
||||
// if (src.Images.Backdrops != null)
|
||||
// res = res.Concat(src.Images.Backdrops.ToRemoteImageInfos(ImageType.Backdrop));
|
||||
// }
|
||||
if (!string.IsNullOrEmpty(src.CoverUrl))
|
||||
{
|
||||
res.Add(new RemoteImageInfo(){
|
||||
Type = ImageType.Backdrop,
|
||||
Url = src.CoverUrl,
|
||||
Language = Constants.ProviderMetadataLanguage,
|
||||
ProviderName = Constants.ProviderName
|
||||
});
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(src.LogoUrl))
|
||||
{
|
||||
res.Add(new RemoteImageInfo(){
|
||||
Type = ImageType.Logo,
|
||||
Url = src.LogoUrl,
|
||||
Language = Constants.ProviderMetadataLanguage,
|
||||
ProviderName = Constants.ProviderName
|
||||
});
|
||||
}
|
||||
|
||||
// Stills are 16:9 frames: useful both as backdrops and landscape thumbs.
|
||||
// Cap the amount to keep remote-image lookups reasonably fast.
|
||||
const int maxStills = 12;
|
||||
var stills = images?.Items?
|
||||
.Where(i => !string.IsNullOrEmpty(i.ImageUrl))
|
||||
.Take(maxStills)
|
||||
?? Enumerable.Empty<FilmImage>();
|
||||
foreach (var still in stills)
|
||||
{
|
||||
foreach (var type in new[] { ImageType.Thumb, ImageType.Backdrop })
|
||||
{
|
||||
res.Add(new RemoteImageInfo(){
|
||||
Type = type,
|
||||
Url = still.ImageUrl,
|
||||
ThumbnailUrl = still.PreviewUrl,
|
||||
Language = Constants.ProviderMetadataLanguage,
|
||||
ProviderName = Constants.ProviderName
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -38,8 +38,13 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
|
||||
{
|
||||
using var httpClient = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }, true);
|
||||
var sanitizer = new RemoteImageUrlSanitizer(httpClient);
|
||||
var sanitized = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
var res = await Task.WhenAll(images.Select(async i => {
|
||||
var sanitizedUrl = await sanitizer.SanitizeRemoteImageUrl(i.Url);
|
||||
if (!sanitized.TryGetValue(i.Url, out var sanitizedUrl))
|
||||
{
|
||||
sanitizedUrl = await sanitizer.SanitizeRemoteImageUrl(i.Url);
|
||||
sanitized[i.Url] = sanitizedUrl;
|
||||
}
|
||||
if (string.IsNullOrEmpty(sanitizedUrl))
|
||||
return null;
|
||||
|
||||
|
||||
@@ -38,7 +38,9 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
|
||||
=> new ImageType[]
|
||||
{
|
||||
ImageType.Primary,
|
||||
ImageType.Backdrop
|
||||
ImageType.Backdrop,
|
||||
ImageType.Thumb,
|
||||
ImageType.Logo
|
||||
};
|
||||
|
||||
public override async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
||||
@@ -48,8 +50,15 @@ namespace Jellyfin.Plugin.Kinopoisk.MetadataProviders
|
||||
return Enumerable.Empty<RemoteImageInfo>();
|
||||
|
||||
var film = await _apiClient.GetSingleFilm(kinopoiskId, cancellationToken);
|
||||
var images = await _apiClient.GetFilmImages(kinopoiskId, cancellationToken);
|
||||
|
||||
return await FilterEmptyImages(film.ToRemoteImageInfos());
|
||||
var all = film.ToRemoteImageInfos(images).ToArray();
|
||||
var filtered = await FilterEmptyImages(all);
|
||||
_logger.LogInformation(
|
||||
"Kinopoisk returned {Count} images for {Name}",
|
||||
filtered.Count(),
|
||||
item.Name);
|
||||
return filtered;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,9 @@ namespace KinopoiskUnofficialInfo.ApiClient
|
||||
public Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null)
|
||||
=> TryGetValue(GenerateKey(nameof(GetSingleFilm), filmId), c => c.GetSingleFilm(filmId, cancellationToken));
|
||||
|
||||
public Task<FilmImagesResponse> GetFilmImages(int filmId, CancellationToken? cancellationToken = null)
|
||||
=> TryGetValue(GenerateKey(nameof(GetFilmImages), filmId), c => c.GetFilmImages(filmId, cancellationToken));
|
||||
|
||||
public Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null)
|
||||
=> TryGetValue(GenerateKey(nameof(GetStaff), filmId), c => c.GetStaff(filmId, cancellationToken));
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Extra image fields returned by /api/v2.2/films/{id} which are not part of
|
||||
/// the generated (older) client model.
|
||||
/// </summary>
|
||||
public partial class Film
|
||||
{
|
||||
[JsonProperty("coverUrl")]
|
||||
public string CoverUrl { get; set; }
|
||||
|
||||
[JsonProperty("logoUrl")]
|
||||
public string LogoUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Stills/backdrops returned by /api/v2.2/films/{id}/images.
|
||||
/// The endpoint does not report dimensions or the image kind, only URLs.
|
||||
/// </summary>
|
||||
public class FilmImagesResponse
|
||||
{
|
||||
[JsonProperty("total")]
|
||||
public int Total { get; set; }
|
||||
|
||||
[JsonProperty("totalPages")]
|
||||
public int TotalPages { get; set; }
|
||||
|
||||
[JsonProperty("items")]
|
||||
public List<FilmImage> Items { get; set; } = new List<FilmImage>();
|
||||
}
|
||||
|
||||
public class FilmImage
|
||||
{
|
||||
[JsonProperty("imageUrl")]
|
||||
public string ImageUrl { get; set; }
|
||||
|
||||
[JsonProperty("previewUrl")]
|
||||
public string PreviewUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
Task<PersonResponse> GetPerson(int personId, CancellationToken? cancellationToken = null);
|
||||
Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<FilmImagesResponse> GetFilmImages(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<VideoResponse> GetTrailers(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<FilmSearchResponse> SearchByKeyword(string keyword, int page = 1, CancellationToken? cancellationToken = null);
|
||||
|
||||
@@ -50,6 +50,40 @@ namespace KinopoiskUnofficialInfo.ApiClient
|
||||
public Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null)
|
||||
=> Invoke((ct) => _apiClient.FilmsAsync(filmId, ct), cancellationToken);
|
||||
|
||||
public async Task<FilmImagesResponse> GetFilmImages(int filmId, CancellationToken? cancellationToken = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var httpClient = _httpClientFactory.CreateClient();
|
||||
using var request = new HttpRequestMessage(
|
||||
HttpMethod.Get,
|
||||
$"https://kinopoiskapiunofficial.tech/api/v2.2/films/{filmId}/images");
|
||||
request.Headers.Add("X-API-KEY", _apiToken);
|
||||
using var response = await httpClient.SendAsync(
|
||||
request,
|
||||
HttpCompletionOption.ResponseContentRead,
|
||||
cancellationToken ?? CancellationToken.None);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Kinopoisk images request for film {FilmId} returned {StatusCode}",
|
||||
filmId,
|
||||
(int)response.StatusCode);
|
||||
return new FilmImagesResponse();
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<FilmImagesResponse>(json)
|
||||
?? new FilmImagesResponse();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Extra artwork must never break metadata updates.
|
||||
_logger.LogWarning(ex, "Failed to load images for film {FilmId}", filmId);
|
||||
return new FilmImagesResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null)
|
||||
=> Invoke((ct) => _apiClient.StaffAllAsync(filmId, ct), cancellationToken);
|
||||
|
||||
|
||||
Reference in new issue
Block a user