Some premiere date improvements
This commit is contained in:
1 parent
fe545c7a14
commit
d08e0096db
19 files changed
+498
-341
No files matched your search
@@ -0,0 +1,7 @@
|
||||
namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
{
|
||||
public class CountryItem
|
||||
{
|
||||
public string Country { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
{
|
||||
@@ -37,29 +39,19 @@ namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
public string FilmLength { get; set; }
|
||||
public string Slogan { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Type { get; set; } // FILM, TV_SHOW
|
||||
public FilmType Type { get; set; }
|
||||
public string RatingMpaa { get; set; }
|
||||
public string RatingAgeLimits { get; set; }
|
||||
public string PremiereRu { get; set; }
|
||||
public int? RatingAgeLimits { get; set; }
|
||||
public DateTime? PremiereRu { get; set; }
|
||||
public string Distributors { get; set; }
|
||||
public string PremiereWorld { get; set; }
|
||||
public string PremiereDigital { get; set; }
|
||||
public DateTime? PremiereWorld { get; set; }
|
||||
public DateTime? PremiereDigital { get; set; }
|
||||
public string PremiereWorldCountry { get; set; }
|
||||
public string PremiereDvd { get; set; }
|
||||
public string PremiereBluRay { get; set; }
|
||||
public DateTime? PremiereDvd { get; set; }
|
||||
public DateTime? PremiereBluRay { get; set; }
|
||||
public string DistributorRelease { get; set; }
|
||||
public CountryItem[] Countries { get; set; }
|
||||
public GenreItem[] Genres { get; set; }
|
||||
|
||||
public class CountryItem
|
||||
{
|
||||
public string Country { get; set; }
|
||||
}
|
||||
|
||||
public class GenreItem
|
||||
{
|
||||
public string Genre { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class FilmExtId
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
{
|
||||
[DataContract]
|
||||
public enum FilmType
|
||||
{
|
||||
[EnumMember(Value = "UNKNOWN")]
|
||||
Unknown,
|
||||
|
||||
[EnumMember(Value = "FILM")]
|
||||
Film,
|
||||
|
||||
[EnumMember(Value = "TV_SHOW")]
|
||||
TvShow
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
{
|
||||
public class GenreItem
|
||||
{
|
||||
public string Genre { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,12 @@ namespace Jellyfin.Plugin.Kinopoisk.ApiModel
|
||||
public int FilmId { get; set; }
|
||||
public string NameRu { get; set; }
|
||||
public string NameEn { get; set; }
|
||||
public string Type { get; set; }
|
||||
public FilmType Type { get; set; }
|
||||
public string Year { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string FilmLength { get; set; }
|
||||
public CountryItem[] Countries { get; set; }
|
||||
public GenreItem[] Genres { get; set; }
|
||||
public string Rating { get; set; }
|
||||
public string RatingVoteCount { get; set; }
|
||||
public string PosterUrl { get; set; }
|
||||
|
||||
@@ -13,21 +13,68 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
{
|
||||
public static class ApiModelExtensions
|
||||
{
|
||||
public static RemoteSearchResult ToRemoteSearchResult(this FilmDetails src)
|
||||
{
|
||||
if (src?.Data is null)
|
||||
return null;
|
||||
|
||||
var res = new RemoteSearchResult() {
|
||||
Name = src.GetLocalName(),
|
||||
ImageUrl = src.Data.PosterUrl,
|
||||
PremiereDate = src.Data.GetPremiereDate(),
|
||||
Overview = src.Data.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
res.SetProviderId(Utils.ProviderId, Convert.ToString(src.Data.FilmId));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static IEnumerable<RemoteSearchResult> ToRemoteSearchResults(this SearchResult src)
|
||||
{
|
||||
if (src?.Films is null)
|
||||
return Enumerable.Empty<RemoteSearchResult>();
|
||||
|
||||
return src.Films.Select(s => s.ToRemoteSearchResult());
|
||||
}
|
||||
|
||||
public static RemoteSearchResult ToRemoteSearchResult(this SearchResult.Film src)
|
||||
{
|
||||
if (src is null)
|
||||
return null;
|
||||
|
||||
var res = new RemoteSearchResult() {
|
||||
Name = src.GetLocalName(),
|
||||
ImageUrl = src.PosterUrl,
|
||||
PremiereDate = src.GetPremiereDate(),
|
||||
Overview = src.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
res.SetProviderId(Utils.ProviderId, Convert.ToString(src.FilmId));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Series ToSeries(this FilmDetails src)
|
||||
{
|
||||
if (src.Data is null)
|
||||
if (src?.Data is null)
|
||||
return null;
|
||||
|
||||
var res = new Series();
|
||||
|
||||
FillCommonFilmInfo(src, res);
|
||||
|
||||
res.EndDate = src.Data.GetEndDate();
|
||||
res.Status = src.Data.IsContinuing()
|
||||
? SeriesStatus.Continuing
|
||||
: SeriesStatus.Ended;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Movie ToMovie(this FilmDetails src)
|
||||
{
|
||||
if (src.Data is null)
|
||||
if (src?.Data is null)
|
||||
return null;
|
||||
|
||||
var res = new Movie();
|
||||
@@ -40,19 +87,16 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
private static void FillCommonFilmInfo(FilmDetails src, BaseItem dst)
|
||||
{
|
||||
dst.SetProviderId(Utils.ProviderId, Convert.ToString(src.Data.FilmId));
|
||||
dst.Name = src.Data.NameRu;
|
||||
if (string.IsNullOrWhiteSpace(dst.Name))
|
||||
dst.Name = src.Data.NameEn;
|
||||
// if (!string.IsNullOrWhiteSpace(film.Data.WebUrl))
|
||||
// result.Item.HomePageUrl = film.Data.WebUrl;
|
||||
dst.ProductionYear = Utils.GetFirstYear(src.Data.Year);
|
||||
dst.Name = src.GetLocalName();
|
||||
dst.OriginalTitle = src.GetOriginalNameIfNotSame();
|
||||
dst.PremiereDate = src.Data.GetPremiereDate();
|
||||
dst.Overview = src.Data.Description;
|
||||
if (src.Data.Countries != null)
|
||||
dst.ProductionLocations = src.Data.Countries.Select(c => c.Country).ToArray();
|
||||
if (src.Data.Genres != null)
|
||||
foreach(var genre in src.Data.Genres.Select(c => c.Genre))
|
||||
dst.AddGenre(genre);
|
||||
if (!string.IsNullOrWhiteSpace(src.Data.RatingAgeLimits))
|
||||
if (src.Data.RatingAgeLimits.HasValue)
|
||||
dst.OfficialRating = $"{src.Data.RatingAgeLimits}+";
|
||||
else
|
||||
dst.OfficialRating = src.Data.RatingMpaa;
|
||||
@@ -190,5 +234,157 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTime? GetPremiereDate(this FilmData src)
|
||||
{
|
||||
var res = src.IsRussianSpokenOriginated()
|
||||
? src.PremiereRu
|
||||
: src.PremiereWorld;
|
||||
if (src.PremiereRu < res)
|
||||
res = src.PremiereRu;
|
||||
if (src.PremiereWorld < res)
|
||||
res = src.PremiereWorld;
|
||||
if (src.PremiereDigital < res)
|
||||
res = src.PremiereDigital;
|
||||
if (src.PremiereDvd < res)
|
||||
res = src.PremiereDvd;
|
||||
if (src.PremiereBluRay < res)
|
||||
res = src.PremiereBluRay;
|
||||
|
||||
if (res.HasValue)
|
||||
return res;
|
||||
|
||||
var firstYear = GetFirstYear(src.Year);
|
||||
if (firstYear != null)
|
||||
return new DateTime(firstYear.Value, 1, 1);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static DateTime? GetPremiereDate(this SearchResult.Film src)
|
||||
{
|
||||
var firstYear = GetFirstYear(src.Year);
|
||||
if (firstYear != null)
|
||||
return new DateTime(firstYear.Value, 1, 1);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static DateTime? GetEndDate(this FilmData src)
|
||||
{
|
||||
var lastYear = GetLastYear(src.Year);
|
||||
if (lastYear != null)
|
||||
return new DateTime(lastYear.Value, 12, 31);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsContinuing(this FilmData src)
|
||||
=> IsСontinuing(src?.Year);
|
||||
|
||||
public static string GetLocalName(this FilmDetails src)
|
||||
{
|
||||
var res = src?.Data?.NameRu;
|
||||
if (string.IsNullOrWhiteSpace(res))
|
||||
res = src?.Data?.NameEn;
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string GetLocalName(this SearchResult.Film src)
|
||||
{
|
||||
var res = src?.NameRu;
|
||||
if (string.IsNullOrWhiteSpace(res))
|
||||
res = src?.NameEn;
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string GetOriginalName(this FilmDetails src)
|
||||
=> src.IsRussianSpokenOriginated()
|
||||
? src?.Data?.NameRu
|
||||
: src?.Data?.NameEn;
|
||||
|
||||
public static string GetOriginalNameIfNotSame(this FilmDetails src)
|
||||
{
|
||||
var localName = src.GetLocalName();
|
||||
var originalName = src.GetOriginalName();
|
||||
if (!string.IsNullOrWhiteSpace(originalName) && !string.Equals(localName, originalName))
|
||||
return originalName;
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static bool IsRussianSpokenOriginated(this FilmDetails src)
|
||||
=> src?.Data?.IsRussianSpokenOriginated() ?? false;
|
||||
|
||||
public static bool IsRussianSpokenOriginated(this FilmData src)
|
||||
=> src?.Countries?.IsRussianSpokenOriginated() ?? false;
|
||||
|
||||
public static bool IsRussianSpokenOriginated(this IEnumerable<CountryItem> src)
|
||||
{
|
||||
if (src is null)
|
||||
return false;
|
||||
|
||||
foreach(var country in src)
|
||||
switch(country.Country)
|
||||
{
|
||||
case "Россия":
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int? GetFirstYear(string years)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(years))
|
||||
return null;
|
||||
|
||||
years = years.Trim();
|
||||
|
||||
if (int.TryParse(years, out var res))
|
||||
return res;
|
||||
|
||||
var i = 0;
|
||||
while (true) {
|
||||
if (i > 4)
|
||||
return null;
|
||||
if (!char.IsDigit(years[i]))
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
|
||||
return Convert.ToInt32(years.Substring(0, i));
|
||||
}
|
||||
|
||||
public static bool IsСontinuing(string years)
|
||||
=> years?.EndsWith("-...") ?? false;
|
||||
|
||||
public static int? GetLastYear(string years)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(years))
|
||||
return null;
|
||||
|
||||
years = years.Trim();
|
||||
|
||||
if (int.TryParse(years, out var res))
|
||||
return res;
|
||||
|
||||
var i = 0;
|
||||
Func<int> startindex = () => years.Length - 1 - i;
|
||||
while (true) {
|
||||
if (i > 4)
|
||||
return null;
|
||||
if (!char.IsDigit(years[startindex()]))
|
||||
{
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return i > 0
|
||||
? (int?)Convert.ToInt32(years.Substring(startindex()))
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.Kinopoisk.ApiModel;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
@@ -13,107 +14,16 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Kinopoisk
|
||||
{
|
||||
public class KinopoiskMovieProvider : IRemoteMetadataProvider<Movie, MovieInfo>
|
||||
public class KinopoiskMovieProvider : KinopoiskVideoBaseProvider<Movie, MovieInfo>
|
||||
{
|
||||
private readonly ILogger<KinopoiskMovieProvider> _logger;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly KinopoiskApiProxy _apiProxy;
|
||||
|
||||
public string Name => Utils.ProviderName;
|
||||
|
||||
public KinopoiskMovieProvider(ILogger<KinopoiskMovieProvider> logger,
|
||||
IHttpClient httpClient,
|
||||
IJsonSerializer jsonSerializer)
|
||||
: base(logger, httpClient, jsonSerializer)
|
||||
{
|
||||
if (logger is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
if (httpClient is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
|
||||
if (jsonSerializer is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(jsonSerializer));
|
||||
}
|
||||
|
||||
this._logger = logger;
|
||||
this._httpClient = httpClient;
|
||||
this._jsonSerializer = jsonSerializer;
|
||||
this._apiProxy = new KinopoiskApiProxy(logger, httpClient, jsonSerializer);
|
||||
}
|
||||
|
||||
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
return _httpClient.GetResponse(new HttpRequestOptions
|
||||
{
|
||||
CancellationToken = cancellationToken,
|
||||
Url = url
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new MetadataResult<Movie>()
|
||||
{
|
||||
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 = film.ToMovie();
|
||||
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(MovieInfo searchInfo, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Utils.TryGetKinopoiskId(searchInfo, _logger, out var kinopoiskId))
|
||||
{
|
||||
var film = await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken);
|
||||
var singleResult = new RemoteSearchResult() {
|
||||
Name = film.Data.NameRu ?? film.Data.NameEn,
|
||||
ImageUrl = film.Data.PosterUrl,
|
||||
ProductionYear = Utils.GetFirstYear(film.Data.Year),
|
||||
Overview = film.Data.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
singleResult.SetProviderId(Utils.ProviderId, Convert.ToString(film.Data.FilmId));
|
||||
return Enumerable.Repeat(singleResult, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
var searchResults = await _apiProxy.SearchByKeyword(searchInfo.Name, cancellationToken: cancellationToken);
|
||||
return searchResults.Films.Select(f => {
|
||||
var res = new RemoteSearchResult() {
|
||||
Name = f.NameRu ?? f.NameEn,
|
||||
ImageUrl = f.PosterUrl,
|
||||
ProductionYear = Utils.GetFirstYear(f.Year),
|
||||
Overview = f.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
res.SetProviderId(Utils.ProviderId, Convert.ToString(f.FilmId));
|
||||
return res;
|
||||
});
|
||||
}
|
||||
}
|
||||
protected override Movie ConvertResponseToItem(FilmDetails apiResponse)
|
||||
=> apiResponse.ToMovie();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.Kinopoisk.ApiModel;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities.TV;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
@@ -13,107 +14,17 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Kinopoisk
|
||||
{
|
||||
public class KinopoiskSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>
|
||||
public class KinopoiskSeriesProvider : KinopoiskVideoBaseProvider<Series, SeriesInfo>
|
||||
{
|
||||
private readonly ILogger<KinopoiskSeriesProvider> _logger;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly KinopoiskApiProxy _apiProxy;
|
||||
|
||||
public string Name => Utils.ProviderName;
|
||||
|
||||
public KinopoiskSeriesProvider(ILogger<KinopoiskSeriesProvider> logger,
|
||||
IHttpClient httpClient,
|
||||
IJsonSerializer jsonSerializer)
|
||||
: base(logger, httpClient, jsonSerializer)
|
||||
{
|
||||
if (logger is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
if (httpClient is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
|
||||
if (jsonSerializer is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(jsonSerializer));
|
||||
}
|
||||
|
||||
this._logger = logger;
|
||||
this._httpClient = httpClient;
|
||||
this._jsonSerializer = jsonSerializer;
|
||||
this._apiProxy = new KinopoiskApiProxy(logger, httpClient, jsonSerializer);
|
||||
}
|
||||
|
||||
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
return _httpClient.GetResponse(new HttpRequestOptions
|
||||
{
|
||||
CancellationToken = cancellationToken,
|
||||
Url = url
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new MetadataResult<Series>()
|
||||
{
|
||||
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 = film.ToSeries();
|
||||
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(SeriesInfo searchInfo, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Utils.TryGetKinopoiskId(searchInfo, _logger, out var kinopoiskId))
|
||||
{
|
||||
var film = await _apiProxy.GetSingleFilm(kinopoiskId, cancellationToken);
|
||||
var singleResult = new RemoteSearchResult() {
|
||||
Name = film.Data.NameRu ?? film.Data.NameEn,
|
||||
ImageUrl = film.Data.PosterUrl,
|
||||
ProductionYear = Utils.GetFirstYear(film.Data.Year),
|
||||
Overview = film.Data.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
singleResult.SetProviderId(Utils.ProviderId, Convert.ToString(film.Data.FilmId));
|
||||
return Enumerable.Repeat(singleResult, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
var searchResults = await _apiProxy.SearchByKeyword(searchInfo.Name, cancellationToken: cancellationToken);
|
||||
return searchResults.Films.Select(f => {
|
||||
var res = new RemoteSearchResult() {
|
||||
Name = f.NameRu ?? f.NameEn,
|
||||
ImageUrl = f.PosterUrl,
|
||||
ProductionYear = Utils.GetFirstYear(f.Year),
|
||||
Overview = f.Description,
|
||||
SearchProviderName = Utils.ProviderName
|
||||
};
|
||||
res.SetProviderId(Utils.ProviderId, Convert.ToString(f.FilmId));
|
||||
return res;
|
||||
});
|
||||
}
|
||||
}
|
||||
protected override Series ConvertResponseToItem(FilmDetails apiResponse)
|
||||
=> apiResponse.ToSeries();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.Kinopoisk.ApiModel;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Providers;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Kinopoisk
|
||||
{
|
||||
public abstract class KinopoiskVideoBaseProvider<TItemType, TLookupInfoType> : IRemoteMetadataProvider<TItemType, TLookupInfoType>
|
||||
where TItemType : BaseItem, IHasLookupInfo<TLookupInfoType>
|
||||
where TLookupInfoType : ItemLookupInfo, new()
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly KinopoiskApiProxy _apiProxy;
|
||||
|
||||
public string Name => Utils.ProviderName;
|
||||
|
||||
public KinopoiskVideoBaseProvider(ILogger logger,
|
||||
IHttpClient httpClient,
|
||||
IJsonSerializer jsonSerializer)
|
||||
{
|
||||
if (logger is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
if (httpClient is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
|
||||
if (jsonSerializer is null)
|
||||
{
|
||||
throw new System.ArgumentNullException(nameof(jsonSerializer));
|
||||
}
|
||||
|
||||
this._logger = logger;
|
||||
this._httpClient = httpClient;
|
||||
this._jsonSerializer = jsonSerializer;
|
||||
this._apiProxy = new KinopoiskApiProxy(logger, httpClient, jsonSerializer);
|
||||
}
|
||||
|
||||
protected abstract TItemType ConvertResponseToItem(FilmDetails apiResponse);
|
||||
|
||||
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
return _httpClient.GetResponse(new HttpRequestOptions
|
||||
{
|
||||
CancellationToken = cancellationToken,
|
||||
Url = url
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,26 +11,6 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
public const string ProviderDescription = "Загружает рейтинги сайта КиноПоиск";
|
||||
public const string ProviderMetadataLanguage = "ru";
|
||||
|
||||
public static int? GetFirstYear(string years)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(years))
|
||||
return null;
|
||||
|
||||
years = years.Trim();
|
||||
|
||||
if (int.TryParse(years, out var res))
|
||||
return res;
|
||||
|
||||
var i = 0;
|
||||
while (i < 4) {
|
||||
if (!char.IsDigit(years[i]))
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
|
||||
return Convert.ToInt32(years.Substring(0, i));
|
||||
}
|
||||
|
||||
public static bool TryGetKinopoiskId(IHasProviderIds item, ILogger logger, out int result)
|
||||
{
|
||||
var kinopoiskIdStr = item.GetProviderId(Utils.ProviderId);
|
||||
@@ -44,7 +24,7 @@ namespace Jellyfin.Plugin.Kinopoisk
|
||||
|
||||
if (!int.TryParse(kinopoiskIdStr, out result))
|
||||
{
|
||||
logger.LogDebug($"Can't parse ProviderId to int, skipping ({kinopoiskIdStr})");
|
||||
logger.LogWarning($"Can't parse Kinopoisk ProviderId to int, skipping ({kinopoiskIdStr})");
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user