fix rating values

This commit is contained in:
Luke Pulverenti 2013-06-18 16:54:32 -04:00
parent a039197082
commit 9fc6bc3385

View File

@ -37,6 +37,49 @@ namespace MediaBrowser.Server.Implementations.Localization
public LocalizationManager(IServerConfigurationManager configurationManager) public LocalizationManager(IServerConfigurationManager configurationManager)
{ {
_configurationManager = configurationManager; _configurationManager = configurationManager;
ExtractAll();
}
private void ExtractAll()
{
var type = GetType();
var resourcePath = type.Namespace + ".Ratings.";
var localizationPath = LocalizationPath;
var existingFiles = Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly)
.Select(Path.GetFileName)
.ToList();
if (!Directory.Exists(localizationPath))
{
Directory.CreateDirectory(localizationPath);
}
// Extract from the assembly
foreach (var resource in type.Assembly
.GetManifestResourceNames()
.Where(i => i.StartsWith(resourcePath)))
{
var filename = "ratings-" + resource.Substring(resourcePath.Length);
if (!existingFiles.Contains(filename))
{
using (var stream = type.Assembly.GetManifestResourceStream(resource))
{
using (var fs = new FileStream(Path.Combine(localizationPath, filename), FileMode.Create, FileAccess.Write, FileShare.Read))
{
stream.CopyTo(fs);
}
}
}
}
foreach (var file in Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly))
{
LoadRatings(file);
}
} }
/// <summary> /// <summary>
@ -128,15 +171,7 @@ namespace MediaBrowser.Server.Implementations.Localization
{ {
Dictionary<string, ParentalRating> value; Dictionary<string, ParentalRating> value;
if (!_allParentalRatings.TryGetValue(countryCode, out value)) _allParentalRatings.TryGetValue(countryCode, out value);
{
value = LoadRatings(countryCode);
if (value != null)
{
_allParentalRatings.TryAdd(countryCode, value);
}
}
return value; return value;
} }
@ -144,17 +179,11 @@ namespace MediaBrowser.Server.Implementations.Localization
/// <summary> /// <summary>
/// Loads the ratings. /// Loads the ratings.
/// </summary> /// </summary>
/// <param name="countryCode">The country code.</param> /// <param name="file">The file.</param>
private Dictionary<string, ParentalRating> LoadRatings(string countryCode) /// <returns>Dictionary{System.StringParentalRating}.</returns>
private void LoadRatings(string file)
{ {
var path = GetRatingsFilePath(countryCode); var dict = File.ReadAllLines(file).Select(i =>
if (string.IsNullOrEmpty(path))
{
return null;
}
return File.ReadAllLines(path).Select(i =>
{ {
if (!string.IsNullOrWhiteSpace(i)) if (!string.IsNullOrWhiteSpace(i))
{ {
@ -176,47 +205,10 @@ namespace MediaBrowser.Server.Implementations.Localization
}) })
.Where(i => i != null) .Where(i => i != null)
.ToDictionary(i => i.Name); .ToDictionary(i => i.Name);
}
/// <summary> var countryCode = Path.GetFileNameWithoutExtension(file).Split('-').Last();
/// Gets the ratings file.
/// </summary>
/// <param name="countryCode">The country code.</param>
private string GetRatingsFilePath(string countryCode)
{
countryCode = countryCode.ToLower();
var path = Path.Combine(LocalizationPath, "ratings-" + countryCode + ".txt"); _allParentalRatings.TryAdd(countryCode, dict);
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))
{
stream.CopyTo(fs);
}
}
}
return path;
} }
/// <summary> /// <summary>
@ -235,23 +227,17 @@ namespace MediaBrowser.Server.Implementations.Localization
if (!ratingsDictionary.TryGetValue(rating, out value)) if (!ratingsDictionary.TryGetValue(rating, out value))
{ {
var stripped = StripCountry(rating); // If we don't find anything check all ratings systems
foreach (var dictionary in _allParentalRatings.Values)
ratingsDictionary.TryGetValue(stripped, out value); {
if (dictionary.TryGetValue(rating, out value))
{
return value.Value;
}
}
} }
return value == null ? (int?)null : value.Value; return value == null ? (int?)null : value.Value;
} }
/// <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;
}
} }
} }