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

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