Python module (submodule repositary), which provides content (video streams) from various online stream sources to corresponding Enigma2, Kodi, Plex plugins

replay.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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
  14. import ssl
  15. if "_create_unverified_context" in dir(ssl):
  16. ssl._create_default_https_context = ssl._create_unverified_context
  17. from SourceBase import SourceBase
  18. import util
  19. API_URL = 'http://replay.lsm.lv/%s/'
  20. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  21. headers0 = headers2dict("""
  22. 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
  23. """)
  24. import HTMLParser
  25. h = HTMLParser.HTMLParser()
  26. from YouTubeVideoUrl import YouTubeVideoUrl
  27. class Source(SourceBase):
  28. def __init__(self,country="lv",cfg_path=None):
  29. self.name = "replay"
  30. self.title = "Replay.lv (LTV)"
  31. self.img = "http://replay.lsm.lv/apple-touch-icon.png"
  32. self.desc = "LSM replay.lv satura skatīšanās"
  33. self.country=country
  34. self.pic_size = "327x250" #"1000x765"
  35. def get_content(self, data):
  36. print "[replay] get_content:", data
  37. if "::" in data:
  38. data = data.split("::")[1]
  39. path = data.split("?")[0]
  40. clist = path.split("/")[0]
  41. params = data[data.find("?"):] if "?" in data else ""
  42. qs = dict(map(lambda x:x.split("="),re.findall("\w+=\w+",params)))
  43. lang = qs["lang"] if "lang" in qs else self.country
  44. content=[]
  45. content.append(("..return", "back","","Return back"))
  46. if clist=="home":
  47. content.extend([
  48. ("Live streams", "replay::tiesraide","","TV live streams"),
  49. ("Search LV", "replay::search/?term={0}&lang=lv","","Search content LV"),
  50. ("Last videos LV", "replay::visi/jaunakie/?source=ltv&lang=lv","","Last aired videos LV"),
  51. ("Last videos by categories LV", "replay::kategorijas/?lang=lv","","Last videos by categories LV"),
  52. ("All programs LV", "replay::raidijumi/?type=video","","All programs by name LV"),
  53. ("Programs by categories LV", "replay::categories?lang=lv","","All programs by categories LV"),
  54. #("Channels", "replay::channels?language=%s"%self.country,"","TV channels"),
  55. ("Videos by popularity LV", "replay::visi/popularie/?source=ltv&lang=lv","","Programs by popularity"),
  56. ("Search RU", "replay::search/?term={0}&lang=ru","","Search content RU"),
  57. ("Last videos RU", "replay::vse/novie/?source=ltv&lang=ru","","Last aired videos RU"),
  58. ("Last videos by categories RU", "replay::kategorijas/?lang=ru","","Last videos by categories RU"),
  59. ("All programs RU", "replay::peredachi/?lang=ru&type=video","","All programs by name"),
  60. ("Programs by categories RU", "replay::categories?lang=ru","","Programs by categories RU")
  61. ])
  62. return content
  63. ### programmu kategorijas ###
  64. elif clist=="categories":
  65. url = "http://replay.lsm.lv/lv/raidijumi/?lang=lv&type=video" if lang =="lv" else "http://replay.lsm.lv/ru/peredachi/?lang=ru&type=video"
  66. r = self._http_request(url)
  67. for item in re.findall(r'<a .+href="(\?lang=\w+&type=video&theme=\d+)">([^<]+)</a>\t', r):
  68. title = item[1]
  69. data2 = url.split("?")[0]+item[0]
  70. data2 = data2.replace(API_URL%lang,"")
  71. img = ""
  72. desc = title
  73. content.append((title,self.name+"::"+data2,img,desc))
  74. return content
  75. ### jaunāko raidijumu kategorijas ###
  76. elif clist=="kategorijas":
  77. url = "http://replay.lsm.lv/lv/" if lang =="lv" else "http://replay.lsm.lv/ru/"
  78. r = self._http_request(url)
  79. for item in re.findall(r'<a href="/(lv|ru)/kategorija/(\w+)/">.+?<i class="[^"]+"></i>.+?<span>([^<]+)</span>', r, re.DOTALL):
  80. title = item[2]
  81. data2 = "kategorija/%s/?lang=%s"%(item[1],item[0])
  82. img = ""
  83. desc = title
  84. content.append((title,self.name+"::"+data2,img,desc))
  85. return content
  86. ### Tiešraides kanānālu saraksts
  87. elif path=="tiesraide":
  88. url = "http://replay.lsm.lv/styles/main.css"
  89. r= self._http_request(url)
  90. for item in re.findall(r'channel-logo--(\w+)\{background-image:url\("([^"]+\.png)"', r):
  91. ch = item[0]
  92. title = ch.upper()
  93. data2 = "tiesraide/%s/"%ch
  94. img = "http://replay.lsm.lv"+item[1]
  95. veids = "video "if "tv" in ch else "audio"
  96. desc = title+" tiesraide (%s)"%veids
  97. content.append((title,self.name+"::"+data2,img,desc))
  98. return content
  99. ### Kanāla tiesraide
  100. elif clist == "tiesraide" and "/" in data:
  101. ch = data.split('/')[1]
  102. veids = "video" if "tv" in ch else "audio"
  103. #url = "http://replay.lsm.lv/lv/tiesraide/ltv7/"
  104. url = "http://replay.lsm.lv/lv/tiesraide/%s/"%ch
  105. r= self._http_request(url)
  106. m = re.search('%s/">.+?<h5>([^<]+)+</h5>.*?<time>([^<]+)</time>'%ch, r, re.DOTALL)
  107. tagad = m.group(1).strip() if m else ""
  108. laiks = m.group(2).strip() if m else ""
  109. laiks = h.unescape(laiks).encode("utf8")
  110. m = re.search("<h1>([^<]+)</h1>", r)
  111. title = m.group(1).strip() if m else path.split("/")[1].upper()
  112. title = "%s - %s (%s)"%(title,tagad,laiks)
  113. if veids == "video":
  114. m = re.search('<div class="video"><iframe.+src="([^"]+)"', r)
  115. if not m:
  116. content=("No stream found %s"%data,"","","No stream found")
  117. return content
  118. url = m.group(1)
  119. headers = headers2dict("""
  120. 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
  121. Referer: http://replay.lsm.lv/lv/ieraksts/ltv/70398/tiesa-runa.-lielbritanija-gatavojas-referendumam-par-tu/
  122. """)
  123. r = self._http_request(url,headers=headers)
  124. m = re.search('<div class="video-player"><iframe.+src="([^"]+)"', r)
  125. if not m:
  126. content=("No stream found %s"%data,"","","No stream found")
  127. return content
  128. url = m.group(1)
  129. r = self._http_request(url,headers=headers)
  130. m = re.search('"([^"]+m3u8[^"]+)"', r)
  131. if not m:
  132. content=("No stream found %s"%data,"","","No stream found")
  133. return content
  134. data2 = m.group(1).replace("\\","")
  135. #r = self._http_request(data2, headers=headers)
  136. else: # audio
  137. lrn = ch.replace("lr","")
  138. url = "http://www.latvijasradio.lsm.lv/lv/tiesraide/?channel=%s"%lrn
  139. r = self._http_request(url)
  140. m = re.search('"file":"([^"]+?m3u8.*?)"', r)
  141. if not m:
  142. content=("No stream found %s"%data,"","","No stream found")
  143. return content
  144. data2 = m.group(1).replace("\\","")
  145. img = ""
  146. desc = ""
  147. content =(title,data2,img,desc)
  148. return content
  149. #m = re.search(r'(\?page=\d+)" class=" paging__prev', r, re.IGNORECASE)
  150. #if m:
  151. # data = re.sub("\?page=\d+", "", data)
  152. # data2 = data+m.group(1)
  153. # content.append(("Previous page",self.name+"::"+data2,"","Previous page"))
  154. r = self.call(data, lang=lang)
  155. if not r:
  156. return content
  157. if clist == "search":
  158. #for r2 in re.findall('<article itemtype="http://schema.org/Article" itemscope class="thumbnail thumbnail--default ">(.+?)</article>', r2, re.DOTALL):
  159. for item in re.findall('itemprop="image" data-image="([^"]+)".+?<figcaption><h5 itemprop="name"><a itemprop="url" href="([^<]+)">([^<]+)</a></h5></figcaption>', r):
  160. title = item[2]
  161. data2 = item[1].replace("/%s/"%lang,"")+"?lang=%s"%lang
  162. img = "http://replay.lsm.lv" + item[0]
  163. desc = title
  164. content.append((title,self.name+"::"+data2,img,desc))
  165. #for item in re.findall('itemprop="image" data-image="([^"]+)".+?<figcaption><h4 itemprop="about"><a href="([^"]+)">([^<]+)</a></h4>.*?<h5 itemprop="name"><a itemprop="url" href="([^"]+)">([^<]+)</a></h5>.+?datetime="([^"]+)" class="thumbnail__date ">([^<]+)</time>', r2):
  166. for item in re.findall('itemprop="image" data-image="([^"]+)".+? class="icon-(ltv|lr).+?<figcaption><h4 itemprop="about"><a href="([^"]+)">([^<]+)</a></h4>.*?<h5 itemprop="name"><a itemprop="url" href="([^"]+)">([^<]+)</a></h5>.+?datetime="([^"]+)" class="thumbnail__date ">([^<]+)</time>', r):
  167. if item[1]=="lr":continue
  168. title = "%s - %s (%s)"%(item[3],item[5],item[7])
  169. data2 = item[4].replace("/%s/"%lang,"")+"?lang=%s"%lang
  170. img = item[0].replace("https:","http:")
  171. desc = title
  172. content.append((title,self.name+"::"+data2,img,desc))
  173. ### Raidijumi (programmas) ###
  174. elif clist in ( "raidijumi","peredachi"):
  175. for item in re.findall('<li itemprop="name"><a href="([^"]+)" itemprop="url">([^<]+)', r):
  176. #for item in re.findall('<li itemprop="name"><a href="([^"]+)" itemprop="url">([^<]+)</a></li>', r):
  177. title = item[1]
  178. data2 = item[0].replace("/%s/"%lang,"")+"?lang=%s"%lang
  179. img = ""
  180. desc = ""
  181. content.append((title,self.name+"::"+data2,img,desc))
  182. ### Raidijuma ieraksti speciālie###
  183. elif clist in ( "visi","vse",):
  184. for item in re.findall('(?i)<figure><a href="([^"]+)" itemprop="image" data-image="([^"]+)".+class="thumbnail__duration">([^<]+)</time></figure><figcaption><h4 itemprop="about"><a href="[^"]+">([^<]+)</a></h4>.+>([^<]+).*</h5>.+>([^<]+)</time></figcaption>', r):
  185. title = item[3]
  186. data2 = item[0].replace("/%s/"%lang,"")+"?lang=%s"%lang
  187. img = item[1].replace("https:","http:")
  188. desc = "%s - %s\n%s"%(item[5],item[2],item[4])
  189. content.append((title,self.name+"::"+data2,img,desc))
  190. ### Raidijuma ieraksti (videos)
  191. elif clist in ("raidijums","peredacha","kategorija"):
  192. for item in re.findall('<article .+ href="([^"]+)".+image="([^"]+)".+class="thumbnail__duration">([^<]+).+">([^<]+).+class="thumbnail__date ">([^"]+)</time></figcaption></article>', r):
  193. title = item[3]
  194. data2 = item[0].replace("/%s/"%lang,"")+"?lang=%s"%lang
  195. img = item[1].replace("https:","http:")
  196. desc = "%s - %s"%(item[4],item[2])
  197. content.append((title,self.name+"::"+data2,img,desc))
  198. ### Ieraksts (video) ###
  199. elif clist in ("ieraksts","statja"):
  200. m = re.search('src="([^"]+)"></iframe>', r)
  201. if m:
  202. url2 = m.group(1)
  203. headers = headers2dict("""
  204. 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
  205. Referer: http://replay.lsm.lv/lv/ieraksts/ltv/70398/tiesa-runa.-lielbritanija-gatavojas-referendumam-par-tu/
  206. """)
  207. r2 = self._http_request(url2,headers=headers)
  208. m = re.search('"file":"([^"]+)', r2)
  209. if m:
  210. data2 = m.group(1).replace("\\","")
  211. m = re.search('"idstring":"([^"]+)', r2)
  212. title = m.group(1) if m else ""
  213. title = title.decode("unicode-escape").encode("utf8")
  214. title = title.replace("\n","")
  215. img = ""
  216. desc = ""
  217. if "youtube" in data2:
  218. video_id = re.search(r"/watch\?v=([^&]+)",data2).group(1)
  219. data2 = YouTubeVideoUrl().extract(video_id)
  220. if not data2:
  221. content=("No stream found %s"%data,"","","No stream found")
  222. return content
  223. content =(title,data2,img,desc)
  224. return content
  225. content=("No stream found %s"%data,"","","No stream found")
  226. return content
  227. m = re.search(r'href="\?([^"]+)" class=" paging__next', r)
  228. if m:
  229. page = int(re.search("page=(\d+)",m.group(1)).group(1))
  230. if "page="in data:
  231. data2 = re.sub("page=\d+","page=%i"%page,data)
  232. else:
  233. if "?" in data:
  234. data2 =data+"&page=%i"%page
  235. else:
  236. data2 =data+"?page=%i"%page
  237. content.append(("Next page",self.name+"::"+data2,"","Next page"))
  238. return content
  239. def is_video(self,data):
  240. if "::" in data:
  241. data = data.split("::")[1]
  242. cmd = data.split("/")
  243. if cmd[0] in ("ieraksts","statja"):
  244. return True
  245. elif cmd[0]=="tiesraide" and len(cmd)>1:
  246. return True
  247. else:
  248. return False
  249. def call(self, data,headers=headers0,lang=""):
  250. if not lang: lang = self.country
  251. url = API_URL%lang + data
  252. #print "[TVPlay Api] url: ",url
  253. result = []
  254. content = self._http_request(url,headers=headers0)
  255. return content
  256. if __name__ == "__main__":
  257. country= "lv"
  258. c = Source(country)
  259. if len(sys.argv)>1:
  260. data= sys.argv[1]
  261. else:
  262. data = "home"
  263. content = c.get_content(data)
  264. for item in content:
  265. print item
  266. #cat = api.get_categories(country)
  267. #chan = api.get_channels("lv")
  268. #prog = api.get_programs(channel=6400)
  269. #prog = api.get_programs(category=55)
  270. #seas = api.get_seasons(program=6453)
  271. #str = api.get_streams(660243)
  272. #res = api.get_videos(802)
  273. #formats = api.getAllFormats()
  274. #det = api.detailed("1516")
  275. #vid = api.getVideos("13170")
  276. pass