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

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