mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-24 02:56:31 +00:00
fixed ratings. moved them to static text files
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
using MediaBrowser.Controller.Localization;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Localization;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
using MoreLinq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Localization
|
||||
{
|
||||
@@ -13,6 +18,37 @@ namespace MediaBrowser.Server.Implementations.Localization
|
||||
/// </summary>
|
||||
public class LocalizationManager : ILocalizationManager
|
||||
{
|
||||
/// <summary>
|
||||
/// The _configuration manager
|
||||
/// </summary>
|
||||
private readonly IServerConfigurationManager _configurationManager;
|
||||
|
||||
/// <summary>
|
||||
/// The us culture
|
||||
/// </summary>
|
||||
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalizationManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="configurationManager">The configuration manager.</param>
|
||||
public LocalizationManager(IServerConfigurationManager configurationManager)
|
||||
{
|
||||
_configurationManager = configurationManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the localization path.
|
||||
/// </summary>
|
||||
/// <value>The localization path.</value>
|
||||
public string LocalizationPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return Path.Combine(_configurationManager.ApplicationPaths.ProgramDataPath, "localization");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cultures.
|
||||
/// </summary>
|
||||
@@ -56,10 +92,125 @@ namespace MediaBrowser.Server.Implementations.Localization
|
||||
/// <returns>IEnumerable{ParentalRating}.</returns>
|
||||
public IEnumerable<ParentalRating> GetParentalRatings()
|
||||
{
|
||||
return Ratings.RatingsDict
|
||||
.Select(k => new ParentalRating {Name = k.Key, Value = k.Value})
|
||||
.OrderBy(p => p.Value)
|
||||
.Where(p => p.Value > 0);
|
||||
var path = GetRatingsFile();
|
||||
|
||||
return File.ReadAllLines(path).Select(i =>
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(i))
|
||||
{
|
||||
var parts = i.Split(',');
|
||||
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
int value;
|
||||
|
||||
if (int.TryParse(parts[1], NumberStyles.Integer, UsCulture, out value))
|
||||
{
|
||||
return new ParentalRating { Name = parts[0], Value = value };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
})
|
||||
.Where(i => i != null)
|
||||
.OrderBy(p => p.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ratings file.
|
||||
/// </summary>
|
||||
/// <returns>System.String.</returns>
|
||||
private string GetRatingsFile()
|
||||
{
|
||||
var countryCode = _configurationManager.Configuration.MetadataCountryCode;
|
||||
|
||||
if (string.IsNullOrEmpty(countryCode))
|
||||
{
|
||||
countryCode = "us";
|
||||
}
|
||||
|
||||
return GetRatingsFile(countryCode).Result ?? GetRatingsFile("us").Result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ratings file.
|
||||
/// </summary>
|
||||
/// <param name="countryCode">The country code.</param>
|
||||
/// <returns>Task{System.String}.</returns>
|
||||
private async Task<string> GetRatingsFile(string countryCode)
|
||||
{
|
||||
countryCode = countryCode.ToLower();
|
||||
|
||||
var path = Path.Combine(LocalizationPath, "ratings-" + countryCode + ".txt");
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
// Extract embedded resource
|
||||
|
||||
var type = GetType();
|
||||
var resourcePath = type.Namespace + ".Ratings." + countryCode + ".txt";
|
||||
|
||||
using (var stream = type.Assembly.GetManifestResourceStream(resourcePath))
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var parentPath = Path.GetDirectoryName(path);
|
||||
|
||||
if (!Directory.Exists(parentPath))
|
||||
{
|
||||
Directory.CreateDirectory(parentPath);
|
||||
}
|
||||
|
||||
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
|
||||
{
|
||||
await stream.CopyToAsync(fs).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rating level.
|
||||
/// </summary>
|
||||
/// <param name="rating">The rating.</param>
|
||||
/// <returns>System.Int32.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">rating</exception>
|
||||
public int? GetRatingLevel(string rating)
|
||||
{
|
||||
if (string.IsNullOrEmpty(rating))
|
||||
{
|
||||
throw new ArgumentNullException("rating");
|
||||
}
|
||||
|
||||
var ratingsDictionary = GetParentalRatings().ToDictionary(i => i.Name);
|
||||
|
||||
if (ratingsDictionary.ContainsKey(rating))
|
||||
return ratingsDictionary[rating].Value;
|
||||
|
||||
var stripped = StripCountry(rating);
|
||||
|
||||
if (ratingsDictionary.ContainsKey(stripped))
|
||||
return ratingsDictionary[stripped].Value;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strips the country.
|
||||
/// </summary>
|
||||
/// <param name="rating">The rating.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
private static string StripCountry(string rating)
|
||||
{
|
||||
int start = rating.IndexOf('-');
|
||||
return start > 0 ? rating.Substring(start + 1) : rating;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
AU-G,1
|
||||
AU-PG,5
|
||||
AU-M,6
|
||||
AU-M15+,8
|
||||
AU-R18+,9
|
||||
AU-X18+,10
|
||||
@@ -0,0 +1,7 @@
|
||||
GB-U,1
|
||||
GB-PG,5
|
||||
GB-12,6
|
||||
GB-12A,7
|
||||
GB-15,8
|
||||
GB-18,9
|
||||
GB-R18,15
|
||||
@@ -0,0 +1,6 @@
|
||||
NL-AL,1
|
||||
NL-MG6,2
|
||||
NL-6,3
|
||||
NL-9,5
|
||||
NL-12,6
|
||||
NL-16,8
|
||||
@@ -0,0 +1,22 @@
|
||||
G,1
|
||||
E,1
|
||||
EC,1
|
||||
TV-G,1
|
||||
TV-Y,2
|
||||
TV-Y7,3
|
||||
TV-Y7-FV,4
|
||||
PG,5
|
||||
TV-PG,5
|
||||
PG-13,7
|
||||
T,7
|
||||
TV-14,8
|
||||
R,9
|
||||
M,9
|
||||
TV-MA,9
|
||||
NC-17,10
|
||||
AO,15
|
||||
RP,15
|
||||
UR,15
|
||||
NR,15
|
||||
X,15
|
||||
XXX,100
|
||||
@@ -204,6 +204,10 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Localization\Ratings\us.txt" />
|
||||
<EmbeddedResource Include="Localization\Ratings\au.txt" />
|
||||
<EmbeddedResource Include="Localization\Ratings\gb.txt" />
|
||||
<EmbeddedResource Include="Localization\Ratings\nl.txt" />
|
||||
<Content Include="swagger-ui\css\hightlight.default.css">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
||||
Reference in New Issue
Block a user