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

tvdom.py 13KB

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