Skip to content

Commit 025a42e

Browse files
committed
fix: can not get japanese logo. #87
1 parent b798b5f commit 025a42e

12 files changed

+166
-34
lines changed

Jellyfin.Plugin.MetaShark.Test/MovieProviderTest.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class MovieProviderTest
3030

3131

3232
[TestMethod]
33-
public void TestGetMetadata()
33+
public void TestSearch()
3434
{
3535
var httpClientFactory = new DefaultHttpClientFactory();
3636
var libraryManagerStub = new Mock<ILibraryManager>();
@@ -52,6 +52,29 @@ public void TestGetMetadata()
5252
}).GetAwaiter().GetResult();
5353
}
5454

55+
[TestMethod]
56+
public void TestGetMetadata()
57+
{
58+
var httpClientFactory = new DefaultHttpClientFactory();
59+
var libraryManagerStub = new Mock<ILibraryManager>();
60+
var httpContextAccessorStub = new Mock<IHttpContextAccessor>();
61+
var doubanApi = new DoubanApi(loggerFactory);
62+
var tmdbApi = new TmdbApi(loggerFactory);
63+
var omdbApi = new OmdbApi(loggerFactory);
64+
var imdbApi = new ImdbApi(loggerFactory);
65+
66+
Task.Run(async () =>
67+
{
68+
var info = new MovieInfo() { Name = "姥姥的外孙", MetadataLanguage = "zh" };
69+
var provider = new MovieProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, httpContextAccessorStub.Object, doubanApi, tmdbApi, omdbApi, imdbApi);
70+
var result = await provider.GetMetadata(info, CancellationToken.None);
71+
Assert.IsNotNull(result);
72+
73+
var str = result.ToJson();
74+
Console.WriteLine(result.ToJson());
75+
}).GetAwaiter().GetResult();
76+
}
77+
5578
[TestMethod]
5679
public void TestGetMetadataAnime()
5780
{

Jellyfin.Plugin.MetaShark/Configuration/PluginConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class PluginConfiguration : BasePluginConfiguration
2626
/// <summary>
2727
/// 豆瓣海报使用大图
2828
/// </summary>
29-
public bool EnableDoubanLargePoster { get; set; } = false;
29+
public bool EnableDoubanLargePoster { get; set; } = true;
3030
/// <summary>
3131
/// 豆瓣背景图使用原图
3232
/// </summary>

Jellyfin.Plugin.MetaShark/Model/DoubanSubject.cs

+25
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,32 @@ public string[] Genres
112112
}
113113

114114

115+
[JsonIgnore]
116+
public string PrimaryLanguageCode
117+
{
118+
get
119+
{
120+
var languageCodeMap = new Dictionary<string, string>() {
121+
{ "日语", "ja" },
122+
{ "法语", "fr" },
123+
{ "德语", "de" },
124+
{ "俄语", "ru" },
125+
{ "韩语", "ko" },
126+
{ "泰语", "th" },
127+
{ "泰米尔语", "ta" },
128+
};
129+
var primaryLanguage = this.Language.Split("/").Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).FirstOrDefault();
130+
if (!string.IsNullOrEmpty(primaryLanguage))
131+
{
132+
if (languageCodeMap.TryGetValue(primaryLanguage, out var lang))
133+
{
134+
return lang;
135+
}
136+
}
115137

138+
return string.Empty;
139+
}
140+
}
116141
}
117142

118143
public class DoubanCelebrity

Jellyfin.Plugin.MetaShark/Providers/BaseProvider.cs

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using Jellyfin.Plugin.MetaShark.Core;
2121
using Microsoft.AspNetCore.Http;
2222
using MediaBrowser.Controller.Entities.TV;
23+
using MediaBrowser.Model.Providers;
24+
using TMDbLib.Objects.Languages;
2325

