RatingsDefinition.cs 3.7 KB

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