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

play24.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. API_URL = 'http://replay.lsm.lv/'
  18. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  19. headers0 = headers2dict("""
  20. 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
  21. """)
  22. import HTMLParser
  23. h = HTMLParser.HTMLParser()
  24. class Source(SourceBase):
  25. def __init__(self,country="lv",cfg_path=None):
  26. self.name = "play24"
  27. self.title = "Play24.lv"
  28. self.img = "http://play24.lv/images/play24-logo-black.png"
  29. self.desc = "play24.lv (Riga24TV) satura skatīšanās"
  30. self.country=country
  31. def get_content(self, data):
  32. print "[play24] 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 stream", "play24::tiesraide","","TV live streams"),
  45. ("Last videos", "play24::jaunakie","","Last videos"),
  46. ("Categories", "play24::kategorijas","","Categories"),
  47. ("Programs", "play24::raidijumi","","Programs"),
  48. ])
  49. return content
  50. ### Jaunākie video ###
  51. elif clist=="jaunakie":
  52. url = "http://play24.lv/"
  53. r = self._http_request(url)
  54. for item in re.findall(' <div class="top-article__image">.*?<a class="top-article__image-link" href="([^"]+)">.*?<img.+?src="([^"]+)".+?alt="([^"]+)" />.+?</picture>', r, re.DOTALL):
  55. title = item[2]
  56. title = h.unescape(title.decode("utf8")).encode("utf8")
  57. img = item[1]
  58. data2 = item[0].replace("http://play24.lv/","")
  59. desc = title
  60. content.append((title,self.name+"::"+data2,img,desc))
  61. return content
  62. ### Kategorijas ###
  63. elif clist=="kategorijas":
  64. url = "http://play24.lv/"
  65. r = self._http_request(url)
  66. r2 = r[r.find('<div class="footer-navigation">'):]
  67. for item in re.findall('<a href="http://play24.lv/(kategorija/[^"]+)" class="navigation__link">([^<]+)</a>', r2, re.DOTALL):
  68. title = item[1]
  69. data2 = item[0]
  70. img = ""
  71. desc = title
  72. content.append((title,self.name+"::"+data2,img,desc))
  73. return content
  74. elif clist=="kategorija":
  75. url = "http://play24.lv/"+data
  76. r = self._http_request(url)
  77. for article in re.findall(r"<article\b[^>]*>(.+?)</article>", r, re.DOTALL):
  78. m = re.search('<a class="masonry-item__link" href="http://play24\.lv/([^"]+)">', article, re.DOTALL)
  79. data2 = m.group(1) if m else ""
  80. m = re.search('<img src="([^"]+)" alt="([^"]+)" />', article, re.DOTALL)
  81. if m:
  82. img = m.group(1)
  83. title = m.group(2)
  84. title = h.unescape(title.decode("utf8")).encode("utf8")
  85. else:
  86. img = ""
  87. title = ""
  88. m = re.search(r'<span class="masonry-item__tags">\s+<a href="([^"]+)">([^<]+)</a>.*?</span>', article, re.DOTALL)
  89. progr = m.group(2) if m else ""
  90. m = re.search('<span class="masonry-item__date">([^<]+)</span>', article, re.DOTALL)
  91. date = m.group(1).strip() if m else ""
  92. if date:
  93. title = title + " (%s %s)"%(date,progr)
  94. desc = title + "\n%s - %s"%(progr,date)
  95. content.append((title,self.name+"::"+data2,img,desc))
  96. m = re.search(r'<li><a href="http://play24\.lv/([^"]+)" rel="next">&raquo;</a></li>', r, re.DOTALL)
  97. if m:
  98. data2 = m.group(1)
  99. content.append(("Next page",self.name+"::"+data2,"","Next page"))
  100. return content
  101. ### Raidijumi (programmas)
  102. elif clist=="raidijumi":
  103. url = "http://play24.lv/"
  104. r = self._http_request(url)
  105. for item in re.findall(r'<li class="tag-box__item">.*?<a href="http://play24\.lv/(birka/[^"]+)">([^<]+)</a>.*?</li>', r, re.DOTALL):
  106. title = item[1]
  107. title = h.unescape(title.decode("utf8")).encode("utf8")
  108. data2 = item[0]
  109. img = ""
  110. desc = title
  111. content.append((title,self.name+"::"+data2,img,desc))
  112. return content
  113. ### Programmas (video saraksts)
  114. elif clist=="birka":
  115. url = "http://play24.lv/"+data
  116. r = self._http_request(url)
  117. for item in re.findall(r'<article\b[^>]*>.+?<a class="masonry-item__link" href="http://play24.lv/([^"]+)">.*?<img src="([^"]+)" alt="([^"]+)" />.*?<span class="masonry-item__tags">.+?<a href="([^"]+)">([^<]+)</a>.*?<span class="masonry-item__date">([^<]+)</span>.*?</article>', r, re.DOTALL):
  118. title = item[2]
  119. title = h.unescape(title.decode("utf8")).encode("utf8")
  120. title = title + " (%s)"%item[5].strip()
  121. img = item[1]
  122. data2 = item[0]
  123. desc = title + "\n%s - %s"%(item[4],item[5].strip())
  124. content.append((title,self.name+"::"+data2,img,desc))
  125. m = re.search(r'<li><a href="http://play24\.lv/([^"]+)" rel="next">&raquo;</a></li>', r, re.DOTALL)
  126. if m:
  127. data2 = m.group(1)
  128. content.append(("Next page",self.name+"::"+data2,"","Next page"))
  129. return content
  130. elif clist == "video" or clist == "tiesraide":
  131. if clist == "video":
  132. url = "http://play24.lv/"+data
  133. r = self._http_request(url)
  134. # var ov_video_id = '59422';
  135. m = re.search(r"var ov_video_id = '(\d+)';", r, re.DOTALL)
  136. if m:
  137. id = m.group(1)
  138. else:
  139. return ("No stream found %s"%data,"","","No stream found")
  140. m = re.search('<meta name="description" content="([^"]+)" />', r, re.DOTALL)
  141. desc = m.group(1) if m else ""
  142. desc = h.unescape(desc.decode("utf8")).encode("utf8")
  143. url = "http://player.tvnet.lv/v/%s"%id
  144. else:
  145. url = "http://player.tvnet.lv/l/11"
  146. desc = ""
  147. r = self._http_request(url)
  148. m = re.search('<h1 class="static title">.+?<a href="[^"]+">([^<]+)</a>', r, re.DOTALL)
  149. title = m.group(1) if m else ""
  150. s = {}
  151. for item in re.findall('source src="([^"]+)" data-stream="([^"]+)" data-quality="([^"]+)"', r, re.DOTALL):
  152. s[item[1]] = (item[0],item[2])
  153. data2 = ""
  154. for t in ("hls","http","rtmp"):
  155. if t in s:
  156. data2 = s[t][0]
  157. break
  158. return (title,data2,"",desc)
  159. def is_video(self,data):
  160. if "::" in data:
  161. data = data.split("::")[1]
  162. cmd = data.split("/")
  163. if cmd[0] in ("video","tiesraide"):
  164. return True
  165. else:
  166. return False
  167. def call(self, data,headers=headers0,lang=""):
  168. if not lang: lang = self.country
  169. url = API_URL%lang + data
  170. #print "[TVPlay Api] url: ",url
  171. result = []
  172. content = self._http_request(url)
  173. return content
  174. if __name__ == "__main__":
  175. country= "lv"
  176. c = Source(country)
  177. if len(sys.argv)>1:
  178. data= sys.argv[1]
  179. else:
  180. data = "home"
  181. content = c.get_content(data)
  182. for item in content:
  183. print item
  184. #cat = api.get_categories(country)
  185. #chan = api.get_channels("lv")
  186. #prog = api.get_programs(channel=6400)
  187. #prog = api.get_programs(category=55)
  188. #seas = api.get_seasons(program=6453)
  189. #str = api.get_streams(660243)
  190. #res = api.get_videos(802)
  191. #formats = api.getAllFormats()
  192. #det = api.detailed("1516")
  193. #vid = api.getVideos("13170")
  194. pass