Kodi plugin to to play various online streams (mostly Latvian)

play24.py 8.9KB

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