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

viaplay.py 23KB

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