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

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