Skip to content

Commit

Permalink
fix: properly implement the respect preferred image _option_
Browse files Browse the repository at this point in the history
- Hook up the code to actually use the option to respect the preferred image, instead of just always doing it.
  • Loading branch information
revam committed Oct 9, 2024
1 parent dd9a2ce commit 123016f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Shokofin/Providers/ImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
var list = new List<RemoteImageInfo>();
var metadataLanguage = item.GetPreferredMetadataLanguage();
var baseKind = item.GetBaseItemKind();
var sortPreferred = Plugin.Instance.Configuration.RespectPreferredImage;
var trackerId = Plugin.Instance.Tracker.Add($"Providing images for {baseKind} \"{item.Name}\". (Path=\"{item.Path}\")");
try {
switch (item) {
case Episode episode: {
if (Lookup.TryGetEpisodeIdFor(episode, out var episodeId)) {
var episodeImages = await ApiClient.GetEpisodeImages(episodeId);
if (episodeImages is not null)
AddImagesForEpisode(ref list, episodeImages, metadataLanguage);
AddImagesForEpisode(ref list, episodeImages, metadataLanguage, sortPreferred);
Logger.LogInformation("Getting {Count} images for episode {EpisodeName} (Episode={EpisodeId},Language={MetadataLanguage})", list.Count, episode.Name, episodeId, metadataLanguage);
} break;
}
case Series series: {
if (Lookup.TryGetSeriesIdFor(series, out var seriesId)) {
var seriesImages = await ApiClient.GetSeriesImages(seriesId);
var sortPreferred = true;
if (seriesImages is not null) {
AddImagesForSeries(ref list, seriesImages, metadataLanguage, sortPreferred);
sortPreferred = false;
Expand Down Expand Up @@ -96,7 +96,6 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
if (Lookup.TryGetSeriesIdFor(season, out var seriesId)) {
var seasonInfo = await ApiManager.GetSeasonInfoForSeries(seriesId);
var seriesImages = await ApiClient.GetSeriesImages(seriesId);
var sortPreferred = true;
if (seriesImages is not null) {
AddImagesForSeries(ref list, seriesImages, metadataLanguage, sortPreferred);
sortPreferred = false;
Expand All @@ -121,7 +120,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
if (Lookup.TryGetEpisodeIdFor(movie, out var episodeId)) {
var episodeImages = await ApiClient.GetEpisodeImages(episodeId);
if (episodeImages is not null)
AddImagesForSeries(ref list, episodeImages, metadataLanguage);
AddImagesForSeries(ref list, episodeImages, metadataLanguage, sortPreferred);
Logger.LogInformation("Getting {Count} images for movie {MovieName} (Episode={EpisodeId},Language={MetadataLanguage})", list.Count, movie.Name, episodeId, metadataLanguage);
}
break;
Expand All @@ -134,7 +133,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
if (!string.IsNullOrEmpty(seriesId)) {
var seriesImages = await ApiClient.GetSeriesImages(seriesId);
if (seriesImages is not null)
AddImagesForSeries(ref list, seriesImages, metadataLanguage);
AddImagesForSeries(ref list, seriesImages, metadataLanguage, sortPreferred);
Logger.LogInformation("Getting {Count} images for collection {CollectionName} (Group={GroupId},Series={SeriesId},Language={MetadataLanguage})", list.Count, collection.Name, groupId, groupId == null ? seriesId : null, metadataLanguage);
}
break;
Expand All @@ -151,13 +150,16 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
}
}

public static void AddImagesForEpisode(ref List<RemoteImageInfo> list, API.Models.EpisodeImages images, string metadataLanguage)
public static void AddImagesForEpisode(ref List<RemoteImageInfo> list, API.Models.EpisodeImages images, string metadataLanguage, bool sortList)
{
foreach (var image in images.Thumbnails.OrderByDescending(image => image.IsPreferred))
IEnumerable<API.Models.Image> imagesList = sortList
? images.Thumbnails.OrderByDescending(image => image.IsPreferred)
: images.Thumbnails;
foreach (var image in imagesList)
AddImage(ref list, ImageType.Primary, image, metadataLanguage);
}

private static void AddImagesForSeries(ref List<RemoteImageInfo> list, API.Models.Images images, string metadataLanguage, bool sortList = true)
private static void AddImagesForSeries(ref List<RemoteImageInfo> list, API.Models.Images images, string metadataLanguage, bool sortList)
{
IEnumerable<API.Models.Image> imagesList = sortList
? images.Posters.OrderByDescending(image => image.IsPreferred)
Expand Down

0 comments on commit 123016f

Please sign in to comment.