mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
239 lines
9.0 KiB
C#
239 lines
9.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs;
|
|
using API.DTOs.Recommendation;
|
|
using API.DTOs.Scrobbling;
|
|
using API.DTOs.SeriesDetail;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using API.Extensions;
|
|
using API.Helpers.Builders;
|
|
using Flurl.Http;
|
|
using Kavita.Common;
|
|
using Kavita.Common.EnvironmentInfo;
|
|
using Kavita.Common.Helpers;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Services.Plus;
|
|
#nullable enable
|
|
|
|
/// <summary>
|
|
/// Used for matching and fetching metadata on a series
|
|
/// </summary>
|
|
internal class ExternalMetadataIdsDto
|
|
{
|
|
public long? MalId { get; set; }
|
|
public int? AniListId { get; set; }
|
|
|
|
public string? SeriesName { get; set; }
|
|
public string? LocalizedSeriesName { get; set; }
|
|
public MediaFormat? PlusMediaFormat { get; set; } = MediaFormat.Unknown;
|
|
}
|
|
|
|
internal class SeriesDetailPlusAPIDto
|
|
{
|
|
public IEnumerable<MediaRecommendationDto> Recommendations { get; set; }
|
|
public IEnumerable<UserReviewDto> Reviews { get; set; }
|
|
public IEnumerable<RatingDto> Ratings { get; set; }
|
|
}
|
|
|
|
public interface IExternalMetadataService
|
|
{
|
|
Task<ExternalSeriesDetailDto?> GetExternalSeriesDetail(int? aniListId, long? malId, int? seriesId);
|
|
Task<SeriesDetailPlusDto?> GetSeriesDetail(int userId, int seriesId);
|
|
}
|
|
|
|
public class ExternalMetadataService : IExternalMetadataService
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ILogger<ExternalMetadataService> _logger;
|
|
|
|
public ExternalMetadataService(IUnitOfWork unitOfWork, ILogger<ExternalMetadataService> logger)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_logger = logger;
|
|
|
|
FlurlHttp.ConfigureClient(Configuration.KavitaPlusApiUrl, cli =>
|
|
cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves Metadata about a Recommended External Series
|
|
/// </summary>
|
|
/// <param name="aniListId"></param>
|
|
/// <param name="malId"></param>
|
|
/// <param name="seriesId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="KavitaException"></exception>
|
|
public async Task<ExternalSeriesDetailDto?> GetExternalSeriesDetail(int? aniListId, long? malId, int? seriesId)
|
|
{
|
|
if (!aniListId.HasValue && !malId.HasValue)
|
|
{
|
|
throw new KavitaException("Unable to find valid information from url for External Load");
|
|
}
|
|
|
|
var license = (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey)).Value;
|
|
return await GetSeriesDetail(license, aniListId, malId, seriesId);
|
|
|
|
}
|
|
|
|
public async Task<SeriesDetailPlusDto?> GetSeriesDetail(int userId, int seriesId)
|
|
{
|
|
var series =
|
|
await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(seriesId,
|
|
SeriesIncludes.Metadata | SeriesIncludes.Library | SeriesIncludes.Volumes | SeriesIncludes.Chapters);
|
|
if (series == null || series.Library.Type == LibraryType.Comic) return new SeriesDetailPlusDto();
|
|
var license = (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey)).Value;
|
|
|
|
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);
|
|
|
|
|
|
try
|
|
{
|
|
var result = await (Configuration.KavitaPlusApiUrl + "/api/metadata/series-detail")
|
|
.WithHeader("Accept", "application/json")
|
|
.WithHeader("User-Agent", "Kavita")
|
|
.WithHeader("x-license-key", license)
|
|
.WithHeader("x-installId", HashUtil.ServerToken())
|
|
.WithHeader("x-kavita-version", BuildInfo.Version)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithTimeout(TimeSpan.FromSeconds(Configuration.DefaultTimeOutSecs))
|
|
.PostJsonAsync(new PlusSeriesDtoBuilder(series).Build())
|
|
.ReceiveJson<SeriesDetailPlusAPIDto>();
|
|
|
|
|
|
var recs = await ProcessRecommendations(series, user!, result.Recommendations);
|
|
return new SeriesDetailPlusDto()
|
|
{
|
|
Recommendations = recs,
|
|
Ratings = result.Ratings,
|
|
Reviews = result.Reviews
|
|
};
|
|
}
|
|
catch (FlurlHttpException ex)
|
|
{
|
|
if (ex.StatusCode == 404)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An error happened during the request to Kavita+ API");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private async Task<RecommendationDto> ProcessRecommendations(Series series, AppUser user, IEnumerable<MediaRecommendationDto> recs)
|
|
{
|
|
var recDto = new RecommendationDto()
|
|
{
|
|
ExternalSeries = new List<ExternalSeriesDto>(),
|
|
OwnedSeries = new List<SeriesDto>()
|
|
};
|
|
|
|
var canSeeExternalSeries = user is {AgeRestriction: AgeRating.NotApplicable} &&
|
|
await _unitOfWork.UserRepository.IsUserAdminAsync(user);
|
|
foreach (var rec in recs)
|
|
{
|
|
// Find the series based on name and type and that the user has access too
|
|
var seriesForRec = await _unitOfWork.SeriesRepository.GetSeriesDtoByNamesAndMetadataIdsForUser(user.Id, rec.RecommendationNames,
|
|
series.Library.Type, ScrobblingService.CreateUrl(ScrobblingService.AniListWeblinkWebsite, rec.AniListId),
|
|
ScrobblingService.CreateUrl(ScrobblingService.MalWeblinkWebsite, rec.MalId));
|
|
|
|
if (seriesForRec != null)
|
|
{
|
|
recDto.OwnedSeries.Add(seriesForRec);
|
|
continue;
|
|
}
|
|
|
|
if (!canSeeExternalSeries) continue;
|
|
// We can show this based on user permissions
|
|
if (string.IsNullOrEmpty(rec.Name) || string.IsNullOrEmpty(rec.SiteUrl) || string.IsNullOrEmpty(rec.CoverUrl)) continue;
|
|
recDto.ExternalSeries.Add(new ExternalSeriesDto()
|
|
{
|
|
Name = string.IsNullOrEmpty(rec.Name) ? rec.RecommendationNames.First() : rec.Name,
|
|
Url = rec.SiteUrl,
|
|
CoverUrl = rec.CoverUrl,
|
|
Summary = rec.Summary,
|
|
AniListId = rec.AniListId,
|
|
MalId = rec.MalId
|
|
});
|
|
}
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(user.Id, recDto.OwnedSeries);
|
|
|
|
recDto.OwnedSeries = recDto.OwnedSeries.DistinctBy(s => s.Id).OrderBy(r => r.Name).ToList();
|
|
recDto.ExternalSeries = recDto.ExternalSeries.DistinctBy(s => s.Name.ToNormalized()).OrderBy(r => r.Name).ToList();
|
|
|
|
return recDto;
|
|
}
|
|
|
|
|
|
private async Task<ExternalSeriesDetailDto?> GetSeriesDetail(string license, int? aniListId, long? malId, int? seriesId)
|
|
{
|
|
var payload = new ExternalMetadataIdsDto()
|
|
{
|
|
AniListId = aniListId,
|
|
MalId = malId,
|
|
SeriesName = string.Empty,
|
|
LocalizedSeriesName = string.Empty
|
|
};
|
|
if (seriesId is > 0)
|
|
{
|
|
var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(seriesId.Value, SeriesIncludes.Metadata | SeriesIncludes.Library);
|
|
if (series != null)
|
|
{
|
|
if (payload.AniListId <= 0)
|
|
{
|
|
payload.AniListId = ScrobblingService.ExtractId<int>(series.Metadata.WebLinks, ScrobblingService.AniListWeblinkWebsite);
|
|
}
|
|
if (payload.MalId <= 0)
|
|
{
|
|
payload.MalId = ScrobblingService.ExtractId<long>(series.Metadata.WebLinks, ScrobblingService.MalWeblinkWebsite);
|
|
}
|
|
payload.SeriesName = series.Name;
|
|
payload.LocalizedSeriesName = series.LocalizedName;
|
|
payload.PlusMediaFormat = ConvertToMediaFormat(series.Library.Type, series.Format);
|
|
}
|
|
|
|
}
|
|
try
|
|
{
|
|
return await (Configuration.KavitaPlusApiUrl + "/api/metadata/series/detail")
|
|
.WithHeader("Accept", "application/json")
|
|
.WithHeader("User-Agent", "Kavita")
|
|
.WithHeader("x-license-key", license)
|
|
.WithHeader("x-installId", HashUtil.ServerToken())
|
|
.WithHeader("x-kavita-version", BuildInfo.Version)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithTimeout(TimeSpan.FromSeconds(Configuration.DefaultTimeOutSecs))
|
|
.PostJsonAsync(payload)
|
|
.ReceiveJson<ExternalSeriesDetailDto>();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "An error happened during the request to Kavita+ API");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static MediaFormat ConvertToMediaFormat(LibraryType libraryType, MangaFormat seriesFormat)
|
|
{
|
|
return libraryType switch
|
|
{
|
|
LibraryType.Manga => seriesFormat == MangaFormat.Epub ? MediaFormat.LightNovel : MediaFormat.Manga,
|
|
LibraryType.Comic => MediaFormat.Comic,
|
|
LibraryType.Book => MediaFormat.Book,
|
|
_ => MediaFormat.Unknown
|
|
};
|
|
}
|
|
}
|