Skip to content

Commit

Permalink
fix: can not get japanese logo. #87
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Sep 4, 2024
1 parent b798b5f commit 025a42e
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 34 deletions.
25 changes: 24 additions & 1 deletion Jellyfin.Plugin.MetaShark.Test/MovieProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MovieProviderTest


[TestMethod]
public void TestGetMetadata()
public void TestSearch()
{
var httpClientFactory = new DefaultHttpClientFactory();
var libraryManagerStub = new Mock<ILibraryManager>();
Expand All @@ -52,6 +52,29 @@ public void TestGetMetadata()
}).GetAwaiter().GetResult();
}

[TestMethod]
public void TestGetMetadata()
{
var httpClientFactory = new DefaultHttpClientFactory();
var libraryManagerStub = new Mock<ILibraryManager>();
var httpContextAccessorStub = new Mock<IHttpContextAccessor>();
var doubanApi = new DoubanApi(loggerFactory);
var tmdbApi = new TmdbApi(loggerFactory);
var omdbApi = new OmdbApi(loggerFactory);
var imdbApi = new ImdbApi(loggerFactory);

Task.Run(async () =>
{
var info = new MovieInfo() { Name = "姥姥的外孙", MetadataLanguage = "zh" };
var provider = new MovieProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, httpContextAccessorStub.Object, doubanApi, tmdbApi, omdbApi, imdbApi);
var result = await provider.GetMetadata(info, CancellationToken.None);
Assert.IsNotNull(result);

var str = result.ToJson();
Console.WriteLine(result.ToJson());
}).GetAwaiter().GetResult();
}

[TestMethod]
public void TestGetMetadataAnime()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PluginConfiguration : BasePluginConfiguration
/// <summary>
/// 豆瓣海报使用大图
/// </summary>
public bool EnableDoubanLargePoster { get; set; } = false;
public bool EnableDoubanLargePoster { get; set; } = true;
/// <summary>
/// 豆瓣背景图使用原图
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,32 @@ public string[] Genres
}


[JsonIgnore]
public string PrimaryLanguageCode
{
get
{
var languageCodeMap = new Dictionary<string, string>() {
{ "日语", "ja" },
{ "法语", "fr" },
{ "德语", "de" },
{ "俄语", "ru" },
{ "韩语", "ko" },
{ "泰语", "th" },
{ "泰米尔语", "ta" },
};
var primaryLanguage = this.Language.Split("/").Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).FirstOrDefault();
if (!string.IsNullOrEmpty(primaryLanguage))
{
if (languageCodeMap.TryGetValue(primaryLanguage, out var lang))
{
return lang;
}
}

return string.Empty;
}
}
}

public class DoubanCelebrity
Expand Down
20 changes: 20 additions & 0 deletions Jellyfin.Plugin.MetaShark/Providers/BaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using Jellyfin.Plugin.MetaShark.Core;
using Microsoft.AspNetCore.Http;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Providers;
using TMDbLib.Objects.Languages;

namespace Jellyfin.Plugin.MetaShark.Providers
{
Expand Down Expand Up @@ -563,6 +565,24 @@ protected string AdjustImageLanguage(string imageLanguage, string requestLanguag
return imageLanguage;
}

// 把第一个备选图片语言设为空,提高图片优先级,保证备选语言图片优先级比英文高
protected List<RemoteImageInfo> AdjustImageLanguagePriority(IList<RemoteImageInfo> images, string preferLanguage, string alternativeLanguage)
{
var imagesOrdered = images.OrderByLanguageDescending(preferLanguage, alternativeLanguage).ToList();

// 不存在默认语言图片,且备选语言是日语
if (alternativeLanguage == "ja" && imagesOrdered.Where(x => x.Language == preferLanguage).Count() == 0)
{
var idx = imagesOrdered.FindIndex(x => x.Language == alternativeLanguage);
if (idx >= 0)
{
imagesOrdered[idx].Language = null;
}
}

return imagesOrdered;
}

/// <summary>
/// Maps the TMDB provided roles for crew members to Jellyfin roles.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
CommunityRating = episodeResult.VoteAverage,
VoteCount = episodeResult.VoteCount,
ProviderName = Name,
Type = ImageType.Primary
Type = ImageType.Primary,
});
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.Providers;

namespace Jellyfin.Plugin.MetaShark.Providers
{
public static class EnumerableExtensions
{
private const int MaxPriority = 99;

public static IEnumerable<RemoteImageInfo> OrderByLanguageDescending(this IEnumerable<RemoteImageInfo> remoteImageInfos, params string[] requestedLanguages)
{
if (requestedLanguages.Length <= 0)
{
requestedLanguages = new[] { "en" };
}

var requestedLanguagePriorityMap = new Dictionary<string, int>();
for (int i = 0; i < requestedLanguages.Length; i++)
{
if (string.IsNullOrEmpty(requestedLanguages[i]))
{
continue;
}
requestedLanguagePriorityMap.Add(NormalizeLanguage(requestedLanguages[i]), MaxPriority - i);
}

return remoteImageInfos.OrderByDescending(delegate (RemoteImageInfo i)
{
if (string.IsNullOrEmpty(i.Language))
{
return 3;
}

if (requestedLanguagePriorityMap.TryGetValue(NormalizeLanguage(i.Language), out int priority))
{
return priority;
}

return string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase) ? 2 : 0;
}).ThenByDescending((RemoteImageInfo i) => i.CommunityRating.GetValueOrDefault()).ThenByDescending((RemoteImageInfo i) => i.VoteCount.GetValueOrDefault());
}

private static string NormalizeLanguage(string language)
{
if (string.IsNullOrEmpty(language))
{
return language;
}

return language.Split('-')[0].ToLower();
}
}
}
30 changes: 17 additions & 13 deletions Jellyfin.Plugin.MetaShark/Providers/MovieImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Providers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -53,16 +52,16 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
{
return Enumerable.Empty<RemoteImageInfo>();
}
var imageLanguages = this.GetImageLanguageParam(item.PreferredMetadataLanguage, primary.Language);
var backdropImgs = await this.GetBackdrop(item, imageLanguages, cancellationToken).ConfigureAwait(false);
var logoImgs = await this.GetLogos(item, imageLanguages, cancellationToken).ConfigureAwait(false);
var backdropImgs = await this.GetBackdrop(item, primary.PrimaryLanguageCode, cancellationToken).ConfigureAwait(false);
var logoImgs = await this.GetLogos(item, primary.PrimaryLanguageCode, cancellationToken).ConfigureAwait(false);

