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

tvdom.py 13KB

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