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

tvdom.py 13KB

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