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

ltc.py 61KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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,urlparse
  10. #from xml.sax.saxutils import unescape,escape
  11. from urllib import quote, unquote
  12. import datetime
  13. import HTMLParser
  14. import json
  15. from SourceBase import SourceBase, stream_type
  16. import util
  17. from util import unescape
  18. from collections import OrderedDict
  19. import ssl
  20. if "_create_unverified_context" in dir(ssl):
  21. ssl._create_default_https_context = ssl._create_unverified_context
  22. 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"
  23. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  24. h = HTMLParser.HTMLParser()
  25. class Source(SourceBase):
  26. def __init__(self,cfg_path=None):
  27. self.name = "ltc"
  28. self.title = "Shortcut.lv (lattelecom.tv)"
  29. self.img = "shortcut.png"
  30. self.desc = "Shortcut.lv (lattelecom.tv) satura skatīšanās"
  31. self.token = "" # manstv.lattelecom.tv
  32. self.session_id = "" # www.shortcut.lv
  33. self.api_url = "https://manstv.lattelecom.tv/api/v1.4/get/"
  34. self.api_url2 = "https://www.shortcut.lv/"
  35. self.headers = headers2dict("""
  36. User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
  37. Host: manstv.lattelecom.tv
  38. Connection: Keep-Alive
  39. """)
  40. self.headers2 = headers2dict("""
  41. Connection: keep-alive
  42. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  43. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  44. Accept-Language: en-US,en;q=0.8
  45. """)
  46. self.headers3 = headers2dict("""
  47. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
  48. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  49. X-Requested-With: XMLHttpRequest
  50. """)
  51. self.channels = None
  52. ### specific service info ###
  53. self.ch = []
  54. self.ch2 = []
  55. self.ch_id={}
  56. self.ch_id2={}
  57. self.ch_name={}
  58. self.epg=[]
  59. self.epg_id={}
  60. self.epg_id2={}
  61. self.epg_date={}
  62. self.epg_ch={}
  63. self.epgdates = []
  64. self.today = datetime.date.today()
  65. self.today2 = self.today.strftime("%d.%m.%Y")
  66. cur_directory = os.path.dirname(os.path.abspath(__file__))
  67. if not cfg_path: cfg_path = cur_directory
  68. self.config_file = os.path.join(cfg_path,self.name+".cfg")
  69. self.options = OrderedDict([("user","lietotajs"),("password","parole")])
  70. self.options_read()
  71. def get_content(self, data):
  72. print "[ltc] get_content:", data
  73. source, data, path, plist, clist, params, qs = self.parse_data(data)
  74. content=[]
  75. content.append(("..return", "back","back.png","Return back"))
  76. if clist=="home":
  77. content.extend([
  78. ("TV tiešraides", "ltc::tiesraide","","TV tiešraides"),
  79. ("TV arhīvs", "ltc::arhivs","","TV arhīvs atseviškiem kanāliem"),
  80. ("Bērnu", "ltc::videonoma/bernu","","Videonomas bērnu filmas un seriāli"),
  81. ("Filmas", "ltc::videonoma/filmas","","Videonomas filmas"),
  82. ("Premiere filmas", "ltc::videonoma/premiere","","Videonomas premiere filmas"),
  83. ("Seriāli", "ltc::videonoma/series","","Videonomas seriāli"),
  84. ("360 Play", "ltc::videonoma/360play","","Videonomas lattelecom saturs"), # TODO
  85. #("Videonoma", "ltc::videonoma","","Filmas (arī Priemiere) un seriāli"),
  86. ("Meklēt filmas", "ltc::viss/search/?cat=movies&q={0}","","Meklēt filmu sadaļa"),
  87. ("Meklēt seriālus", "ltc::viss/search/?cat=series&q={0}","", "Meklēt seriālu sadaļā"),
  88. #("Meklēt TV arhīvā", "ltc::viss/search/?cat=archive&q={0}","", "Meklēt TV arhīvā"),
  89. #("Meklēt TV arhīvā", "ltc::search/{0}","","Meklēt TV arhīvā"), #TODO
  90. ])
  91. return content
  92. ### Meklēt TV (1.4 API)
  93. elif clist == "search": #TODO vod/search/{q}
  94. #r = self.call("vod/"+data)
  95. return content
  96. ### Meklēsana video (www )###
  97. elif path == 'viss/search/': #TODO vod/search/{q}
  98. r = self.call2(data)
  99. if "cat=movies" in data:
  100. result = re.findall(r'class="forward-link" href="/([^"]+)/">(<div class="new-movie">(\w+)</div>)*<img src="([^"]+)".+?class="categorie-one-title">([^<]+)</div><div class="categorie-one-subtitle">([^<]+)<', r, re.DOTALL)
  101. elif "cat=series" in data:
  102. result = re.findall(r'class="forward-link" href="/([^"]+)/">(<div class="new-movie">(\w+)</div>)*<img src="([^"]+)".+?class="categorie-one-title">([^<]+)</div><div class="categorie-one-subtitle">([^<]+)<', r, re.DOTALL)
  103. else:
  104. result = re.findall(r'class="forward-link" href="/([^"]+)/">(<div class="new-movie">(\w+)</div>)*<img src="([^"]+)".+?class="categorie-one-title">([^<]+)</div><div class="categorie-one-subtitle">([^<]+)<', r, re.DOTALL)
  105. for item in result:
  106. title1 = item[4]
  107. title2 = item[5]
  108. if title1 <> title2:
  109. title = "%s ~ %s" % (title1, title2)
  110. if item[1]:
  111. title = title + "[J]" if item[2] == "Jaunums" else title + "[P]"
  112. data2 = item[0]
  113. if re.search("e\d+$", data2):
  114. data2 += "?series"
  115. img = "https://www.shortcut.lv" + item[3]
  116. desc = title
  117. content.append((title,self.name+"::"+data2,img,desc))
  118. return content
  119. ### Tiešraides kanānālu saraksts ###
  120. elif data=="tiesraide":
  121. #r = self.call2(data)
  122. #m = re.search("var xprs_ides_array = (.+);", r, re.MULTILINE)
  123. #if m:
  124. # js=json.loads(m.group(1))
  125. #else:
  126. # raise Exception("Error reading channel list")
  127. r = self.call2("program-snippet.json")
  128. try:
  129. js = json.loads(r)
  130. except:
  131. js = {}
  132. for item in self.get_channels():
  133. if item["live"]=='0':continue
  134. title = item["name"]
  135. data2 = "content/live-streams/%s?include=quality"%item["id"]
  136. if item["xprs_id"] in js:
  137. epg = js[item["xprs_id"]]
  138. desc = "%s - %s (%s-%s)"%(title,epg[0]["t"],epg[0]["hs"],epg[0]["he"])
  139. title = desc
  140. img = "https://www.shortcut.lv" + epg[0]["p"]
  141. #img = "https://manstv.shortcut.lv/"+ item['broadcast_default_picture']
  142. else:
  143. img = "https://manstv.shortcut.lv/"+ 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)"%(item["title"], item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"))
  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-%s\n%s\n%s"%(ch_name,item["title"], item["date"], item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"),
  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,"next.png","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. if item["time_stop2"] > datetime.datetime.now(): continue
  220. ch = self.get_channel_by_id2(item["channel_id"])
  221. ch_name = ch["name"] if ch else item["channel_id"]
  222. #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"])
  223. title = u"%s (%s-%s)"%(item["title"], item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"))
  224. #data2 = "archive/get-stream/%s?channelid=%s"%(item["id"],ch["id"])
  225. data2 = "content/record-streams/%s?include=quality"%item["id"]
  226. img = "https://manstv.lattelecom.tv/"+ item["url"]
  227. desc = u"%s - %s\n%s %s-%s\n%s\n%s"%(ch_name,item["title"], item["date"], item["time_start2"].strftime("%H:%M"),item["time_stop2"].strftime("%H:%M"),
  228. item["category1"],item["description"])
  229. content.append((title,self.name+"::"+data2,img,desc))
  230. #date2=datetime.datetime.strptime(qs["date"], '%Y-%m-%d').date()-datetime.timedelta(days=1)
  231. date2 = datetime.date(*tuple(map(int, qs["date"].split("-")))) - datetime.timedelta(days=1)
  232. date2 = date2.strftime("%Y-%m-%d")
  233. if "date=" in data:
  234. data2 = re.sub("date=[\d-]+","date="+date2,data)
  235. else:
  236. data2 = data + "&date=%s"%date2
  237. content.append(("Previous day (%s)"%date2,self.name+"::"+data2,"","Go previous day"))
  238. return content
  239. ### Arhīva datumi
  240. elif clist=="arhivs" and len(data.split("/"))==2: # TODO - pasreiz nestrada
  241. ch = data.split("/")[1]
  242. r= self.call(data)
  243. m = re.search('class="spac no_select">([^<]+)</span>', r, re.IGNORECASE)
  244. if m:
  245. ch_name = m.group(1)
  246. else:
  247. ch_name = ""
  248. today = datetime.date.today()
  249. for i in range(7):
  250. date = today-datetime.timedelta(i)
  251. title = ch_name + " - " + date.strftime("%d.%m.%Y")
  252. data2 = "%s/%s"%(data,date.strftime("%Y-%m-%d"))
  253. img = ""
  254. desc = title
  255. content.append((title,self.name+"::"+data2,img,desc))
  256. return content
  257. ### Arhīva dienas raidijumi
  258. elif clist=="arhivs" and len(data.split("/"))==3: # TODO - nestrādā
  259. ch = data.split("/")[1]
  260. date = data.split("/")[2]
  261. r= self.call(data)
  262. m = re.search('class="spac no_select">([^<]+)</span>', r, re.IGNORECASE)
  263. if m:
  264. ch_name = m.group(1)
  265. else:
  266. ch_name = ""
  267. 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):
  268. title = "%s - %s"%(item[1],item[2])
  269. data2 = item[0]
  270. img = ""
  271. desc = "%s (%s)\n%s"%(ch_name,date,title)
  272. content.append((title,self.name+"::"+data2,img,desc))
  273. return content
  274. ### Videonoma galvenā (vecā, vairs nestrada filtri)
  275. elif data=="videonoma":
  276. content.extend([
  277. ("Filmas - jaunākās", "ltc::videonoma?page=0&genre=all_movies&sorts=laiks&cnt=40&clear=true&filter={}","https://www.shortcut.lv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Jaunākās filmas"),
  278. ("Filmas - pēc nosaukuma", "ltc::videonoma?page=0&genre=all_movies&sorts=title&cnt=40&clear=true&filter={}","https://www.shortcut.lv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas pēc nosaukuma"),
  279. ("Filmas - latviski", 'ltc::videonoma?page=0&genre=all_movies&sorts=laiks&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.shortcut.lv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas latviešu valodā"),
  280. ("Filmas - pēc reitinga", "ltc::videonoma?page=0&genre=all_movies&sorts=imbd&cnt=40&clear=true&filter={}","https://www.shortcut.lv/media/features/2013-12-17/aa3b_videonoma_dropdown_filmas.png","Filmas pēc IMBD reitinga"),
  281. ("Filmas bērniem - jaunākās", "ltc::videonoma?page=0&genre=3&sorts=laiks&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_berniem.png","Jaunākie bernu video"),
  282. ("Filmas bērniem - pēc nosaukuma", "ltc::videonoma?page=0&genre=3&sorts=title&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video pēc nosaukuma"),
  283. ("Filmas bērniem - latviski", 'ltc::videonoma?page=0&genre=3&sorts=laiks&cnt=40&clear=true&filter={"valoda":[\"lv\"]}',"https://www.shortcut.lv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video latviski"),
  284. ("Filmas bērniem - pēc reitinga", "ltc::videonoma?page=0&genre=3&sorts=imbd&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_berniem.png","Bērnu video pēc IMBD reitinga"),
  285. ("Sērijas - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Jaunākās sērijas"),
  286. ("Sērijas - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas pēc pēc nosaukuma"),
  287. ("Sērijas animācijas - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={\"zanrs\":[\"201\"]}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  288. ("Sērijas animācijas - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={\"zanrs\":[\"201\"]}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  289. ("Sērijas ģimenes - jaunākās", "ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={\"zanrs\":[\"195\"]}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  290. ("Sērijas ģimenes - pēc nosaukuma", "ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={\"zanrs\":[\"195\"]}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Animācijas serijas"),
  291. ("Sērijas latviski - jaunākās", 'ltc::videonoma?page=0&genre=27&sorts=laiks&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas latviski"),
  292. ("Sērijas latviski - pēc nosaukuma", 'ltc::videonoma?page=0&genre=27&sorts=title&cnt=40&clear=true&filter={"valoda":["lv"]}',"https://www.shortcut.lv/images/redesign/videonoma_dropdown_raidijumi.png","Sērijas latviski"),
  293. ("Koncerti - jaunākie", "ltc::videonoma?page=0&genre=19&sorts=laiks&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_koncerti.png","Jaunākie koncerti"),
  294. ("Koncerti - pēc nosaukuma", "ltc::videonoma?page=0&genre=19&sorts=title&cnt=40&clear=true&filter={}","https://www.shortcut.lv/images/redesign/videonoma_dropdown_koncerti.png","Koncerti pēc pēc nosaukuma"),
  295. ])
  296. return content
  297. ### Videonomas saraksti (vecie, vairs nestrādā filtri)
  298. elif path == "videonoma":
  299. url = "https://www.shortcut.lv/movies-snippet.json"+params
  300. r = self._http_request(url,headers=self.headers2)
  301. if not r:
  302. return content
  303. js = json.loads(r,"utf8")
  304. if not r:
  305. return content
  306. for item in js["movies"]:
  307. if not item["title"]:
  308. continue
  309. title = item["title"].encode("utf8")
  310. data2 = item["url"][1:].encode("utf8")
  311. if data2[-1]=="/": data2=data2[:-1]
  312. if "/raidijumi/" in data2:
  313. data2 += "?series" # TODO
  314. img = "https://www.shortcut.lv"+item["image"].encode("utf8")
  315. desc = "%s\n%s"%(title,item["genre"].encode("utf8"))
  316. if "is_premium" in item and item["is_premium"]:
  317. title = title + " [P]"
  318. desc = desc + "\nPremium"
  319. content.append((title,self.name+"::"+data2,img,desc))
  320. m = re.search("page=(\d+)",data)
  321. if m:
  322. page = int(m.group(1))
  323. data2 = re.sub("page=\d+","page=%s"%(page+1),data)
  324. content.append(("Next page",self.name+"::"+data2,"next.png","Go to next page"))
  325. return content
  326. ### Filmas galvenā ###
  327. elif data == "videonoma/filmas":
  328. content.extend([
  329. ("Aktuālās", "ltc::viss/actual_films","","Nesen pievienotas filmas"),
  330. ("Komēdijas", "ltc::viss/comedy","","Komēdijas"),
  331. ("Detektīvi", "ltc::viss/crime","","Detektivi"),
  332. ("Ģimenes", "ltc::viss/family","","Ģimenes filmas"),
  333. ("Asa sižeta", "ltc::viss/action","","Asa sižeta filmas"),
  334. ("Romantika", "ltc::viss/romance","","Romantiskās filmas"),
  335. ("Piedzīvojumu", "ltc::viss/adventure","","Piedzivojumu filmas"),
  336. ("Supervaroņi", "ltc::viss/superheros","","Supervaroņu filmas"),
  337. ("Fantastika", "ltc::viss/scifi","","Fantastikas filmas"),
  338. ("Dokumentalās", "ltc::viss/documentaries","","Dokumentalās filmas"),
  339. ("Šausmu", "ltc::viss/horror","","Šausmu filmas"),
  340. ("Retro", "ltc::viss/retro_films","","Retro filmas"),
  341. ("Koncerti un karaoke", "ltc::viss/concerts_karaoke","","Koncerti un filmas"),
  342. ])
  343. ### Sērijas galvenā ###
  344. elif data == "videonoma/series":
  345. content.extend([
  346. ("Aktuālie seriāli", "ltc::viss/actual_series?series","","Nesen pievienotie seriāli"),
  347. ("Aktuālie šovi", "ltc::viss/actual_series?series","","Nesen pievienotie TV šovi"),
  348. ("Populārākie seriāli", "ltc::viss/popular_series?series","","Populārākie seriāli"),
  349. ("Dokumentalie seriāli", "ltc::viss/documentary?series","","Dokumentalie seriāli"),
  350. ("Izklaides pārraides", "ltc::viss/entertainment?series","","Izklaides pārraides"),
  351. ("Detektivseriāli", "ltc::viss/Crime_series?series","","Detektivseriāli"),
  352. ("Dramas seriāli", "ltc::viss/drama_series?series","","Dramas seriāli"),
  353. ("Par dabu", "ltc::viss/nature?series","","Seriāli par dabu"),
  354. ("Gardēžiem", "ltc::viss/gourmets?series","","Seriāli gardēžiem"),
  355. ("Mistērija", "ltc::viss/mystery_series?series","","Mistērijas seriāli"),
  356. ("Oriģinālsaturs", "ltc::viss/originals_series?series","","Oriģinālsatura seriāli"),
  357. ("Klasika", "ltc::viss/classic?series","","Klasika"),
  358. ])
  359. ### Bērnu galvenā ###
  360. elif data == "videonoma/bernu":
  361. content.extend([
  362. ("Nesen pievienotais", "ltc::viss/actual_kid","","Nesen pievienotais bernu saturs"),
  363. ("Bērnu Premiere", "ltc::viss/kids_premiere","","Bērnu Premiere saturs"),
  364. ("Multfilmas", "ltc::viss/animation2","","Multfilmas"),
  365. ("Bērnu seriāli", "ltc::viss/kids_series?series","","Bērnu seriāli"),
  366. ("Ģimenes filmas", "ltc::viss/family","","Ģimenes filmas"),
  367. ("Multfilmas latviski", "ltc::viss/animation_lv","","Multfilmas latviski"),
  368. ("Retro multfilmas", "ltc::viss/retro_animation","","Retro multfilmas"),
  369. ("Latviešu multfilmas", "ltc::viss/animation","","Latviešu multfilmas"),
  370. ("Raidijumi berniem", "ltc::viss/kids_events","","Raidijumi berniem"),
  371. ])
  372. ### Filmas premiere galvenā ###
  373. elif data == "videonoma/premiere":
  374. content.extend([
  375. ("Aktuālās Premiere filmas", "ltc::viss/actual_premiere","","Nesen pievienotās Premiere filmas"),
  376. ("Tikko no kinoteātra", "ltc::viss/just_from_cinema","","Filmas tikko no kinoteātra"),
  377. ("Bērnu Premiere", "ltc::viss/kids_premiere","","Bērnu Premiere saturs"),
  378. ("Komēdijas Premiere", "ltc::viss/comedy_premiere","","Premiere komēdijas filmas"),
  379. ("Detektivi Premiere", "ltc::viss/crime_premiere","","Premiere detektivfilmas"),
  380. ("Piedzīvojumu Premiere", "ltc::viss/adventure_premiere","","Premiere piedzivojumu filmas"),
  381. ("Fantastika Premiere", "ltc::viss/scifi_premiere","","Premiere fantastikas filmas"),
  382. ("Šausmu Premiere", "ltc::viss/horror_premiere","","Premiere šausmu filmas"),
  383. ])
  384. ### 360 Play galvenā ###
  385. elif data == "videonoma/360play":
  386. content.extend([
  387. ("Viss", "ltc::viss/bezmaksas?series","","Viss bezmaksas saturs"),
  388. ("360TV", "ltc::viss/360_free?series","","360TV bezmaksas saturs"),
  389. ("Red Bull", "ltc::viss/Red_bull?series","","Red Bull bezmaksas saturs"),
  390. ])
  391. r = self.call
  392. ### Videonomas saraksti ###
  393. elif plist[0] == "viss" or "load_more" in data:
  394. #content.append(("Videonomas saraksts", "", "", ""))
  395. if "viss" in data:
  396. r = self.call2(data)
  397. m = re.search('(<div class="container categorie">.+?)</div><div class="loader off">', r, re.DOTALL)
  398. html = m.group(1) if m else ""
  399. else:
  400. url = self.api_url2 + path
  401. params = data.split("?")[1]
  402. r = self._http_request(url, params, headers=self.headers3 )
  403. js = json.loads(r)
  404. html = js["data"] if "data" in r else ""
  405. result = re.findall("<div title=.+?</a>", html, re.DOTALL)
  406. for it in result:
  407. title1 = re.search('<div class="categorie-one-title">(.+?)<', it, re.DOTALL).group(1)
  408. title2 = re.search('<div class="categorie-one-subtitle">(.+?)<', it, re.DOTALL).group(1)
  409. if title1 <> title2:
  410. title = "%s ~ %s" % (title1, title2)
  411. else:
  412. title = title1
  413. img = self.api_url2 + re.search('img src="(.+?)"', it).group(1)
  414. data2 = re.search('href="/(.+?)/"', it).group(1)
  415. if "?series" in data or re.search("e\d+$", data2):
  416. data2 += "?series"
  417. if "Jaunums" in it:
  418. title += " [J]"
  419. if "Premiere" in it:
  420. title += " [P]"
  421. desc = title
  422. content.append((title,self.name+"::"+data2,img,desc))
  423. if not "end-reached-box" in r:
  424. if "load_more" in data:
  425. page = int(qs["pack"])
  426. data2 = re.sub("pack=\d+","pack=%s"%(page+1),data)
  427. else:
  428. section = re.search('data-section="(\d+)"', r).group(1)
  429. data2 = "load_more.json?pack=1&section=%s" % section
  430. content.append(("Next page",self.name+"::"+data2,"next.png","Go to next page"))
  431. return content
  432. ### Sērijas
  433. elif clist=="videonoma" and (params=="?series" or "season_nr" in qs):
  434. url = "https://www.shortcut.lv/"+path
  435. r = self._http_request(url,headers=self.headers2)
  436. if not r:
  437. return content
  438. m = re.search(r'data-movieid=\d+><div class="lv">([^<]+)</div><div[^>]+>([^<]+)<', r)
  439. if m:
  440. title1 = unescape(m.group(1))
  441. title2 = unescape(m.group(2))
  442. else:
  443. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  444. title1 = title2 = unescape(m.group(1)) if m else "Video"
  445. if 'episode_switcher_title' in r:
  446. set1 = title1.replace("\xe2\x80\x93", "-")
  447. if " - " in set1:
  448. set1 = set1.split(" - ")[0]
  449. set2 = title2.replace("\xe2\x80\x93", "-")
  450. if " - " in set2:
  451. set2 = set2.split(" - ")[0]
  452. if set1 <> set2:
  453. raidijums = "%s ~ %s" % (set1, set2)
  454. else:
  455. raidijums = set1
  456. else:
  457. m = re.search('<div class="movie_titles"><div class="en">([^<]+)</div>', r)
  458. raidijums = m.group(1) if m else "Series"
  459. img0 = re.search('<meta name="og:image" content="([^"]+)">', r).group(1) if re.search('<meta name="dr:say:img" content="([^"]+)">', r) else ""
  460. m = re.search('season_choice', r)
  461. # Ir sezonas
  462. if m:
  463. result = re.findall(r"""season_choice\(.+?'epizode','([^']*)',[^,]+,'(\d+)',[^,]+,[^,]+\)">([^<]+)<""", r, re.MULTILINE)
  464. if not result:
  465. raise Exception("No seasons find!")
  466. vid = result[0][0]
  467. if not "season_nr" in qs: # Sezonu saraksts
  468. for s,it in enumerate(result):
  469. title = "%s - %s" % (raidijums, it[2])
  470. data2 = path+"?season_nr=%s"%(it[1])
  471. img = img0
  472. desc = title
  473. content.append((title, self.name + "::" + data2, img, desc))
  474. #return content
  475. else: # Sezonas epizožu saraksts
  476. if not "season_nr" in qs:
  477. qs["season_nr"]="0"
  478. # https://www.shortcut.lv/api/episode-loader-design17/0?chunk_size=4&sorting=epizode&series_id=dora_the_explorer
  479. # https://www.shortcut.lv/api/episode-loader-design17/3?chunk_size=4&sorting=epizode&series_id=dora_the_explorer&movie_id=10161&z360tv=&is_active_season=false HTTP/1.1
  480. # https://www.shortcut.lv/api/episode-loader-design17/3?chunk_size=4&sorting=epizode&series_id=dora_the_explorer
  481. url = "https://www.shortcut.lv/api/episode-loader-design17/%s?chunk_size=4&sorting=epizode&series_id=%s" % (qs["season_nr"], vid)
  482. r = self._http_request(url,headers=self.headers2)
  483. try:
  484. js = json.loads(r)
  485. except:
  486. raise Exception("Error getting episode list")
  487. result = re.findall(r'class="" href="([^"]+)".+?image:url\(([^ \)]+)\); "></div><div class="episode_titlez_design17">([^<]+)</div><div class="episode_number_design17">([^<]+)</div></a>', js["data"])
  488. for item in result:
  489. title = "%s - %s (%s)"%(raidijums,item[2],item[3])
  490. data2 = item[0][1:]
  491. img = "https://www.shortcut.lv"+item[1]
  492. desc = title
  493. content.append((title,self.name+"::"+data2,img,desc))
  494. #return content
  495. # Nav sezonu
  496. else:
  497. result = re.findall(r'class="" href="([^"]+)".+?image:url\(([^ \)]+)\).+?class="episode_titlez_design17">([^<]+)</div><div class="episode_number_design17">([^<]+)</div></a>', r)
  498. for item in result:
  499. title = "%s - %s(%s)"%(raidijums,item[2],item[3])
  500. data2 = item[0][1:]
  501. img = "https://www.shortcut.lv"+item[1]
  502. desc = title
  503. content.append((title,self.name+"::"+data2,img,desc))
  504. #return content
  505. if not result: # nav ne epizožu, atgriež pats sevi bez ?series
  506. title = desc = raidijums
  507. img = img0
  508. data2 = path
  509. content.append((title,self.name+"::"+data2,img,desc))
  510. return content
  511. ### Videonomas video
  512. elif clist=="videonoma" and len(data.split("/"))> 2:
  513. ch = data.split("/")[1]
  514. video_id=data[data.find("/")+1:]
  515. #video_id=data.split("/")[-1]
  516. if not self.is_logedin2():
  517. if not self.login2():
  518. 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 ")
  519. return content
  520. data2 = self.get_noma_url(video_id)
  521. if not data2:
  522. content=("No stream found for '%s'"%data,"","","No stream found")
  523. return content
  524. r = self.call2(data)
  525. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  526. if m:
  527. title = m.group(1)
  528. else:
  529. title = ""
  530. desc = title
  531. content = (title,data2,"",desc)
  532. return content
  533. return content
  534. def get_streams(self, data):
  535. print "[ltc] get_streams:", data
  536. if "::" in data: data = data.split("::")[1]
  537. if not self.is_video(data):
  538. return []
  539. ### Video nomas strīmus pagaidām dabu no mājas lapas TODO
  540. if data.split("/")[0]=="videonoma" and len(data.split("/"))>1:
  541. #video_id=data.split("/")[-1]
  542. streams = self.get_stream_url2(data)
  543. else:
  544. if not self.is_logedin():
  545. if not self.login():
  546. return []
  547. r = self.call(data)
  548. if not r: return []
  549. if "errors" in r:
  550. return []
  551. self.refresh_token()
  552. token = "&auth_token=app_%s"%(self.token)
  553. vid = data.split("/")[2].split("?")[0]
  554. vtype = data.split("/")[1]
  555. if vtype == "live-streams":
  556. ch = self.get_channel_by_id(vid)
  557. offset = "%2B03%3A00" # TODO - vajag aktuālo laika nobidi no GMS
  558. data2 = "tv/epg-live/%s/?offset=%s"%(ch["xprs_id"],offset)
  559. r2 = self.call(data2)
  560. if r2 and r2["items"]:
  561. title = r2["items"][0]["title"].encode("utf8")
  562. t1 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_start"])).strftime('%H:%M')
  563. t2 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_stop"])).strftime('%H:%M')
  564. title = "%s (%s-%s)"%(title,t1,t2)
  565. img = "https://manstv.lattelecom.tv/cache/" + r2["items"][0]["url"]
  566. desc = r2["items"][0]["description"]
  567. else:
  568. title = ch["name"]
  569. desc = title
  570. elif vtype == "record-streams":
  571. epg = self.get_epg_id(vid)
  572. if epg:
  573. title = epg["title"].encode("utf8")
  574. t1 = datetime.datetime.fromtimestamp(int(epg["unix_start"])).strftime('%H:%M')
  575. t2 = datetime.datetime.fromtimestamp(int(epg["unix_stop"])).strftime('%H:%M')
  576. date = epg["date"]
  577. title = "%s (%s %s-%s)"%(title,date,t1,t2)
  578. desc = epg["description"]
  579. else:
  580. title = desc = data
  581. streams = []
  582. for s in r["data"]:
  583. stream = util.item()
  584. stream["url"]=(s["attributes"]["stream-url"]+token.encode("utf8")).encode("utf8")
  585. stream["name"]=title
  586. stream["desc"]=desc
  587. stream["type"]="hls"
  588. m=re.search(".+_(\w\w)_(\w\w)\.\w+",s["id"])
  589. if m:
  590. stream["quality"]=m.group(2).encode("utf8")
  591. stream["lang"]=m.group(1).encode("utf8")
  592. streams.append(stream)
  593. ### TODO - sakārtot sarakstu, lai pirmais ir labakais video
  594. qlist = ["???","lq","mq","hq","hd"]
  595. llist = ["fr","en","ru","lv"]
  596. for s in streams:
  597. lv = llist.index(s["lang"])*10 if s["lang"] in llist else 0
  598. qv=qlist.index(s["quality"]) if s["quality"] in qlist else 0
  599. s["order"] = lv+qv
  600. streams = sorted(streams,key=lambda item: item["order"],reverse=True)
  601. return streams
  602. def is_video(self,data):
  603. if "::" in data:
  604. data = data.split("::")[1]
  605. cmd = data.split("/")
  606. if "get-stream" in data:
  607. return True
  608. elif cmd[0] in ("content") and cmd[1] in ("live-streams","record-streams","vod-streams"):
  609. return True
  610. elif cmd[0]=="arhivs" and len(cmd)==4:
  611. return True
  612. elif cmd[0]=="videonoma" and len(cmd) >= 3 and not "?" in data:
  613. return True
  614. else:
  615. return False
  616. def get_channels(self):
  617. if self.ch:
  618. return self.ch
  619. if not self.check_logedin():
  620. self.login() # citādi nerāda TV3, LNT, TV6
  621. r= self.call("tv/channels")
  622. self.ch=[]
  623. for i,item in enumerate(r["items"]):
  624. self.ch.append(item)
  625. self.ch_id[item["id"]]=i
  626. self.ch_id2[item["xprs_id"]]=i
  627. self.ch_name[item["name"]]=i
  628. return self.ch
  629. def get_channel_by_id(self,chid):
  630. if not self.ch:
  631. self.get_channels()
  632. if not self.ch:
  633. return None
  634. return self.ch[self.ch_id[chid]] if self.ch_id.has_key(chid) else None
  635. def get_channel_by_id2(self,chid):
  636. if not self.ch:
  637. self.get_channels()
  638. if not self.ch:
  639. return None
  640. return self.ch[self.ch_id2[chid]] if self.ch_id2.has_key(chid) else None
  641. def get_channels2(self):
  642. if self.ch2:
  643. return self.ch2
  644. r= self.call2("tiesraide")
  645. self.ch2=[]
  646. for item in re.findall(r'<div class="entry forward-link.+?data-xprs="(\d+)".+?title="([^"]+)" href="/tiesraide/([^"]+)"', r, re.DOTALL):
  647. ch={}
  648. ch["id2"]=item[0]
  649. ch["name"]=item[2]
  650. ch["title"]=item[1]
  651. self.ch2.append(ch)
  652. return self.ch2
  653. def get_channel_by_name2(self,name):
  654. if not self.ch2:
  655. self.get_channels2()
  656. for c in self.ch2:
  657. if c["name"]==name:
  658. return c
  659. return None
  660. def get_channel_by_name(self,name):
  661. if not self.ch:
  662. self.get_channels()
  663. ch2 = self.get_channel_by_name2(name)
  664. if not ch2:
  665. return None
  666. ch = self.get_channel_by_id2(ch2["id2"])
  667. return ch
  668. def get_epg(self,date=None,update=False):
  669. #https://manstv.lattelecom.tv/api/v1.3/get/tv/epg/?daynight=2016-05-19
  670. today=datetime.date.today()
  671. if not date:
  672. date=datetime.date.today().strftime("%Y-%m-%d")
  673. if update:
  674. self.epg=[]
  675. self.epg_id={}
  676. self.epg_id2={}
  677. self.epg_date={}
  678. self.epg_ch={}
  679. self.epg_cat={}
  680. if not date in self.epgdates:
  681. r=self.call("tv/epg/?daynight=%s"%date)
  682. for item in r["items"]:
  683. if item["id"] in self.epg_id: continue
  684. else:
  685. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  686. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  687. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  688. self.epg.append(item)
  689. index = self.epg.index(item)
  690. self.epg_id[item["id"]]=index
  691. if not item["date"] in self.epg_date:
  692. self.epg_date[item["date"]]=[]
  693. self.epg_date[item["date"]].append(index)
  694. if not item["channel_id"] in self.epg_ch:
  695. self.epg_ch[item["channel_id"]]=[]
  696. self.epg_ch[item["channel_id"]].append(index)
  697. if not date in self.epgdates:
  698. self.epgdates.append(date)
  699. def get_epg_id(self,epgid):
  700. if self.epg and epgid in self.epg_id:
  701. return self.epg[self.epg_id[epgid]]
  702. else:
  703. r=self.call("content/epgs/%s" % epgid)
  704. if not r:
  705. return None
  706. item = r["data"][0]["attributes"]
  707. item["unix_start"] = item["unix-start"]
  708. item["unix_stop"] = item["unix-stop"]
  709. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  710. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  711. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  712. return item
  713. def get_epg_date(self,date,chid):
  714. if not date in self.epgdates:
  715. self.get_epg(date)
  716. items = []
  717. ch = self.get_channel_by_id(chid)
  718. for it in self.epg_date[date]:
  719. item = self.epg[it]
  720. if item["channel_id"]==ch["xprs_id"]:
  721. items.append(item)
  722. return items
  723. def call(self, data,params = None, headers=None):
  724. if not headers: headers = self.headers
  725. #if not lang: lang = self.country
  726. url = self.api_url + data
  727. content = self._http_request(url,params, headers)
  728. if content:
  729. try:
  730. result = json.loads(content)
  731. return result
  732. except Exception, ex:
  733. return None
  734. else:
  735. return None
  736. def call2(self, data,params = None, headers=None):
  737. if not headers: headers = self.headers2
  738. #if not lang: lang = self.country
  739. url = self.api_url2 + data
  740. content = self._http_request(url,params, headers)
  741. return content
  742. def _http_request0(self, url,params = None, headers=None):
  743. if not headers: headers = self.headers
  744. try:
  745. r = urllib2.Request(url, data=params, headers=headers)
  746. u = urllib2.urlopen(r)
  747. content = u.read()
  748. u.close()
  749. return content
  750. except Exception as ex:
  751. return None
  752. def login(self,user="",password=""):
  753. """Login in to site, get token"""
  754. self.options_read()
  755. if not user: user=self.options["user"]
  756. if not password: password = self.options["password"]
  757. # Dabūjam tokenu
  758. url = "https://manstv.lattelecom.tv/api/v1.5/post/user/users/%s" % user
  759. #url = "https://manstv.lattelecom.tv/api/v1.5/post/user/login"
  760. params = "uid=7f777e938d35e017&password=%s&" % (password)
  761. #params = "uid=7f777e938d35e017&password=%s&username=%s&"%(password,user)
  762. headers = headers2dict("""
  763. User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
  764. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  765. Host: manstv.lattelecom.tv
  766. """ )
  767. try:
  768. r = urllib2.Request(url, data=params, headers=headers)
  769. u = urllib2.urlopen(r)
  770. content = u.read()
  771. u.close()
  772. except Exception as ex:
  773. return None
  774. #r = self.call(data, params)
  775. if r and "token" in content:
  776. self.token=re.search('"token":"(.+?)"', content).group(1)
  777. return True
  778. else:
  779. return False
  780. def refresh_token(self):
  781. data = "user/refresh-token//%s"%self.token
  782. r = self.call(data)
  783. if 'token' in r:
  784. self.token = r["token"]
  785. return True
  786. else:
  787. self.token = ""
  788. return False
  789. def check_logedin(self):
  790. if not self.token:
  791. return False
  792. else:
  793. if self.refresh_token():
  794. return True
  795. else:
  796. return False
  797. def is_logedin(self):
  798. if self.token:
  799. return True
  800. else:
  801. return False
  802. #----------------------------------------------------------------------
  803. def get_tv_url(self,video_id):
  804. """Get m3u8 url for given live tv channel"""
  805. url = "https://m.lattelecom.tv/tiesraide/%s"%video_id
  806. data = self.get_stream_url2("tiesraide/%s"%video_id)
  807. #headers = self.headers
  808. #headers["Cookie"] = "%s; %s; _hjIncludedInSample=0; MobBitr=1; MobRentBitr=%s; MobRentLang=%s;"%(self.session_id,self.mobtv_cache,data["quality"],data["language"])
  809. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  810. #html = response.read()
  811. #m3u8 = re.search('x-mpegURL" src="(.*?)"', html).group(1) if "x-mpegURL" in html else ""
  812. return data["stream"] if data else None
  813. #----------------------------------------------------------------------
  814. def get_noma_url(self,video_id,quality="hq",language="lv"):
  815. """Get m3u8 url for given rental video"""
  816. video_id1,video_id2=video_id.split("/")
  817. data = self.get_stream_url2(video_id)
  818. #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"])
  819. #headers = self.headers
  820. #headers["Cookie"] = "%s; %s; MobRentBitr=%s; MobBitr=1; MobRentLang=%s"%(self.session_id,self.mobtv_cache,quality,language)
  821. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  822. #m3u8 = response.read()
  823. return data["stream"] if data else None
  824. #----------------------------------------------------------------------
  825. def get_arhivs_url(self,video_id):
  826. """Get m3u8 url for given archive video"""
  827. data = self.get_video_data(video_id)
  828. #url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=arhivs&event_id=%s&bitrate=mhq"%(video_id) #,data["quality"])
  829. #headers = self.headers
  830. #headers["Cookie"] = "%s; %s; MobBitr=1; "%(self.session_id,self.mobtv_cache)
  831. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  832. #m3u8 = response.read()
  833. return data["stream"] if data else None
  834. def get_stream_url(self,video_id,type="vod",chid=""):
  835. "Get stream urls (hls) using manstv api"
  836. if type=="vod":
  837. data = "vod/get-stream/%s"%video_id
  838. elif type=="tv":
  839. data = "tv/get-stream?id=%s"%video_id
  840. elif type=="archive":
  841. if not chid: return None
  842. data="archive/get-stream/%s?channelid=%s"%(video_id,chid)
  843. else:
  844. return None
  845. if not self.is_logedin():
  846. if not self.login():
  847. return None
  848. r = self.call(data)
  849. if not r:
  850. return None
  851. self.refresh_token()
  852. token = "&auth_token=app_%s"%(self.token)
  853. hls={}
  854. hls["stream"]=r["stream"]+token
  855. hls["streams"]=[]
  856. if "items" in r:
  857. for s in r["items"]["streams"]:
  858. s["stream"] += token
  859. hls["streams"].append(s)
  860. return hls
  861. def get_info2(self, data):
  862. # Get movie info (for VOD)
  863. nfo = {}
  864. tt = lambda p,r,d: re.search(p,r).group(1) if re.search(p,r) else d
  865. tt2 = lambda p,r,d: (re.sub("[\.]","",re.search(p,r).group(1))).split(", ") if re.search(p,r) else d
  866. r = self.call2(data)
  867. m = re.search(r'data-movieid=\d+><div class="lv">([^<]+)</div><div[^>]+>([^<]+)<', r)
  868. if m:
  869. title1 = unescape(m.group(1))
  870. title2 = unescape(m.group(2))
  871. else:
  872. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  873. title1 = title2 = unescape(m.group(1)) if m else "Video"
  874. nfo["title"] = title1
  875. if not title1 == title2:
  876. nfo["originaltitle"] = title2
  877. if nfo["originaltitle"] and not nfo["title"]:
  878. nfo["title"] = nfo["originaltitle"]
  879. if 'episode_switcher_title' in r:
  880. set1 = title1.replace("\xe2\x80\x93", "-")
  881. if " - " in set1:
  882. set1 = set1.split(" - ")[0]
  883. set2 = title2.replace("\xe2\x80\x93", "-")
  884. if " - " in set2:
  885. set2 = set2.split(" - ")[0]
  886. if set1 <> set2:
  887. nfo["set"] = "%s ~ %s" % (set1, set2)
  888. m = re.search(r"<br>(S(\d+)E(\d+))<", r, re.IGNORECASE | re.DOTALL)
  889. if m:
  890. #nfo["title"] = nfo["title"] + " (%s)" % m.group(1)
  891. #nfo["originaltitle"] = nfo["originaltitle"] + " (%s)" % m.group(1)
  892. nfo["season"] = m.group(2)
  893. nfo["episode"] = m.group(2)
  894. nfo["thumb"] = tt('<meta name="og:image" content="([^"]+)"', r,"")
  895. nfo["thumb"] = nfo["thumb"].replace("http:", "https:")
  896. nfo["year"] = tt('movie-informatio-title">Gads<.+?content">([^<]+)<', r, "")
  897. nfo["runtime"] = tt('movie-informatio-title">Garums<.+?content">([^<]+)<', r, "")
  898. nfo["quality"] = tt('movie-informatio-title">Kvalitāte<.+?content">([^<]+)<', r, "")
  899. nfo["genre"]=tt('movie-informatio-title">Žanrs<.+?content">([^<]+)<', r, "")
  900. nfo["director"] = tt('movie-informatio-title">Režisor.<.+?content">([^<]+)<', r, "")
  901. nfo["actor"] = tt2('class="movie-informatio-content actors">([^<]+)<', r, "")
  902. nfo["language"] = tt2('movie-informatio-title">Valodas*<.+?content">([^<]+)<', r, "")
  903. nfo["subtitles"] = tt2('movie-informatio-title">Subtitri<.+?content">([^<]+)<', r, "")
  904. nfo["plot"] = tt('class="movie-informatio-content introduction">([^<]+)<', r, "")
  905. nfo["tagline"] = nfo["plot"]
  906. return nfo
  907. def get_stream_url2(self,data):
  908. video_id=data[data.find("/")+1:]
  909. if not self.is_logedin2():
  910. if not self.login2():
  911. return []
  912. #data2 = self.get_noma_url(video_id)
  913. nfo = self.get_info2(data)
  914. title = util.nfo2title(nfo)
  915. desc = util.nfo2desc(nfo)
  916. img = nfo["thumb"]
  917. #xml = util.nfo2xml(nfo)
  918. url = "https://www.shortcut.lv/xmls/%s.xml"%video_id
  919. headers = self.headers2
  920. headers["Cookie"] = self.session_id
  921. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  922. r = response.read()
  923. servers = re.findall("(?s)<origin>([^<]+)</origin>", r)
  924. streams_xml = re.findall('<stream quality="\w+">(mp4:\w+(\w\w)_(\w\w).mp4)</stream>',r)
  925. if not streams_xml:
  926. raise Exception("No stream, buy movie?")
  927. resource_id = re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  928. token = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  929. streams=[]
  930. captions = []
  931. llist = ["fr","en","ru","lv"]
  932. for s in re.findall('<subtitles code="([^"]+)">([^<]+)</subtitles>', r, re.DOTALL):
  933. sub = {}
  934. sub["url"] = s[1]
  935. sub["lang"] = s[0]
  936. sub["name"] = "captions (vtt)"
  937. sub["type"] = "vtt"
  938. sub["order"] = llist.index(sub["lang"])*10 if sub["lang"] in llist else 0
  939. captions.append(sub)
  940. captions = sorted(captions,key=lambda item: item["order"],reverse=True)
  941. for s in streams_xml:
  942. for server in servers:
  943. stream = util.item()
  944. server2 = self.load_balancer(server)
  945. url = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(server2,s[0],resource_id,token)
  946. # TODO Engima2 gstreamer vajag lai padod playlist nevis chunklist
  947. # r3 = self._http_request(url)
  948. # sss = re.findall(r"#EXT-X-STREAM-INF:.*?BANDWIDTH=(\d+).*?\n(.+?)$", r3, re.IGNORECASE | re.MULTILINE)
  949. # if sss:
  950. # url2 = sss[0][1]
  951. # stream["headers"] = {"Referer":url}
  952. # if url2.startswith("http"):
  953. # url = url2
  954. # else:
  955. # url = util.hls_base(url)+url2
  956. stream["url"]=url
  957. stream["lang"]=s[1]
  958. stream["quality"]=s[2]
  959. stream["name"]= unescape(title)
  960. stream["desc"]= unescape(desc)
  961. stream["img"] = img
  962. stream["nfo"] = {"movie":nfo}
  963. stream["type"]="hls" #stream_type(url)
  964. stream["subs"] = []
  965. for c in captions:
  966. c2= c.copy()
  967. #c2["url"] ="http://%s/mobile-vod/%s/%s?resource_id=%s&auth_token=%s"%(server2,s[0],c["url"],resource_id,token)
  968. c2["url"] ="http://%s/mobile-vod/%s/%s"%(server2,s[0],c["url"])
  969. stream["subs"].append(c2)
  970. pass
  971. streams.append(stream)
  972. break # TODO ņem tikai pirmo serveri, varētu pārbaudit, kurš no tiem strādā, kurš ne
  973. return streams
  974. #data = {}
  975. #data["server"] = re.findall("(?s)<origin>([^<]+)</origin>", r)[1]
  976. #data["language"]=re.findall('(?s)<language code="([^"]+)">', r)
  977. #data["language"]="lv" if "lv" in data["language"] else "ru" if "ru" in data["language"] else "en"
  978. #data["qs"]=re.findall('(?s)<stream quality="([^"]+)">([^<]+)</stream>', r)
  979. #data["qs"]=dict(data["qs"])
  980. #qs = data["qs"].keys()
  981. #data["quality"] = "hd" if "hd" in qs else "hq" if "hq" in qs else "mhq" if "mhq" in qs else "lq"
  982. #data["mp4"] = data["qs"][data["quality"]]
  983. #data["token"] = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  984. #data["resource_id"]=re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  985. #data["server"]=self.load_balancer(data["server"])
  986. #data["hls"] = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(data["server"],data["mp4"],data["resource_id"],data["token"])
  987. ##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"])
  988. ### wwww.shortcut.lv izsaukumi
  989. def login2(self,user="",password=""):
  990. """Login in to site, create session cookies"""
  991. if not user: user=self.options["user"]
  992. if not password: password = self.options["password"]
  993. url0 = "https://www.shortcut.lv"
  994. class NoRedirectHandler(urllib2.HTTPRedirectHandler):
  995. def http_error_302(self, req, fp, code, msg, headers):
  996. infourl = urllib.addinfourl(fp, headers, req.get_full_url())
  997. infourl.status = code
  998. infourl.code = code
  999. return infourl
  1000. http_error_300 = http_error_302
  1001. http_error_301 = http_error_302
  1002. http_error_303 = http_error_302
  1003. http_error_307 = http_error_302
  1004. # Dabūjam sesijas id un url_gif, kas redirektējas uz auth_url
  1005. headers = headers2dict("""
  1006. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1007. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  1008. Accept-Language: en-US,en;q=0.5
  1009. DNT: 1
  1010. Connection: keep-alive
  1011. """)
  1012. response = urllib2.urlopen(urllib2.Request(url0, headers=headers))
  1013. session_id = response.headers["set-cookie"].split(";")[0]
  1014. html = response.read()
  1015. url_gif = url0 + re.search('(/auth/\d+\.gif)', html).group(1)
  1016. # Dabūtjam auth_url
  1017. headers = headers2dict("""
  1018. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1019. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  1020. Accept-Language: en-US,en;q=0.5
  1021. DNT: 1
  1022. Referer: https://www.shortcut.lv/
  1023. """)
  1024. headers["Cookie"] = session_id
  1025. urllib2.install_opener(urllib2.build_opener(NoRedirectHandler()))
  1026. response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers))
  1027. if response.code == 302:
  1028. url_auth = response.headers["location"]
  1029. else:
  1030. self.error = u"auth.gif nenostrādāja"
  1031. raise Exception(u"auth.gif nenostrādāja")
  1032. #return False
  1033. # Pierakstāmies iekš auth.lattelecom.lv
  1034. headers = headers2dict("""
  1035. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1036. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  1037. Accept-Language: en-US,en;q=0.5
  1038. DNT: 1
  1039. Referer: https://www.shortcut.lv/
  1040. """)
  1041. response = urllib2.urlopen(urllib2.Request(url_auth, headers=headers))
  1042. if not response.code == 302:
  1043. self.error = u"pierakstīšanās auth.lattelecom.lv nenostrādāja"
  1044. return False
  1045. #raise "pierakstīšanās auth.lattelecom.lv nenostrādāja"
  1046. # Mēģinam ielogoties
  1047. headers = headers2dict("""
  1048. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1049. Accept: text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
  1050. Accept-Language: en-US,en;q=0.5
  1051. DNT: 1
  1052. X-Requested-With: XMLHttpRequest
  1053. Referer: https://www.shortcut.lv/
  1054. """)
  1055. headers["Cookie"] = session_id
  1056. #data = "login=yes&email=%s&passw=%s"%(user,password)
  1057. url = "https://www.shortcut.lv/login.json?callback=jQuery111303344749731668816_1463817318435&username=%s&password=%s&captcha=&sid=&_="%(user,password)
  1058. req = urllib2.Request(url, headers=headers)
  1059. response = urllib2.urlopen(req)
  1060. #with open("auth.htm","w") as f: f.write(response.read())
  1061. if not response.code == 200:
  1062. #self.error = u"kļūda ielogojoties"
  1063. raise Exception(u"kļūda ielogojoties")
  1064. html = response.read()
  1065. if not '"success":true' in html:
  1066. err = re.search('"error":"(.+?)"',html).group(1) if re.search('"error":"(.+?)"',html) else ""
  1067. raise Exception(u"Kļūda ielogojoties - %s"%err.decode("utf8"))
  1068. self.session_id = session_id
  1069. self.headers2["Cookie"] = "%s; "%(self.session_id)
  1070. self.error = ""
  1071. return True
  1072. def check_logedin2(self):
  1073. if not self.session_id:
  1074. return False
  1075. else:
  1076. url = "https://www.shortcut.lv/profils"
  1077. response = urllib2.urlopen(urllib2.Request(url, headers=self.headers2))
  1078. if response.code == 200:
  1079. return True
  1080. else:
  1081. self.session_id = ""
  1082. return False
  1083. def is_logedin2(self):
  1084. if self.session_id:
  1085. return True
  1086. else:
  1087. return False
  1088. def load_balancer(self,server,streams=[]):
  1089. headers = headers2dict("""
  1090. Connection: keep-alive
  1091. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  1092. X-Requested-With: ShockwaveFlash/21.0.0.242
  1093. """)
  1094. url = "http://%s/loadbalancer"%server
  1095. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  1096. r = response.read()
  1097. statuss = re.search("<status>(\d+)</status>",r).group(1)
  1098. edge = re.search("<edge>([^<>]+)</edge>",r).group(1)
  1099. return edge
  1100. def test_hls(self,url):
  1101. headers = headers2dict("""
  1102. Connection: keep-alive
  1103. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  1104. """)
  1105. try:
  1106. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  1107. except Exception as ex:
  1108. print "hls failed: %s %s"%(ex.getcode(),url)
  1109. return False
  1110. if response.code == 200:
  1111. html = response.read()
  1112. url0 = re.search("(http://[^/]+/)",url).group(1)
  1113. chunklist = re.search("(chunklist.+)",html).group(1).strip()
  1114. url2 = url.split('/')[:-1]
  1115. url2 ="/".join(url2)+"/"+chunklist
  1116. try:
  1117. response2 = urllib2.urlopen(urllib2.Request(url2, headers=headers))
  1118. except Exception as ex:
  1119. print "hls chunk failed: %s %s"%(ex.getcode(),url2)
  1120. return False
  1121. if response2.code == 200:
  1122. return True
  1123. else:
  1124. return False
  1125. else:
  1126. return False
  1127. if __name__ == "__main__":
  1128. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  1129. import run
  1130. source = Source()
  1131. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  1132. run.run(source, data)
  1133. sys.exit()
  1134. c.get_info2(sys.argv[1])
  1135. if len(sys.argv)>1 and not "ltc::" in sys.argv[1]:
  1136. vid = vid2 = sys.argv[1]
  1137. password = sys.argv[2]
  1138. print "login - %s"%c.login("ivars777",password)
  1139. #vid = "1069"
  1140. #vid = "1462566072086"
  1141. #channelid="101"
  1142. #vid = "1350462656767"
  1143. #data = c.get_stream_url(vid,"vod")
  1144. #call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",data["stream"]])
  1145. #pass
  1146. print "login2 - %s"%c.login2("ivars777",password)
  1147. #vid2 = "animation/ultimate_avengers_ii"
  1148. #vid2 = "animation/ice_age"
  1149. #vid2 = "tiesraide/ltv1"
  1150. #vid2 = "arhivs/1456521417815"
  1151. streams = c.get_stream_url2(vid2)
  1152. stream = streams[-1]
  1153. print
  1154. #for s in data:
  1155. #call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",stream["url"]])
  1156. cmd = ["ffplay.exe",
  1157. "-headers","Referer:%s"%(quote(stream["headers"]["Referer"])),
  1158. "-headers","Connection:Keep-Alive",
  1159. stream["url"]]
  1160. print " ".join(cmd)
  1161. call(cmd)
  1162. pass
  1163. else:
  1164. if len(sys.argv)>1:
  1165. data= sys.argv[1]
  1166. else:
  1167. data = "ltc::home"
  1168. content = c.get_content(data)
  1169. for item in content:
  1170. print item
  1171. #cat = api.get_categories(country)
  1172. #chan = api.get_channels("lv")
  1173. #prog = api.get_programs(channel=6400)
  1174. #prog = api.get_programs(category=55)
  1175. #seas = api.get_seasons(program=6453)
  1176. #str = api.get_streams(660243)
  1177. #res = api.get_videos(802)
  1178. #formats = api.getAllFormats()
  1179. #det = api.detailed("1516")
  1180. #vid = api.getVideos("13170")
  1181. pass