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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 requests
  13. import datetime, re, sys,os
  14. import ConfigParser
  15. import ssl
  16. ssl._create_default_https_context = ssl._create_unverified_context
  17. from SourceBase import SourceBase
  18. import resolver
  19. try:
  20. import util
  21. except:
  22. sys.path.insert(0,'..')
  23. import util
  24. from YouTubeVideoUrl import YouTubeVideoUrl
  25. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  26. import HTMLParser
  27. h = HTMLParser.HTMLParser()
  28. class Source(SourceBase):
  29. def __init__(self, country="",cfg_path=None):
  30. self.name = "lmt"
  31. self.title = "LMT straume"
  32. self.img = "http://www.lob.lv/images/logo/lmt_straume_vert_rgb.png"
  33. self.desc = "LMT straume - dažādi video latviesu valodā"
  34. self.headers = headers2dict("""
  35. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  36. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  37. Accept-Language: en-US,en;q=0.5
  38. """)
  39. self.headers2 = headers2dict("""
  40. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  41. X-Requested-With: XMLHttpRequest
  42. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  43. """)
  44. self.url = "http://straume.lmt.lv/lv/"
  45. ######### Entry point ########
  46. def get_content(self, data):
  47. print "[lmt] get_content:", data
  48. source, data, path, plist, clist, params, qs = self.parse_data(data)
  49. content = []
  50. content.append(("..return", "back","","Return back"))
  51. if clist=="home":
  52. content.extend([
  53. ("Meklēt", "lmt::meklet?q={0}","","Meklēt"),
  54. ("Straumes", "lmt::straumes","","Tiešraides un aktuāli video"),
  55. #("TV", "lmt::tv","","TV tiešraides (tikai LMT tīklā)"),
  56. ("Jaunākie", "lmt::video/jaunakie?videoPage=1", "", "Visu žanru jaunākie video"),
  57. ("Sports", "lmt::video/sports?videoPage=1", "", "Sports"),
  58. ("Kultūra un māksla", "lmt::video/kultura?videoPage=1", "", "Kultūra un māksla"),
  59. ("Konferences", "lmt::video/konferences?videoPage=1", "", "Konferences"),
  60. ("Raidījumi", "lmt::video/raidijumi?videoPage=1", "", "Raidījumi"),
  61. ("Notikumi", "lmt::video/notikumi?videoPage=1", "", "Notikumi"),
  62. ("Filmas un seriāli", "lmt::video/filmas?videoPage=1", "", "Filmas un seriāli"),
  63. ("Dažādi video", "lmt::video/dazadi?videoPage=1", "", "Dažādi video"),
  64. ("Viedtelevīzija", "lmt::video/viedtelevizija?videoPage=1", "", "Viedtelevīzija"),
  65. ])
  66. return content
  67. elif clist in ("meklet","video", "straumes","video-saraksts"):
  68. r=self.call(data)
  69. result = re.findall('<a class="video-picture" (.+?)</li>', r, re.IGNORECASE | re.MULTILINE)
  70. for r2 in result:
  71. m = re.search('<a class="video-title" href="/lv/([^"]+)">([^<]+)<', r2)
  72. title = m.group(2)
  73. data2 = m.group(1)
  74. m = re.search("([^ ]+) 2x", r2)
  75. if m:
  76. img = m.group(1)
  77. else:
  78. m = re.search('<img src="([^"]+)', r2)
  79. img = m.group(1) if m else ""
  80. m = re.search('<span class="playlist-overlay">([^<]+)</span>', r2)
  81. overlay = m.group(1) if m else ""
  82. m = re.search('<span class="badge badge-[^>]+>([^<]+)(<[^>]+>([^<]+))*</span>', r2, re.IGNORECASE)
  83. badge = ""
  84. if m:
  85. badge = m.group(1)
  86. if m.group(3):
  87. badge = badge + m.group(3)
  88. categories = re.findall('<span class="category-title">([^<]+)</span>', r2)
  89. categories = "".join(categories)
  90. if overlay:
  91. title = "%s [%s]"%(title,overlay)
  92. if badge:
  93. title = "%s [%s]"%(title,badge)
  94. desc = title
  95. if categories:
  96. desc = desc + "\n"+ categories
  97. content.append((title,self.name+"::"+data2,img,desc))
  98. m = re.search("videoPage=(\d+)",data)
  99. if m:
  100. page = int(m.group(1))+1
  101. data2 = re.sub(r"videoPage=\d+", r"videoPage=%s"%page, data)
  102. content.append(("Next page",self.name+"::"+data2,self.img,"Next page"))
  103. #print content
  104. return content
  105. ### kaut kas neparedzets ###
  106. else:
  107. return content
  108. def is_video(self,data):
  109. source,data,path,plist,clist,params,qs = self.parse_data(data)
  110. if not clist in ("meklet","video", "straumes","video-saraksts","home"):
  111. return True
  112. def call(self, data,params=None,headers=None,lang=""):
  113. if not headers: headers = self.headers
  114. url = self.url+data
  115. r = requests.get(url,headers = headers)
  116. return r.content
  117. #result = self._http_request(url,params,headers=headers)
  118. return result
  119. def get_streams(self,data):
  120. print "[lmt] get_streams:", data
  121. if not self.is_video(data):
  122. return []
  123. source,data,path,plist,clist,params,qs = self.parse_data(data)
  124. r = self.call(path)
  125. title = re.search("<h1>(.+?)</h1", r, re.IGNORECASE).group(1)
  126. m = re.search('<a class="category-title".+?[^>]+>([^<]+)<', r, re.IGNORECASE | re.DOTALL)
  127. categories = m.group(1) if m else ""
  128. m = re.search('<span class="category-title">([^<]+)</span>.+?</p>', r, re.IGNORECASE | re.DOTALL)
  129. if m:
  130. categories = categories + m.group(1)
  131. if categories:
  132. tite = "%s [%s]"%(title,categories)
  133. img = re.search('<meta property="twitter:image" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  134. desc = title + "\n" + re.search('<meta property="og:description" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  135. m = re.search('file: "([^"]+)"', r, re.IGNORECASE)
  136. if m:
  137. data2 = m.group(1)
  138. stream = util.item()
  139. stream["name"] = title
  140. stream["url"] = data2
  141. stream["img"] = img
  142. stream["desc"] = desc
  143. stream["resolver"] = "lmt"
  144. return [stream]
  145. elif re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r):
  146. m = re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r)
  147. video_id = m.group(1)
  148. #http://www.youtube.com/embed/RUyQ_JJ6A84?rel=0&fs=1&wmode=transparent
  149. data2 = YouTubeVideoUrl().extract(video_id)
  150. s = util.item()
  151. s["name"] = title
  152. s["url"] = data2
  153. s["desc"] = desc
  154. s["img"] = img
  155. s["resolver"] = "lmt"
  156. return [s]
  157. elif 'src="http://cdn.tiesraides.lv/lmtstraume.lv/' in r:
  158. m = re.search('src="(http://cdn\.tiesraides\.lv/[^"]+)"',r)
  159. url = m.group(1)
  160. # src="http://cdn.tiesraides.lv/lmtstraume.lv/live-record2-ip/40?c=614127284dcd58d8a84afcf498a3ac7a&v=1405"
  161. r = self._http_request(url)
  162. #http://edge-telia2.tiesraides.lv/live-record2/lmtstraume.lv.40_1/manifest.f4m
  163. m = re.search("'(http://.+?\.m3u8)'",r)
  164. data2 = m.group(1) if m else ""
  165. s = util.item()
  166. s["name"] = title
  167. s["url"] = data2
  168. s["desc"] = desc
  169. s["img"] = img
  170. s["resolver"] = "lmt"
  171. return [s]
  172. else:
  173. raise Exception("No stream found")
  174. # streams = resolver.resolve(url)
  175. # for s in streams:
  176. # s["name"] = title
  177. # s["desc"] = desc
  178. # s["img"] = img
  179. # streams.append(s)
  180. # return streams
  181. if __name__ == "__main__":
  182. country= "lv"
  183. c = Source(country)
  184. if len(sys.argv)>1:
  185. data= sys.argv[1]
  186. else:
  187. data = "home"
  188. content = c.get_content(data)
  189. for item in content:
  190. print item
  191. #cat = api.get_categories(country)
  192. #chan = api.get_channels("lv")
  193. #prog = api.get_programs(channel=6400)
  194. #prog = api.get_programs(category=55)
  195. #seas = api.get_seasons(program=6453)
  196. #str = api.get_streams(660243)
  197. #res = api.get_videos(802)
  198. #formats = api.getAllFormats()
  199. #det = api.detailed("1516")
  200. #vid = api.getVideos("13170")
  201. pass