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

play24.py 8.6KB

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