From 097b0514c4161342e7bdd6e921a090d263246ff4 Mon Sep 17 00:00:00 2001 From: cxfksword <718792+cxfksword@users.noreply.github.com> Date: Fri, 19 May 2023 21:40:58 +0800 Subject: [PATCH] Optimize person identity --- .../PersonProviderTest.cs | 26 +++++- Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs | 13 ++- .../Model/DoubanSubject.cs | 1 + .../Providers/PersonProvider.cs | 89 +++++++++++-------- 4 files changed, 88 insertions(+), 41 deletions(-) diff --git a/Jellyfin.Plugin.MetaShark.Test/PersonProviderTest.cs b/Jellyfin.Plugin.MetaShark.Test/PersonProviderTest.cs index bfe9b91..c48054e 100644 --- a/Jellyfin.Plugin.MetaShark.Test/PersonProviderTest.cs +++ b/Jellyfin.Plugin.MetaShark.Test/PersonProviderTest.cs @@ -3,6 +3,7 @@ using Jellyfin.Plugin.MetaShark.Core; using Jellyfin.Plugin.MetaShark.Providers; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Moq; @@ -41,7 +42,30 @@ namespace Jellyfin.Plugin.MetaShark.Test Task.Run(async () => { - var info = new PersonLookupInfo() { Name = "柊瑠美", ProviderIds = new Dictionary() { { BaseProvider.DoubanProviderId, "1023337" } } }; + var info = new PersonLookupInfo() { ProviderIds = new Dictionary() { { BaseProvider.DoubanProviderId, "1016771" } } }; + var provider = new PersonProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, httpContextAccessorStub.Object, doubanApi, tmdbApi, omdbApi); + var result = await provider.GetMetadata(info, CancellationToken.None); + Assert.IsNotNull(result); + + var str = result.ToJson(); + Console.WriteLine(result.ToJson()); + }).GetAwaiter().GetResult(); + } + + [TestMethod] + public void TestGetMetadataByTmdb() + { + + var doubanApi = new DoubanApi(loggerFactory); + var tmdbApi = new TmdbApi(loggerFactory); + var omdbApi = new OmdbApi(loggerFactory); + var httpClientFactory = new DefaultHttpClientFactory(); + var libraryManagerStub = new Mock(); + var httpContextAccessorStub = new Mock(); + + Task.Run(async () => + { + var info = new PersonLookupInfo() { ProviderIds = new Dictionary() { { MetadataProvider.Tmdb.ToString(), "78871" } } }; var provider = new PersonProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, httpContextAccessorStub.Object, doubanApi, tmdbApi, omdbApi); var result = await provider.GetMetadata(info, CancellationToken.None); Assert.IsNotNull(result); diff --git a/Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs b/Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs index 23070d9..ef34897 100644 --- a/Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs +++ b/Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs @@ -71,7 +71,7 @@ namespace Jellyfin.Plugin.MetaShark.Api Regex regGender = new Regex(@"性别: \n(.+?)\n", RegexOptions.Compiled); Regex regConstellation = new Regex(@"星座: \n(.+?)\n", RegexOptions.Compiled); Regex regBirthdate = new Regex(@"出生日期: \n(.+?)\n", RegexOptions.Compiled); - Regex regLifedate = new Regex(@"生卒日期: \n(.+?) 至", RegexOptions.Compiled); + Regex regLifedate = new Regex(@"生卒日期: \n(.+?) 至 (.+)", RegexOptions.Compiled); Regex regBirthplace = new Regex(@"出生地: \n(.+?)\n", RegexOptions.Compiled); Regex regCelebrityRole = new Regex(@"职业: \n(.+?)\n", RegexOptions.Compiled); Regex regNickname = new Regex(@"更多外文名: \n(.+?)\n", RegexOptions.Compiled); @@ -523,10 +523,14 @@ namespace Jellyfin.Plugin.MetaShark.Api var gender = info.GetMatchGroup(this.regGender); var constellation = info.GetMatchGroup(this.regConstellation); var birthdate = info.GetMatchGroup(this.regBirthdate); - var lifedate = info.GetMatchGroup(this.regLifedate); - if (string.IsNullOrEmpty(birthdate)) + + // 生卒日期 + var enddate = string.Empty; + var match = this.regLifedate.Match(info); + if (match.Success && match.Groups.Count > 2) { - birthdate = lifedate; + birthdate = match.Groups[1].Value.Trim(); + enddate = match.Groups[2].Value.Trim(); } var birthplace = info.GetMatchGroup(this.regBirthplace); @@ -538,6 +542,7 @@ namespace Jellyfin.Plugin.MetaShark.Api celebrity.Img = img; celebrity.Gender = gender; celebrity.Birthdate = birthdate; + celebrity.Enddate = enddate; celebrity.Nickname = nickname; celebrity.Imdb = imdb; celebrity.Birthplace = birthplace; diff --git a/Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs b/Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs index b65a398..80d047b 100644 --- a/Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs +++ b/Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs @@ -120,6 +120,7 @@ namespace Jellyfin.Plugin.MetaShark.Model public string Gender { get; set; } public string Constellation { get; set; } public string Birthdate { get; set; } + public string Enddate { get; set; } public string Birthplace { get; set; } public string Nickname { get; set; } public string Imdb { get; set; } diff --git a/Jellyfin.Plugin.MetaShark/Providers/PersonProvider.cs b/Jellyfin.Plugin.MetaShark/Providers/PersonProvider.cs index 4345fe0..2618c7e 100644 --- a/Jellyfin.Plugin.MetaShark/Providers/PersonProvider.cs +++ b/Jellyfin.Plugin.MetaShark/Providers/PersonProvider.cs @@ -1,20 +1,20 @@ -using Jellyfin.Plugin.MetaShark.Api; -using Jellyfin.Plugin.MetaShark.Core; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Providers; -using Microsoft.Extensions.Logging; -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using TMDbLib.Objects.Find; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Jellyfin.Plugin.MetaShark.Api; +using Jellyfin.Plugin.MetaShark.Core; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Providers; +using TMDbLib.Objects.Find; namespace Jellyfin.Plugin.MetaShark.Providers { @@ -97,8 +97,16 @@ namespace Jellyfin.Plugin.MetaShark.Providers // Name = c.Name.Trim(), // 名称需保持和info.Name一致,不然会导致关联不到影片,自动被删除 HomePageUrl = c.Site, Overview = c.Intro, - PremiereDate = DateTime.ParseExact(c.Birthdate, "yyyy年MM月dd日", System.Globalization.CultureInfo.CurrentCulture) }; + if (DateTime.TryParseExact(c.Birthdate, "yyyy年MM月dd日", null, DateTimeStyles.None, out var premiereDate)) + { + item.PremiereDate = premiereDate; + item.ProductionYear = premiereDate.Year; + } + if (DateTime.TryParseExact(c.Enddate, "yyyy年MM月dd日", null, DateTimeStyles.None, out var endDate)) + { + item.EndDate = endDate; + } if (!string.IsNullOrWhiteSpace(c.Birthplace)) { item.ProductionLocations = new[] { c.Birthplace }; @@ -125,38 +133,47 @@ namespace Jellyfin.Plugin.MetaShark.Providers } } + // jellyfin强制最后一定使用默认的TheMovieDb插件获取一次,这里不太必要(除了使用自己的域名) var personTmdbId = info.GetProviderId(MetadataProvider.Tmdb); this.Log($"GetPersonMetadata of [personTmdbId]: {personTmdbId}"); if (!string.IsNullOrEmpty(personTmdbId)) { - var person = await this._tmdbApi.GetPersonAsync(personTmdbId.ToInt(), cancellationToken).ConfigureAwait(false); - if (person != null) + return await this.GetMetadataByTmdb(personTmdbId.ToInt(), info, cancellationToken).ConfigureAwait(false); + } + + return result; + } + + public async Task> GetMetadataByTmdb(int personTmdbId, PersonLookupInfo info, CancellationToken cancellationToken) + { + var result = new MetadataResult(); + var person = await this._tmdbApi.GetPersonAsync(personTmdbId, cancellationToken).ConfigureAwait(false); + if (person != null) + { + var item = new Person { - var item = new Person - { - // Name = info.Name.Trim(), // 名称需保持和info.Name一致,不然会导致关联不到影片,自动被删除 - HomePageUrl = person.Homepage, - Overview = person.Biography, - PremiereDate = person.Birthday?.ToUniversalTime(), - EndDate = person.Deathday?.ToUniversalTime() - }; + // Name = info.Name.Trim(), // 名称需保持和info.Name一致,不然会导致关联不到影片,自动被删除 + HomePageUrl = person.Homepage, + Overview = person.Biography, + PremiereDate = person.Birthday?.ToUniversalTime(), + EndDate = person.Deathday?.ToUniversalTime() + }; - if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth)) - { - item.ProductionLocations = new[] { person.PlaceOfBirth }; - } - - item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture)); - if (!string.IsNullOrEmpty(person.ImdbId)) - { - item.SetProviderId(MetadataProvider.Imdb, person.ImdbId); - } - - result.HasMetadata = true; - result.Item = item; - - return result; + if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth)) + { + item.ProductionLocations = new[] { person.PlaceOfBirth }; } + + item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture)); + if (!string.IsNullOrEmpty(person.ImdbId)) + { + item.SetProviderId(MetadataProvider.Imdb, person.ImdbId); + } + + result.HasMetadata = true; + result.Item = item; + + return result; } return result;