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

play24.py 8.5KB

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