2426
namespace Jellyfin.Plugin.MetaShark.Providers
2527
{
@@ -563,6 +565,24 @@ protected string AdjustImageLanguage(string imageLanguage, string requestLanguag
563565
return imageLanguage;
564566
}
565567

568+
// 把第一个备选图片语言设为空,提高图片优先级,保证备选语言图片优先级比英文高
569+
protected List<RemoteImageInfo> AdjustImageLanguagePriority(IList<RemoteImageInfo> images, string preferLanguage, string alternativeLanguage)
570+
{
571+
var imagesOrdered = images.OrderByLanguageDescending(preferLanguage, alternativeLanguage).ToList();
572+
573+
// 不存在默认语言图片,且备选语言是日语
574+
if (alternativeLanguage == "ja" && imagesOrdered.Where(x => x.Language == preferLanguage).Count() == 0)
575+
{
576+
var idx = imagesOrdered.FindIndex(x => x.Language == alternativeLanguage);
577+
if (idx >= 0)
578+
{
579+
imagesOrdered[idx].Language = null;
580+
}
581+
}
582+
583+
return imagesOrdered;
584+
}
585+
566586
/// <summary>
567587
/// Maps the TMDB provided roles for crew members to Jellyfin roles.
568588
/// </summary>

Jellyfin.Plugin.MetaShark/Providers/EpisodeImageProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
8282
CommunityRating = episodeResult.VoteAverage,
8383
VoteCount = episodeResult.VoteCount,
8484
ProviderName = Name,
85-
Type = ImageType.Primary
85+
Type = ImageType.Primary,
8686
});
8787
}
8888
return result;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using MediaBrowser.Model.Providers;
5+
6+
namespace Jellyfin.Plugin.MetaShark.Providers
7+
{
8+
public static class EnumerableExtensions
9+
{
10+
private const int MaxPriority = 99;
11+
12+
public static IEnumerable<RemoteImageInfo> OrderByLanguageDescending(this IEnumerable<RemoteImageInfo> remoteImageInfos, params string[] requestedLanguages)
13+
{
14+
if (requestedLanguages.Length <= 0)
15+
{
16+
requestedLanguages = new[] { "en" };
17+
}
18+
19+
var requestedLanguagePriorityMap = new Dictionary<string, int>();
20+
for (int i = 0; i < requestedLanguages.Length; i++)
21+
{
22+
if (string.IsNullOrEmpty(requestedLanguages[i]))
23+
{
24+
continue;
25+
}
26+
requestedLanguagePriorityMap.Add(NormalizeLanguage(requestedLanguages[i]), MaxPriority - i);
27+
}
28+
29+
return remoteImageInfos.OrderByDescending(delegate (RemoteImageInfo i)
30+
{
31+
if (string.IsNullOrEmpty(i.Language))
32+
{
33+
return 3;
34+
}
35+
36+
if (requestedLanguagePriorityMap.TryGetValue(NormalizeLanguage(i.Language), out int priority))
37+
{
38+
return priority;
39+
}
40+
41+
return string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase) ? 2 : 0;
42+
}).ThenByDescending((RemoteImageInfo i) => i.CommunityRating.GetValueOrDefault()).ThenByDescending((RemoteImageInfo i) => i.VoteCount.GetValueOrDefault());
43+
}
44+
45+
private static string NormalizeLanguage(string language)
46+
{
47+
if (string.IsNullOrEmpty(language))
48+
{
49+
return language;
50+
}
51+
52+
return language.Split('-')[0].ToLower();
53+
}
54+
}
55+
}

Jellyfin.Plugin.MetaShark/Providers/MovieImageProvider.cs

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using MediaBrowser.Controller.Providers;
88
using MediaBrowser.Model.Dto;
99
using MediaBrowser.Model.Entities;
10-
using MediaBrowser.Model.Extensions;
1110
using MediaBrowser.Model.Providers;
1211
using Microsoft.AspNetCore.Http;
1312
using Microsoft.Extensions.Logging;
@@ -53,16 +52,16 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
5352
{
5453
return Enumerable.Empty<RemoteImageInfo>();
5554
}
56-
var imageLanguages = this.GetImageLanguageParam(item.PreferredMetadataLanguage, primary.Language);
57-
var backdropImgs = await this.GetBackdrop(item, imageLanguages, cancellationToken).ConfigureAwait(false);
58-
var logoImgs = await this.GetLogos(item, imageLanguages, cancellationToken).ConfigureAwait(false);
55+
var backdropImgs = await this.GetBackdrop(item, primary.PrimaryLanguageCode, cancellationToken).ConfigureAwait(false);
56+
var logoImgs = await this.GetLogos(item, primary.PrimaryLanguageCode, cancellationToken).ConfigureAwait(false);
5957

6058
var res = new List<RemoteImageInfo> {
6159
new RemoteImageInfo
6260
{
6361
ProviderName = this.Name,
6462
Url = this.GetDoubanPoster(primary),
6563
Type = ImageType.Primary,
64+
Language = "zh",
6665
},
6766
};
6867
res.AddRange(backdropImgs);
@@ -133,7 +132,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
133132
/// Query for a background photo
134133
/// </summary>
135134
/// <param name="cancellationToken">Instance of the <see cref="CancellationToken"/> interface.</param>
136-
private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, string imageLanguages, CancellationToken cancellationToken)
135+
private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, string alternativeImageLanguage, CancellationToken cancellationToken)
137136
{
138137
var sid = item.GetProviderId(DoubanProviderId);
139138
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
@@ -157,6 +156,7 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, stri
157156
Height = x.Height,
158157
Width = x.Width,
159158
Type = ImageType.Backdrop,
159+
Language = "zh",
160160
};
161161
}
162162
else
@@ -166,46 +166,48 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, stri
166166
ProviderName = this.Name,
167167
Url = this.GetProxyImageUrl(x.Large),
168168
Type = ImageType.Backdrop,
169+
Language = "zh",
169170
};
170171
}
171172
}).ToList();
172173

