RatingsDefinition.cs 3.9 KB

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