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

tvdom.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/usr/bin/env python
  2. # coding=utf8
  3. #
  4. # This file is part of PlayStream - enigma2 plugin to play video streams from various sources
  5. # Copyright (c) 2016 ivars777 (ivars777@gmail.com)
  6. # Distributed under the GNU GPL v3. For full terms see http://www.gnu.org/licenses/gpl-3.0.en.html
  7. #
  8. try:
  9. import json
  10. except:
  11. import simplejson as json
  12. import urllib2, urllib
  13. import datetime, re, sys,os
  14. from collections import OrderedDict
  15. import ssl
  16. ssl._create_default_https_context = ssl._create_unverified_context
  17. from SourceBase import SourceBase
  18. API_URL = 'http://replay.lsm.lv/'
  19. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  20. headers0 = headers2dict("""
  21. User-Agent: Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
  22. """)
  23. import HTMLParser
  24. h = HTMLParser.HTMLParser()
  25. class Source(SourceBase):
  26. def __init__(self,country="lv",cfg_path=None):
  27. self.name = "tvdom"
  28. self.title = "TVDom.tv"
  29. self.img = "https://tvdom.tv/front/assets/images/logo.png"
  30. self.desc = "TVDom.tv portāla satura skatīšanās"
  31. self.headers = headers0
  32. self.country=country
  33. self.session = None
  34. self.token = None
  35. cur_directory = os.path.dirname(os.path.abspath(__file__))
  36. if not cfg_path: cfg_path = cur_directory
  37. self.config_file = os.path.join(cfg_path,self.name+".cfg")
  38. self.options = OrderedDict([("user","lietotajs"),("password","parole")])
  39. self.options_read()
  40. def login(self,user="",password=""):
  41. self.options_read()
  42. if not user: user=self.options["user"]
  43. if not password: password = self.options["password"]
  44. headers = headers2dict("""
  45. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
  46. Accept: */*
  47. Accept-Language: en-US,en;q=0.5
  48. Accept-Encoding: gzip, deflate, br
  49. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  50. X-Requested-With: XMLHttpRequest
  51. Referer: https://tvdom.tv/
  52. """)
  53. url = "https://tvdom.tv/infinity/on_register_user"
  54. params = "email=%s&password=%s&remember=false&auth_type=login"%(user,password)
  55. import requests
  56. try:
  57. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  58. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  59. except:
  60. pass
  61. r = requests.post(url, data=params, headers=headers)
  62. js = json.loads(r.content)
  63. if 'success' in r.content:
  64. self.token = js["access_token"]
  65. if 'PHPSESSID' in r.cookies:
  66. self.session = r.cookies["PHPSESSID"]
  67. return True
  68. else:
  69. raise Exception(js["error"])
  70. def get_content(self, data):
  71. print "[tvdom] get_content:", data
  72. source,data,path,plist,clist,params,qs = self.parse_data(data)
  73. lang = qs["lang"] if "lang" in qs else self.country
  74. content=[]
  75. content.append(("..return", "back","","Return back"))
  76. if clist=="home":
  77. content.extend([
  78. ("Live stream", "tvdom::tiesraides","","TV live streams"),
  79. ("Archive - categories", "tvdom::arhivs","","Video archive by categories"),
  80. ("Search", "tvdom::search/?srch-term={0}","","Search archive"),
  81. #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
  82. ])
  83. return content
  84. ### Tiesraides kanalu saraksts ###
  85. elif data=="tiesraides":
  86. ch_name = {"49":"Дом Кино","50":"Карусель","51":"Время","52":"Музыка Первого","53":"Телекафе"}
  87. url = "https://tvdom.tv/"
  88. r = self._http_request(url)
  89. #channels1 = re.findall(r'data-xprs_status="\d" data-href="/tiesraides/([^/]+)/[^"]+">.+?src="([^"]+)" alt="([^"]+)">', r, re.DOTALL)
  90. channels2 = re.findall(r'<a class="channel-name">([^<]*)</a>\s+<div class="redirect-to-url" data-href="/tiesraides/([^/]+)/[^"]+">.+?<img style="width:100%;" src="([^"]+)".+?<h3>([^<]+)</h3>.+?<p class="unix">([^<]+)</p>', r, re.DOTALL)
  91. channels = {}
  92. for item in channels2:
  93. title = item[0]
  94. title = h.unescape(title.decode("utf8")).encode("utf8")
  95. img = "https://tvdom.tv"+item[2]
  96. data2 = "tiesraides/%s/"%item[1]
  97. desc = "%s\n%s\n%s"%(title,item[3],item[4])
  98. channels[item[1]]={"title":title,"img":img,"desc":desc}
  99. #content.append((title,self.name+"::"+data2,img,desc))
  100. for r2 in re.findall(r'<div class="thumbnail-menu pull-left updater"(.+?)</div>\s+</div>', r, re.DOTALL):
  101. ch = re.search('data-href="/tiesraides/([^/]+)/[^"]+"', r2, re.DOTALL).group(1)
  102. data2 = "tiesraides/"+ch
  103. m = re.search('src="(.+?)" alt="(.+?)">', r2, re.DOTALL)
  104. title = m.group(2)
  105. img = m.group(1)
  106. desc = title
  107. if ch in channels:
  108. img = channels[ch]["img"]
  109. desc = channels[ch]["desc"]
  110. else:
  111. m = re.search('<img src="([^"]+)" alt="aaaaaa([^"]+)"><h3>.+<p class="unix">([^<]+)</p>', r2, re.DOTALL)
  112. if m:
  113. desc = "%s\n%s\n%s"%(title,m.group(1),m.group(3))
  114. img = m.group(2)
  115. content.append((title,self.name+"::"+data2,img,desc))
  116. return content
  117. elif clist == "tiesraides":
  118. if not self.session:
  119. self.login()
  120. url = "https://tvdom.tv/" + data
  121. headers = self.headers
  122. headers["Cookie"] = "PHPSESSID=%s; neverending_story=1;"%self.session
  123. r = self._http_request(url,headers=headers)
  124. m = re.search("var streamConnectionUrl = '([^']+)'", r, re.DOTALL)
  125. if m:
  126. data2 = m.group(1)
  127. else:
  128. return ("No stream found %s"%data,"","","No stream found")
  129. m = re.search('title: "([^"]+)"', r, re.DOTALL)
  130. title = m.group(1) if m else data2
  131. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  132. desc = m.group(1) if m else title
  133. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  134. desc = m.group(1) if m else title
  135. m = re.search('var promo_image *= "([^"]+)', r, re.DOTALL)
  136. img = m.group(1) if m else ""
  137. return (title,data2,img,desc)
  138. ### Search ###
  139. elif clist=="search":
  140. url = "https://tvdom.tv/" + data
  141. r = self._http_request(url)
  142. for item in re.findall(r'<li data-xprs-search="\d+" data-href="([^"]+)".*?<img class="img-responsive" src="([^"]+)".*?<h3>([^<]+)</h3>.*?<h5>([^<]+)</h5>', r, re.DOTALL):
  143. title = item[2] + " "+ item[3]
  144. title = h.unescape(title.decode("utf8")).encode("utf8")
  145. img = "https://tvdom.tv" + item[1]
  146. data2 = item[0][1:]
  147. desc = title
  148. content.append((title,self.name+"::"+data2,img,desc))
  149. return content
  150. ### Arhīva kategorijas ###
  151. elif data=="arhivs":
  152. url = "https://tvdom.tv/"+data
  153. r = self._http_request(url)
  154. for item in re.findall('pointer" href="/([^"]+)">([^<]+)</a>', r, re.DOTALL):
  155. title = item[1]
  156. title = h.unescape(title.decode("utf8")).encode("utf8")
  157. img = ""
  158. data2 = item[0]
  159. desc = title
  160. content.append((title,self.name+"::"+data2,img,desc))
  161. return content
  162. ### Arhīva kategorijas programmas ###
  163. elif clist=="arhivs":
  164. url = "https://tvdom.tv/"+data
  165. r = self._http_request(url)
  166. for item in re.findall(r"""<li><div class="thumbnail pull-left" onclick="location\.href='([^']+)'" data-toggle="popover" title="([^"]+)" data-content="([^"]*)".+?<img class="img-responsive archive-image" src="([^"]+)""", r, re.DOTALL):
  167. #for item in re.findall(r"""<li><div class="thumbnail pull-left" onclick="location\.href='([^']+)'" data-toggle="popover" title="([^"]+)" data-content="([^"]+)".+?<img class="img-responsive archive-image" src="([^"]+)""", r, re.DOTALL):
  168. title = item[1].replace("&lt;br&gt;"," - ")
  169. title = h.unescape(title.decode("utf8")).encode("utf8")
  170. img = "https://tvdom.tv"+item[3]
  171. data2 = item[0][1:]
  172. desc = item[2]
  173. content.append((title,self.name+"::"+data2,img,desc))
  174. return content
  175. ### Arhīva programmas video saraksts ###
  176. elif clist=="play_arhivs" and len(data.split("/"))==3 and not re.search("_\d+",plist[2]):
  177. url = "https://tvdom.tv/"+data
  178. r = self._http_request(url)
  179. vid=re.search(r"id:(\d+), type: type", r, re.DOTALL).group(1)
  180. data2 = data+"_"+vid
  181. m = re.search('program_title = "([^"]+)"', r, re.DOTALL)
  182. title = m.group(1) if m else data2
  183. m = re.search('<a class="episode">Pārraides laiks ēterā: <span>([^<]+)</span></a>', r, re.DOTALL)
  184. datums = m.group(1) if m else ""
  185. title = title + " " + datums
  186. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  187. desc = m.group(1) if m else title
  188. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  189. desc = m.group(1) if m else title
  190. m = re.search('var share_image *= "([^"]+)', r, re.DOTALL)
  191. img = m.group(1) if m else ""
  192. content.append((title,self.name+"::"+data2,img,desc))
  193. i = r.find('<span class="slider-top-title"')
  194. if i>0: r = r[:i]
  195. for item in re.findall('<div class="col-md-9 redirect-to-url same-event" data-href="/([^"]+)">.+?image" src="([^"]+)".+?<h3 class="same-title">([^<]+)</h3>.*?<h5 class="same-online">([^<]+)</h5>', r, re.DOTALL):
  196. title = item[2] + " " + item[3]
  197. title = h.unescape(title.decode("utf8")).encode("utf8")
  198. img = "https://tvdom.tv"+item[1]
  199. data2 = item[0]
  200. desc = title # TODO
  201. content.append((title,self.name+"::"+data2,img,desc))
  202. return content
  203. ### Arhīva video
  204. elif clist=="play_arhivs" and len(data.split("/"))==3 and re.search("_\d+",plist[2]):
  205. url = "https://tvdom.tv/" + data
  206. headers = self.headers
  207. headers["Cookie"] = "PHPSESSID=%s; neverending_story=1;"%self.session
  208. r = self._http_request(url,headers=headers)
  209. m = re.search('var streamConnectionUrl = "([^"]+)"', r, re.DOTALL)
  210. if m:
  211. data2 = m.group(1)
  212. else:
  213. return ("No stream found %s"%data,"","","No stream found")
  214. m = re.search('program_title = "([^"]+)"', r, re.DOTALL)
  215. title = m.group(1) if m else data2
  216. m = re.search('<a class="episode">Pārraides laiks ēterā: <span>([^<]+)</span></a>', r, re.DOTALL)
  217. datums = m.group(1) if m else ""
  218. title = title + " " + datums
  219. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  220. desc = m.group(1) if m else title
  221. m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
  222. desc = m.group(1) if m else title
  223. m = re.search('var share_image *= "([^"]+)', r, re.DOTALL)
  224. img = m.group(1) if m else ""
  225. return (title,data2,img,desc)
  226. def is_video(self,data):
  227. source,data,path,plist,clist,params,qs = self.parse_data(data)
  228. cmd = data.split("/")
  229. if cmd[0] in ("tiesraides") and len(cmd)>1:
  230. return True
  231. elif cmd[0]=="play_arhivs" and len(cmd)==3 and re.search("_\d+",plist[2]):
  232. return True
  233. else:
  234. return False
  235. def call(self, data,headers=headers0,lang=""):
  236. if not lang: lang = self.country
  237. url = API_URL%lang + data
  238. #print "[TVPlay Api] url: ",url
  239. result = []
  240. content = self._http_request(url)
  241. return content
  242. if __name__ == "__main__":
  243. country= "lv"
  244. c = Source(country)
  245. if len(sys.argv)>1:
  246. data= sys.argv[1]
  247. else:
  248. data = "home"
  249. content = c.get_content(data)
  250. for item in content:
  251. print item
  252. #cat = api.get_categories(country)
  253. #chan = api.get_channels("lv")
  254. #prog = api.get_programs(channel=6400)
  255. #prog = api.get_programs(category=55)
  256. #seas = api.get_seasons(program=6453)
  257. #str = api.get_streams(660243)
  258. #res = api.get_videos(802)
  259. #formats = api.getAllFormats()
  260. #det = api.detailed("1516")
  261. #vid = api.getVideos("13170")
  262. pass