Значительно улучшено авто-привязка фильмов при добавлении в медиатеку.

Пытаюсь найти идентификатор кинопоиска по паттерну kp-12345 или kp12345 в имени файла/папки.
  Пытаюсь найти единственное совпадение на кинопоиске по имени + году.
  Пытаюсь перебрать все совпадения по имени + сравнение IMDB ID.
This commit is contained in:
PingWin committed 2021-05-23 15:31:18 +00:00
1 parent b8b92cf60b
commit ea2ef35664
37 files changed
+723 -155

No files matched your search

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
namespace KinopoiskUnofficialInfo.ApiClient
{
public class CachedKinopoiskApiClient : IKinopoiskApiClient
{
private readonly IKinopoiskApiClient _innerClient;
private readonly IMemoryCache _cache;
private readonly ILogger<CachedKinopoiskApiClient> _logger;
public CachedKinopoiskApiClient(IKinopoiskApiClient innerClient, IMemoryCache cache, ILogger<CachedKinopoiskApiClient> logger)
{
_innerClient = innerClient ?? throw new ArgumentNullException(nameof(innerClient));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public CachedKinopoiskApiClient(string apiToken, ILogger<KinopoiskApiClient> innerLogger, IHttpClientFactory httpClientFactory, IMemoryCache cache, ILogger<CachedKinopoiskApiClient> logger)
: this(new KinopoiskApiClient(apiToken, innerLogger, httpClientFactory), cache, logger)
{
}
public Task<PersonResponse> GetPerson(int personId, CancellationToken? cancellationToken = null)
=> TryGetValue(GenerateKey(nameof(GetPerson), personId), c => c.GetPerson(personId, cancellationToken));
public Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null)
=> TryGetValue(GenerateKey(nameof(GetSingleFilm), filmId), c => c.GetSingleFilm(filmId, cancellationToken));
public Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null)
=> TryGetValue(GenerateKey(nameof(GetStaff), filmId), c => c.GetStaff(filmId, cancellationToken));
public Task<VideoResponse> GetTrailers(int filmId, CancellationToken? cancellationToken = null)
=> TryGetValue(GenerateKey(nameof(GetTrailers), filmId), c => c.GetTrailers(filmId, cancellationToken));
public Task<FilmSearchResponse> SearchByKeyword(string keyword, int page = 1, CancellationToken? cancellationToken = null)
=> TryGetValue(GenerateKey(nameof(SearchByKeyword), keyword, page), c => c.SearchByKeyword(keyword, page, cancellationToken));
private static string GenerateKey(params object[] objects)
{
var key = string.Empty;
foreach (var obj in objects)
{
var objType = obj.GetType();
if (objType.IsPrimitive || objType == typeof(string))
{
key += obj + ";";
}
else
{
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
var currentValue = propertyInfo.GetValue(obj, null);
if (currentValue == null)
{
continue;
}
key += propertyInfo.Name + "=" + currentValue + ";";
}
}
}
return key;
}
private Task<T> TryGetValue<T>(string key, Func<IKinopoiskApiClient, Task<T>> resultFactory)
{
return _cache.GetOrCreateAsync(key, async entry =>
{
_logger.LogDebug($"Entry '{key}' not found in cache, requesting from server");
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
var result = await resultFactory.Invoke(_innerClient).ConfigureAwait(false);
return result;
});
}
}
}
@@ -60,6 +60,17 @@ namespace KinopoiskUnofficialInfo.ApiClient
=> Invoke((ct) => _apiClient.StaffAsync(personId, ct), cancellationToken);
public Task<VideoResponse> GetTrailers(int filmId, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.VideosAsync(filmId, ct), cancellationToken);
{
return Invoke(async (ct) => {
try {
return await _apiClient.VideosAsync(filmId, ct);
} catch (ApiException e)
{
if (e.StatusCode == 404)
return new VideoResponse();
throw;
}
}, cancellationToken);
}
}
}
@@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
@@ -22,7 +23,7 @@ namespace KinopoiskUnofficialInfo.ApiClient
#pragma warning disable CS0618
property.MemberConverter = null;
#pragma warning restore CS0618
property.Converter = new JsonProfessionKeyConverter();
property.Converter = new ProfessionKeyConverter();
}
if (type == typeof(VideoResponse_trailers))
@@ -33,6 +34,17 @@ namespace KinopoiskUnofficialInfo.ApiClient
property.DefaultValue = -1;
}
}
if (type == typeof(Budget))
{
if (property.PropertyType == typeof(Int32))
{
#pragma warning disable CS0618
property.MemberConverter = null;
#pragma warning restore CS0618
property.Converter = new IntegerOverflowConverter();
}
}
}
return list;
}
@@ -0,0 +1,26 @@
using System;
using Newtonsoft.Json;
namespace KinopoiskUnofficialInfo.ApiClient
{
public class IntegerOverflowConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanConvert(System.Type objectType)
=> objectType == typeof(Int32);
public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
{
if (int.TryParse(Convert.ToString(reader.Value), out var result))
return result;
return -1;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
@@ -4,7 +4,7 @@ using Newtonsoft.Json.Converters;
namespace KinopoiskUnofficialInfo.ApiClient
{
public class JsonProfessionKeyConverter : StringEnumConverter
public class ProfessionKeyConverter : StringEnumConverter
{
public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
{