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

hqqresolver.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: UTF-8 -*-
  2. # * GNU General Public License for more details.
  3. # *
  4. # *
  5. # * You should have received a copy of the GNU General Public License
  6. # * along with this program; see the file COPYING. If not, write to
  7. # * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  8. # * http://www.gnu.org/copyleft/gpl.html
  9. # *
  10. # *
  11. # * based on https://gitorious.org/iptv-pl-dla-openpli/ urlresolver
  12. # */
  13. from StringIO import StringIO
  14. import json
  15. import re
  16. import util
  17. import base64
  18. import urllib
  19. __name__ = 'hqq'
  20. def supports(url):
  21. return _regex(url) is not None
  22. def _decode(data):
  23. def O1l(string):
  24. ret = ""
  25. i = len(string) - 1
  26. while i >= 0:
  27. ret += string[i]
  28. i -= 1
  29. return ret
  30. def l0I(string):
  31. enc = ""
  32. dec = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  33. i = 0
  34. while True:
  35. h1 = dec.find(string[i])
  36. i += 1
  37. h2 = dec.find(string[i])
  38. i += 1
  39. h3 = dec.find(string[i])
  40. i += 1
  41. h4 = dec.find(string[i])
  42. i += 1
  43. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4
  44. o1 = bits >> 16 & 0xff
  45. o2 = bits >> 8 & 0xff
  46. o3 = bits & 0xff
  47. if h3 == 64:
  48. enc += unichr(o1)
  49. else:
  50. if h4 == 64:
  51. enc += unichr(o1) + unichr(o2)
  52. else:
  53. enc += unichr(o1) + unichr(o2) + unichr(o3)
  54. if i >= len(string):
  55. break
  56. return enc
  57. escape = re.search("var _escape=\'([^\']+)", l0I(O1l(data))).group(1)
  58. return escape.replace('%', '\\').decode('unicode-escape')
  59. def _decode2(file_url):
  60. def K12K(a, typ='b'):
  61. codec_a = ["G", "L", "M", "N", "Z", "o", "I", "t", "V", "y", "x", "p", "R", "m", "z", "u",
  62. "D", "7", "W", "v", "Q", "n", "e", "0", "b", "="]
  63. codec_b = ["2", "6", "i", "k", "8", "X", "J", "B", "a", "s", "d", "H", "w", "f", "T", "3",
  64. "l", "c", "5", "Y", "g", "1", "4", "9", "U", "A"]
  65. if 'd' == typ:
  66. tmp = codec_a
  67. codec_a = codec_b
  68. codec_b = tmp
  69. idx = 0
  70. while idx < len(codec_a):
  71. a = a.replace(codec_a[idx], "___")
  72. a = a.replace(codec_b[idx], codec_a[idx])
  73. a = a.replace("___", codec_b[idx])
  74. idx += 1
  75. return a
  76. def _xc13(_arg1):
  77. _lg27 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  78. _local2 = ""
  79. _local3 = [0, 0, 0, 0]
  80. _local4 = [0, 0, 0]
  81. _local5 = 0
  82. while _local5 < len(_arg1):
  83. _local6 = 0
  84. while _local6 < 4 and (_local5 + _local6) < len(_arg1):
  85. _local3[_local6] = _lg27.find(_arg1[_local5 + _local6])
  86. _local6 += 1
  87. _local4[0] = ((_local3[0] << 2) + ((_local3[1] & 48) >> 4))
  88. _local4[1] = (((_local3[1] & 15) << 4) + ((_local3[2] & 60) >> 2))
  89. _local4[2] = (((_local3[2] & 3) << 6) + _local3[3])
  90. _local7 = 0
  91. while _local7 < len(_local4):
  92. if _local3[_local7 + 1] == 64:
  93. break
  94. _local2 += chr(_local4[_local7])
  95. _local7 += 1
  96. _local5 += 4
  97. return _local2
  98. return _xc13(K12K(file_url, 'e'))
  99. def resolve(url):
  100. m = _regex(url)
  101. if m:
  102. vid = m.group('vid')
  103. headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  104. 'Content-Type': 'text/html; charset=utf-8'}
  105. player_url = "http://hqq.tv/player/embed_player.php?vid=%s&autoplay=no" % vid
  106. data = util.request(player_url, headers)
  107. b64enc = re.search('base64([^\"]+)', data, re.DOTALL)
  108. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  109. enc = b64dec and re.search("\'([^']+)\'", b64dec).group(1)
  110. if enc:
  111. data = re.findall('<input name="([^"]+?)" [^>]+? value="([^"]+?)">', _decode(enc))
  112. post_data = {}
  113. for idx in range(len(data)):
  114. post_data[data[idx][0]] = data[idx][1]
  115. data = util.post(player_url, post_data, headers)
  116. b64enc = re.search('base64([^\"]+)', data, re.DOTALL)
  117. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  118. enc = b64dec and re.search("\'([^']+)\'", b64dec).group(1)
  119. if enc:
  120. data = re.findall('<input name="([^"]+?)" [^>]+? value="([^"]*)">', _decode(enc))
  121. post_data = {}
  122. for idx in range(len(data)):
  123. post_data[data[idx][0]] = data[idx][1]
  124. data = urllib.unquote(util.request("http://hqq.tv/sec/player/embed_player.php?" +
  125. urllib.urlencode(post_data), headers))
  126. server_1 = re.search("server_1: (\w+)",data).group(1)
  127. link_1 = re.search("link_1: (\w+)",data).group(1)
  128. vid_server = re.search(r'var\s*%s\s*=\s*"([^"]*?)"'%server_1, data)
  129. vid_link = re.search(r'var\s*%s\s*=\s*"([^"]*?)"'%link_1, data)
  130. at = re.search(r'var\s*at\s*=\s*"([^"]*?)"', data)
  131. vid = re.search('vid: "([^"]+)"',data)
  132. if vid_server and vid_link and at:
  133. get_data = {'server_1': vid_server.group(1),
  134. 'link_1': vid_link.group(1),
  135. 'at': at.group(1),
  136. 'adb': '0/',
  137. 'b':'1',
  138. 'vid': vid.group(1)}
  139. html = util.request("http://hqq.tv/player/get_md5.php?"+urllib.urlencode(get_data), headers)
  140. data = json.load(StringIO(html))
  141. if 'file' in data:
  142. file_url = _decode2(data['file'])
  143. file_url = re.sub(r'\?socket=?$', '.mp4.m3u8',file_url)
  144. return [{'url': file_url, 'quality': '360p',"headers":{"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.70 Mobile/13C71 Safari/601.1.46"}}]
  145. return None
  146. def _regex(url):
  147. match = re.search("(hqq|netu)\.tv/watch_video\.php\?v=(?P<vid>[0-9A-Z]+)", url)
  148. if match:
  149. return match
  150. match = re.search(r'(hqq|netu)\.tv/player/embed_player\.php\?vid=(?P<vid>[0-9A-Z]+)', url)
  151. if match:
  152. return match
  153. match = re.search(r'(hqq|netu)\.tv/player/hash\.php\?hash=\d+', url)
  154. if match:
  155. match = re.search(r'var\s+vid\s*=\s*\'(?P<vid>[^\']+)\'', urllib.unquote(util.request(url)))
  156. if match:
  157. return match
  158. b64enc = re.search(r'data:text/javascript\;charset\=utf\-8\;base64([^\"]+)', url)
  159. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  160. enc = b64dec and re.search(r"\'([^']+)\'", b64dec).group(1)
  161. if enc:
  162. decoded = _decode(enc)
  163. match = re.search(r'<input name="vid"[^>]+? value="(?P<vid>[^"]+?)">', decoded)
  164. if re.search(r'<form(.+?)action="[^"]*(hqq|netu)\.tv/player/embed_player\.php"[^>]*>',
  165. decoded) and match:
  166. return match
  167. return None