From d2933cab7355b9a77ec802e4bc1efae9a3bf9743 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 10 Jun 2013 13:46:11 -0400 Subject: fixed ratings. moved them to static text files --- .../Localization/LocalizationManager.cs | 161 ++++++++++++++++++++- 1 file changed, 156 insertions(+), 5 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs') diff --git a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs index 8705d912f..1965531af 100644 --- a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs +++ b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs @@ -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 /// public class LocalizationManager : ILocalizationManager { + /// + /// The _configuration manager + /// + private readonly IServerConfigurationManager _configurationManager; + + /// + /// The us culture + /// + private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + /// + /// Initializes a new instance of the class. + /// + /// The configuration manager. + public LocalizationManager(IServerConfigurationManager configurationManager) + { + _configurationManager = configurationManager; + } + + /// + /// Gets the localization path. + /// + /// The localization path. + public string LocalizationPath + { + get + { + return Path.Combine(_configurationManager.ApplicationPaths.ProgramDataPath, "localization"); + } + } + /// /// Gets the cultures. /// @@ -56,10 +92,125 @@ namespace MediaBrowser.Server.Implementations.Localization /// IEnumerable{ParentalRating}. public IEnumerable 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); + } + + /// + /// Gets the ratings file. + /// + /// System.String. + private string GetRatingsFile() + { + var countryCode = _configurationManager.Configuration.MetadataCountryCode; + + if (string.IsNullOrEmpty(countryCode)) + { + countryCode = "us"; + } + + return GetRatingsFile(countryCode).Result ?? GetRatingsFile("us").Result; + } + + /// + /// Gets the ratings file. + /// + /// The country code. + /// Task{System.String}. + private async Task 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; + } + + /// + /// Gets the rating level. + /// + /// The rating. + /// System.Int32. + /// rating + 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; + } + + /// + /// Strips the country. + /// + /// The rating. + /// System.String. + private static string StripCountry(string rating) + { + int start = rating.IndexOf('-'); + return start > 0 ? rating.Substring(start + 1) : rating; } } } -- cgit v1.2.3