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

viaplay.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #!/usr/bin/env python
  2. # coding=utf8
  3. #
  4. # This file is part of PlayStream - enigma2 plugin to play video streams from various sources
  5. # Copyright (c) 2016 ivars777 (ivars777@gmail.com)
  6. # Distributed under the GNU GPL v3. For full terms see http://www.gnu.org/licenses/gpl-3.0.en.html
  7. #
  8. try:
  9. import json
  10. except:
  11. import simplejson as json
  12. import requests
  13. try:
  14. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  15. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  16. except:
  17. pass
  18. import urlparse, urllib
  19. import datetime, time,re, sys,os
  20. from collections import OrderedDict
  21. import ssl
  22. ssl._create_default_https_context = ssl._create_unverified_context
  23. from SourceBase import SourceBase
  24. try:
  25. import util
  26. except:
  27. parent = os.path.dirname(os.path.abspath(__file__))
  28. parent = os.sep.join(parent.split(os.sep)[:-1])
  29. sys.path.insert(0,parent)
  30. import util
  31. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  32. class Source(SourceBase):
  33. def __init__(self,language="en",cfg_path=None):
  34. self.name = "viaplay"
  35. self.title = "viaplay.lv"
  36. self.img = "https://yt3.ggpht.com/-noVdjbNR-V8/AAAAAAAAAAI/AAAAAAAAAAA/yZ9XNP5urLY/s900-c-k-no-mo-rj-c0xffffff/photo.jpg"
  37. self.desc = "Viaplay.lv saturs"
  38. self.url = "https://viaplay.lv/"
  39. self.headers = headers2dict("""
  40. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
  41. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  42. Connection: keep-alive
  43. Upgrade-Insecure-Requests: 1
  44. """)
  45. #self.language=language
  46. cur_directory = os.path.dirname(os.path.abspath(__file__))
  47. if not cfg_path: cfg_path = cur_directory
  48. self.config_file = os.path.join(cfg_path,self.name+".cfg")
  49. self.options = OrderedDict([("user","change_user"),("password","change_password"),("device","")])
  50. self.options_read()
  51. self.device = self.options["device"]
  52. self.r = None # requests
  53. self.play_session = None
  54. self.s = None
  55. def login(self,user="",password=""):
  56. self.options_read()
  57. if not user: user=self.options["user"]
  58. if not password: password = self.options["password"]
  59. self.s = requests.Session()
  60. ### Dabu sesijas ID ===
  61. headers = headers2dict("""
  62. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
  63. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  64. Accept-Language: en-US,en;q=0.5
  65. Accept-Encoding: gzip, deflate, br
  66. Referer: https://viaplay.lv/
  67. Cookie: ott_cookies_confirmed=1;
  68. DNT: 1
  69. Connection: keep-alive
  70. Upgrade-Insecure-Requests: 1
  71. """)
  72. r = requests.get(self.url,headers=headers)
  73. if not "PLAY_SESSION" in r.cookies:
  74. return False
  75. self.play_session = r.cookies["PLAY_SESSION"]
  76. self.csrfToken = re.search("csrfToken=(.+)",self.play_session).group(1)
  77. ### Ielogojamies ###
  78. headers = headers2dict("""
  79. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
  80. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  81. Accept-Language: en-US,en;q=0.5
  82. Accept-Encoding: gzip, deflate, br
  83. Referer: https://viaplay.lv/
  84. Cookie: ott_cookies_confirmed=1; PLAY_SESSION=e618c42b377a65021298ff63309d5a907988ed1b-PSESSIONID=b010ea1b-fc5e-4a18-aa15-ebbe8b57b3f0&csrfToken=b4eb35263d9be16ef9f7b2f5d10a8ee99dfe75a8-1478051634814-63682b20f1e7e5579de6d056
  85. DNT: 1
  86. Connection: keep-alive
  87. Upgrade-Insecure-Requests: 1
  88. Content-Type: application/x-www-form-urlencoded
  89. """)
  90. url = "https://viaplay.lv/tdi/login/nav/form?csrfToken=%s"%self.csrfToken
  91. # https://viaplay.lv/tdi/login/nav/form?_infuse=1&_ts=1490554674901&csrfToken=
  92. params = "nav_redirectUri=https%3A%2F%2Fviaplay.lv%2F&nav_email={}&nav_password={}".format(urllib.quote(user),urllib.quote(password))
  93. # nav_redirectUri=https%3A%2F%2Fviaplay.lv%2F&nav_email=ivars777%40gmail.com&nav_password=kaskade7&nav_remember=true
  94. headers["Cookie"] = "ott_cookies_confirmed=1; PLAY_SESSION=%s;"%self.play_session
  95. if self.device:
  96. headers["Cookie"] += "ott_dids=%s"%self.device
  97. #cookie = dict(PLAY_SESSION=self.play_session,_hjIncludedInSample=1, mobileAppPromo="shown")
  98. r = requests.post(url,params,headers=headers,allow_redirects=False)
  99. if not "Set-Cookie" in r.headers:
  100. self.play_session = None
  101. return False
  102. if not "ott_web_sac" in r.cookies:
  103. self.play_session = None
  104. return False
  105. self.ott = r.cookies["ott_web_sac"]
  106. ### Dabu iekārtas ID ###
  107. if not self.device:
  108. headers = headers2dict("""
  109. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
  110. Accept: application/xml, text/xml, */*; q=0.01
  111. Accept-Language: en-US,en;q=0.5
  112. Accept-Encoding: gzip, deflate, br
  113. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  114. X-Requested-With: XMLHttpRequest
  115. Referer: https://viaplay.lv/movies/me-and-earl-and-the-dying-girl
  116. DNT: 1
  117. Connection: keep-alive """)
  118. url = "https://viaplay.lv/tdi/account/device/create?_infuse=1&csrfToken=%s"%self.csrfToken
  119. params = "successRedirectUri=https%3A%2F%2Fviaplay.lv%2Fmovies%2F&slotId=&title=Enigma2"
  120. headers["Cookie"] = "PLAY_SESSION=%s; ott_cookies_confirmed=1; ott_web_sac=%s;"%(self.play_session,self.ott)
  121. #cookie = dict(PLAY_SESSION=self.play_session,_hjIncludedInSample=1, mobileAppPromo="shown")
  122. r = requests.post(url,params,headers=headers,allow_redirects=False)
  123. if not ("Set-Cookie" in r.headers and "ott_dids" in r.headers["Set-Cookie"]):
  124. self.play_session = None
  125. return False
  126. self.device = r.cookies["ott_dids"]
  127. self.options["device"] = self.device
  128. self.options_write(self.options)
  129. return True
  130. def logout(self):
  131. return True
  132. def is_logedin(self):
  133. if self.play_session:
  134. return True
  135. else:
  136. return False
  137. def get_video_info(self,vid):
  138. import demjson
  139. ### Dabu strimus ###
  140. headers = headers2dict("""
  141. Host: viaplay.lv
  142. 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
  143. Accept: application/xml, text/xml, */*; q=0.01
  144. Accept-Language: en-US,en;q=0.5
  145. Accept-Encoding: gzip, deflate, br
  146. X-Requested-With: XMLHttpRequest
  147. DNT: 1
  148. Connection: keep-alive
  149. Referer: https://viaplay.lv/
  150. """)
  151. url = "https://viaplay.lv/prehravac/init?_infuse=1&productId=%s"%vid #t110623
  152. headers["Cookie"] = "ott_cookies_confirmed=1; ott_dids=%s; PLAY_SESSION=%s"%(self.device,self.play_session)
  153. r = requests.get(url,headers=headers,allow_redirects=False)
  154. statuss = re.search("<status>(.+?)</status>", r.content).group(1)
  155. if statuss.lower() <> "ok":
  156. raise Exception(statuss)
  157. #print r.content
  158. m = re.search(r"<!\[CDATA\[\s+var TDIPlayerOptions = (.+?);[\n\t\s]+\]\]>\s+</script>", r.content, re.DOTALL)
  159. if not m:
  160. raise "Can not find stream info"
  161. txt = m.group(1)
  162. txt = re.sub("// .+$", "", txt, flags=re.MULTILINE)
  163. #print txt
  164. #for m in re.finditer("// .+$", txt, re.MULTILINE):
  165. # txt = txt[:m.start()] + txt[m.end():]
  166. #print txt
  167. js = demjson.decode(txt)
  168. return js
  169. #return txt
  170. def get_content(self, data):
  171. print "[%s] get_content:"%self.name, data
  172. source,data,path,plist,clist,params,qs = self.parse_data(data)
  173. content=[]
  174. content.append(("..return", "back","","Return back"))
  175. if clist=="home":
  176. content.extend([
  177. ("Search", "viaplay::search-results-all?query={0}",self.img,"Meklēt"),
  178. ("Filmas", "viaplay::movies",self.img,"Filmas"),
  179. ("Seriāli", "viaplay::series",self.img,"Seriāli"),
  180. ("Bērniem", "viaplay::kids",self.img,"Bērniem"),
  181. ("Dokumentalās filmas", "viaplay::documentary",self.img,"Dokumentalās filmas"),
  182. ("Sports", "viaplay::live",self.img,"Sports"),
  183. ])
  184. return content
  185. ### Meklēt ###
  186. elif clist=="search-results-all":
  187. url = "https://viaplay.lv/"+data
  188. r = self._http_request(url)
  189. result = re.findall(r'<div id="product-(\w+)".+?<a href="([^"]+)">.+?<img data-srcset="([^ ]+).+?alt="([^"]+)">.+?<h3 class="is-size-h6">([^<]+)</h3>.*?<p>([^<]+).+?</p>.+?<p class="promo-notice">([^<]+)</p>.+?<p class="info">([^<]+)</p>', r, re.DOTALL)
  190. for item in result:
  191. vid = item[0]
  192. data2 = item[1].replace("https://viaplay.lv/","")
  193. img = item[2]
  194. ep = item[3]
  195. title= item[4]
  196. seas = item[5].replace("\n","").replace("\t","")
  197. desc = item[6]
  198. desc2 = item[7]
  199. if ep==title:
  200. title = "%s (%s)"%(title,seas)
  201. else:
  202. title = "%s - %s%s"%(title,seas,ep)
  203. desc = "%s\n%s\n%s"%(title,desc2,desc)
  204. content.append((title,self.name+"::"+data2,img,desc))
  205. return content
  206. ### Sadalas ##
  207. elif data in ["movies","series","kids","documentary"]:
  208. r = self._http_request(self.url+data)
  209. # https://viaplay.lv/tdi/movies/next?sections[]=MOVIES&genres[]=a3591&sort[]=latest&offset=0
  210. # https://viaplay.lv/tdi/series/next?sections[]=SERIES&sort[]=latest&offset=0
  211. # https://viaplay.lv/tdi/kids/next?sections[]=KIDS&cat[]=SERIES&cat[]=MOVIE&sort[]=latest&offset=18
  212. # https://viaplay.lv/kids?sections[]=KIDS&cat[]=SERIES&sort[]=latest
  213. sections = {"movies":"MOVIES","series":"SERIES","kids":"KIDS","documentary":"DOCUMENTS"}
  214. nosaukums = {"movies":"Flmas","series":"Seriāli","kids":"Bērnu","documentary":"Dokumentalās"}
  215. #availability = {"new":"jaunākās","last":"pēdējā iespēja"}
  216. sort = OrderedDict([("latest","jaunākais"),("title","pēc nosaukuma"),("popular","pēc popularitātes"),("year","pēc gada")])
  217. for s in sort:
  218. if data in ("movies","series"):
  219. title = "%s - %s"%(nosaukums[data],sort[s])
  220. data2 = "%s/next?sections[]=%s&sort[]=%s"%(data,sections[data],s)
  221. content.append((title,self.name+"::"+data2,self.img,title))
  222. else:
  223. title = "%s filmas - %s"%(nosaukums[data],sort[s])
  224. data2 = "%s/next?sections[]=%s&cat[]=MOVIE&sort[]=%s"%(data,sections[data],s)
  225. content.append((title,self.name+"::"+data2,self.img,title))
  226. title = "%s seriāli - %s"%(nosaukums[data],sort[s])
  227. data2 = "%s/next?sections[]=%s&cat[]=SERIES&sort[]=%s"%(data,sections[data],s)
  228. content.append((title,self.name+"::"+data2,self.img,title))
  229. # Pievienojam žanru sarakstu
  230. result = re.findall(r'name="genres\[\]" value="([^"]+)">.+?class="">([^<]+)</label>', r, re.DOTALL)
  231. for item in result:
  232. s = "latest"
  233. genre = item[1].replace("&amp;","&")
  234. title = "%s: %s"%(nosaukums[data],genre)
  235. data2 = "%s/next?sections[]=%s&genres[]=%s&sort[]=%s"%(data,sections[data],item[0],s)
  236. content.append((title,self.name+"::"+data2,self.img,title))
  237. return content
  238. ### Filmu/seriālu/sēriju saraksts ###
  239. elif clist in ("movies","series","kids","documentary") and plist[1] == "next":
  240. url = "https://viaplay.lv/tdi/"+data
  241. r = self._http_request(url)
  242. if clist == "series" and "season" in qs:
  243. result = re.findall(r'<div id="product-(\w+)".+?<a href="([^"]+)">.+?<img data-srcset="([^ ]+).+?alt="([^"]+)">.+?<h3 class="is-size-h6">([^<]+)</h3>.*?<p>([^<]+).+?</p>.+?<p class="promo-notice">([^<]+)</p>.+?<p class="info">([^<]+)</p>', r, re.DOTALL)
  244. for item in result:
  245. vid = item[0]
  246. data2 = item[1].replace("https://viaplay.lv/","")
  247. img = item[2]
  248. ep = item[3]
  249. title= item[4]
  250. seas = item[5]
  251. desc = item[6]
  252. desc2 = item[7]
  253. title = "%s - %s%s"%(title,seas,ep)
  254. desc = "%s\n%s\n%s"%(title,desc2,desc)
  255. content.append((title,self.name+"::"+data2,img,desc))
  256. else: # filmas
  257. result = re.findall(r'<div id="product-(\w+)".+?<a href="([^"]+)">.+?<img data-srcset="([^ ]+).+?alt="([^"]+)">.+?<p>([^<]+)</p>.+?<p class="promo-notice">([^<]+).+?<p class="is-strong detail">(.+?)</p>.+?<p class="info">([^<]+)</p>', r, re.DOTALL)
  258. for item in result:
  259. vid = item[0]
  260. data2 = item[1].replace("https://viaplay.lv/","")
  261. img = item[2]
  262. title = item[3]
  263. year = item[4]
  264. year = year.replace("\n","").replace("\t","")
  265. title = title +"(%s)"%year
  266. desc= item[5]
  267. genre = re.findall(">([^<]+)<", item[6], re.DOTALL)
  268. genre = ("".join(genre)).replace("&amp;","&")
  269. desc2 = item[7]
  270. desc = "%s\n%s\n%s"%(genre,desc2,desc)
  271. content.append((title,self.name+"::"+data2, img,desc))
  272. m = re.search(r"data\('href', 'https://viaplay\.lv/tdi/([^']+)'\)", r, re.DOTALL)
  273. if m:
  274. data2 = m.group(1)
  275. content.append(("Next page",self.name+"::"+data2,img,"Next page"))
  276. return content
  277. ### Seriāls ###
  278. elif clist == "series" and len(plist)==2:
  279. url = "https://viaplay.lv/"+data
  280. r = self._http_request(url)
  281. result = re.findall(r'<li>.*?<a class="tdi" href="https://viaplay\.lv/([^"]+)" data-related-ancestor="\.js-tdi-items-filter-and-items">([^<]+)</a>.*?</li>', r, re.DOTALL)
  282. for item in result:
  283. title = item[1]
  284. data2 = item[0]
  285. data2 = data2.replace("series/","series/next/")
  286. data2 = data2+"&sort[]=ord"
  287. #series/littlest-pet-shop?season=t6821
  288. #series/next/peppa-pig?season=t8430
  289. # &sort[]=ord
  290. if "availability=" in data2: continue
  291. content.append((title,self.name+"::"+data2,self.img,title)) #TODO bilde
  292. return content
  293. def is_video(self,data):
  294. source,data,path,plist,clist,params,qs = self.parse_data(data)
  295. if clist in ("movies","documentary","kids") and len(plist)>1 and plist[1]<>"next":
  296. return True
  297. elif clist == "series" and len(plist)>1 and plist[1] == "episode":
  298. return True
  299. else:
  300. return False
  301. def get_streams(self, data):
  302. print "[viaplay] get_streams:", data
  303. if not self.is_video(data):
  304. return []
  305. source,data,path,plist,clist,params,qs = self.parse_data(data)
  306. if not self.is_logedin():
  307. self.login()
  308. if not self.is_logedin():
  309. raise Exception("Could not login to viaplay.lv, check username/password in options")
  310. streams = []
  311. url = "https://viaplay.lv/"+data
  312. r = self._http_request(url)
  313. if clist in ("series","livestream"): # TODO nerāda overtime u.c.
  314. m = re.search(r'<h1 class="is-bottom-sticked is-size-h2">(.+?)</h1>.*?<h2 class="is-size-h4">.*?<p class="is-size-h6 is-strong is-bottom-sticked">(.+?)<div class="toggler-content">\s+<p>(.+?)</p>', r, re.DOTALL)
  315. if not m:
  316. raise Exception("Problem getting video information")
  317. title = m.group(1).replace("\n"," ").replace("\t","").strip()
  318. title = re.sub("<[^>]+>","",title).strip()
  319. desc2 = m.group(2).replace("\n"," ").replace("\t","").strip()
  320. desc2 = re.sub("<[^>]+>","",desc2).strip()
  321. desc = m.group(3)
  322. desc = "%s\n%s"%(desc2,desc)
  323. vid = re.search('data-productid="(\w+)"',r).group(1)
  324. else:
  325. m = re.search(r'<h1 class="is-strong is-bottom-sticked is-size-h2" jnp-id="(\w+)">([^<]+)</h1>.*?<h2 class="is-strong is-size-h4">([^<]+)</h2>.*?<p class="is-size-h6 is-strong is-bottom-sticked">(.+?)<div class="toggler-content">\s+<p>(.+?)</p>', r, re.DOTALL)
  326. if not m:
  327. raise Exception("Problem getting video information")
  328. title = m.group(2).strip()
  329. title2 = m.group(3).strip()
  330. title = "%s | %s"%(title,title2)
  331. desc = m.group(5).strip()
  332. desc2 = m.group(4).strip()
  333. desc2 = re.sub("<[^>]+>","",desc2)
  334. desc2 = desc2.replace("\n"," ").replace("\t","")
  335. desc = "%s\n%s"%(desc2,desc)
  336. vid = m.group(1)
  337. js = self.get_video_info(vid)
  338. #for m in re.finditer(r"lang: '(?P<lang>\w+)',\s+src: '(?P<url>[^']+)',\s+type: '(?P<mime>[^']+)',\s+drm: \[(?P<drm>.+?)\]\s*\}", r, re.DOTALL):
  339. if not js:
  340. return []
  341. tracks = js["tracks"]
  342. #if not tracks["HLS"]:
  343. # raise Exception("Encrypted DASH playing not yet implemented")
  344. captions = []
  345. llist = ["fr","en","ru","lv"]
  346. for st in js["plugins"]["settings"]["subtitles"]:
  347. sub = {}
  348. sub["url"] = st["src"]
  349. sub["lang"] = st["srclang"]
  350. sub["name"] = st["label"]
  351. sub["type"] = "vtt"
  352. sub["order"] = llist.index(sub["lang"])*10 if sub["lang"] in llist else 0
  353. captions.append(sub)
  354. captions = sorted(captions,key=lambda item: item["order"],reverse=True)
  355. for s in tracks["HLS"] if tracks["HLS"] else tracks["DASH"] :
  356. stype = "DASH" if "dash" in s["type"] else "HLS"
  357. if "drm" in s: ###
  358. # TODO, encrypted stream
  359. raise Exception("Can not play DRM protected stream!\nOnly local and Russian content available without DRM")
  360. continue
  361. url = s["src"]
  362. #urlp = util.streamproxy_encode(s["src"])
  363. stream = util.item()
  364. stream["url"]=url
  365. stream["resolver"] = "viaplay"
  366. stream["lang"]=s["lang"]
  367. stream["quality"]="variant"
  368. stream["bitrate"]= "1000000"
  369. stream["name"]= title
  370. stream["desc"]=desc
  371. stream["type"]=stype
  372. stream["subs"] = captions
  373. print url
  374. if stype=="DASH": streams.append(stream)
  375. if stype == "HLS": # izvelkam individuālos strimus
  376. r = requests.get(url)
  377. result = re.findall("#EXT-X-STREAM-INF:BANDWIDTH=(\d+),RESOLUTION=(\d+x\d+)\n(\w+.m3u8)", r.content)
  378. if not result:
  379. continue
  380. for s2 in result:
  381. ### TODO vajag lietot cookie ar tokenu no playlista requesta
  382. if "set-cookie" in r.headers:
  383. headers = {"Cookie":r.headers["set-cookie"]}
  384. else:
  385. headers={}
  386. #url2 = re.sub(r"(http.*://.+/)\w+.m3u8", r"\1"+s2[2], url)
  387. url2 = "/".join(url.split("/")[:-1])+"/"+s2[2]
  388. #r2 = requests.get(url2,headers=headers)
  389. #if "set-cookie" in r2.headers:
  390. #headers = {"Cookie":r2.headers["set-cookie"]}
  391. #else:
  392. #headers={}
  393. #url2p=util.streamproxy_encode(url2,headers)
  394. stream = util.item()
  395. stream["url"]=url2
  396. stream["lang"]=s["lang"]
  397. stream["quality"]="%s"%(s2[1])
  398. stream["name"]= title
  399. stream["desc"]=desc
  400. stream["bitrate"]=s2[0]
  401. stream["type"]="DASH" if "dash" in s["type"] else "HLS"
  402. streams.append(stream)
  403. ### TODO - sakārtot sarakstu, lai pirmais ir labakais video
  404. qlist = ["","512","640","758","1024","variant"]
  405. llist = ["lt","et","fr","en","ru","lv"]
  406. for s in streams:
  407. lv = llist.index(s["lang"])*10000000 if s["lang"] in llist else 0
  408. #qv=qlist.index(s["quality"]) if s["quality"] in qlist else 0
  409. qv = int(s["bitrate"]) if s["bitrate"] else 0
  410. s["order"] = lv+qv
  411. #print s["lang"],s["quality"],s["bitrate"],s["order"]
  412. streams = sorted(streams,key=lambda item: item["order"],reverse=True)
  413. return streams
  414. def call(self, data,params = None, headers=None):
  415. if not headers: headers = self.headers
  416. #if not lang: lang = self.country
  417. url = "https://viaplay.lv/tdi/" + data
  418. content = self._http_request(url, params, headers)
  419. return content
  420. if __name__ == "__main__":
  421. if len(sys.argv)>1:
  422. data= sys.argv[1]
  423. else:
  424. data = "kids/child-and-karlson"
  425. c = Source()
  426. print "login: %s"%c.login()
  427. if "/" in data:
  428. streams = c.get_streams(data)
  429. util.play_video(streams)
  430. else:
  431. vinfo = c.get_video_info(data)
  432. if "HLS" in vinfo["tracks"] and vinfo["tracks"]["HLS"]:
  433. url = vinfo["tracks"]["HLS"][0]["src"]
  434. urlp = util.streamproxy_encode(url)
  435. util.player(urlp)
  436. else:
  437. print "No HLS stream"
  438. sys.exit()
  439. r = requests.get("https://viaplay.lv/movies?sections[]=MOVIES")
  440. result = re.findall(r'<div id="product-(\w+)".+?<a href="([^"]+)">.+?<img data-srcset="([^ ]+).+?alt="([^"]+)">.+?<p class="promo-notice">([^<]+)<', r.content, re.DOTALL)
  441. for item in result:
  442. vid = item[0]
  443. url = item[1]
  444. img = item[2]
  445. title = item[3]
  446. desc= item[4]
  447. print "\n%s (%s):"%(title,vid)
  448. vinfo = c.get_video_info(vid)
  449. if "HLS" in vinfo["tracks"]:
  450. for s in vinfo["tracks"]["HLS"]:
  451. print "HLS %s: \n%s"%(s["lang"],s["src"])
  452. if "DASH" in vinfo["tracks"]:
  453. for s in vinfo["tracks"]["DASH"]:
  454. print "DASH %s: \n%s"%(s["lang"],s["src"])
  455. #except Exception,ex:
  456. #print ex.message
  457. #content = c.get_content(data)
  458. #for item in content:
  459. # print item
  460. pass