Plex plugin to to play various online streams (mostly Latvian).

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is Mozilla Universal charset detector code.
  3. #
  4. # The Initial Developer of the Original Code is
  5. # Netscape Communications Corporation.
  6. # Portions created by the Initial Developer are Copyright (C) 2001
  7. # the Initial Developer. All Rights Reserved.
  8. #
  9. # Contributor(s):
  10. # Mark Pilgrim - port to Python
  11. # Shy Shalom - original C code
  12. #
  13. # This library is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU Lesser General Public
  15. # License as published by the Free Software Foundation; either
  16. # version 2.1 of the License, or (at your option) any later version.
  17. #
  18. # This library is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. # Lesser General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU Lesser General Public
  24. # License along with this library; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  26. # 02110-1301 USA
  27. ######################### END LICENSE BLOCK #########################
  28. from .charsetprober import CharSetProber
  29. from .constants import eNotMe
  30. from .compat import wrap_ord
  31. FREQ_CAT_NUM = 4
  32. UDF = 0 # undefined
  33. OTH = 1 # other
  34. ASC = 2 # ascii capital letter
  35. ASS = 3 # ascii small letter
  36. ACV = 4 # accent capital vowel
  37. ACO = 5 # accent capital other
  38. ASV = 6 # accent small vowel
  39. ASO = 7 # accent small other
  40. CLASS_NUM = 8 # total classes
  41. Latin1_CharToClass = (
  42. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 00 - 07
  43. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 08 - 0F
  44. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 10 - 17
  45. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 18 - 1F
  46. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 20 - 27
  47. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 28 - 2F
  48. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 30 - 37
  49. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 38 - 3F
  50. OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 40 - 47
  51. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 48 - 4F
  52. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 50 - 57
  53. ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, # 58 - 5F
  54. OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 60 - 67
  55. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 68 - 6F
  56. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 70 - 77
  57. ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, # 78 - 7F
  58. OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, # 80 - 87
  59. OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, # 88 - 8F
  60. UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 90 - 97
  61. OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, # 98 - 9F
  62. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A0 - A7
  63. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A8 - AF
  64. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B0 - B7
  65. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B8 - BF
  66. ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, # C0 - C7
  67. ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, # C8 - CF
  68. ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, # D0 - D7
  69. ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, # D8 - DF
  70. ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, # E0 - E7
  71. ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, # E8 - EF
  72. ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, # F0 - F7
  73. ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO, # F8 - FF
  74. )
  75. # 0 : illegal
  76. # 1 : very unlikely
  77. # 2 : normal
  78. # 3 : very likely
  79. Latin1ClassModel = (
  80. # UDF OTH ASC ASS ACV ACO ASV ASO
  81. 0, 0, 0, 0, 0, 0, 0, 0, # UDF
  82. 0, 3, 3, 3, 3, 3, 3, 3, # OTH
  83. 0, 3, 3, 3, 3, 3, 3, 3, # ASC
  84. 0, 3, 3, 3, 1, 1, 3, 3, # ASS
  85. 0, 3, 3, 3, 1, 2, 1, 2, # ACV
  86. 0, 3, 3, 3, 3, 3, 3, 3, # ACO
  87. 0, 3, 1, 3, 1, 1, 1, 3, # ASV
  88. 0, 3, 1, 3, 1, 1, 3, 3, # ASO
  89. )
  90. class Latin1Prober(CharSetProber):
  91. def __init__(self):
  92. CharSetProber.__init__(self)
  93. self.reset()
  94. def reset(self):
  95. self._mLastCharClass = OTH
  96. self._mFreqCounter = [0] * FREQ_CAT_NUM
  97. CharSetProber.reset(self)
  98. def get_charset_name(self):
  99. return "windows-1252"
  100. def feed(self, aBuf):
  101. aBuf = self.filter_with_english_letters(aBuf)
  102. for c in aBuf:
  103. charClass = Latin1_CharToClass[wrap_ord(c)]
  104. freq = Latin1ClassModel[(self._mLastCharClass * CLASS_NUM)
  105. + charClass]
  106. if freq == 0:
  107. self._mState = eNotMe
  108. break
  109. self._mFreqCounter[freq] += 1
  110. self._mLastCharClass = charClass
  111. return self.get_state()
  112. def get_confidence(self):
  113. if self.get_state() == eNotMe:
  114. return 0.01
  115. total = sum(self._mFreqCounter)
  116. if total < 0.01:
  117. confidence = 0.0
  118. else:
  119. confidence = ((self._mFreqCounter[3] - self._mFreqCounter[1] * 20.0)
  120. / total)
  121. if confidence < 0.0:
  122. confidence = 0.0
  123. # lower the confidence of latin1 so that other more accurate
  124. # detector can take priority.
  125. confidence = confidence * 0.73
  126. return confidence