173174
}
174175
}
175176

176-
// 背景图缺失,从TheMovieDb补充背景图
177-
if (list.Count == 0 && config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
177+
// 添加 TheMovieDb 背景图为备选
178+
if (config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
178179
{
179180
var language = item.GetPreferredMetadataLanguage();
180181
var movie = await this._tmdbApi
181-
.GetMovieAsync(tmdbId.ToInt(), language, imageLanguages, cancellationToken)
182+
.GetMovieAsync(tmdbId.ToInt(), language, language, cancellationToken)
182183
.ConfigureAwait(false);
183184

184185
if (movie != null && !string.IsNullOrEmpty(movie.BackdropPath))
185186
{
186-
this.Log("GetBackdrop from tmdb id: {0} lang: {1}", tmdbId, imageLanguages);
187+
this.Log("GetBackdrop from tmdb id: {0} lang: {1}", tmdbId, language);
187188
list.Add(new RemoteImageInfo
188189
{
189190
ProviderName = this.Name,
190191
Url = this._tmdbApi.GetBackdropUrl(movie.BackdropPath),
191192
Type = ImageType.Backdrop,
193+
Language = language,
192194
});
193195
}
194196
}
195197

196198
return list;
197199
}
198200

199-
private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, string imageLanguages, CancellationToken cancellationToken)
201+
private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, string alternativeImageLanguage, CancellationToken cancellationToken)
200202
{
201203
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
202204
var list = new List<RemoteImageInfo>();
203205
var language = item.GetPreferredMetadataLanguage();
204206
if (this.config.EnableTmdbLogo && !string.IsNullOrEmpty(tmdbId))
205207
{
206-
this.Log("GetLogos from tmdb id: {0} lang: {1}", tmdbId, imageLanguages);
208+
this.Log("GetLogos from tmdb id: {0}", tmdbId);
207209
var movie = await this._tmdbApi
208-
.GetMovieAsync(tmdbId.ToInt(), language, imageLanguages, cancellationToken)
210+
.GetMovieAsync(tmdbId.ToInt(), null, null, cancellationToken)
209211
.ConfigureAwait(false);
210212

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

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

230234
}

Jellyfin.Plugin.MetaShark/Providers/MovieProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo in
8585
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
8686
{
8787
var fileName = this.GetOriginalFileName(info);
88-
this.Log($"GetMovieMetadata of [name]: {info.Name} [fileName]: {fileName} EnableTmdb: {config.EnableTmdb}");
8988
var result = new MetadataResult<Movie>();
9089

9190
// 使用刷新元数据时,providerIds会保留旧有值,只有识别/新增才会没值
@@ -95,6 +94,7 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
9594
// 注意:会存在元数据有tmdbId,但metaSource没值的情况(之前由TMDB插件刮削导致)
9695
var hasTmdbMeta = metaSource == MetaSource.Tmdb && !string.IsNullOrEmpty(tmdbId);
9796
var hasDoubanMeta = metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid);
97+
this.Log($"GetMovieMetadata of [name]: {info.Name} [fileName]: {fileName} metaSource: {metaSource} EnableTmdb: {config.EnableTmdb}");
9898
if (!hasDoubanMeta && !hasTmdbMeta)
9999
{
100100
// 处理extras影片

Jellyfin.Plugin.MetaShark/Providers/PersonImageProvider.cs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
4949
ProviderName = this.Name,
5050
Url = this.GetProxyImageUrl(celebrity.Img),
5151
Type = ImageType.Primary,
52+
Language = "zh",
5253
});
5354
}
5455

@@ -68,6 +69,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
6869
Width = x.Width,
6970
Height = x.Height,
7071
Type = ImageType.Primary,
72+
Language = "zh",
7173
});
7274
});
7375
}

Jellyfin.Plugin.MetaShark/Providers/SeasonImageProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using MediaBrowser.Controller.Library;
66
using MediaBrowser.Controller.Providers;
77
using MediaBrowser.Model.Entities;
8-
using MediaBrowser.Model.Extensions;
98
using MediaBrowser.Model.Providers;
109
using Microsoft.AspNetCore.Http;
1110
using Microsoft.Extensions.Logging;
@@ -62,6 +61,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
6261
ProviderName = primary.Name,
6362
Url = this.GetDoubanPoster(primary),
6463
Type = ImageType.Primary,
64+
Language = "zh",
6565
},
6666
};
6767
return res;

0 commit comments

Comments
 (0)