Kodi plugin to to play various online streams (mostly Latvian)

tvdom.py 12KB

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