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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 False
  22. return _regex(url) is not None
  23. def _decode(data):
  24. def O1l(string):
  25. ret = ""
  26. i = len(string) - 1
  27. while i >= 0:
  28. ret += string[i]
  29. i -= 1
  30. return ret
  31. def l0I(string):
  32. enc = ""
  33. dec = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  34. i = 0
  35. while True:
  36. h1 = dec.find(string[i])
  37. i += 1
  38. h2 = dec.find(string[i])
  39. i += 1
  40. h3 = dec.find(string[i])
  41. i += 1
  42. h4 = dec.find(string[i])
  43. i += 1
  44. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4
  45. o1 = bits >> 16 & 0xff
  46. o2 = bits >> 8 & 0xff
  47. o3 = bits & 0xff
  48. if h3 == 64:
  49. enc += unichr(o1)
  50. else:
  51. if h4 == 64:
  52. enc += unichr(o1) + unichr(o2)
  53. else:
  54. enc += unichr(o1) + unichr(o2) + unichr(o3)
  55. if i >= len(string):
  56. break
  57. return enc
  58. escape = re.search("var _escape=\'([^\']+)", l0I(O1l(data))).group(1)
  59. return escape.replace('%', '\\').decode('unicode-escape')
  60. def _decode2(file_url):
  61. def K12K(a, typ='b'):
  62. codec_a = ["G", "L", "M", "N", "Z", "o", "I", "t", "V", "y", "x", "p", "R", "m", "z", "u",
  63. "D", "7", "W", "v", "Q", "n", "e", "0", "b", "="]
  64. codec_b = ["2", "6", "i", "k", "8", "X", "J", "B", "a", "s", "d", "H", "w", "f", "T", "3",
  65. "l", "c", "5", "Y", "g", "1", "4", "9", "U", "A"]
  66. if 'd' == typ:
  67. tmp = codec_a
  68. codec_a = codec_b
  69. codec_b = tmp
  70. idx = 0
  71. while idx < len(codec_a):
  72. a = a.replace(codec_a[idx], "___")
  73. a = a.replace(codec_b[idx], codec_a[idx])
  74. a = a.replace("___", codec_b[idx])
  75. idx += 1
  76. return a
  77. def _xc13(_arg1):
  78. _lg27 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  79. _local2 = ""
  80. _local3 = [0, 0, 0, 0]
  81. _local4 = [0, 0, 0]
  82. _local5 = 0
  83. while _local5 < len(_arg1):
  84. _local6 = 0
  85. while _local6 < 4 and (_local5 + _local6) < len(_arg1):
  86. _local3[_local6] = _lg27.find(_arg1[_local5 + _local6])
  87. _local6 += 1
  88. _local4[0] = ((_local3[0] << 2) + ((_local3[1] & 48) >> 4))
  89. _local4[1] = (((_local3[1] & 15) << 4) + ((_local3[2] & 60) >> 2))
  90. _local4[2] = (((_local3[2] & 3) << 6) + _local3[3])
  91. _local7 = 0
  92. while _local7 < len(_local4):
  93. if _local3[_local7 + 1] == 64:
  94. break
  95. _local2 += chr(_local4[_local7])
  96. _local7 += 1
  97. _local5 += 4
  98. return _local2
  99. return _xc13(K12K(file_url, 'e'))
  100. def resolve(url):
  101. m = _regex(url)
  102. if m:
  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. if "goo.gl" in url:
  106. data = util.request(url, headers)
  107. player_url = re.search("var ppage = '(.+?)'",data).group(1)
  108. player_url = urllib.unquote(player_url)
  109. else:
  110. vid = m.group('vid')
  111. player_url = "http://hqq.tv/player/embed_player.php?vid=%s&autoplay=no" % vid
  112. data = util.request(player_url, headers)
  113. b64enc = re.search('base64([^\"]+)', data, re.DOTALL)
  114. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  115. enc = b64dec and re.search("\'([^']+)\'", b64dec).group(1)
  116. if enc:
  117. data = re.findall('<input name="([^"]+?)" [^>]+? value="([^"]+?)">', _decode(enc))
  118. post_data = {}
  119. for idx in range(len(data)):
  120. post_data[data[idx][0]] = data[idx][1]
  121. data = util.post(player_url, post_data, headers)
  122. b64enc = re.search('base64([^\"]+)', data, re.DOTALL)
  123. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  124. enc = b64dec and re.search("\'([^']+)\'", b64dec).group(1)
  125. if enc:
  126. data = re.findall('<input name="([^"]+?)" [^>]+? value="([^"]*)">', _decode(enc))
  127. post_data = {}
  128. for idx in range(len(data)):
  129. post_data[data[idx][0]] = data[idx][1]
  130. data = urllib.unquote(util.request("http://hqq.tv/sec/player/embed_player.php?" +
  131. urllib.urlencode(post_data), headers))
  132. server_1 = re.search("server_1: (\w+)",data).group(1)
  133. link_1 = re.search("link_1: (\w+)",data).group(1)
  134. vid_server = re.search(r'var\s*%s\s*=\s*"([^"]*?)"'%server_1, data)
  135. vid_link = re.search(r'var\s*%s\s*=\s*"([^"]*?)"'%link_1, data)
  136. at = re.search(r'var\s*at\s*=\s*"([^"]*?)"', data)
  137. vid = re.search('vid: "([^"]+)"',data)
  138. subs = re.search('sub:"(.+?)"',data).group(1) if re.search('sub:"(.+?)"',data) else ""
  139. subs_lang = re.search('sublangs:"(.+?)"',data).group(1) if re.search('sub:"(.+?)"',data) else ""
  140. if vid_server and vid_link and at:
  141. get_data = {'server_1': vid_server.group(1),
  142. 'link_1': vid_link.group(1),
  143. 'at': at.group(1),
  144. 'adb': '0/',
  145. 'b':'1',
  146. 'vid': vid.group(1)}
  147. # X-Requested-With: XMLHttpRequest
  148. headers["X-Requested-With"] = "XMLHttpRequest"
  149. html = util.request("http://hqq.tv/player/get_md5.php?"+urllib.urlencode(get_data), headers)
  150. data = json.load(StringIO(html))
  151. if 'file' in data:
  152. file_url = _decode2(data['file'])
  153. file_url = re.sub(r'\?socket=?$', '.mp4.m3u8',file_url)
  154. stream = {
  155. 'url': file_url,
  156. 'subs':[{"url":subs,'name':subs_lang,"lang":subs_lang}],
  157. 'quality': '?',
  158. "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"}
  159. }
  160. return [stream]
  161. return []
  162. def _regex(url):
  163. # https://goo.gl/yMTzqf
  164. match = re.search("(hqq|netu)\.tv/watch_video\.php\?v=(?P<vid>[0-9A-Z]+)", url)
  165. if match:
  166. return match
  167. match = re.search(r'(hqq|netu)\.tv/player/embed_player\.php\?vid=(?P<vid>[0-9A-Z]+)', url)
  168. if match:
  169. return match
  170. match = re.search(r'(hqq|netu)\.tv/player/hash\.php\?hash=\d+', url)
  171. if match:
  172. match = re.search(r'var\s+vid\s*=\s*\'(?P<vid>[^\']+)\'', urllib.unquote(util.request(url)))
  173. if match:
  174. return match
  175. # https://goo.gl/yMTzqf
  176. match = re.search("(goo)\.gl/(?P<vid>[\w]+)", url)
  177. if match:
  178. return match
  179. b64enc = re.search(r'data:text/javascript\;charset\=utf\-8\;base64([^\"]+)', url)
  180. b64dec = b64enc and base64.decodestring(b64enc.group(1))
  181. enc = b64dec and re.search(r"\'([^']+)\'", b64dec).group(1)
  182. if enc:
  183. decoded = _decode(enc)
  184. match = re.search(r'<input name="vid"[^>]+? value="(?P<vid>[^"]+?)">', decoded)
  185. if re.search(r'<form(.+?)action="[^"]*(hqq|netu)\.tv/player/embed_player\.php"[^>]*>',
  186. decoded) and match:
  187. return match
  188. return None