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

util.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 else nfo2title(stream["nfo"])
  125. desc = stream["desc"] if not "nfo" in stream 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 check_version(package,url="http://feed.blue.lv/Packages"):
  168. "Return current package version from OPKG feed"
  169. url = "http://feed.blue.lv/Packages"
  170. r = requests.get(url)
  171. if not r.ok:
  172. return ""
  173. m = re.search("Package: %s\nVersion: (.+?)\n"%package, r.content)
  174. if not m:
  175. return ""
  176. return m.group(1)
  177. SPLIT_CHAR = "~"
  178. SPLIT_CODE = urllib.quote(SPLIT_CHAR)
  179. EQ_CODE = urllib.quote("=")
  180. COL_CODE = urllib.quote(":")
  181. SPACE_CODE = urllib.quote(" ")
  182. def make_fname(title):
  183. "Make file name from title"
  184. title = title.strip()
  185. fname0 = re.sub("[/\n\r\t,:]"," ",title)
  186. fname0 = re.sub("['""]","",fname0)
  187. return fname0
  188. def hls_base(url):
  189. url2 = url.split("?")[0]
  190. url2 = "/".join(url2.split("/")[0:-1])+ "/"
  191. return url2
  192. def stream_change(stream):
  193. #return stream # TODO
  194. if "resolver" in stream and stream["resolver"] in ("viaplay","hqq","filmas") or \
  195. "surl" in stream and re.search("https*://(hqq|goo\.gl)",stream["surl"]):
  196. stream["url"] = streamproxy_encode(stream["url"],stream["headers"])
  197. stream["headers"] = {}
  198. return stream
  199. else:
  200. return stream
  201. def streamproxy_encode(url,headers=[],proxy_url=None):
  202. PROXY_URL = "http://localhost:8880/"
  203. if not "?" in url:
  204. url = url+"?"
  205. url2 = url.replace(SPLIT_CHAR,SPLIT_CODE).replace(":",COL_CODE).replace(" ",SPACE_CODE)
  206. if not proxy_url:
  207. proxy_url = PROXY_URL
  208. url2 = proxy_url + url2
  209. if headers:
  210. headers2 = []
  211. for h in headers:
  212. headers2.append("%s=%s"%(h,headers[h].replace("=",EQ_CODE).replace(SPLIT_CHAR,SPLIT_CODE).replace(" ",SPACE_CODE)))
  213. headers2 = SPLIT_CHAR.join(headers2)
  214. url2 = url2+SPLIT_CHAR+headers2
  215. #return url2.encode("utf8") if isinstance(url2,unicode) else url2
  216. return url2
  217. def streamproxy_decode(urlp):
  218. import urlparse
  219. path = urlp.replace(re.search("http://[^/]+",urlp).group(0),"")
  220. p = path.split(SPLIT_CHAR)
  221. url = urllib.unquote(p[0][1:])
  222. #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"}
  223. headers={}
  224. if len(p)>1:
  225. for h in p[1:]:
  226. #h = urllib.unquote()
  227. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  228. return url,headers
  229. def streamproxy_encode2(url,headers=[],proxy_url=None):
  230. PROXY_URL = "http://localhost:8880/"
  231. #url2 = url.replace(SPLIT_CHAR,SPLIT_CODE).replace(":",COL_CODE).replace(" ",SPACE_CODE)
  232. url2 = urllib.quote_plus(url)
  233. if not proxy_url:
  234. proxy_url = PROXY_URL
  235. url2 = proxy_url + url2+"/?"
  236. if headers:
  237. headers2 = []
  238. for h in headers:
  239. headers2.append("%s=%s"%(h,headers[h].replace("=",EQ_CODE).replace(SPLIT_CHAR,SPLIT_CODE).replace(" ",SPACE_CODE)))
  240. headers2 = SPLIT_CHAR.join(headers2)
  241. url2 = url2+SPLIT_CHAR+headers2
  242. return url2
  243. def streamproxy_decode2(urlp):
  244. path = urlp.replace(re.search("http://[^/]+",urlp).group(0),"")
  245. p = path.split(SPLIT_CHAR)
  246. url = urllib.unquote_plus(p[0][1:-2])
  247. #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"}
  248. headers={}
  249. if len(p)>1:
  250. for h in p[1:]:
  251. #h = urllib.unquote()
  252. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  253. return url,headers
  254. class Captions(object):
  255. def __init__(self,uri):
  256. self.uri = uri
  257. self.subs = []
  258. self.styles = {}
  259. if uri.startswith("http"):
  260. r = requests.get(uri)
  261. if r.status_code == 200:
  262. self.loads(r.content)
  263. def loads(self,s):
  264. if "WEBVTT" in s[:s.find("\n")]: # vtt captions
  265. self.load_vtt(s)
  266. elif "<?xml" in s[:s.find("\n")]:
  267. self.load_ttaf(s)
  268. else:
  269. self.load_vtt(s) # TODO
  270. def load_ttaf(self,s):
  271. for r2 in re.findall("<style .+?/>", s):
  272. st = {}
  273. for a in re.findall(r'(\w+)="([^ "]+)"', r2):
  274. st[a[0]] = a[1]
  275. if a[0] == "id":
  276. sid = a[1]
  277. self.styles[sid] = st
  278. for r2 in re.findall("<p .+?</p>", s):
  279. sub = {}
  280. sub["begin"] = str2sec(re.search('begin="([^"]+)"', r2).group(1)) if re.search('begin="([^"]+)"', r2) else -1
  281. sub["end"] = str2sec(re.search('end="([^"]+)"', r2).group(1)) if re.search('end="([^"]+)"', r2) else -1
  282. sub["style"] = re.search('style="([^"]+)"', r2).group(1) if re.search('style="([^"]+)"', r2) else None
  283. sub["text"] = re.search("<p[^>]+>(.+)</p>", r2).group(1).replace("\n","")
  284. sub["text"] = re.sub("<br\s*?/>","\n",sub["text"])
  285. sub["text"] = re.sub("<.+?>"," ",sub["text"])
  286. self.subs.append(sub)
  287. pass
  288. def load_vtt(self,s):
  289. f = StringIO.StringIO(s)
  290. while True:
  291. line = f.readline()
  292. if not line:
  293. break
  294. m = re.search(r"([\d\.\,:]+)\s*-->\s*([\d\.\,\:]+)",line)
  295. if m:
  296. sub = {}
  297. sub["begin"] = str2sec(m.group(1))
  298. sub["end"] = str2sec(m.group(2))
  299. sub["style"] = None
  300. sub["text"] = []
  301. line = f.readline()
  302. while line.strip():
  303. txt = line.strip()
  304. if isinstance(txt,unicode):
  305. txt = txt.encode("utf8")
  306. sub["text"].append(txt)
  307. line = f.readline()
  308. sub["text"] = "\n".join(sub["text"])
  309. self.subs.append(sub)
  310. else:
  311. continue
  312. pass
  313. def get_srt(self):
  314. out = ""
  315. i = 0
  316. for sub in self.subs:
  317. i +=1
  318. begin = sub["begin"]
  319. begin = "%s,%03i"%(str(datetime.timedelta(seconds=begin/1000)),begin%1000)
  320. end = sub["end"]
  321. end = "%s,%03i"%(str(datetime.timedelta(seconds=end/1000)),end%1000)
  322. txt2 = sub["text"]
  323. out += "%s\n%s --> %s\n%s\n\n\n"%(i,begin,end,txt2)
  324. return out
  325. def str2sec(r):
  326. # Convert str time to miliseconds
  327. r= r.replace(",",".")
  328. m = re.search(r"(\d+\:)*(\d+)\:(\d+\.\d+)", r)
  329. if m:
  330. sec = int(m.group(1)[:-1])*60*60*1000 if m.group(1) else 0
  331. sec += int(m.group(2))*60*1000 + int(float(m.group(3))*1000)
  332. return sec
  333. else:
  334. return -1
  335. #c = Captions("http://195.13.216.2/mobile-vod/mp4:lb_barbecue_fr_lq.mp4/lb_barbecue_lv.vtt")
  336. #c = Captions("http://www.bbc.co.uk/iplayer/subtitles/ng/modav/bUnknown-0edd6227-0f38-411c-8d46-fa033c4c61c1_b05ql1s3_1479853893356.xml")
  337. #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"
  338. #c = Captions(url)
  339. #pass
  340. def ttaf2srt(s):
  341. out = u""
  342. i = 0
  343. for p,txt in re.findall("<p ([^>]+)>(.+?)</p>", s, re.DOTALL):
  344. i +=1
  345. begin = re.search('begin="(.+?)"',p).group(1)
  346. begin = begin.replace(".",",")
  347. end = re.search('end="(.+?)"',p).group(1)
  348. end = end.replace(".",",")
  349. txt2 = re.sub("<br */>","\n",txt)
  350. out += "%s\n%s --> %s\n%s\n\n"%(i,begin,end,txt2)
  351. return out
  352. def item():
  353. """Default item content"""
  354. stream0 = {
  355. 'name': '', #
  356. 'url': '',
  357. 'quality': '?',
  358. 'surl': '',
  359. 'subs': [],
  360. 'headers': {},
  361. "desc":"",
  362. "img":"",
  363. "lang":"",
  364. "type":"",
  365. "resolver":"",
  366. "order":0,
  367. "live":False
  368. }
  369. return stream0
  370. class _StringCookieJar(cookielib.LWPCookieJar):
  371. def __init__(self, string=None, filename=None, delayload=False, policy=None):
  372. cookielib.LWPCookieJar.__init__(self, filename, delayload, policy)
  373. if string and len(string) > 0:
  374. self._cookies = pickle.loads(str(string))
  375. def dump(self):
  376. return pickle.dumps(self._cookies)
  377. def init_urllib(cache=None):
  378. """
  379. Initializes urllib cookie handler
  380. """
  381. global _cookie_jar
  382. data = None
  383. if cache is not None:
  384. data = cache.get(CACHE_COOKIES)
  385. _cookie_jar = _StringCookieJar(data)
  386. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(_cookie_jar))
  387. urllib2.install_opener(opener)
  388. def cache_cookies(cache):
  389. """
  390. Saves cookies to cache
  391. """
  392. global _cookie_jar
  393. if _cookie_jar:
  394. cache.set(CACHE_COOKIES, _cookie_jar.dump())
  395. def request0(url, headers={}):
  396. debug('request: %s' % url)
  397. req = urllib2.Request(url, headers=headers)
  398. req.add_header('User-Agent', UA)
  399. try:
  400. response = urllib2.urlopen(req)
  401. data = response.read()
  402. response.close()
  403. except urllib2.HTTPError, error:
  404. data = error.read()
  405. debug('len(data) %s' % len(data))
  406. return data
  407. def request(url, headers={}):
  408. debug('request: %s' % url)
  409. #req = urllib2.Request(url, headers=headers)
  410. #req.add_header('User-Agent', UA)
  411. if 'User-Agent' not in headers:
  412. headers['User-Agent']= UA
  413. try:
  414. r = requests.get(url, headers=headers)
  415. data = r.content
  416. except:
  417. data = r.content
  418. debug('len(data) %s' % len(data))
  419. return data
  420. def post(url, data, headers={}):
  421. postdata = urllib.urlencode(data)
  422. #req = urllib2.Request(url, postdata, headers)
  423. #req.add_header('User-Agent', UA)
  424. import requests
  425. if 'User-Agent' not in headers:
  426. headers['User-Agent']= UA
  427. try:
  428. r = requests.post(url, data=postdata,headers=headers)
  429. data = r.content
  430. except urllib2.HTTPError, error:
  431. data = r.content
  432. return data
  433. def post0(url, data, headers={}):
  434. postdata = urllib.urlencode(data)
  435. req = urllib2.Request(url, postdata, headers)
  436. req.add_header('User-Agent', UA)
  437. try:
  438. response = urllib2.urlopen(req)
  439. data = response.read()
  440. response.close()
  441. except urllib2.HTTPError, error:
  442. data = error.read()
  443. return data
  444. def post_json(url, data, headers={}):
  445. postdata = json.dumps(data)
  446. headers['Content-Type'] = 'application/json'
  447. req = urllib2.Request(url, postdata, headers)
  448. req.add_header('User-Agent', UA)
  449. response = urllib2.urlopen(req)
  450. data = response.read()
  451. response.close()
  452. return data
  453. #def run_parallel_in_threads(target, args_list):
  454. #result = Queue.Queue()
  455. ## wrapper to collect return value in a Queue
  456. #def task_wrapper(*args):
  457. #result.put(target(*args))
  458. #threads = [threading.Thread(target=task_wrapper, args=args) for args in args_list]
  459. #for t in threads:
  460. #t.start()
  461. #for t in threads:
  462. #t.join()
  463. #return result
  464. def substr(data, start, end):
  465. i1 = data.find(start)
  466. i2 = data.find(end, i1)
  467. return data[i1:i2]
  468. def save_to_file(url, file):
  469. try:
  470. return save_data_to_file(request(url), file)
  471. except:
  472. traceback.print_exc()
  473. def save_data_to_file(data, file):
  474. try:
  475. f = open(file, 'wb')
  476. f.write(data)
  477. f.close()
  478. info('File %s saved' % file)
  479. return True
  480. except:
  481. traceback.print_exc()
  482. def read_file(file):
  483. if not os.path.exists(file):
  484. return ''
  485. f = open(file, 'r')
  486. data = f.read()
  487. f.close()
  488. return data
  489. def _substitute_entity(match):
  490. ent = match.group(3)
  491. if match.group(1) == '#':
  492. # decoding by number
  493. if match.group(2) == '':
  494. # number is in decimal
  495. return unichr(int(ent))
  496. elif match.group(2) == 'x':
  497. # number is in hex
  498. return unichr(int('0x' + ent, 16))
  499. else:
  500. # they were using a name
  501. cp = n2cp.get(ent)
  502. if cp:
  503. return unichr(cp)
  504. else:
  505. return match.group()
  506. def decode_html(data):
  507. if not type(data) == str:
  508. return data
  509. try:
  510. if not type(data) == unicode:
  511. data = unicode(data, 'utf-8', errors='ignore')
  512. entity_re = re.compile(r'&(#?)(x?)(\w+);')
  513. return entity_re.subn(_substitute_entity, data)[0]
  514. except:
  515. traceback.print_exc()
  516. print[data]
  517. return data
  518. def unescape(s0):
  519. #s2 = re.sub("&#\w+;",HTMLParser.HTMLParser().unescape("\1"),s)
  520. s0 = s0.replace("&amp;","&")
  521. for s in re.findall("&#\w+;",s0):
  522. s2 = HTMLParser.HTMLParser().unescape(s)
  523. if isinstance(s0,str):
  524. s2 = s2.encode("utf8")
  525. s0 = s0.replace(s,s2)
  526. pass
  527. return s0
  528. def debug(text):
  529. if LOG > 1:
  530. print('[DEBUG] ' + str([text]))
  531. def info(text):
  532. if LOG > 0:
  533. print('[INFO] ' + str([text]))
  534. def error(text):
  535. print('[ERROR] ' + str([text]))
  536. _diacritic_replace = {u'\u00f3': 'o',
  537. u'\u0213': '-',
  538. u'\u00e1': 'a',
  539. u'\u010d': 'c',
  540. u'\u010c': 'C',
  541. u'\u010f': 'd',
  542. u'\u010e': 'D',
  543. u'\u00e9': 'e',
  544. u'\u011b': 'e',
  545. u'\u00ed': 'i',
  546. u'\u0148': 'n',
  547. u'\u0159': 'r',
  548. u'\u0161': 's',
  549. u'\u0165': 't',
  550. u'\u016f': 'u',
  551. u'\u00fd': 'y',
  552. u'\u017e': 'z',
  553. u'\xed': 'i',
  554. u'\xe9': 'e',
  555. u'\xe1': 'a',
  556. }
  557. def replace_diacritic(string):
  558. ret = []
  559. for char in string:
  560. if char in _diacritic_replace:
  561. ret.append(_diacritic_replace[char])
  562. else:
  563. ret.append(char)
  564. return ''.join(ret)
  565. def params(url=None):
  566. if not url:
  567. url = sys.argv[2]
  568. param = {}
  569. paramstring = url
  570. if len(paramstring) >= 2:
  571. params = url
  572. cleanedparams = params.replace('?', '')
  573. if (params[len(params) - 1] == '/'):
  574. params = params[0:len(params) - 2]
  575. pairsofparams = cleanedparams.split('&')
  576. param = {}
  577. for i in range(len(pairsofparams)):
  578. splitparams = {}
  579. splitparams = pairsofparams[i].split('=')
  580. if (len(splitparams)) == 2:
  581. param[splitparams[0]] = splitparams[1]
  582. for p in param.keys():
  583. param[p] = param[p].decode('hex')
  584. return param
  585. def int_to_base(number, base):
  586. digs = string.digits + string.letters
  587. if number < 0:
  588. sign = -1
  589. elif number == 0:
  590. return digs[0]
  591. else:
  592. sign = 1
  593. number *= sign
  594. digits = []
  595. while number:
  596. digits.append(digs[number % base])
  597. number /= base
  598. if sign < 0:
  599. digits.append('-')
  600. digits.reverse()
  601. return ''.join(digits)
  602. def extract_jwplayer_setup(data):
  603. """
  604. Extracts jwplayer setup configuration and returns it as a dictionary.
  605. :param data: A string to extract the setup from
  606. :return: A dictionary containing the setup configuration
  607. """
  608. data = re.search(r'<script.+?}\(\'(.+)\',\d+,\d+,\'([\w\|]+)\'.*</script>', data, re.I | re.S)
  609. if data:
  610. replacements = data.group(2).split('|')
  611. data = data.group(1)
  612. for i in reversed(range(len(replacements))):
  613. if len(replacements[i]) > 0:
  614. data = re.sub(r'\b%s\b' % int_to_base(i, 36), replacements[i], data)
  615. data = re.search(r'\.setup\(([^\)]+?)\);', data)
  616. if data:
  617. return json.loads(data.group(1).decode('string_escape'))
  618. #return demjson.decode(data.group(1).decode('string_escape')) ### III
  619. return None
  620. #def parse_html(url):
  621. # return BeautifulSoup(request(url), 'html5lib', from_encoding='utf-8')
  622. if __name__ == "__main__":
  623. s = 'B\xc4\x93thovena D\xc4\x81rgumu Taka (2014)/Beethoven&#x27;s Treasure [LV]'
  624. #s = s.decode("utf8")
  625. #s=unescape(s)
  626. #url = "http://localhost:88/https://walterebert.com/playground/video/hls/ts/480x270.m3u8?token=xxxx~User-Agent=Enigma2~Cookie=xxxxx"
  627. 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"
  628. 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"}
  629. 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="
  630. headers={}
  631. print url
  632. url = "replay::tiesraide/ltv1/"
  633. url = "ltc::content/live-streams/103?include=quality"
  634. urlp = streamproxy_encode2(url,headers)
  635. print urlp
  636. url2,headers2 = streamproxy_decode2(urlp)
  637. print url2
  638. player(urlp)
  639. pass