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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 not self.is_video(data):
  537. return []
  538. source, data, path, plist, clist, params, qs = self.parse_data(data)
  539. #if "::" in data: data = data.split("::")[1]
  540. ### Video nomas strīmus pagaidām dabu no mājas lapas TODO
  541. if data.split("/")[0]=="videonoma" and len(data.split("/"))>1:
  542. #video_id=data.split("/")[-1]
  543. streams = self.get_stream_url2(data)
  544. else:
  545. if not self.is_logedin():
  546. if not self.login():
  547. return []
  548. if plist[1] == "catchup":
  549. start = int(qs["start"]) + 1
  550. #url = "https://manstv.lattelecom.tv/api/v1.7/get/content/epgs/?filter[channel]=101&filter[utFrom]=1551551500&include=channel&page[size]=40page[number]=1"
  551. data2 = "content/epgs/?filter[channel]=%s&filter[utFrom]=%s&filter[utTo]=%s&include=channel&page[size]=40page[number]=1" % (plist[2], start, start )
  552. r2 = self.call(data2)
  553. if "data" in r2:
  554. epgid = r2["data"][0]["id"]
  555. else:
  556. return []
  557. data = "ltc::content/record-streams/%s?include=quality" % epgid
  558. source, data, path, plist, clist, params, qs = self.parse_data(data)
  559. r = self.call(data)
  560. if not r: return []
  561. if "errors" in r:
  562. return []
  563. self.refresh_token()
  564. token = "&auth_token=app_%s"%(self.token)
  565. vid = data.split("/")[2].split("?")[0]
  566. vtype = data.split("/")[1]
  567. if vtype == "live-streams":
  568. ch = self.get_channel_by_id(vid)
  569. offset = "%2B03%3A00" # TODO - vajag aktuālo laika nobidi no GMS
  570. data2 = "tv/epg-live/%s/?offset=%s"%(ch["xprs_id"],offset)
  571. r2 = self.call(data2)
  572. if r2 and r2["items"]:
  573. title = r2["items"][0]["title"].encode("utf8")
  574. t1 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_start"])).strftime('%H:%M')
  575. t2 = datetime.datetime.fromtimestamp(int(r2["items"][0]["unix_stop"])).strftime('%H:%M')
  576. title = "%s (%s-%s)"%(title,t1,t2)
  577. img = "https://manstv.lattelecom.tv/cache/" + r2["items"][0]["url"]
  578. desc = r2["items"][0]["description"]
  579. else:
  580. title = ch["name"]
  581. desc = title
  582. img = ""
  583. elif vtype == "record-streams":
  584. epg = self.get_epg_id(vid)
  585. if epg:
  586. title = epg["title"].encode("utf8")
  587. t1 = datetime.datetime.fromtimestamp(int(epg["unix_start"])).strftime('%H:%M')
  588. t2 = datetime.datetime.fromtimestamp(int(epg["unix_stop"])).strftime('%H:%M')
  589. date = epg["date"]
  590. #title = "%s [%s %s-%s]"%(title,date,t1,t2)
  591. desc = title + "\n" + epg["description"].encode("utf8")
  592. img = epg["img"].encode("utf8") if "img" in epg else ""
  593. else:
  594. title = desc = data
  595. img = ""
  596. streams = []
  597. for s in r["data"]:
  598. stream = util.item()
  599. stream["url"]=(s["attributes"]["stream-url"]+token.encode("utf8")).encode("utf8")
  600. stream["name"]=title
  601. stream["desc"]=desc
  602. stream["img"] = img
  603. stream["type"]="hls"
  604. m=re.search(".+_(\w\w)_(\w\w)\.\w+",s["id"])
  605. if m:
  606. stream["quality"]=m.group(2).encode("utf8")
  607. stream["lang"]=m.group(1).encode("utf8")
  608. streams.append(stream)
  609. ### TODO - sakārtot sarakstu, lai pirmais ir labakais video
  610. qlist = ["???","lq","mq","hq","hd"]
  611. llist = ["fr","en","ru","lv"]
  612. for s in streams:
  613. lv = llist.index(s["lang"])*10 if s["lang"] in llist else 0
  614. qv=qlist.index(s["quality"]) if s["quality"] in qlist else 0
  615. s["order"] = lv+qv
  616. streams = sorted(streams,key=lambda item: item["order"],reverse=True)
  617. return streams
  618. def is_video(self,data):
  619. if "::" in data:
  620. data = data.split("::")[1]
  621. cmd = data.split("/")
  622. if "get-stream" in data:
  623. return True
  624. elif cmd[0] in ("content") and cmd[1] in ("live-streams","record-streams","vod-streams", "catchup"):
  625. return True
  626. elif cmd[0]=="arhivs" and len(cmd)==4:
  627. return True
  628. elif cmd[0]=="videonoma" and len(cmd) >= 3 and not "?" in data:
  629. return True
  630. else:
  631. return False
  632. def get_channels(self):
  633. if self.ch:
  634. return self.ch
  635. if not self.check_logedin():
  636. self.login() # citādi nerāda TV3, LNT, TV6
  637. r= self.call("tv/channels")
  638. self.ch=[]
  639. for i,item in enumerate(r["items"]):
  640. self.ch.append(item)
  641. self.ch_id[item["id"]]=i
  642. self.ch_id2[item["xprs_id"]]=i
  643. self.ch_name[item["name"]]=i
  644. return self.ch
  645. def get_channel_by_id(self,chid):
  646. if not self.ch:
  647. self.get_channels()
  648. if not self.ch:
  649. return None
  650. return self.ch[self.ch_id[chid]] if self.ch_id.has_key(chid) else None
  651. def get_channel_by_id2(self,chid):
  652. if not self.ch:
  653. self.get_channels()
  654. if not self.ch:
  655. return None
  656. return self.ch[self.ch_id2[chid]] if self.ch_id2.has_key(chid) else None
  657. def get_channels2(self):
  658. if self.ch2:
  659. return self.ch2
  660. r= self.call2("tiesraide")
  661. self.ch2=[]
  662. for item in re.findall(r'<div class="entry forward-link.+?data-xprs="(\d+)".+?title="([^"]+)" href="/tiesraide/([^"]+)"', r, re.DOTALL):
  663. ch={}
  664. ch["id2"]=item[0]
  665. ch["name"]=item[2]
  666. ch["title"]=item[1]
  667. self.ch2.append(ch)
  668. return self.ch2
  669. def get_channel_by_name2(self,name):
  670. if not self.ch2:
  671. self.get_channels2()
  672. for c in self.ch2:
  673. if c["name"]==name:
  674. return c
  675. return None
  676. def get_channel_by_name(self,name):
  677. if not self.ch:
  678. self.get_channels()
  679. ch2 = self.get_channel_by_name2(name)
  680. if not ch2:
  681. return None
  682. ch = self.get_channel_by_id2(ch2["id2"])
  683. return ch
  684. def get_epg(self,date=None,update=False):
  685. #https://manstv.lattelecom.tv/api/v1.3/get/tv/epg/?daynight=2016-05-19
  686. today=datetime.date.today()
  687. if not date:
  688. date=datetime.date.today().strftime("%Y-%m-%d")
  689. if update:
  690. self.epg=[]
  691. self.epg_id={}
  692. self.epg_id2={}
  693. self.epg_date={}
  694. self.epg_ch={}
  695. self.epg_cat={}
  696. if not date in self.epgdates:
  697. r=self.call("tv/epg/?daynight=%s"%date)
  698. for item in r["items"]:
  699. if item["id"] in self.epg_id: continue
  700. else:
  701. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  702. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  703. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  704. self.epg.append(item)
  705. index = self.epg.index(item)
  706. self.epg_id[item["id"]]=index
  707. if not item["date"] in self.epg_date:
  708. self.epg_date[item["date"]]=[]
  709. self.epg_date[item["date"]].append(index)
  710. if not item["channel_id"] in self.epg_ch:
  711. self.epg_ch[item["channel_id"]]=[]
  712. self.epg_ch[item["channel_id"]].append(index)
  713. if not date in self.epgdates:
  714. self.epgdates.append(date)
  715. def get_epg_id(self,epgid):
  716. if self.epg and epgid in self.epg_id:
  717. return self.epg[self.epg_id[epgid]]
  718. else:
  719. r=self.call("content/epgs/%s" % epgid)
  720. if not r:
  721. return None
  722. item = r["data"][0]["attributes"]
  723. item["unix_start"] = item["unix-start"]
  724. item["unix_stop"] = item["unix-stop"]
  725. item["time_start2"] = datetime.datetime.fromtimestamp(int(item["unix_start"]))
  726. item["time_stop2"] = datetime.datetime.fromtimestamp(int(item["unix_stop"]))
  727. item["date"]=item["time_start2"].strftime("%Y-%m-%d")
  728. item["img"] = "https://manstv.lattelecom.tv/" + item["url"]
  729. return item
  730. def get_epg_date(self,date,chid):
  731. if not date in self.epgdates:
  732. self.get_epg(date)
  733. items = []
  734. ch = self.get_channel_by_id(chid)
  735. for it in self.epg_date[date]:
  736. item = self.epg[it]
  737. if item["channel_id"]==ch["xprs_id"]:
  738. items.append(item)
  739. items.reverse()
  740. return items
  741. def call(self, data,params = None, headers=None):
  742. if not headers: headers = self.headers
  743. #if not lang: lang = self.country
  744. url = self.api_url + data
  745. content = self._http_request(url,params, headers)
  746. if content:
  747. try:
  748. result = json.loads(content)
  749. return result
  750. except Exception, ex:
  751. return None
  752. else:
  753. return None
  754. def call2(self, data,params = None, headers=None):
  755. if not headers: headers = self.headers2
  756. #if not lang: lang = self.country
  757. url = self.api_url2 + data
  758. content = self._http_request(url,params, headers)
  759. return content
  760. def _http_request0(self, url,params = None, headers=None):
  761. if not headers: headers = self.headers
  762. try:
  763. r = urllib2.Request(url, data=params, headers=headers)
  764. u = urllib2.urlopen(r)
  765. content = u.read()
  766. u.close()
  767. return content
  768. except Exception as ex:
  769. return None
  770. def login(self,user="",password=""):
  771. """Login in to site, get token"""
  772. self.options_read()
  773. if not user: user=self.options["user"]
  774. if not password: password = self.options["password"]
  775. # Dabūjam tokenu
  776. url = "https://manstv.lattelecom.tv/api/v1.5/post/user/users/%s" % user
  777. #url = "https://manstv.lattelecom.tv/api/v1.5/post/user/login"
  778. params = "uid=7f777e938d35e017&password=%s&" % (password)
  779. #params = "uid=7f777e938d35e017&password=%s&username=%s&"%(password,user)
  780. headers = headers2dict("""
  781. User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
  782. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  783. Host: manstv.lattelecom.tv
  784. """ )
  785. try:
  786. r = urllib2.Request(url, data=params, headers=headers)
  787. u = urllib2.urlopen(r)
  788. content = u.read()
  789. u.close()
  790. except Exception as ex:
  791. return None
  792. #r = self.call(data, params)
  793. if r and "token" in content:
  794. self.token=re.search('"token":"(.+?)"', content).group(1)
  795. return True
  796. else:
  797. return False
  798. def refresh_token(self):
  799. data = "user/refresh-token//%s"%self.token
  800. r = self.call(data)
  801. if 'token' in r:
  802. self.token = r["token"]
  803. return True
  804. else:
  805. self.token = ""
  806. return False
  807. def check_logedin(self):
  808. if not self.token:
  809. return False
  810. else:
  811. if self.refresh_token():
  812. return True
  813. else:
  814. return False
  815. def is_logedin(self):
  816. if self.token:
  817. return True
  818. else:
  819. return False
  820. #----------------------------------------------------------------------
  821. def get_tv_url(self,video_id):
  822. """Get m3u8 url for given live tv channel"""
  823. url = "https://m.lattelecom.tv/tiesraide/%s"%video_id
  824. data = self.get_stream_url2("tiesraide/%s"%video_id)
  825. #headers = self.headers
  826. #headers["Cookie"] = "%s; %s; _hjIncludedInSample=0; MobBitr=1; MobRentBitr=%s; MobRentLang=%s;"%(self.session_id,self.mobtv_cache,data["quality"],data["language"])
  827. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  828. #html = response.read()
  829. #m3u8 = re.search('x-mpegURL" src="(.*?)"', html).group(1) if "x-mpegURL" in html else ""
  830. return data["stream"] if data else None
  831. #----------------------------------------------------------------------
  832. def get_noma_url(self,video_id,quality="hq",language="lv"):
  833. """Get m3u8 url for given rental video"""
  834. video_id1,video_id2=video_id.split("/")
  835. data = self.get_stream_url2(video_id)
  836. #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"])
  837. #headers = self.headers
  838. #headers["Cookie"] = "%s; %s; MobRentBitr=%s; MobBitr=1; MobRentLang=%s"%(self.session_id,self.mobtv_cache,quality,language)
  839. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  840. #m3u8 = response.read()
  841. return data["stream"] if data else None
  842. #----------------------------------------------------------------------
  843. def get_arhivs_url(self,video_id):
  844. """Get m3u8 url for given archive video"""
  845. data = self.get_video_data(video_id)
  846. #url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=arhivs&event_id=%s&bitrate=mhq"%(video_id) #,data["quality"])
  847. #headers = self.headers
  848. #headers["Cookie"] = "%s; %s; MobBitr=1; "%(self.session_id,self.mobtv_cache)
  849. #response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  850. #m3u8 = response.read()
  851. return data["stream"] if data else None
  852. def get_stream_url(self,video_id,type="vod",chid=""):
  853. "Get stream urls (hls) using manstv api"
  854. if type=="vod":
  855. data = "vod/get-stream/%s"%video_id
  856. elif type=="tv":
  857. data = "tv/get-stream?id=%s"%video_id
  858. elif type=="archive":
  859. if not chid: return None
  860. data="archive/get-stream/%s?channelid=%s"%(video_id,chid)
  861. else:
  862. return None
  863. if not self.is_logedin():
  864. if not self.login():
  865. return None
  866. r = self.call(data)
  867. if not r:
  868. return None
  869. self.refresh_token()
  870. token = "&auth_token=app_%s"%(self.token)
  871. hls={}
  872. hls["stream"]=r["stream"]+token
  873. hls["streams"]=[]
  874. if "items" in r:
  875. for s in r["items"]["streams"]:
  876. s["stream"] += token
  877. hls["streams"].append(s)
  878. return hls
  879. def get_info2(self, data):
  880. # Get movie info (for VOD)
  881. nfo = {}
  882. tt = lambda p,r,d: re.search(p,r).group(1) if re.search(p,r) else d
  883. tt2 = lambda p,r,d: (re.sub("[\.]","",re.search(p,r).group(1))).split(", ") if re.search(p,r) else d
  884. r = self.call2(data)
  885. m = re.search(r'data-movieid=\d+><div class="lv">([^<]+)</div><div[^>]+>([^<]+)<', r)
  886. if m:
  887. title1 = unescape(m.group(1))
  888. title2 = unescape(m.group(2))
  889. else:
  890. m = re.search('<meta name="dr:say:title" content="([^"]+)">', r, re.IGNORECASE)
  891. title1 = title2 = unescape(m.group(1)) if m else "Video"
  892. nfo["title"] = title1
  893. if not title1 == title2:
  894. nfo["originaltitle"] = title2
  895. if nfo["originaltitle"] and not nfo["title"]:
  896. nfo["title"] = nfo["originaltitle"]
  897. if 'episode_switcher_title' in r:
  898. set1 = title1.replace("\xe2\x80\x93", "-")
  899. if " - " in set1:
  900. set1 = set1.split(" - ")[0]
  901. set2 = title2.replace("\xe2\x80\x93", "-")
  902. if " - " in set2:
  903. set2 = set2.split(" - ")[0]
  904. if set1 <> set2:
  905. nfo["set"] = "%s ~ %s" % (set1, set2)
  906. m = re.search(r"<br>(S(\d+)E(\d+))<", r, re.IGNORECASE | re.DOTALL)
  907. if m:
  908. #nfo["title"] = nfo["title"] + " (%s)" % m.group(1)
  909. #nfo["originaltitle"] = nfo["originaltitle"] + " (%s)" % m.group(1)
  910. nfo["season"] = m.group(2)
  911. nfo["episode"] = m.group(2)
  912. nfo["thumb"] = tt('<meta name="og:image" content="([^"]+)"', r,"")
  913. nfo["thumb"] = nfo["thumb"].replace("http:", "https:")
  914. nfo["year"] = tt('movie-informatio-title">Gads<.+?content">([^<]+)<', r, "")
  915. nfo["runtime"] = tt('movie-informatio-title">Garums<.+?content">([^<]+)<', r, "")
  916. nfo["quality"] = tt('movie-informatio-title">Kvalitāte<.+?content">([^<]+)<', r, "")
  917. nfo["genre"]=tt('movie-informatio-title">Žanrs<.+?content">([^<]+)<', r, "")
  918. nfo["director"] = tt('movie-informatio-title">Režisor.<.+?content">([^<]+)<', r, "")
  919. nfo["actor"] = tt2('class="movie-informatio-content actors">([^<]+)<', r, "")
  920. nfo["language"] = tt2('movie-informatio-title">Valodas*<.+?content">([^<]+)<', r, "")
  921. nfo["subtitles"] = tt2('movie-informatio-title">Subtitri<.+?content">([^<]+)<', r, "")
  922. nfo["plot"] = tt('class="movie-informatio-content introduction">([^<]+)<', r, "")
  923. nfo["tagline"] = nfo["plot"]
  924. return nfo
  925. def get_stream_url2(self,data):
  926. video_id=data[data.find("/")+1:]
  927. if not self.is_logedin2():
  928. if not self.login2():
  929. return []
  930. #data2 = self.get_noma_url(video_id)
  931. nfo = self.get_info2(data)
  932. title = util.nfo2title(nfo)
  933. desc = util.nfo2desc(nfo)
  934. img = nfo["thumb"]
  935. #xml = util.nfo2xml(nfo)
  936. url = "https://www.shortcut.lv/xmls/%s.xml"%video_id
  937. headers = self.headers2
  938. headers["Cookie"] = self.session_id
  939. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  940. r = response.read()
  941. servers = re.findall("(?s)<origin>([^<]+)</origin>", r)
  942. streams_xml = re.findall('<stream quality="\w+">(mp4:\w+(\w\w)_(\w\w).mp4)</stream>',r)
  943. if not streams_xml:
  944. raise Exception("No stream, buy movie?")
  945. resource_id = re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  946. token = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  947. streams=[]
  948. captions = []
  949. llist = ["fr","en","ru","lv"]
  950. for s in re.findall('<subtitles code="([^"]+)">([^<]+)</subtitles>', r, re.DOTALL):
  951. sub = {}
  952. sub["url"] = s[1]
  953. sub["lang"] = s[0]
  954. sub["name"] = "captions (vtt)"
  955. sub["type"] = "vtt"
  956. sub["order"] = llist.index(sub["lang"])*10 if sub["lang"] in llist else 0
  957. captions.append(sub)
  958. captions = sorted(captions,key=lambda item: item["order"],reverse=True)
  959. for s in streams_xml:
  960. for server in servers:
  961. stream = util.item()
  962. server2 = self.load_balancer(server)
  963. url = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(server2,s[0],resource_id,token)
  964. # TODO Engima2 gstreamer vajag lai padod playlist nevis chunklist
  965. # r3 = self._http_request(url)
  966. # sss = re.findall(r"#EXT-X-STREAM-INF:.*?BANDWIDTH=(\d+).*?\n(.+?)$", r3, re.IGNORECASE | re.MULTILINE)
  967. # if sss:
  968. # url2 = sss[0][1]
  969. # stream["headers"] = {"Referer":url}
  970. # if url2.startswith("http"):
  971. # url = url2
  972. # else:
  973. # url = util.hls_base(url)+url2
  974. stream["url"]=url
  975. stream["lang"]=s[1]
  976. stream["quality"]=s[2]
  977. stream["name"]= unescape(title)
  978. stream["desc"]= unescape(desc)
  979. stream["img"] = img
  980. stream["nfo"] = {"movie":nfo}
  981. stream["type"]="hls" #stream_type(url)
  982. stream["subs"] = []
  983. for c in captions:
  984. c2= c.copy()
  985. #c2["url"] ="http://%s/mobile-vod/%s/%s?resource_id=%s&auth_token=%s"%(server2,s[0],c["url"],resource_id,token)
  986. c2["url"] ="http://%s/mobile-vod/%s/%s"%(server2,s[0],c["url"])
  987. stream["subs"].append(c2)
  988. pass
  989. streams.append(stream)
  990. break # TODO ņem tikai pirmo serveri, varētu pārbaudit, kurš no tiem strādā, kurš ne
  991. return streams
  992. #data = {}
  993. #data["server"] = re.findall("(?s)<origin>([^<]+)</origin>", r)[1]
  994. #data["language"]=re.findall('(?s)<language code="([^"]+)">', r)
  995. #data["language"]="lv" if "lv" in data["language"] else "ru" if "ru" in data["language"] else "en"
  996. #data["qs"]=re.findall('(?s)<stream quality="([^"]+)">([^<]+)</stream>', r)
  997. #data["qs"]=dict(data["qs"])
  998. #qs = data["qs"].keys()
  999. #data["quality"] = "hd" if "hd" in qs else "hq" if "hq" in qs else "mhq" if "mhq" in qs else "lq"
  1000. #data["mp4"] = data["qs"][data["quality"]]
  1001. #data["token"] = re.search("(?s)<auth_token>([^<]+)</auth_token>", r).group(1)
  1002. #data["resource_id"]=re.search("(?s)<resource_id>([^<]+)</resource_id>", r).group(1)
  1003. #data["server"]=self.load_balancer(data["server"])
  1004. #data["hls"] = "http://%s/mobile-vod/%s/playlist.m3u8?resource_id=%s&auth_token=%s"%(data["server"],data["mp4"],data["resource_id"],data["token"])
  1005. ##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"])
  1006. ### wwww.shortcut.lv izsaukumi
  1007. def login2(self,user="",password=""):
  1008. """Login in to site, create session cookies"""
  1009. if not user: user=self.options["user"]
  1010. if not password: password = self.options["password"]
  1011. url0 = "https://www.shortcut.lv"
  1012. class NoRedirectHandler(urllib2.HTTPRedirectHandler):
  1013. def http_error_302(self, req, fp, code, msg, headers):
  1014. infourl = urllib.addinfourl(fp, headers, req.get_full_url())
  1015. infourl.status = code
  1016. infourl.code = code
  1017. return infourl
  1018. http_error_300 = http_error_302
  1019. http_error_301 = http_error_302
  1020. http_error_303 = http_error_302
  1021. http_error_307 = http_error_302
  1022. # Dabūjam sesijas id un url_gif, kas redirektējas uz auth_url
  1023. headers = headers2dict("""
  1024. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1025. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  1026. Accept-Language: en-US,en;q=0.5
  1027. DNT: 1
  1028. Connection: keep-alive
  1029. """)
  1030. response = urllib2.urlopen(urllib2.Request(url0, headers=headers))
  1031. session_id = response.headers["set-cookie"].split(";")[0]
  1032. html = response.read()
  1033. url_gif = url0 + re.search('(/auth/\d+\.gif)', html).group(1)
  1034. # Dabūtjam auth_url
  1035. headers = headers2dict("""
  1036. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1037. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  1038. Accept-Language: en-US,en;q=0.5
  1039. DNT: 1
  1040. Referer: https://www.shortcut.lv/
  1041. """)
  1042. headers["Cookie"] = session_id
  1043. urllib2.install_opener(urllib2.build_opener(NoRedirectHandler()))
  1044. response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers))
  1045. if response.code == 302:
  1046. url_auth = response.headers["location"]
  1047. else:
  1048. self.error = u"auth.gif nenostrādāja"
  1049. raise Exception(u"auth.gif nenostrādāja")
  1050. #return False
  1051. # Pierakstāmies iekš auth.lattelecom.lv
  1052. headers = headers2dict("""
  1053. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1054. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  1055. Accept-Language: en-US,en;q=0.5
  1056. DNT: 1
  1057. Referer: https://www.shortcut.lv/
  1058. """)
  1059. response = urllib2.urlopen(urllib2.Request(url_auth, headers=headers))
  1060. if not response.code == 302:
  1061. self.error = u"pierakstīšanās auth.lattelecom.lv nenostrādāja"
  1062. return False
  1063. #raise "pierakstīšanās auth.lattelecom.lv nenostrādāja"
  1064. # Mēģinam ielogoties
  1065. headers = headers2dict("""
  1066. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  1067. Accept: text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
  1068. Accept-Language: en-US,en;q=0.5
  1069. DNT: 1
  1070. X-Requested-With: XMLHttpRequest
  1071. Referer: https://www.shortcut.lv/
  1072. """)
  1073. headers["Cookie"] = session_id
  1074. #data = "login=yes&email=%s&passw=%s"%(user,password)
  1075. url = "https://www.shortcut.lv/login.json?callback=jQuery111303344749731668816_1463817318435&username=%s&password=%s&captcha=&sid=&_="%(user,password)
  1076. req = urllib2.Request(url, headers=headers)
  1077. response = urllib2.urlopen(req)
  1078. #with open("auth.htm","w") as f: f.write(response.read())
  1079. if not response.code == 200:
  1080. #self.error = u"kļūda ielogojoties"
  1081. raise Exception(u"kļūda ielogojoties")
  1082. html = response.read()
  1083. if not '"success":true' in html:
  1084. err = re.search('"error":"(.+?)"',html).group(1) if re.search('"error":"(.+?)"',html) else ""
  1085. raise Exception(u"Kļūda ielogojoties - %s"%err.decode("utf8"))
  1086. self.session_id = session_id
  1087. self.headers2["Cookie"] = "%s; "%(self.session_id)
  1088. self.error = ""
  1089. return True
  1090. def check_logedin2(self):
  1091. if not self.session_id:
  1092. return False
  1093. else:
  1094. url = "https://www.shortcut.lv/profils"
  1095. response = urllib2.urlopen(urllib2.Request(url, headers=self.headers2))
  1096. if response.code == 200:
  1097. return True
  1098. else:
  1099. self.session_id = ""
  1100. return False
  1101. def is_logedin2(self):
  1102. if self.session_id:
  1103. return True
  1104. else:
  1105. return False
  1106. def load_balancer(self,server,streams=[]):
  1107. headers = headers2dict("""
  1108. Connection: keep-alive
  1109. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  1110. X-Requested-With: ShockwaveFlash/21.0.0.242
  1111. """)
  1112. url = "http://%s/loadbalancer"%server
  1113. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  1114. r = response.read()
  1115. statuss = re.search("<status>(\d+)</status>",r).group(1)
  1116. edge = re.search("<edge>([^<>]+)</edge>",r).group(1)
  1117. return edge
  1118. def test_hls(self,url):
  1119. headers = headers2dict("""
  1120. Connection: keep-alive
  1121. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  1122. """)
  1123. try:
  1124. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  1125. except Exception as ex:
  1126. print "hls failed: %s %s"%(ex.getcode(),url)
  1127. return False
  1128. if response.code == 200:
  1129. html = response.read()
  1130. url0 = re.search("(http://[^/]+/)",url).group(1)
  1131. chunklist = re.search("(chunklist.+)",html).group(1).strip()
  1132. url2 = url.split('/')[:-1]
  1133. url2 ="/".join(url2)+"/"+chunklist
  1134. try:
  1135. response2 = urllib2.urlopen(urllib2.Request(url2, headers=headers))
  1136. except Exception as ex:
  1137. print "hls chunk failed: %s %s"%(ex.getcode(),url2)
  1138. return False
  1139. if response2.code == 200:
  1140. return True
  1141. else:
  1142. return False
  1143. else:
  1144. return False
  1145. if __name__ == "__main__":
  1146. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  1147. import run
  1148. source = Source()
  1149. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  1150. run.run(source, data)
  1151. sys.exit()
  1152. c.get_info2(sys.argv[1])
  1153. if len(sys.argv)>1 and not "ltc::" in sys.argv[1]:
  1154. vid = vid2 = sys.argv[1]
  1155. password = sys.argv[2]
  1156. print "login - %s"%c.login("ivars777",password)
  1157. #vid = "1069"
  1158. #vid = "1462566072086"
  1159. #channelid="101"
  1160. #vid = "1350462656767"
  1161. #data = c.get_stream_url(vid,"vod")
  1162. #call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",data["stream"]])
  1163. #pass
  1164. print "login2 - %s"%c.login2("ivars777",password)
  1165. #vid2 = "animation/ultimate_avengers_ii"
  1166. #vid2 = "animation/ice_age"
  1167. #vid2 = "tiesraide/ltv1"
  1168. #vid2 = "arhivs/1456521417815"
  1169. streams = c.get_stream_url2(vid2)
  1170. stream = streams[-1]
  1171. print
  1172. #for s in data:
  1173. #call([r"c:\Program Files\VideoLAN\VLC\vlc.exe",stream["url"]])
  1174. cmd = ["ffplay.exe",
  1175. "-headers","Referer:%s"%(quote(stream["headers"]["Referer"])),
  1176. "-headers","Connection:Keep-Alive",
  1177. stream["url"]]
  1178. print " ".join(cmd)
  1179. call(cmd)
  1180. pass
  1181. else:
  1182. if len(sys.argv)>1:
  1183. data= sys.argv[1]
  1184. else:
  1185. data = "ltc::home"
  1186. content = c.get_content(data)
  1187. for item in content:
  1188. print item
  1189. #cat = api.get_categories(country)
  1190. #chan = api.get_channels("lv")
  1191. #prog = api.get_programs(channel=6400)
  1192. #prog = api.get_programs(category=55)
  1193. #seas = api.get_seasons(program=6453)
  1194. #str = api.get_streams(660243)
  1195. #res = api.get_videos(802)
  1196. #formats = api.getAllFormats()
  1197. #det = api.detailed("1516")
  1198. #vid = api.getVideos("13170")
  1199. pass