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

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