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

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