RatingsDefinition.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace MediaBrowser.Controller.Localization
  7. {
  8. /// <summary>
  9. /// Class RatingsDefinition
  10. /// </summary>
  11. public class RatingsDefinition
  12. {
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="RatingsDefinition" /> class.
  15. /// </summary>
  16. /// <param name="file">The file.</param>
  17. /// <param name="logger">The logger.</param>
  18. /// <param name="configurationManager">The configuration manager.</param>
  19. public RatingsDefinition(string file, IServerConfigurationManager configurationManager)
  20. {
  21. this.file = file;
  22. if (!Load())
  23. {
  24. Init(configurationManager.Configuration.MetadataCountryCode.ToUpper());
  25. }
  26. }
  27. /// <summary>
  28. /// Inits the specified country.
  29. /// </summary>
  30. /// <param name="country">The country.</param>
  31. protected void Init(string country)
  32. {
  33. //intitialze based on country
  34. switch (country)
  35. {
  36. case "US":
  37. RatingsDict = new USRatingsDictionary();
  38. break;
  39. case "GB":
  40. RatingsDict = new GBRatingsDictionary();
  41. break;
  42. case "NL":
  43. RatingsDict = new NLRatingsDictionary();
  44. break;
  45. case "AU":
  46. RatingsDict = new AURatingsDictionary();
  47. break;
  48. default:
  49. RatingsDict = new USRatingsDictionary();
  50. break;
  51. }
  52. Save();
  53. }
  54. /// <summary>
  55. /// The file
  56. /// </summary>
  57. readonly string file;
  58. /// <summary>
  59. /// Save to file
  60. /// </summary>
  61. public void Save()
  62. {
  63. // Use simple text serialization - no need for xml
  64. using (var fs = new StreamWriter(file))
  65. {
  66. foreach (var pair in RatingsDict)
  67. {
  68. fs.WriteLine(pair.Key + "," + pair.Value);
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Load from file
  74. /// </summary>
  75. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  76. protected bool Load()
  77. {
  78. // Read back in our simple serialized format
  79. RatingsDict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  80. try
  81. {
  82. using (var fs = new StreamReader(file))
  83. {
  84. while (!fs.EndOfStream)
  85. {
  86. var line = fs.ReadLine() ?? "";
  87. var values = line.Split(',');
  88. if (values.Length == 2)
  89. {
  90. int value;
  91. if (int.TryParse(values[1], out value))
  92. {
  93. RatingsDict[values[0].Trim()] = value;
  94. }
  95. else
  96. {
  97. //Logger.Error("Invalid line in ratings file " + file + "(" + line + ")");
  98. }
  99. }
  100. }
  101. }
  102. }
  103. catch
  104. {
  105. // Couldn't load - probably just not there yet
  106. return false;
  107. }
  108. return true;
  109. }
  110. /// <summary>
  111. /// The ratings dict
  112. /// </summary>
  113. public Dictionary<string, int> RatingsDict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  114. }
  115. }