Skip to content

Commit

Permalink
feat: add movie logo. close #63
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Feb 3, 2024
1 parent 9c04488 commit 513a668
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 73 deletions.
12 changes: 11 additions & 1 deletion Jellyfin.Plugin.MetaShark/Api/TmdbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TmdbApi(ILoggerFactory loggerFactory)
var config = Plugin.Instance?.Configuration;
var apiKey = string.IsNullOrEmpty(config?.TmdbApiKey) ? DEFAULT_API_KEY : config.TmdbApiKey;
var host = string.IsNullOrEmpty(config?.TmdbHost) ? DEFAULT_API_HOST : config.TmdbHost;
_tmDbClient = new TMDbClient(apiKey, true, host, null, config.GetTmdbWebProxy());
_tmDbClient = new TMDbClient(apiKey, true, host, null, config?.GetTmdbWebProxy());
_tmDbClient.Timeout = TimeSpan.FromSeconds(10);
// Not really interested in NotFoundException
_tmDbClient.ThrowApiExceptions = false;
Expand Down Expand Up @@ -597,6 +597,16 @@ public async Task<IReadOnlyList<SearchCollection>> SearchCollectionAsync(string
return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.StillSizes[^1], filePath).ToString();
}

public string? GetLogoUrl(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return null;
}

return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.LogoSizes[^1], filePath).ToString();
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.MetaShark/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ <h3>TheMovieDb</h3>
<label class="emby-checkbox-label" for="EnableTmdbBackdrop">
<input id="EnableTmdbBackdrop" name="EnableTmdbBackdrop" type="checkbox"
is="emby-checkbox" />
<span>使用TheMovieDb补全背景图</span>
<span>使用TheMovieDb补全背景图和商标</span>
</label>
<div class="fieldDescription">勾选后,当影片在豆瓣找不到背景图时,改使用TheMovieDb的补全</div>
<div class="fieldDescription">勾选后,当影片在豆瓣找不到背景图或商标时,改使用TheMovieDb的补全</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label" for="EnableTmdbCollection">
Expand Down
106 changes: 72 additions & 34 deletions Jellyfin.Plugin.MetaShark/Providers/MovieImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public MovieImageProvider(IHttpClientFactory httpClientFactory, ILoggerFactory l
public IEnumerable<ImageType> GetSupportedImages(BaseItem item) => new List<ImageType>
{
ImageType.Primary,
ImageType.Backdrop
ImageType.Backdrop,
ImageType.Logo,
};

/// <inheritdoc />
Expand All @@ -47,12 +48,13 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
this.Log($"GetImages for item: {item.Name} [metaSource]: {metaSource}");
if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid))
{
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken);
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken).ConfigureAwait(false);
if (primary == null || string.IsNullOrEmpty(primary.Img))
{
return Enumerable.Empty<RemoteImageInfo>();
}
var backdropImgs = await GetBackdrop(item, cancellationToken);
var backdropImgs = await this.GetBackdrop(item, cancellationToken).ConfigureAwait(false);
var logoImgs = await this.GetLogos(item, cancellationToken).ConfigureAwait(false);

var res = new List<RemoteImageInfo> {
new RemoteImageInfo
Expand All @@ -63,14 +65,16 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
},
};
res.AddRange(backdropImgs);
res.AddRange(logoImgs);
return res;
}

var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (metaSource == MetaSource.Tmdb && !string.IsNullOrEmpty(tmdbId))
{
var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi
// 设定language会导致图片被过滤,这里设为null,保持取全部语言图片
var movie = await this._tmdbApi
.GetMovieAsync(tmdbId.ToInt(), null, null, cancellationToken)
.ConfigureAwait(false);

Expand All @@ -81,37 +85,41 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell

var remoteImages = new List<RemoteImageInfo>();

for (var i = 0; i < movie.Images.Posters.Count; i++)
{
var poster = movie.Images.Posters[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbApi.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
remoteImages.AddRange(movie.Images.Posters.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetPosterUrl(x.FilePath),
Type = ImageType.Primary,
});
}

for (var i = 0; i < movie.Images.Backdrops.Count; i++)
{
var backdrop = movie.Images.Backdrops[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbApi.GetPosterUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

remoteImages.AddRange(movie.Images.Backdrops.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetBackdropUrl(x.FilePath),
Type = ImageType.Backdrop,
RatingType = RatingType.Score
});
}
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

remoteImages.AddRange(movie.Images.Logos.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetLogoUrl(x.FilePath),
Type = ImageType.Logo,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

return remoteImages.OrderByLanguageDescending(language);
}
Expand All @@ -133,7 +141,7 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, Canc
// 从豆瓣获取背景图
if (!string.IsNullOrEmpty(sid))
{
var photo = await this._doubanApi.GetWallpaperBySidAsync(sid, cancellationToken);
var photo = await this._doubanApi.GetWallpaperBySidAsync(sid, cancellationToken).ConfigureAwait(false);
if (photo != null && photo.Count > 0)
{
this.Log("GetBackdrop from douban sid: {0}", sid);
Expand Down Expand Up @@ -187,5 +195,35 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, Canc
return list;
}

private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, CancellationToken cancellationToken)
{
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
var list = new List<RemoteImageInfo>();
var language = item.GetPreferredMetadataLanguage();
if (this.config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
{
var movie = await this._tmdbApi
.GetMovieAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false);

if (movie != null && movie.Images != null)
{
list.AddRange(movie.Images.Logos.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetLogoUrl(x.FilePath),
Type = ImageType.Logo,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));
}
}

return list.OrderByLanguageDescending(language);
}

}
}
110 changes: 74 additions & 36 deletions Jellyfin.Plugin.MetaShark/Providers/SeriesImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public SeriesImageProvider(IHttpClientFactory httpClientFactory, ILoggerFactory
public IEnumerable<ImageType> GetSupportedImages(BaseItem item) => new List<ImageType>
{
ImageType.Primary,
ImageType.Backdrop
ImageType.Backdrop,
ImageType.Logo,
};

/// <inheritdoc />
Expand All @@ -47,12 +48,13 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
this.Log($"GetImages for item: {item.Name} [metaSource]: {metaSource}");
if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid))
{
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken);
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken).ConfigureAwait(false);
if (primary == null || string.IsNullOrEmpty(primary.Img))
{
return Enumerable.Empty<RemoteImageInfo>();
}
var dropback = await GetBackdrop(item, cancellationToken);
var backdropImgs = await this.GetBackdrop(item, cancellationToken).ConfigureAwait(false);
var logoImgs = await this.GetLogos(item, cancellationToken).ConfigureAwait(false);

