Kodi plugin to to play various online streams (mostly Latvian)

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. import sys, os, os.path, re, sys
  9. import urllib,urllib2
  10. from xml.sax.saxutils import unescape,escape
  11. from urllib import quote, unquote
  12. import datetime
  13. import HTMLParser
  14. import json
  15. import datetime
  16. from SourceBase import SourceBase, stream_type
  17. import util
  18. from collections import OrderedDict
  19. API_URL = 'https://m.lattelecom.tv/'
  20. user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; da-dk) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3"
  21. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  22. h = HTMLParser.HTMLParser()
  23. class Source(SourceBase):
  24. def __init__(self):
  25. self.name = "ltc"
  26. self.title = "Shortcut.lv (lattelecom.tv)"
  27. self.img = "picons/shortcut.png"
  28. self.desc = "Shortcut.lv (lattelecom.tv) satura skatīšanās"
  29. self.token = "" # manstv.lattelecom.tv
  30. self.session_id = "" # www.lattelecom.tv
  31. self.api_url = "https://manstv.lattelecom.tv/api/v1.4/get/"
  32. self.api_url2 = "https://www.lattelecom.tv/"
  33. self.headers = headers2dict("""
  34. User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
  35. Host: manstv.lattelecom.tv
  36. Connection: Keep-Alive
  37. """)
  38. self.headers2 = headers2dict("""
  39. Host: www.lattelecom.tv
  40. Connection: keep-alive
  41. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  42. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  43. Accept-Language: en-US,en;q=0.8
  44. """)
  45. self.channels = None
  46. ### specific service info ###
  47. self.ch=[]
  48. self.ch2=[]
  49. self.ch_id={}
  50. self.ch_id2={}
  51. self.ch_name={}
  52. self.epg=[]
  53. self.epg_id={}
  54. self.epg_id2={}
  55. self.epg_date={}
  56. self.epg_ch={}
  57. self.epgdates = []
  58. self.today = datetime.date.today()
  59. self.today2 = self.today.strftime("%d.%m.%Y")
  60. cur_directory = os.path.dirname(os.path.abspath(__file__))
  61. self.config_file = os.path.join(cur_directory,self.name+".cfg")
  62. self.options = OrderedDict([("user","lietotajs"),("password","parole")])
  63. self.options_read()
  64. def get_content(self, data):
  65. print "[ltc] get_content:", data
  66. if "::" in data:
  67. data = data.split("::")[1]
  68. path = data.split("?")[0]
  69. clist = path.split("/")[0]
  70. params = data[data.find("?"):] if "?" in data else ""
  71. qs = dict(map(lambda x:x.split("="),re.findall("\w+=[\w-]+",params)))
  72. #lang = qs["lang"] if "lang" in qs else self.country
  73. content=[]
  74. content.append(("..return", "back","","Return back"))
  75. if clist=="home":
  76. content.extend([
  77. ("Search TV", "ltc::search/{0}","","Search in TV archive"),
  78. ("Search Video", "ltc::meklet?q={0}","","Search in video"),
  79. ("Live streams", "ltc::tiesraide","","TV live streams"),
  80. ("Archive", "ltc::arhivs","","TV live archive"),
  81. ("Video", "ltc::videonoma","","Video on demand"),
  82. ])
  83. return content
  84. ### Meklēt TV (1.4 API)
  85. elif clist == "search": #TODO vod/search/{q}
  86. #r = self.call("vod/"+data)
  87. return content
  88. ### Meklēsana video (www )###
  89. elif clist == "meklet": #TODO vod/search/{q}
  90. r = self.call2(data)
  91. for res in re.findall('<div class="(program follow|movie forward)-link">(.+?</div></div>.*?)</div>', r, re.DOTALL):
  92. r2 = res[1]
  93. if "movie" in res[0]:
  94. #if not "permalink" in r2:
  95. #continue
  96. m = re.search('<a class="title" href="([^"]+)/">([^<]+)</a>', r2, re.DOTALL)
  97. if not m: continue
  98. title = m.group(2)
  99. data2 = "videonoma" + m.group(1)
  100. #data2 = "arhivs" + m.group(1)
  101. m = re.search('src="([^"]+)"', r2, re.DOTALL)
  102. img = "https://www.lattelecom.tv/"+m.group(1) if m else ""
  103. m = re.search('<div class="introduction">([^<]+)<', r2, re.DOTALL)
  104. desc = m.group(1) if m else ""
  105. m = re.search('class="genre".+?>([^<]+)<', r2, re.DOTALL)
  106. if m:
  107. desc = m.group(1)+"\n"+desc
  108. else:
  109. m = re.search('<a href="/([^"]+)" class="title">([^<]+)</a>', r2, re.DOTALL)
  110. if not m: continue
  111. title = m.group(2)
  112. data2 = m.group(1)
  113. dd = data2.split("/")
  114. if dd[0]=="arhivs":
  115. ch_name = dd[1]
  116. ch = self.get_channel_by_name(ch_name)
  117. data2 = "archive/get-stream/%s?channelid=%s"%(dd[3],ch["id"]) if ch else ""
  118. #ltc::archive/get-stream/1464120671889?channelid=101
  119. else:
  120. continue
  121. m = re.search(r"url\((.+?)\)", r2, re.DOTALL)
  122. img = "https://www.lattelecom.tv"+m.group(1) if m else ""
  123. m = re.search('<span class="sub">([^<]+)</span>', r2, re.DOTALL)
  124. sub = m.group(1) if m else ""
  125. title = title + " " + sub
  126. desc = title
  127. content.append((title,self.name+"::"+data2,img,desc))
  128. return content
  129. ### Tiešraides kanānālu saraksts ###
  130. elif data=="tiesraide":
  131. r = self.call2(data)
  132. jsdata = re.search("site.program = ([^;]+);",r).group(1)
  133. js=json.loads(jsdata)
  134. for item in self.get_channels():
  135. if item["live"]=='0':continue
  136. title = item["name"]
  137. data2 = "content/live-streams/%s?include=quality"%item["id"]
  138. if item['xprs_id'] in js:
  139. epg = js[item['xprs_id']]
  140. desc = "%s - %s (%s-%s)"%(title,epg[0]["t"],epg[0]["hs"],epg[0]["he"])
  141. img = "https://www.lattelecom.tv" + epg[0]["p"]
  142. else:
  143. img = "https://manstv.lattelecom.tv/"+ item['broadcast_default_picture']
  144. desc = title
  145. content.append((title,self.name+"::"+data2,img,desc))
  146. return content
  147. ### TV arhīva sākums ###
  148. elif data=="archive" or data=="arhivs":
  149. #self.get_epg()
  150. content.extend([
  151. ("Archive - categories", "ltc::archive/categories","","TV live archive by categories"),
  152. ("Archive - channels", "ltc::archive/channels","","TV live archive by channels"),
  153. #("Archive - dates", "ltc::archive/dates","","TV live archive by dates"),
  154. ])
  155. return content
  156. ### Arhīva kategorijas
  157. elif data=="archive/categories":
  158. #https://manstv.lattelecom.tv/api/v1.3/get/archive/records/?filter[category]=13&limit=10&until_id=1458681019238
  159. #https://manstv.lattelecom.tv/api/v1.3/get/archive/categories/
  160. r = self.call(data)
  161. for item in r["items"]:
  162. title = item["name"]
  163. data2 = "archive/records?filter[category]=%s&limit=40"%item["id"]
  164. img = "https://manstv.lattelecom.tv/"+ item['image']
  165. desc = title
  166. content.append((title,self.name+"::"+data2,img,desc))
  167. return content
  168. ### Arhīva kategoriju video
  169. elif "archive/records" in data:
  170. #https://manstv.lattelecom.tv/api/v1.3/get/archive/records/?filter[category]=13&limit=10&until_id=1458681019238
  171. #https://manstv.lattelecom.tv/api/v1.3/get/archive/categories/
  172. r = self.call(data)
  173. for i,item in enumerate(r["items"]):
  174. #if not item["is_archive"]==u"1":continue # TODO jānočeko, kurs no atributiem apzīmē, ka ir arhīvs
  175. if not self.epg_id.has_key(item["id"]):
  176. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  177. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  178. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  179. self.epg.append(item)
  180. index = self.epg.index(item)
  181. self.epg_id[item["id"]]=index
  182. if not item["date"] in self.epg_date:
  183. self.epg_date[item["date"]]=[]
  184. self.epg_date[item["date"]].append(index)
  185. if not item["channel_id"] in self.epg_ch:
  186. self.epg_ch[item["channel_id"]]=[]
  187. self.epg_ch[item["channel_id"]].append(index)
  188. item = self.get_epg_id(item["id"])
  189. ch = self.get_channel_by_id2(item["channel_id"])
  190. ch_name = ch["name"] if ch else item["channel_id"]
  191. title = u"[%s %s-%s] %s - %s"%(item["date"],item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"),ch_name,item["title"])
  192. #data2 = "archive/get-stream/%s?channelid=%s"%(item["id"],ch["id"])
  193. data2 = "content/record-streams/%s?include=quality"%item["id"]
  194. img = "https://manstv.lattelecom.tv/"+ item["url"]
  195. desc = u"%s - %s\n%s - %s\n%s\n%s"%(ch_name,item["title"],item["time_start"],item["time_stop"],
  196. item["category1"],item["description"])
  197. content.append((title,self.name+"::"+data2,img,desc))
  198. if "until_id" in data:
  199. data2 = re.sub("until_id=\d+","until_id="+item["id"],data)
  200. else:
  201. data2 = data + "&until_id=%s"%item["id"]
  202. content.append(("Next page",self.name+"::"+data2,"","Go to next page"))
  203. return content
  204. ### Arhīva kanānālu saraksts
  205. elif data=="archive/channels":
  206. for item in self.get_channels():
  207. if not item["is_archive"]==u"1":continue
  208. title = item["name"]
  209. data2 = "archive/channel/%s?date=%s"%(item["id"],self.today.strftime("%Y-%m-%d"))
  210. img = "https://manstv.lattelecom.tv/"+ item['broadcast_default_picture']
  211. desc = title
  212. content.append((title,self.name+"::"+data2,img,desc))
  213. return content
  214. ### Arhīva kanānāla video saraksta
  215. elif "archive/channel/" in data:
  216. chid = path.split("/")[2]
  217. for item in self.get_epg_date(qs["date"],chid):
  218. #if item["is_archive"]=='0':continue
  219. ch = self.get_channel_by_id2(item["channel_id"])
  220. ch_name = ch["name"] if ch else item["channel_id"]
  221. title = u"[%s %s-%s] %s - %s"%(item["date"],item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"),ch_name,item["title"])
  222. #data2 = "archive/get-stream/%s?channelid=%s"%(item["id"],ch["id"])
  223. data2 = "content/record-streams/%s?include=quality"%item["id"]
  224. img = "https://manstv.lattelecom.tv/"+ item["url"]
  225. desc = u"%s - %s\n%s - %s\n%s\n%s"%(ch_name,item["title"],item["time_start"],item["time_stop"],
  226. item["category1"],item["description"])
  227. content.append((title,self.name+"::"+data2,img,desc))
  228. date2=datetime.datetime.strptime(qs["date"], '%Y-%m-%d').date()-datetime.timedelta(days=1)
  229. date2 = date2.strftime("%Y-%m-%d")
  230. if "date=" in data:
  231. data2 = re.sub("date=[\d-]+","date="+date2,data)
  232. else:
  233. data2 = data + "&date=%s"%date2
  234. content.append(("Previous day (%s)"%date2,self.name+"::"+data2,"","Go previous day"))
  235. return content
  236. ### Arhīva datumi
  237. elif clist=="arhivs" and len(data.split("/"))==2: # TODO - pasreiz nestrada
  238. ch = data.split("/")[1]
  239. r= self.call(data)
  240. m = re.search('class="spac no_select">([^<]+)</span>', r, re.IGNORECASE)
  241. if m:
  242. ch_name = m.group(1)
  243. else:
  244. ch_name = ""
  245. today = datetime.date.today()
  246. for i in range(7):
  247. date = today-datetime.timedelta(i)
  248. title = ch_name + " - " + date.strftime("%d.%m.%Y")
  249. data2 = "%s/%s"%(data,date.strftime("%Y-%m-%d"))
  250. img = ""
  251. desc = title
  252. content.append((title,self.name+"::"+data2,img,desc))
  253. return content
  254. ### Arhīva dienas raidijumi
  255. elif clist=="arhivs" and len(data.split("/"))==3: # TODO - nestrādā
  256. ch = data.split("/")[1]
  257. date = data.split("/")[2]
  258. r= self.call(data)
  259. m = re.search('class="spac no_select">([^<]+)</span>', r, re.IGNORECASE)
  260. if m:
  261. ch_name = m.group(1)
  262. else:
  263. ch_name = ""
  264. for item in re.findall('(?i)href="/([^"]+?)" id="" class="archive_programm_a"><div class="archive_programm "><div class="chanel_time"><span>([^<]+)</span></div><div class="archive_programm_one"><span>([^<]+)', r):
  265. title = "%s - %s"%(item[1],item[2])
  266. data2 = item[0]
  267. img = ""
  268. desc = "%s (%s)\n%s"%(ch_name,date,title)
  269. content.append((title,self.name+"::"+data2,img,desc))
  270. return content
  271. ### Videonoma galvenā
  272. elif data=="videonoma":
  273. content.extend([
  274. ("Filmas - jaunākās", "ltc::videonoma?page=0&genre=all_movies&sorts=laiks&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Jaunākās filmas"),
  275. ("Filmas - pēc nosaukuma", "ltc::videonoma?page=0&genre=all_movies&sorts=title&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas pēc nosaukuma"),
  276. ("Filmas - latviski", 'ltc::videonoma?page=0&genre=all_movies&sorts=title&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.lattelecom.tv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas latviešu valodā"),
  277. ("Filmas - pēc reitinga", "ltc::videonoma?page=0&genre=all_movies&sorts=imbd&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas pēc IMBD reitinga"),
  278. ("Filmas bērniem - jaunākās", "ltc::videonoma?page=0&genre=3&sorts=laiks&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_berniem.png","Jaunākie bernu video"),
  279. ("Filmas bērniem - pēc nosaukuma", "ltc::videonoma?page=0&genre=3&sorts=title&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video pēc nosaukuma"),
  280. ("Filmas bērniem - latviski", 'ltc::videonoma?page=0&genre=3&sorts=title&cnt=40&clear=true&filter={"valoda":[\"lv\"]}',"https://www.lattelecom.tv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video latviski"),
  281. ("Filmas bērniem - pēc reitinga", "ltc::videonoma?page=0&genre=3&sorts=imbd&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video pēc IMBD reitinga"),
  282. ("Sērijas - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Jaunākās sērijas"),
  283. ("Sērijas - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas pēc pēc nosaukuma"),
  284. ("Sērijas animācijas - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={\"zanrs\":[\"201\"]}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  285. ("Sērijas animācijas - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={\"zanrs\":[\"201\"]}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  286. ("Sērijas ģimenes - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={\"zanrs\":[\"195\"]}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  287. ("Sērijas ģimenes - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={\"zanrs\":[\"195\"]}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  288. ("Sērijas latviski - jaunākās", 'ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas latviski"),
  289. ("Sērijas latviski - pēc nosaukuma", 'ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.lattelecom.tv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas latviski"),
  290. ("Koncerti - jaunākie", "ltc::videonoma?page=0&genre=19&sorts=laiks&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_koncerti.png","Jaunākie koncerti"),
  291. ("Koncerti - pēc nosaukuma", "ltc::videonoma?page=0&genre=19&sorts=title&cnt=40&clear=true&filter={}","https://www.lattelecom.tv/images/redesign/videonoma_dropdown_koncerti.png","Koncerti pēc pēc nosaukuma"),
  292. ])
  293. return content
  294. ### Videonomas saraksti
  295. elif path == "videonoma":
  296. url = "https://www.lattelecom.tv/movies-snippet.json"+params
  297. r = self._http_request(url,headers=self.headers2)
  298. if not r:
  299. return content
  300. js = json.loads(r,"utf8")
  301. if not r:
  302. return content
  303. for item in js["movies"]:
  304. if not item["title"]:
  305. continue
  306. title = item["title"].encode("utf8")
  307. data2 = item["url"][1:].encode("utf8")
  308. if data2[-1]=="/": data2=data2[:-1]
  309. if "/raidijumi/" in data2:
  310. data2 += "?series"
  311. img = "https://www.lattelecom.tv"+item["image"].encode("utf8")
  312. desc = "%s\n%s"%(title,item["genre"].encode("utf8"))
  313. content.append((title,self.name+"::"+data2,img,desc))
  314. m = re.search("page=(\d+)",data)
  315. if m:
  316. page = int(m.group(1))
  317. data2 = re.sub("page=\d+","page=%s"%(page+1),data)
  318. content.append(("Next page",self.name+"::"+data2,"","Go to next page"))
  319. return content
  320. ### Sērijas
  321. elif clist=="videonoma" and params=="?series":
  322. url = "https://www.lattelecom.tv/"+path
  323. r = self._http_request(url,headers=self.headers2)
  324. if not r:
  325. return content
  326. m = re.search('<div class="movie_details"><div class="movie_titles"><div class="en">([^<]+?)</div>', r, re.DOTALL | re.IGNORECASE)
  327. raidijums = m.group(1) + " - " if m else ""
  328. m = re.search('data-bid="([^"]+)',r)
  329. if m:
  330. url = "https://www.lattelecom.tv/episode-loader.json?bcast_ids=%s"%(m.group(1))
  331. r = self._http_request(url,headers=self.headers2)
  332. i1 = r.find('{"total":')
  333. i2 = r.find('}<div class=')
  334. js = json.loads(r[i1:i2+1])
  335. for item in js["episodes"]:
  336. title = "%s%s (%s)"%(raidijums,item["title"],item["episode_nr"])
  337. data2 = item["url"][1:]
  338. img = "https://www.lattelecom.tv"+item["img"]
  339. desc = title
  340. content.append((title,self.name+"::"+data2,img,desc))
  341. else:
  342. for item in re.findall('(?si)<a class="elementxxx forward-link[^"]*" href="([^"]+)">.*?<img src="([^"]+)" class="img"><span class="titlez">([^<]+)</span><span class="epizode_number">([^<]+)</span>', r):
  343. title = "%s%s (%s)"%(raidijums,item[2],item[3])
  344. data2 = item[0][1:]
  345. img = "https://www.lattelecom.tv"+item[1]
  346. desc = title
  347. content.append((title,self.name+"::"+data2,img,desc))
  348. return content
  349. ### Videonomas video
  350. elif clist=="videonoma" and len(data.split("/"))>1:
  351. ch = data.split("/")[1]
  352. video_id=data[data.find("/")+1:]
  353. #video_id=data.split("/")[-1]
  354. if not self.is_logedin2():
  355. if not self.login2():
  356. content=("Can not login\nPlease check lattelecom.tv username/password in\n/usr/lib/enigma2/python/Plugins/Extensions/sources/ltc.cfg file","","","No stream found. Please check lattelecom.tv username/password in sources/ltc.cfg file ")
  357. return content
  358. data2 = self.get_noma_url(video_id)
  359. if not data2:
  360. content=("No stream found for '%s'"%data,"","","No stream found")
  361. return content
  362. r = self.call2(data)
  363. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  364. if m:
  365. title = m.group(1)
  366. else:
  367. title = ""
  368. desc = title
  369. content = (title,data2,"",desc)
  370. return content
  371. else:
  372. return content
  373. def get_streams(self, data):
  374. print "[ltc] get_streams:", data
  375. if "::" in data: data = data.split("::")[1]
  376. if not self.is_video(data):
  377. return []
  378. ### Video nomas strīmus pagaidām dabu no mājas lapas TODO
  379. if data.split("/")[0]=="videonoma" and len(data.split("/"))>1:
  380. #video_id=data.split("/")[-1]
  381. streams = self.get_stream_url2(data)
  382. else:
  383. if not self.is_logedin():
  384. if not self.login():
  385. return []
  386. r = self.call(data)
  387. if not r: return []
  388. if "errors" in r:
  389. return []
  390. self.refresh_token()
  391. token = "&auth_token=app_%s"%(self.token)
  392. vid = data.split("/")[2].split("?")[0]
  393. vtype = data.split("/")[1]
  394. if vtype == "live-streams":
  395. ch = self.get_channel_by_id(vid)
  396. offset = "%2B03%3A00" # TODO - vajag aktuālo laika nobidi no GMS
  397. data2 = "tv/epg-live/%s/?offset=%s"%(ch["xprs_id"],offset)
  398. r2 = self.call(data2)
  399. if r2 and r2["items"]:
  400. title = r2["items"][0]["title"].encode("utf8")
  401. t1 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_start"])).strftime('%H:%M')
  402. t2 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_stop"])).strftime('%H:%M')
  403. title = "%s (%s-%s)"%(title,t1,t2)
  404. desc = r2["items"][0]["description"]
  405. else:
  406. title = ch["name"]
  407. desc = title
  408. elif vtype == "record-streams":
  409. epg = self.get_epg_id(vid)
  410. title = epg["title"].encode("utf8")
  411. t1 = datetime.datetime.fromtimestamp(int(epg["unix_start"])).strftime('%H:%M')
  412. t2 = datetime.datetime.fromtimestamp(int(epg["unix_stop"])).strftime('%H:%M')
  413. date = epg["date"]
  414. title = "%s (%s %s-%s)"%(title,date,t1,t2)
  415. desc = epg["description"]
  416. streams = []
  417. for s in r["data"]:
  418. stream = util.item()
  419. stream["url"]=(s["attributes"]["stream-url"]+token.encode("utf8")).encode("utf8")
  420. stream["name"]=title
  421. stream["desc"]=desc
  422. stream["type"]="hls"
  423. m=re.search(".+_(\w\w)_(\w\w)\.\w+",s["id"])
  424. if m:
  425. stream["quality"]=m.group(2).encode("utf8")
  426. stream["lang"]=m.group(1).encode("utf8")
  427. streams.append(stream)
  428. ### TODO - sakārtot sarakstu, lai pirmais ir labakais video
  429. qlist = ["???","lq","mq","hq","hd"]
  430. llist = ["fr","en","ru","lv"]
  431. for s in streams:
  432. lv = llist.index(s["lang"])*10 if s["lang"] in llist else 0
  433. qv=qlist.index(s["quality"]) if s["quality"] in qlist else 0
  434. s["order"] = lv+qv
  435. streams = sorted(streams,key=lambda item: item["order"],reverse=True)
  436. return streams
  437. def is_video(self,data):
  438. if "::" in data:
  439. data = data.split("::")[1]
  440. cmd = data.split("/")
  441. if "get-stream" in data:
  442. return True
  443. elif cmd[0] in ("content") and cmd[1] in ("live-streams","record-streams","vod-streams"):
  444. return True
  445. elif cmd[0]=="arhivs" and len(cmd)==4:
  446. return True
  447. elif cmd[0]=="videonoma" and len(cmd)==3 and not "?" in data:
  448. return True
  449. else:
  450. return False
  451. def get_channels(self):
  452. if self.ch:
  453. return self.ch
  454. if not self.check_logedin():
  455. self.login() # citādi nerāda TV3, LNT, TV6
  456. r= self.call("tv/channels")
  457. self.ch=[]
  458. for i,item in enumerate(r["items"]):
  459. self.ch.append(item)
  460. self.ch_id[item["id"]]=i
  461. self.ch_id2[item["xprs_id"]]=i
  462. self.ch_name[item["name"]]=i
  463. return self.ch
  464. def get_channel_by_id(self,chid):
  465. if not self.ch:
  466. self.get_channels()
  467. if not self.ch:
  468. return None
  469. return self.ch[self.ch_id[chid]] if self.ch_id.has_key(chid) else None
  470. def get_channel_by_id2(self,chid):
  471. if not self.ch:
  472. self.get_channels()
  473. if not self.ch:
  474. return None
  475. return self.ch[self.ch_id2[chid]] if self.ch_id2.has_key(chid) else None
  476. def get_channels2(self):
  477. if self.ch2:
  478. return self.ch2
  479. r= self.call2("tiesraide")
  480. self.ch2=[]
  481. for item in re.findall(r'<div class="entry forward-link.+?data-xprs="(\d+)".+?title="([^"]+)" href="/tiesraide/([^"]+)"', r, re.DOTALL):
  482. ch={}
  483. ch["id2"]=item[0]
  484. ch["name"]=item[2]
  485. ch["title"]=item[1]
  486. self.ch2.append(ch)
  487. return self.ch2
  488. def get_channel_by_name2(self,name):
  489. if not self.ch2:
  490. self.get_channels2()
  491. for c in self.ch2:
  492. if c["name"]==name:
  493. return c
  494. return None
  495. def get_channel_by_name(self,name):
  496. if not self.ch:
  497. self.get_channels()
  498. ch2 = self.get_channel_by_name2(name)
  499. if not ch2:
  500. return None
  501. ch = self.get_channel_by_id2(ch2["id2"])
  502. return ch
  503. def get_epg(self,date=None,update=False):
  504. #https://manstv.lattelecom.tv/api/v1.3/get/tv/epg/?daynight=2016-05-19
  505. today=datetime.date.today()
  506. if not date:
  507. date=datetime.date.today().strftime("%Y-%m-%d")
  508. if update:
  509. self.epg=[]
  510. self.epg_id={}
  511. self.epg_id2={}
  512. self.epg_date={}
  513. slef.epg_ch={}
  514. self.epg_cat={}
  515. if not date in self.epgdates:
  516. r=self.call("tv/epg/?daynight=%s"%date)
  517. for item in r["items"]:
  518. if item["id"] in self.epg_id: continue
  519. else:
  520. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  521. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  522. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  523. self.epg.append(item)
  524. index = self.epg.index(item)
  525. self.epg_id[item["id"]]=index
  526. if not item["date"] in self.epg_date:
  527. self.epg_date[item["date"]]=[]
  528. self.epg_date[item["date"]].append(index)
  529. if not item["channel_id"] in self.epg_ch:
  530. self.epg_ch[item["channel_id"]]=[]
  531. self.epg_ch[item["channel_id"]].append(index)
  532. if not date in self.epgdates:
  533. self.epgdates.append(date)
  534. def get_epg_id(self,epgid):
  535. if not self.epg:
  536. return None
  537. #self.get_epg()
  538. if epgid in self.epg_id:
  539. return self.epg[self.epg_id[epgid]]
  540. return None
  541. def get_epg_date(self,date,chid):
  542. if not date in self.epgdates:
  543. self.get_epg(date)
  544. items = []
  545. ch = self.get_channel_by_id(chid)
  546. for it in self.epg_date[date]:
  547. item = self.epg[it]
  548. if item["channel_id"]==ch["xprs_id"]:
  549. items.append(item)
  550. return items
  551. def call(self, data,params = None, headers=None):
  552. if not headers: headers = self.headers
  553. #if not lang: lang = self.country
  554. url = self.api_url + data
  555. content = self._http_request(url,params, headers)
  556. if content:
  557. try:
  558. result = json.loads(content)
  559. return result
  560. except Exception, ex:
  561. return None
  562. else:
  563. return None
  564. def call2(self, data,params = None, headers=None):
  565. if not headers: headers = self.headers2
  566. #if not lang: lang = self.country
  567. url = self.api_url2 + data
  568. content = self._http_request(url,params, headers)
  569. return content
  570. def _http_request0(self, url,params = None, headers=None):
  571. if not headers: headers = self.headers
  572. try:
  573. r = urllib2.Request(url, data=params, headers=headers)
  574. u = urllib2.urlopen(r)
  575. content = u.read()
  576. u.close()
  577. return content
  578. except Exception as ex:
  579. return None
  580. def login(self,user="",password=""):
  581. """Login in to site, get token"""
  582. self.options_read()
  583. if not user: user=self.options["user"]
  584. if not password: password = self.options["password"]
  585. # Dabūjam tokenu
  586. url = "https://manstv.lattelecom.tv/api/v1.3/post/user/login"
  587. params = "uid=7f777e938d35e017&password=%s&username=%s&"%(password,user)
  588. headers = headers2dict("""
  589. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  590. User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
  591. Host: manstv.lattelecom.tv
  592. """ )
  593. try:
  594. r = urllib2.Request(url, data=params, headers=headers)
  595. u = urllib2.urlopen(r)
  596. content = u.read()
  597. u.close()
  598. except Exception as ex:
  599. return None
  600. #r = self.call(data, params)
  601. if r and "token" in content:
  602. self.token=re.search('"token":"(.+?)"', content).group(1)
  603. return True
  604. else:
  605. return False
  606. def refresh_token(self):
  607. data = "user/refresh-token//%s"%self.token
  608. r = self.call(data)
  609. if 'token' in r:
  610. self.token = r["token"]
  611. return True
  612. else:
  613. self.token = ""
  614. return False
  615. def check_logedin(self):
  616. if not self.token:
  617. return False
  618. else:
  619. if self.refresh_token():
  620. return True
  621. else:
  622. return False
  623. def is_logedin(self):
  624. if self.token:
  625. return True
  626. else:
  627. return False
  628. #----------------------------------------------------------------------
  629. def get_tv_url(self,video_id):
  630. """Get m3u8 url for given live tv channel"""
  631. url = "https://m.lattelecom.tv/tiesraide/%s"%video_id
  632. data = self.get_stream_url2("tiesraide/%s"%video_id)
  633. #headers = self.headers
  634. #headers["Cookie"] = "%s; %s; _hjIncludedInSample=0; MobBitr=1; MobRentBitr=%s; MobRentLang=%s;"%(self.session_id,self.mobtv_cache,data["quality"],data["language"])
  635. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  636. #html = response.read()
  637. #m3u8 = re.search('x-mpegURL" src="(.*?)"', html).group(1) if "x-mpegURL" in html else ""
  638. return data["stream"] if data else None
  639. #----------------------------------------------------------------------
  640. def get_noma_url(self,video_id,quality="hq",language="lv"):
  641. """Get m3u8 url for given rental video"""
  642. video_id1,video_id2=video_id.split("/")
  643. data = self.get_stream_url2(video_id)
  644. #url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=4&video_url=%s&bitrate=%s&lng=%s"%(video_id2,data["quality"],data["language"])
  645. #headers = self.headers
  646. #headers["Cookie"] = "%s; %s; MobRentBitr=%s; MobBitr=1; MobRentLang=%s"%(self.session_id,self.mobtv_cache,quality,language)
  647. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  648. #m3u8 = response.read()
  649. return data["stream"] if data else None
  650. #----------------------------------------------------------------------
  651. def get_arhivs_url(self,video_id):
  652. """Get m3u8 url for given archive video"""
  653. data = self.get_video_data(video_id)
  654. #url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=arhivs&event_id=%s&bitrate=mhq"%(video_id) #,data["quality"])
  655. #headers = self.headers
  656. #headers["Cookie"] = "%s; %s; MobBitr=1; "%(self.session_id,self.mobtv_cache)
  657. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  658. #m3u8 = response.read()
  659. return data["stream"] if data else None
  660. def get_stream_url(self,video_id,type="vod",chid=""):
  661. "Get stream urls (hls) using manstv api"
  662. if type=="vod":
  663. data = "vod/get-stream/%s"%video_id
  664. elif type=="tv":
  665. data = "tv/get-stream?id=%s"%video_id
  666. elif type=="archive":
  667. if not chid: return None
  668. data="archive/get-stream/%s?channelid=%s"%(video_id,chid)
  669. else:
  670. return None
  671. if not self.is_logedin():
  672. if not self.login():
  673. return None
  674. r = self.call(data)
  675. if not r:
  676. return None
  677. self.refresh_token()
  678. token = "&auth_token=app_%s"%(self.token)
  679. hls={}
  680. hls["stream"]=r["stream"]+token
  681. hls["streams"]=[]
  682. if "items" in r:
  683. for s in r["items"]["streams"]:
  684. s["stream"] += token
  685. hls["streams"].append(s)
  686. return hls
  687. def get_stream_url2(self,data):
  688. video_id=data[data.find("/")+1:]
  689. if not self.is_logedin2():
  690. if not self.login2():
  691. return []
  692. #data2 = self.get_noma_url(video_id)
  693. r = self.call2(data)
  694. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  695. if m:
  696. title = m.group(1)
  697. else:
  698. title = ""
  699. desc = title
  700. url = "https://www.lattelecom.tv/xmls/%s.xml"%video_id
  701. headers = self.headers2
  702. headers["Cookie"] = self.session_id
  703. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  704. r = response.read()
  705. servers = re.findall("(?s)<origin>([^<]+)</origin>", r)
  706. streams_xml = re.findall('<stream quality="\w+">(mp4:\w+_(\w\w)_(\w\w).mp4)</stream>',r)
  707. resource_id = re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  708. token = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  709. streams=[]
  710. captions = []
  711. llist = ["fr","en","ru","lv"]
  712. for s in re.findall('<subtitles code="([^"]+)">([^<]+)</subtitles>', r, re.DOTALL):
  713. sub = {}
  714. sub["url"] = s[1]
  715. sub["lang"] = s[0]
  716. sub["name"] = "captions (vtt)"
  717. sub["type"] = "vtt"
  718. sub["order"] = llist.index(sub["lang"])*10 if sub["lang"] in llist else 0
  719. captions.append(sub)
  720. captions = sorted(captions,key=lambda item: item["order"],reverse=True)
  721. for s in streams_xml:
  722. for server in servers:
  723. server2 = self.load_balancer(server)
  724. url = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(server2,s[0],resource_id,token)
  725. stream = util.item()
  726. stream["url"]=url
  727. stream["lang"]=s[1]
  728. stream["quality"]=s[2]
  729. stream["name"]=title
  730. stream["desc"]=desc
  731. stream["type"]="hls" #stream_type(url)
  732. stream["subs"] = []
  733. for c in captions:
  734. c2= c.copy()
  735. #c2["url"] ="http://%s/mobile-vod/%s/%s?resource_id=%s&auth_token=%s"%(server2,s[0],c["url"],resource_id,token)
  736. c2["url"] ="http://%s/mobile-vod/%s/%s"%(server2,s[0],c["url"])
  737. stream["subs"].append(c2)
  738. pass
  739. streams.append(stream)
  740. break # TODO ņem tikai pirmo serveri, varētu pārbaudit, kurš no tiem strādā, kurš ne
  741. return streams
  742. #data = {}
  743. #data["server"] = re.findall("(?s)<origin>([^<]+)</origin>", r)[1]
  744. #data["language"]=re.findall('(?s)<language code="([^"]+)">', r)
  745. #data["language"]="lv" if "lv" in data["language"] else "ru" if "ru" in data["language"] else "en"
  746. #data["qs"]=re.findall('(?s)<stream quality="([^"]+)">([^<]+)</stream>', r)
  747. #data["qs"]=dict(data["qs"])
  748. #qs = data["qs"].keys()
  749. #data["quality"] = "hd" if "hd" in qs else "hq" if "hq" in qs else "mhq" if "mhq" in qs else "lq"
  750. #data["mp4"] = data["qs"][data["quality"]]
  751. #data["token"] = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  752. #data["resource_id"]=re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  753. #data["server"]=self.load_balancer(data["server"])
  754. #data["hls"] = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(data["server"],data["mp4"],data["resource_id"],data["token"])
  755. ##data["hls"]="http://%s/mobile-vod/mp4:%s/playlist.m3u8?resource_id=%s&auth_token=app_%s"%(data["server"],data["mp4"],data["resource_id"],data["token"])
  756. ### wwww.lattelecom.tv izsaukumi
  757. def login2(self,user="",password=""):
  758. """Login in to site, create session cookies"""
  759. if not user: user=self.options["user"]
  760. if not password: password = self.options["password"]
  761. url0 = "https://www.lattelecom.tv"
  762. class NoRedirectHandler(urllib2.HTTPRedirectHandler):
  763. def http_error_302(self, req, fp, code, msg, headers):
  764. infourl = urllib.addinfourl(fp, headers, req.get_full_url())
  765. infourl.status = code
  766. infourl.code = code
  767. return infourl
  768. http_error_300 = http_error_302
  769. http_error_301 = http_error_302
  770. http_error_303 = http_error_302
  771. http_error_307 = http_error_302
  772. # Dabūjam sesijas id un url_gif, kas redirektējas uz auth_url
  773. headers = headers2dict("""
  774. Host: www.lattelecom.tv
  775. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  776. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  777. Accept-Language: en-US,en;q=0.5
  778. DNT: 1
  779. Connection: keep-alive
  780. """)
  781. response = urllib2.urlopen(urllib2.Request(url0, headers=headers))
  782. session_id = response.headers["set-cookie"].split(";")[0]
  783. html = response.read()
  784. url_gif = url0 + re.search('(/auth/\d+\.gif)', html).group(1)
  785. # Dabūtjam auth_url
  786. headers = headers2dict("""
  787. Host: www.lattelecom.tv
  788. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  789. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  790. Accept-Language: en-US,en;q=0.5
  791. DNT: 1
  792. Referer: https://www.lattelecom.tv/
  793. """)
  794. headers["Cookie"] = session_id
  795. urllib2.install_opener(urllib2.build_opener(NoRedirectHandler()))
  796. response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers))
  797. if response.code == 302:
  798. url_auth = response.headers["location"]
  799. else:
  800. self.error = u"auth.gif nenostrādāja"
  801. #raise u"auth.gif nenostrādāja"
  802. return False
  803. # Pierakstāmies iekš auth.lattelecom.lv
  804. headers = headers2dict("""
  805. Host: auth.lattelecom.lv
  806. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  807. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  808. Accept-Language: en-US,en;q=0.5
  809. DNT: 1
  810. Referer: https://www.lattelecom.tv/
  811. """)
  812. response = urllib2.urlopen(urllib2.Request(url_auth, headers=headers))
  813. if not response.code == 302:
  814. self.error = u"pierakstīšanās auth.lattelecom.lv nenostrādāja"
  815. return False
  816. #raise "pierakstīšanās auth.lattelecom.lv nenostrādāja"
  817. # Mēģinam ielogoties
  818. headers = headers2dict("""
  819. Host: www.lattelecom.tv
  820. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  821. Accept: text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
  822. Accept-Language: en-US,en;q=0.5
  823. DNT: 1
  824. X-Requested-With: XMLHttpRequest
  825. Referer: https://www.lattelecom.tv/
  826. """)
  827. headers["Cookie"] = session_id
  828. #data = "login=yes&email=%s&passw=%s"%(user,password)
  829. url = "https://www.lattelecom.tv/login.json?callback=jQuery111303344749731668816_1463817318435&username=%s&password=%s&captcha=&sid=&_="%(user,password)
  830. req = urllib2.Request(url, headers=headers)
  831. response = urllib2.urlopen(req)
  832. #with open("auth.htm","w") as f: f.write(response.read())
  833. if not response.code == 200:
  834. #self.error = u"kļūda ielogojoties"
  835. raise Exception(u"kļūda ielogojoties")
  836. html = response.read()
  837. if not '"success":true' in html:
  838. err = re.search('"error":"(.+?)"',html).group(1) if re.search('"error":"(.+?)"',html) else ""
  839. raise Exception(u"Kļūda ielogojoties - %s"%err.decode("utf8"))
  840. self.session_id = session_id
  841. self.headers2["Cookie"] = "%s; "%(self.session_id)
  842. self.error = ""
  843. return True
  844. def check_logedin2(self):
  845. if not self.session_id:
  846. return False
  847. else:
  848. url = "https://www.lattelecom.tv/profils"
  849. response = urllib2.urlopen(urllib2.Request(url, headers=self.headers2))
  850. if response.code == 200:
  851. return True
  852. else:
  853. self.session_id = ""
  854. return False
  855. def is_logedin2(self):
  856. if self.session_id:
  857. return True
  858. else:
  859. return False
  860. def load_balancer(self,server,streams=[]):
  861. headers = headers2dict("""
  862. Connection: keep-alive
  863. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  864. X-Requested-With: ShockwaveFlash/21.0.0.242
  865. """)
  866. url = "http://%s/loadbalancer"%server
  867. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  868. r = response.read()
  869. statuss = re.search("<status>(\d+)</status>",r).group(1)
  870. edge = re.search("<edge>([^<>]+)</edge>",r).group(1)
  871. return edge
  872. def test_hls(self,url):
  873. headers = headers2dict("""
  874. Connection: keep-alive
  875. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  876. """)
  877. try:
  878. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  879. except Exception as ex:
  880. print "hls failed: %s %s"%(ex.getcode(),url)
  881. return False
  882. if response.code == 200:
  883. html = response.read()
  884. url0 = re.search("(http://[^/]+/)",url).group(1)
  885. chunklist = re.search("(chunklist.+)",html).group(1).strip()
  886. url2 = url.split('/')[:-1]
  887. url2 ="/".join(url2)+"/"+chunklist
  888. try:
  889. response2 = urllib2.urlopen(urllib2.Request(url2, headers=headers))
  890. except Exception as ex:
  891. print "hls chunk failed: %s %s"%(ex.getcode(),url2)
  892. return False
  893. if response2.code == 200:
  894. return True
  895. else:
  896. return False
  897. else:
  898. return False
  899. if __name__ == "__main__":
  900. c = Source()
  901. from subprocess import call
  902. if len(sys.argv)>1 and not "ltc::" in sys.argv[1]:
  903. vid = sys.argv[1]
  904. print "login - %s"%c.login("ivars777","xxx")
  905. vid = "1069"
  906. vid = "1462566072086"
  907. channelid="101"
  908. vid = "1350462656767"
  909. #data = c.get_stream_url(vid,"vod")
  910. #call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",data["stream"]])
  911. pass
  912. print "login2 - %s"%c.login2("ivars777","xxx")
  913. #vid2 = "animation/ultimate_avengers_ii"
  914. #vid2 = "animation/ice_age"
  915. vid2 = "tiesraide/ltv1"
  916. #vid2 = "arhivs/1456521417815"
  917. data = c.get_stream_url2(vid2)
  918. print data
  919. #for s in data:
  920. call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",data["stream"]])
  921. pass
  922. else:
  923. if len(sys.argv)>1:
  924. data= sys.argv[1]
  925. else:
  926. data = "ltc::home"
  927. content = c.get_content(data)
  928. for item in content:
  929. print item
  930. #cat = api.get_categories(country)
  931. #chan = api.get_channels("lv")
  932. #prog = api.get_programs(channel=6400)
  933. #prog = api.get_programs(category=55)
  934. #seas = api.get_seasons(program=6453)
  935. #str = api.get_streams(660243)
  936. #res = api.get_videos(802)
  937. #formats = api.getAllFormats()
  938. #det = api.detailed("1516")
  939. #vid = api.getVideos("13170")
  940. pass