Python module (submodule repositary), which provides content (video streams) from various online stream sources to corresponding Enigma2, Kodi, Plex plugins

util.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. # -*- coding: UTF-8 -*-
  2. # /*
  3. # * Copyright (C) 2011 Libor Zoubek,ivars777
  4. # *
  5. # *
  6. # * This Program is free software; you can redistribute it and/or modify
  7. # * it under the terms of the GNU General Public License as published by
  8. # * the Free Software Foundation; either version 2, or (at your option)
  9. # * any later version.
  10. # *
  11. # * This Program is distributed in the hope that it will be useful,
  12. # * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # * GNU General Public License for more details.
  15. # *
  16. # * You should have received a copy of the GNU General Public License
  17. # * along with this program; see the file COPYING. If not, write to
  18. # * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. # * http://www.gnu.org/copyleft/gpl.html
  20. # *
  21. # */
  22. import os, sys, re
  23. import urllib, urllib2
  24. import datetime
  25. import traceback
  26. import cookielib
  27. import requests
  28. try:
  29. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  30. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  31. except:
  32. pass
  33. from htmlentitydefs import name2codepoint as n2cp
  34. import HTMLParser
  35. import StringIO
  36. #import threading
  37. #import Queue
  38. import pickle
  39. import string
  40. import simplejson as json
  41. #from demjson import demjson
  42. #import demjson
  43. import json
  44. #from bs4 import BeautifulSoup
  45. UA = 'Mozilla/6.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008092417 Firefox/3.0.3'
  46. LOG = 2
  47. _cookie_jar = None
  48. CACHE_COOKIES = 'cookies'
  49. def system():
  50. if "kodi" in sys.executable.lower():
  51. return "kodi"
  52. elif sys.platform == "win32":
  53. return "windows"
  54. elif sys.platform == "linux2":
  55. return "enigma2"
  56. else:
  57. return "unknown"
  58. def nfo2xml(nfo_dict):
  59. nfo_type,nfo = next(nfo_dict.iteritems())
  60. s= "<%s>\n"%nfo_type.encode("utf8")
  61. for k,v in nfo.iteritems():
  62. if isinstance(v,list):
  63. for v2 in v:
  64. if isinstance(v2,unicode): v2 = v2.encode("utf8")
  65. s += " <%s>%s</%s>\n"%(k.encode("utf8"), v2, k.encode("utf8"))
  66. else:
  67. if isinstance(v,unicode): v = v.encode("utf8")
  68. s += " <%s>%s</%s>\n"%(k.encode("utf8"), v, k.encode("utf8"))
  69. s += "</%s>\n"%nfo_type.encode("utf8")
  70. return s
  71. def nfo2desc(nfo):
  72. if not "title" in nfo:
  73. nfo_type, nfo = next(nfo.iteritems())
  74. desc = nfo2title(nfo)
  75. dd = lambda t: "\n" + nfo[t] if t in nfo and nfo[t] else ""
  76. dd2 = lambda t: "\n" + ",".join(nfo[t]) if t in nfo and nfo[t] else ""
  77. def ddd(t,title=""):
  78. if title:
  79. title = title + ": "
  80. if t in nfo and nfo[t]:
  81. if isinstance(nfo[t],list):
  82. return "\n" + title + ",".join(nfo[t])
  83. else:
  84. return "\n" + title + nfo[t]
  85. else:
  86. return ""
  87. desc += ddd("tagline")
  88. if "plot" in nfo and "tagline" in nfo and nfo["tagline"] <> nfo["plot"]:
  89. desc += ddd("plot")
  90. elif "plot" in nfo and not "tagline" in nfo:
  91. desc += ddd("plot")
  92. desc += ddd("genre","Genre")
  93. desc += ddd("runtime","Length")
  94. desc += ddd("director","Director")
  95. desc += ddd("actor","Actors")
  96. desc += ddd("language","Languages")
  97. desc += ddd("quality","Quality")
  98. return desc.encode("utf8") if isinstance(desc,unicode) else desc
  99. def nfo2title(nfo):
  100. if not "title" in nfo:
  101. nfo_type, nfo = next(nfo.iteritems())
  102. title = nfo["title"]
  103. if "originaltitle" in nfo and nfo["originaltitle"] and nfo["originaltitle"]<>nfo["title"]:
  104. title +=" ~ "+nfo["originaltitle"]
  105. if "year" in nfo and nfo["year"]:
  106. title += " (%s)"%nfo["year"]
  107. return title.encode("utf8") if isinstance(title,unicode) else title
  108. def play_video(streams):
  109. if len(streams)>1:
  110. for i,s in enumerate(streams):
  111. print "%s: [%s,%s,%s] %s"%(i,s["quality"],s["lang"],s["type"],s["name"])
  112. a = raw_input("Select stram to play: ")
  113. try:
  114. n = int(a)
  115. except:
  116. n = 0
  117. if n>=len(streams):
  118. stream = streams[-1]
  119. else:
  120. stream = streams[n]
  121. else:
  122. stream = streams[0]
  123. stream = stream_change(stream)
  124. title = stream["name"] if not "nfo" in stream or not stream["nfo"] else nfo2title(stream["nfo"])
  125. desc = stream["desc"] if not "nfo" in stream or not stream["nfo"] else nfo2desc(stream["nfo"])
  126. img = stream["img"]
  127. url = stream["url"]
  128. suburl = ""
  129. print url
  130. if "subs" in stream and stream["subs"]:
  131. suburl = stream["subs"][0]["url"]
  132. print "\n**Download subtitles %s - %s"%(title,suburl)
  133. subs = urllib2.urlopen(suburl).read()
  134. if subs:
  135. fname0 = re.sub("[/\n\r\t,:\?]","_",title)
  136. subext = ".srt"
  137. subfile = os.path.join("",fname0+subext)
  138. if ".xml" in suburl:
  139. subs = ttaf2srt(subs)
  140. with open(subfile,"w") as f:
  141. f.write(subs)
  142. else:
  143. print "\n Error downloading subtitle %s"%suburl
  144. print "\n**Play stream %s\n%s" % (title, url.encode("utf8"))
  145. return player(url,title,suburl,stream["headers"])
  146. def player(url, title = "", suburl= "",headers={}):
  147. from subprocess import call
  148. cmd1 = [r"c:\Program Files\VideoLAN\VLC\vlc.exe",url,
  149. "--meta-title",title.decode("utf8").encode(sys.getfilesystemencoding()),
  150. "--http-user-agent","Enigma2"
  151. ]
  152. # gst-launch-1.0 -v souphttpsrc ssl-strict=false proxy=127.0.0.1:8888 extra-headers="Origin:adadadasd" location="http://bitdash-a.akamaihd.net/content/sintel/sintel.mpd" ! decodebin! autovideosink
  153. cmd2 = [
  154. r"C:\gstreamer\1.0\x86_64\bin\gst-launch-1.0","-v",
  155. "playbin", 'uri="%s"'%url,
  156. #"souphttpsrc", "ssl-strict=false",
  157. #"proxy=127.0.0.1:8888",
  158. #'location="%s"'%url,
  159. #'!decodebin!autovideosink'
  160. ]
  161. cmd3 = ["ffplay.exe",url]
  162. cmd = cmd3 if url.startswith("https") else cmd2
  163. ret = call(cmd)
  164. #if ret:
  165. #a = raw_input("*** Error, continue")
  166. return
  167. def stream_type(data):
  168. data = data.lower()
  169. m = re.search(r"^(\w+)://", data)
  170. prefix = m.group(1) if m else ""
  171. if prefix in ("http","https"):
  172. if ".m3u8" in data:
  173. return "hls"
  174. elif ".mpd" in data:
  175. return "dash"
  176. else:
  177. return "http"
  178. else:
  179. return prefix
  180. def check_version(package,url="http://feed.blue.lv/Packages"):
  181. "Return current package version from OPKG feed"
  182. url = "http://feed.blue.lv/Packages"
  183. r = requests.get(url)
  184. if not r.ok:
  185. return ""
  186. m = re.search("Package: %s\nVersion: (.+?)\n"%package, r.content)
  187. if not m:
  188. return ""
  189. return m.group(1)
  190. SPLIT_CHAR = "~"
  191. SPLIT_CODE = urllib.quote(SPLIT_CHAR)
  192. EQ_CODE = urllib.quote("=")
  193. COL_CODE = urllib.quote(":")
  194. SPACE_CODE = urllib.quote(" ")
  195. def make_fname(title):
  196. "Make file name from title"
  197. title = title.strip()
  198. fname0 = re.sub("[/\n\r\t,:]"," ",title)
  199. fname0 = re.sub("['""]","",fname0)
  200. return fname0
  201. def hls_base(url):
  202. url2 = url.split("?")[0]
  203. url2 = "/".join(url2.split("/")[0:-1])+ "/"
  204. return url2
  205. def stream_change(stream):
  206. #return stream # TODO
  207. if "resolver" in stream and stream["resolver"] in ("viaplay","hqq","filmas") or \
  208. "surl" in stream and re.search("https*://(hqq|goo\.gl)",stream["surl"]):
  209. stream["url"] = streamproxy_encode(stream["url"],stream["headers"])
  210. stream["headers"] = {}
  211. return stream
  212. else:
  213. return stream
  214. def streamproxy_encode(url,headers=[],proxy_url=None):
  215. PROXY_URL = "http://localhost:8880/"
  216. if not "?" in url:
  217. url = url+"?"
  218. url2 = url.replace(SPLIT_CHAR,SPLIT_CODE).replace(":",COL_CODE).replace(" ",SPACE_CODE)
  219. if not proxy_url:
  220. proxy_url = PROXY_URL
  221. url2 = proxy_url + url2
  222. if headers:
  223. headers2 = []
  224. for h in headers:
  225. headers2.append("%s=%s"%(h,headers[h].replace("=",EQ_CODE).replace(SPLIT_CHAR,SPLIT_CODE).replace(" ",SPACE_CODE)))
  226. headers2 = SPLIT_CHAR.join(headers2)
  227. url2 = url2+SPLIT_CHAR+headers2
  228. #return url2.encode("utf8") if isinstance(url2,unicode) else url2
  229. return url2
  230. def streamproxy_decode(urlp):
  231. import urlparse
  232. path = urlp.replace(re.search("http://[^/]+",urlp).group(0),"")
  233. p = path.split(SPLIT_CHAR)
  234. url = urllib.unquote(p[0][1:])
  235. #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"}
  236. headers={}
  237. if len(p)>1:
  238. for h in p[1:]:
  239. #h = urllib.unquote()
  240. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  241. return url,headers
  242. def streamproxy_encode2(url,headers=[],proxy_url=None):
  243. PROXY_URL = "http://localhost:8880/"
  244. #url2 = url.replace(SPLIT_CHAR,SPLIT_CODE).replace(":",COL_CODE).replace(" ",SPACE_CODE)
  245. url2 = urllib.quote_plus(url)
  246. if not proxy_url:
  247. proxy_url = PROXY_URL
  248. url2 = proxy_url + url2+"/?"
  249. if headers:
  250. headers2 = []
  251. for h in headers:
  252. headers2.append("%s=%s"%(h,headers[h].replace("=",EQ_CODE).replace(SPLIT_CHAR,SPLIT_CODE).replace(" ",SPACE_CODE)))
  253. headers2 = SPLIT_CHAR.join(headers2)
  254. url2 = url2+SPLIT_CHAR+headers2
  255. return url2
  256. def streamproxy_decode2(urlp):
  257. path = urlp.replace(re.search("http://[^/]+",urlp).group(0),"")
  258. p = path.split(SPLIT_CHAR)
  259. url = urllib.unquote_plus(p[0][1:-2])
  260. #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"}
  261. headers={}
  262. if len(p)>1:
  263. for h in p[1:]:
  264. #h = urllib.unquote()
  265. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  266. return url,headers
  267. class Captions(object):
  268. def __init__(self,uri):
  269. self.uri = uri
  270. self.subs = []
  271. self.styles = {}
  272. if uri.startswith("http"):
  273. r = requests.get(uri)
  274. if r.status_code == 200:
  275. self.loads(r.content)
  276. def loads(self,s):
  277. if "WEBVTT" in s[:s.find("\n")]: # vtt captions
  278. self.load_vtt(s)
  279. elif "<?xml" in s[:s.find("\n")]:
  280. self.load_ttaf(s)
  281. else:
  282. self.load_vtt(s) # TODO
  283. def load_ttaf(self,s):
  284. for r2 in re.findall("<style .+?/>", s):
  285. st = {}
  286. for a in re.findall(r'(\w+)="([^ "]+)"', r2):
  287. st[a[0]] = a[1]
  288. if a[0] == "id":
  289. sid = a[1]
  290. self.styles[sid] = st
  291. for r2 in re.findall("<p .+?</p>", s):
  292. sub = {}
  293. sub["begin"] = str2sec(re.search('begin="([^"]+)"', r2).group(1)) if re.search('begin="([^"]+)"', r2) else -1
  294. sub["end"] = str2sec(re.search('end="([^"]+)"', r2).group(1)) if re.search('end="([^"]+)"', r2) else -1
  295. sub["style"] = re.search('style="([^"]+)"', r2).group(1) if re.search('style="([^"]+)"', r2) else None
  296. sub["text"] = re.search("<p[^>]+>(.+)</p>", r2).group(1).replace("\n","")
  297. sub["text"] = re.sub("<br\s*?/>","\n",sub["text"])
  298. sub["text"] = re.sub("<.+?>"," ",sub["text"])
  299. self.subs.append(sub)
  300. pass
  301. def load_vtt(self,s):
  302. f = StringIO.StringIO(s)
  303. while True:
  304. line = f.readline()
  305. if not line:
  306. break
  307. m = re.search(r"([\d\.\,:]+)\s*-->\s*([\d\.\,\:]+)",line)
  308. if m:
  309. sub = {}
  310. sub["begin"] = str2sec(m.group(1))
  311. sub["end"] = str2sec(m.group(2))
  312. sub["style"] = None
  313. sub["text"] = []
  314. line = f.readline()
  315. while line.strip():
  316. txt = line.strip()
  317. if isinstance(txt,unicode):
  318. txt = txt.encode("utf8")
  319. sub["text"].append(txt)
  320. line = f.readline()
  321. sub["text"] = "\n".join(sub["text"])
  322. self.subs.append(sub)
  323. else:
  324. continue
  325. pass
  326. def get_srt(self):
  327. out = ""
  328. i = 0
  329. for sub in self.subs:
  330. i +=1
  331. begin = sub["begin"]
  332. begin = "%s,%03i"%(str(datetime.timedelta(seconds=begin/1000)),begin%1000)
  333. end = sub["end"]
  334. end = "%s,%03i"%(str(datetime.timedelta(seconds=end/1000)),end%1000)
  335. txt2 = sub["text"]
  336. out += "%s\n%s --> %s\n%s\n\n\n"%(i,begin,end,txt2)
  337. return out
  338. def str2sec(r):
  339. # Convert str time to miliseconds
  340. r= r.replace(",",".")
  341. m = re.search(r"(\d+\:)*(\d+)\:(\d+\.\d+)", r)
  342. if m:
  343. sec = int(m.group(1)[:-1])*60*60*1000 if m.group(1) else 0
  344. sec += int(m.group(2))*60*1000 + int(float(m.group(3))*1000)
  345. return sec
  346. else:
  347. return -1
  348. #c = Captions("http://195.13.216.2/mobile-vod/mp4:lb_barbecue_fr_lq.mp4/lb_barbecue_lv.vtt")
  349. #c = Captions("http://www.bbc.co.uk/iplayer/subtitles/ng/modav/bUnknown-0edd6227-0f38-411c-8d46-fa033c4c61c1_b05ql1s3_1479853893356.xml")
  350. #url = "http://195.13.216.2/mobile-vod/mp4:ac_now_you_see_me_2_en_lq.mp4/ac_now_you_see_me_2_lv.vtt"
  351. #c = Captions(url)
  352. #pass
  353. def ttaf2srt(s):
  354. out = u""
  355. i = 0
  356. for p,txt in re.findall("<p ([^>]+)>(.+?)</p>", s, re.DOTALL):
  357. i +=1
  358. begin = re.search('begin="(.+?)"',p).group(1)
  359. begin = begin.replace(".",",")
  360. end = re.search('end="(.+?)"',p).group(1)
  361. end = end.replace(".",",")
  362. txt2 = re.sub("<br */>","\n",txt)
  363. out += "%s\n%s --> %s\n%s\n\n"%(i,begin,end,txt2)
  364. return out
  365. def item():
  366. """Default item content"""
  367. stream0 = {
  368. 'name': '', #
  369. 'url': '',
  370. 'quality': '?',
  371. 'surl': '',
  372. 'subs': [],
  373. 'headers': {},
  374. "desc":"",
  375. "img":"",
  376. "lang":"",
  377. "type":"",
  378. "resolver":"",
  379. "order":0,
  380. "live":False
  381. }
  382. return stream0
  383. class _StringCookieJar(cookielib.LWPCookieJar):
  384. def __init__(self, string=None, filename=None, delayload=False, policy=None):
  385. cookielib.LWPCookieJar.__init__(self, filename, delayload, policy)
  386. if string and len(string) > 0:
  387. self._cookies = pickle.loads(str(string))
  388. def dump(self):
  389. return pickle.dumps(self._cookies)
  390. def init_urllib(cache=None):
  391. """
  392. Initializes urllib cookie handler
  393. """
  394. global _cookie_jar
  395. data = None
  396. if cache is not None:
  397. data = cache.get(CACHE_COOKIES)
  398. _cookie_jar = _StringCookieJar(data)
  399. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(_cookie_jar))
  400. urllib2.install_opener(opener)
  401. def cache_cookies(cache):
  402. """
  403. Saves cookies to cache
  404. """
  405. global _cookie_jar
  406. if _cookie_jar:
  407. cache.set(CACHE_COOKIES, _cookie_jar.dump())
  408. def request0(url, headers={}):
  409. debug('request: %s' % url)
  410. req = urllib2.Request(url, headers=headers)
  411. req.add_header('User-Agent', UA)
  412. try:
  413. response = urllib2.urlopen(req)
  414. data = response.read()
  415. response.close()
  416. except urllib2.HTTPError, error:
  417. data = error.read()
  418. debug('len(data) %s' % len(data))
  419. return data
  420. def request(url, headers={}):
  421. debug('request: %s' % url)
  422. #req = urllib2.Request(url, headers=headers)
  423. #req.add_header('User-Agent', UA)
  424. if 'User-Agent' not in headers:
  425. headers['User-Agent']= UA
  426. try:
  427. r = requests.get(url, headers=headers)
  428. data = r.content
  429. except:
  430. data = r.content
  431. debug('len(data) %s' % len(data))
  432. return data
  433. def post(url, data, headers={}):
  434. postdata = urllib.urlencode(data)
  435. #req = urllib2.Request(url, postdata, headers)
  436. #req.add_header('User-Agent', UA)
  437. import requests
  438. if 'User-Agent' not in headers:
  439. headers['User-Agent']= UA
  440. try:
  441. r = requests.post(url, data=postdata,headers=headers)
  442. data = r.content
  443. except urllib2.HTTPError, error:
  444. data = r.content
  445. return data
  446. def post0(url, data, headers={}):
  447. postdata = urllib.urlencode(data)
  448. req = urllib2.Request(url, postdata, headers)
  449. req.add_header('User-Agent', UA)
  450. try:
  451. response = urllib2.urlopen(req)
  452. data = response.read()
  453. response.close()
  454. except urllib2.HTTPError, error:
  455. data = error.read()
  456. return data
  457. def post_json(url, data, headers={}):
  458. postdata = json.dumps(data)
  459. headers['Content-Type'] = 'application/json'
  460. req = urllib2.Request(url, postdata, headers)
  461. req.add_header('User-Agent', UA)
  462. response = urllib2.urlopen(req)
  463. data = response.read()
  464. response.close()
  465. return data
  466. #def run_parallel_in_threads(target, args_list):
  467. #result = Queue.Queue()
  468. ## wrapper to collect return value in a Queue
  469. #def task_wrapper(*args):
  470. #result.put(target(*args))
  471. #threads = [threading.Thread(target=task_wrapper, args=args) for args in args_list]
  472. #for t in threads:
  473. #t.start()
  474. #for t in threads:
  475. #t.join()
  476. #return result
  477. def substr(data, start, end):
  478. i1 = data.find(start)
  479. i2 = data.find(end, i1)
  480. return data[i1:i2]
  481. def save_to_file(url, file):
  482. try:
  483. return save_data_to_file(request(url), file)
  484. except:
  485. traceback.print_exc()
  486. def save_data_to_file(data, file):
  487. try:
  488. f = open(file, 'wb')
  489. f.write(data)
  490. f.close()
  491. info('File %s saved' % file)
  492. return True
  493. except:
  494. traceback.print_exc()
  495. def read_file(file):
  496. if not os.path.exists(file):
  497. return ''
  498. f = open(file, 'r')
  499. data = f.read()
  500. f.close()
  501. return data
  502. def _substitute_entity(match):
  503. ent = match.group(3)
  504. if match.group(1) == '#':
  505. # decoding by number
  506. if match.group(2) == '':
  507. # number is in decimal
  508. return unichr(int(ent))
  509. elif match.group(2) == 'x':
  510. # number is in hex
  511. return unichr(int('0x' + ent, 16))
  512. else:
  513. # they were using a name
  514. cp = n2cp.get(ent)
  515. if cp:
  516. return unichr(cp)
  517. else:
  518. return match.group()
  519. def decode_html(data):
  520. if not type(data) == str:
  521. return data
  522. try:
  523. if not type(data) == unicode:
  524. data = unicode(data, 'utf-8', errors='ignore')
  525. entity_re = re.compile(r'&(#?)(x?)(\w+);')
  526. return entity_re.subn(_substitute_entity, data)[0]
  527. except:
  528. traceback.print_exc()
  529. print[data]
  530. return data
  531. def unescape(s0):
  532. #s2 = re.sub("&#\w+;",HTMLParser.HTMLParser().unescape("\1"),s)
  533. s0 = s0.replace("&amp;","&")
  534. for s in re.findall("&#\w+;",s0):
  535. s2 = HTMLParser.HTMLParser().unescape(s)
  536. if isinstance(s0,str):
  537. s2 = s2.encode("utf8")
  538. s0 = s0.replace(s,s2)
  539. pass
  540. return s0
  541. def debug(text):
  542. if LOG > 1:
  543. print('[DEBUG] ' + str([text]))
  544. def info(text):
  545. if LOG > 0:
  546. print('[INFO] ' + str([text]))
  547. def error(text):
  548. print('[ERROR] ' + str([text]))
  549. _diacritic_replace = {u'\u00f3': 'o',
  550. u'\u0213': '-',
  551. u'\u00e1': 'a',
  552. u'\u010d': 'c',
  553. u'\u010c': 'C',
  554. u'\u010f': 'd',
  555. u'\u010e': 'D',
  556. u'\u00e9': 'e',
  557. u'\u011b': 'e',
  558. u'\u00ed': 'i',
  559. u'\u0148': 'n',
  560. u'\u0159': 'r',
  561. u'\u0161': 's',
  562. u'\u0165': 't',
  563. u'\u016f': 'u',
  564. u'\u00fd': 'y',
  565. u'\u017e': 'z',
  566. u'\xed': 'i',
  567. u'\xe9': 'e',
  568. u'\xe1': 'a',
  569. }
  570. def replace_diacritic(string):
  571. ret = []
  572. for char in string:
  573. if char in _diacritic_replace:
  574. ret.append(_diacritic_replace[char])
  575. else:
  576. ret.append(char)
  577. return ''.join(ret)
  578. def params(url=None):
  579. if not url:
  580. url = sys.argv[2]
  581. param = {}
  582. paramstring = url
  583. if len(paramstring) >= 2:
  584. params = url
  585. cleanedparams = params.replace('?', '')
  586. if (params[len(params) - 1] == '/'):
  587. params = params[0:len(params) - 2]
  588. pairsofparams = cleanedparams.split('&')
  589. param = {}
  590. for i in range(len(pairsofparams)):
  591. splitparams = {}
  592. splitparams = pairsofparams[i].split('=')
  593. if (len(splitparams)) == 2:
  594. param[splitparams[0]] = splitparams[1]
  595. for p in param.keys():
  596. param[p] = param[p].decode('hex')
  597. return param
  598. def int_to_base(number, base):
  599. digs = string.digits + string.letters
  600. if number < 0:
  601. sign = -1
  602. elif number == 0:
  603. return digs[0]
  604. else:
  605. sign = 1
  606. number *= sign
  607. digits = []
  608. while number:
  609. digits.append(digs[number % base])
  610. number /= base
  611. if sign < 0:
  612. digits.append('-')
  613. digits.reverse()
  614. return ''.join(digits)
  615. def extract_jwplayer_setup(data):
  616. """
  617. Extracts jwplayer setup configuration and returns it as a dictionary.
  618. :param data: A string to extract the setup from
  619. :return: A dictionary containing the setup configuration
  620. """
  621. data = re.search(r'<script.+?}\(\'(.+)\',\d+,\d+,\'([\w\|]+)\'.*</script>', data, re.I | re.S)
  622. if data:
  623. replacements = data.group(2).split('|')
  624. data = data.group(1)
  625. for i in reversed(range(len(replacements))):
  626. if len(replacements[i]) > 0:
  627. data = re.sub(r'\b%s\b' % int_to_base(i, 36), replacements[i], data)
  628. data = re.search(r'\.setup\(([^\)]+?)\);', data)
  629. if data:
  630. return json.loads(data.group(1).decode('string_escape'))
  631. #return demjson.decode(data.group(1).decode('string_escape')) ### III
  632. return None
  633. #def parse_html(url):
  634. # return BeautifulSoup(request(url), 'html5lib', from_encoding='utf-8')
  635. if __name__ == "__main__":
  636. s = 'B\xc4\x93thovena D\xc4\x81rgumu Taka (2014)/Beethoven&#x27;s Treasure [LV]'
  637. #s = s.decode("utf8")
  638. #s=unescape(s)
  639. #url = "http://localhost:88/https://walterebert.com/playground/video/hls/ts/480x270.m3u8?token=xxxx~User-Agent=Enigma2~Cookie=xxxxx"
  640. url = "http://hyt4d6.vkcache.com/secip/0/UMQ3q2gNjTlOPnEVm3iTiA/ODAuMjMyLjI0MC42/1479610800/hls-vod-s3/flv/api/files/videos/2015/09/11/144197748923a22.mp4.m3u8http://hyt4d6.vkcache.com/secip/0/Y-ZA1qRm8toplc0dN_L6_w/ODAuMjMyLjI0MC42/1479654000/hls-vod-s3/flv/api/files/videos/2015/09/11/144197748923a22.mp4.m3u8"
  641. 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"}
  642. url = "http://str1e.lattelecom.tv/mobile-vod/mp4:sf_fantastic_beasts_and_where_to_find_them_en_hd.mp4/playlist.m3u8?resource_id=fantastic_beasts_and_where_to_find_them&auth_token=6NAvMFDG+rYTAc4hb5JeL2bmsaRR7bAE23M6KDmhKYOGyXoo0gDpJUE9scYy+nQmfbgk03cWMe9MuXWSH1GqwolEk2jOQ/8Mrg7tOdbwrA8zM7nmkfCZPqQkwajZN4mfSJQVKHqXqJ8="
  643. headers={}
  644. print url
  645. url = "replay::tiesraide/ltv1/"
  646. url = "ltc::content/live-streams/103?include=quality"
  647. urlp = streamproxy_encode2(url,headers)
  648. print urlp
  649. url2,headers2 = streamproxy_decode2(urlp)
  650. print url2
  651. player(urlp)
  652. pass