var res = new List<RemoteImageInfo> {
new RemoteImageInfo
Expand All @@ -62,15 +64,17 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
Type = ImageType.Primary,
},
};
res.AddRange(dropback);
res.AddRange(backdropImgs);
res.AddRange(logoImgs);
return res;
}

var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (metaSource == MetaSource.Tmdb && !string.IsNullOrEmpty(tmdbId))
{
var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi
// 设定language会导致图片被过滤,这里设为null,保持取全部语言图片
var movie = await this._tmdbApi
.GetSeriesAsync(tmdbId.ToInt(), null, null, cancellationToken)

Check warning on line 78 in Jellyfin.Plugin.MetaShark/Providers/SeriesImageProvider.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 78 in Jellyfin.Plugin.MetaShark/Providers/SeriesImageProvider.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
.ConfigureAwait(false);

Expand All @@ -81,37 +85,41 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell

var remoteImages = new List<RemoteImageInfo>();

for (var i = 0; i < movie.Images.Posters.Count; i++)
{
var poster = movie.Images.Posters[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbApi.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
remoteImages.AddRange(movie.Images.Posters.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetPosterUrl(x.FilePath),
Type = ImageType.Primary,
});
}

for (var i = 0; i < movie.Images.Backdrops.Count; i++)
{
var backdrop = movie.Images.Backdrops[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbApi.GetPosterUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

remoteImages.AddRange(movie.Images.Backdrops.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetBackdropUrl(x.FilePath),
Type = ImageType.Backdrop,
RatingType = RatingType.Score
});
}
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

remoteImages.AddRange(movie.Images.Logos.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetLogoUrl(x.FilePath),
Type = ImageType.Logo,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));

return remoteImages.OrderByLanguageDescending(language);
}
Expand Down Expand Up @@ -177,8 +185,8 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, Canc
this.Log("GetBackdrop from tmdb id: {0}", tmdbId);
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = _tmdbApi.GetBackdropUrl(movie.BackdropPath),
ProviderName = this.Name,
Url = this._tmdbApi.GetBackdropUrl(movie.BackdropPath),
Type = ImageType.Backdrop,
});
}
Expand All @@ -187,5 +195,35 @@ private async Task<IEnumerable<RemoteImageInfo>> GetBackdrop(BaseItem item, Canc
return list;
}

private async Task<IEnumerable<RemoteImageInfo>> GetLogos(BaseItem item, CancellationToken cancellationToken)
{
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
var language = item.GetPreferredMetadataLanguage();
var list = new List<RemoteImageInfo>();
if (this.config.EnableTmdbBackdrop && !string.IsNullOrEmpty(tmdbId))
{
var movie = await this._tmdbApi
.GetSeriesAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false);

if (movie != null && movie.Images != null)
{
list.AddRange(movie.Images.Logos.Select(x => new RemoteImageInfo {
ProviderName = this.Name,
Url = this._tmdbApi.GetLogoUrl(x.FilePath),
Type = ImageType.Logo,
CommunityRating = x.VoteAverage,
VoteCount = x.VoteCount,
Width = x.Width,
Height = x.Height,
Language = this.AdjustImageLanguage(x.Iso_639_1, language),
RatingType = RatingType.Score,
}));
}
}

return list.OrderByLanguageDescending(language);
}

}
}

0 comments on commit 513a668

Please sign in to comment.