var res = new List<RemoteImageInfo> {
new RemoteImageInfo
{
ProviderName = this.Name,
Url = this.GetDoubanPoster(primary),
Type = ImageType.Primary,
Language = "zh",
},
};
res.AddRange(backdropImgs);
Expand Down Expand Up @@ -133,7 +132,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
/// Query for a background photo
/// </summary>
/// <param name="cancellationToken">Instance of the <see cref="CancellationToken"/> interface.</param>
private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, string imageLanguages, CancellationToken cancellationToken)
private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, string alternativeImageLanguage, CancellationToken cancellationToken)
{
var sid = item.GetProviderId(DoubanProviderId);
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
Expand All @@ -157,6 +156,7 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, stri
Height = x.Height,
Width = x.Width,
Type = ImageType.Backdrop,
Language = "zh",
};
}
else
Expand All @@ -166,46 +166,48 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, stri
ProviderName = this.Name,
Url = this.GetProxyImageUrl(x.Large),
Type = ImageType.Backdrop,
Language = "zh",
};
}
}).ToList();

}
}

// 背景图缺失,从TheMovieDb补充背景图
if (list.Count == 0 && config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
// 添加 TheMovieDb 背景图为备选
if (config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
{
var language = item.GetPreferredMetadataLanguage();
var movie = await this._tmdbApi
.GetMovieAsync(tmdbId.ToInt(), language, imageLanguages, cancellationToken)
.GetMovieAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false);

if (movie != null && !string.IsNullOrEmpty(movie.BackdropPath))
{
this.Log("GetBackdrop from tmdb id: {0} lang: {1}", tmdbId, imageLanguages);
this.Log("GetBackdrop from tmdb id: {0} lang: {1}", tmdbId, language);
list.Add(new RemoteImageInfo
{
ProviderName = this.Name,
Url = this._tmdbApi.GetBackdropUrl(movie.BackdropPath),
Type = ImageType.Backdrop,
Language = language,
});
}
}

return list;
}

private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, string imageLanguages, CancellationToken cancellationToken)
private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, string alternativeImageLanguage, CancellationToken cancellationToken)
{
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
var list = new List<RemoteImageInfo>();
var language = item.GetPreferredMetadataLanguage();
if (this.config.EnableTmdbLogo && !string.IsNullOrEmpty(tmdbId))
{
this.Log("GetLogos from tmdb id: {0} lang: {1}", tmdbId, imageLanguages);
this.Log("GetLogos from tmdb id: {0}", tmdbId);
var movie = await this._tmdbApi
.GetMovieAsync(tmdbId.ToInt(), language, imageLanguages, cancellationToken)
.GetMovieAsync(tmdbId.ToInt(), null, null, cancellationToken)
.ConfigureAwait(false);

if (movie != null && movie.Images != null)
Expand All @@ -224,7 +226,9 @@ private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, string
}
}

return list.OrderByLanguageDescending(language);
// TODO:jellyfin 内部判断取哪个图片时,还会默认使用 OrderByLanguageDescending 排序一次,这里排序没用
// 默认图片优先级是:默认语言 > 无语言 > en > 其他语言
return this.AdjustImageLanguagePriority(list, language, alternativeImageLanguage);
}

}
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.MetaShark/Providers/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo in
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
{
var fileName = this.GetOriginalFileName(info);
this.Log($"GetMovieMetadata of [name]: {info.Name} [fileName]: {fileName} EnableTmdb: {config.EnableTmdb}");
var result = new MetadataResult<Movie>();

// 使用刷新元数据时,providerIds会保留旧有值,只有识别/新增才会没值
Expand All @@ -95,6 +94,7 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
// 注意:会存在元数据有tmdbId,但metaSource没值的情况(之前由TMDB插件刮削导致)
var hasTmdbMeta = metaSource == MetaSource.Tmdb && !string.IsNullOrEmpty(tmdbId);
var hasDoubanMeta = metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid);
this.Log($"GetMovieMetadata of [name]: {info.Name} [fileName]: {fileName} metaSource: {metaSource} EnableTmdb: {config.EnableTmdb}");
if (!hasDoubanMeta && !hasTmdbMeta)
{
// 处理extras影片
Expand Down
2 changes: 2 additions & 0 deletions Jellyfin.Plugin.MetaShark/Providers/PersonImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
ProviderName = this.Name,
Url = this.GetProxyImageUrl(celebrity.Img),
Type = ImageType.Primary,
Language = "zh",
});
}

Expand All @@ -68,6 +69,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
Width = x.Width,
Height = x.Height,
Type = ImageType.Primary,
Language = "zh",
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.MetaShark/Providers/SeasonImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Providers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -62,6 +61,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
ProviderName = primary.Name,
Url = this.GetDoubanPoster(primary),
Type = ImageType.Primary,
Language = "zh",
},
};
return res;
Expand Down
Loading

0 comments on commit 025a42e

Please sign in to comment.