2020-09-24 17:49:35 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Extensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extension methods for <see cref="IEnumerable{T}"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class EnumerableExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Orders <see cref="RemoteImageInfo"/> by requested language in descending order, prioritizing "en" over other non-matches.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="remoteImageInfos">The remote image infos.</param>
|
|
|
|
|
/// <param name="requestedLanguage">The requested language for the images.</param>
|
|
|
|
|
/// <returns>The ordered remote image infos.</returns>
|
|
|
|
|
public static IEnumerable<RemoteImageInfo> OrderByLanguageDescending(this IEnumerable<RemoteImageInfo> remoteImageInfos, string requestedLanguage)
|
|
|
|
|
{
|
2021-09-08 21:20:11 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(requestedLanguage))
|
|
|
|
|
{
|
|
|
|
|
// Default to English if no requested language is specified.
|
|
|
|
|
requestedLanguage = "en";
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
|
return remoteImageInfos.OrderByDescending(i =>
|
|
|
|
|
{
|
2022-12-29 14:15:48 +00:00
|
|
|
|
// Image priority ordering:
|
|
|
|
|
// - Images that match the requested language
|
|
|
|
|
// - Images with no language
|
|
|
|
|
// - TODO: Images that match the original language
|
|
|
|
|
// - Images in English
|
|
|
|
|
// - Images that don't match the requested language
|
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
|
if (string.Equals(requestedLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2022-12-29 14:15:48 +00:00
|
|
|
|
return 4;
|
2020-09-24 17:49:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 21:20:11 +00:00
|
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
2020-09-24 17:49:35 +00:00
|
|
|
|
{
|
2022-12-29 14:15:48 +00:00
|
|
|
|
return 3;
|
2020-09-24 17:49:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 14:15:48 +00:00
|
|
|
|
if (string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase))
|
2020-09-24 17:49:35 +00:00
|
|
|
|
{
|
2021-09-08 21:20:11 +00:00
|
|
|
|
return 2;
|
2020-09-24 17:49:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
})
|
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
|
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|