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

lmt.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. if "_create_unverified_context" in dir(ssl):
  17. ssl._create_default_https_context = ssl._create_unverified_context
  18. from SourceBase import SourceBase
  19. import resolver
  20. try:
  21. import util
  22. except:
  23. sys.path.insert(0,'..')
  24. import util
  25. from YouTubeVideoUrl import YouTubeVideoUrl
  26. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  27. import HTMLParser
  28. h = HTMLParser.HTMLParser()
  29. class Source(SourceBase):
  30. def __init__(self, country="",cfg_path=None):
  31. self.name = "lmt"
  32. self.title = "LMT straume"
  33. self.img = "lmt.png"
  34. self.desc = "LMT straume - dažādi video latviesu valodā"
  35. self.headers = headers2dict("""
  36. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  37. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  38. Accept-Language: en-US,en;q=0.5
  39. """)
  40. self.headers2 = headers2dict("""
  41. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  42. X-Requested-With: XMLHttpRequest
  43. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  44. """)
  45. self.url = "http://straume.lmt.lv/lv/"
  46. ######### Entry point ########
  47. def get_content(self, data):
  48. print "[lmt] get_content:", data
  49. source, data, path, plist, clist, params, qs = self.parse_data(data)
  50. content = []
  51. content.append(("..return", "back","back.png","Return back"))
  52. if clist=="home":
  53. content.extend([
  54. ("Meklēt", "lmt::meklet?q={0}","","Meklēt"),
  55. ("Straumes", "lmt::straumes","","Tiešraides un aktuāli video"),
  56. #("TV", "lmt::tv","","TV tiešraides (tikai LMT tīklā)"),
  57. ("Jaunākie", "lmt::video/jaunakie?videoPage=1", "", "Visu žanru jaunākie video"),
  58. ("Sports", "lmt::video/sports?videoPage=1", "", "Sports"),
  59. ("Kultūra un māksla", "lmt::video/kultura?videoPage=1", "", "Kultūra un māksla"),
  60. ("Konferences", "lmt::video/konferences?videoPage=1", "", "Konferences"),
  61. ("Raidījumi", "lmt::video/raidijumi?videoPage=1", "", "Raidījumi"),
  62. ("Notikumi", "lmt::video/notikumi?videoPage=1", "", "Notikumi"),
  63. ("Filmas un seriāli", "lmt::video/filmas?videoPage=1", "", "Filmas un seriāli"),
  64. ("Dažādi video", "lmt::video/dazadi?videoPage=1", "", "Dažādi video"),
  65. ("Viedtelevīzija", "lmt::video/viedtelevizija?videoPage=1", "", "Viedtelevīzija"),
  66. ])
  67. return content
  68. elif clist in ("meklet","video", "straumes","video-saraksts"):
  69. r=self.call(data)
  70. result = re.findall('<a class="video-picture" (.+?)</li>', r, re.IGNORECASE | re.MULTILINE)
  71. for r2 in result:
  72. m = re.search('<a class="video-title" href="/lv/([^"]+)">([^<]+)<', r2)
  73. title = m.group(2)
  74. data2 = m.group(1)
  75. m = re.search("([^ ]+) 2x", r2)
  76. if m:
  77. img = m.group(1)
  78. else:
  79. m = re.search('<img src="([^"]+)', r2)
  80. img = m.group(1) if m else ""
  81. if img.startswith("//"):
  82. img = "http:" + img
  83. m = re.search('<span class="playlist-overlay">([^<]+)</span>', r2)
  84. overlay = m.group(1) if m else ""
  85. m = re.search('<span class="badge badge-[^>]+>([^<]+)(<[^>]+>([^<]+))*</span>', r2, re.IGNORECASE)
  86. badge = ""
  87. if m:
  88. badge = m.group(1)
  89. if m.group(3):
  90. badge = badge + m.group(3)
  91. categories = re.findall('<span class="category-title">([^<]+)</span>', r2)
  92. categories = "".join(categories)
  93. if overlay:
  94. title = "%s [%s]"%(title,overlay)
  95. if badge:
  96. title = "%s [%s]"%(title,badge)
  97. desc = title
  98. if categories:
  99. desc = desc + "\n"+ categories
  100. content.append((title,self.name+"::"+data2,img,desc))
  101. m = re.search("videoPage=(\d+)",data)
  102. if m:
  103. page = int(m.group(1))+1
  104. data2 = re.sub(r"videoPage=\d+", r"videoPage=%s"%page, data)
  105. content.append(("Next page",self.name+"::"+data2,"next.png","Next page"))
  106. #print content
  107. return content
  108. ### kaut kas neparedzets ###
  109. else:
  110. return content
  111. def is_video(self,data):
  112. source,data,path,plist,clist,params,qs = self.parse_data(data)
  113. if not clist in ("meklet","video", "straumes","video-saraksts","home"):
  114. return True
  115. def call(self, data,params=None,headers=None,lang=""):
  116. if not headers: headers = self.headers
  117. url = self.url+data
  118. r = requests.get(url,headers = headers)
  119. return r.content
  120. #result = self._http_request(url,params,headers=headers)
  121. # return result
  122. def get_streams(self,data):
  123. print "[lmt] get_streams:", data
  124. if not self.is_video(data):
  125. return []
  126. source,data,path,plist,clist,params,qs = self.parse_data(data)
  127. r = self.call(path)
  128. title = re.search("<h1>(.+?)</h1", r, re.IGNORECASE).group(1)
  129. m = re.search('<a class="category-title".+?[^>]+>([^<]+)<', r, re.IGNORECASE | re.DOTALL)
  130. categories = m.group(1) if m else ""
  131. m = re.search('<span class="category-title">([^<]+)</span>.+?</p>', r, re.IGNORECASE | re.DOTALL)
  132. if m:
  133. categories = categories + m.group(1)
  134. if categories:
  135. tite = "%s [%s]"%(title,categories)
  136. img = re.search('<meta property="twitter:image" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  137. if img.startswith("//"):
  138. img = "http:" + img
  139. desc = title + "\n" + re.search('<meta property="og:description" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  140. m = re.search('file: "([^"]+)"', r, re.IGNORECASE)
  141. if m:
  142. # LMT file
  143. data2 = m.group(1)
  144. if not data2.startswith("http"):
  145. data2 = "http:" + data2
  146. stream = util.item()
  147. stream["name"] = title
  148. stream["url"] = data2
  149. stream["img"] = img
  150. stream["desc"] = desc
  151. stream["resolver"] = "lmt"
  152. return [stream]
  153. elif re.search(r'src="(http*:)*//www.youtube.com/embed/([\w-]+).*"', r):
  154. # Youtube
  155. m = re.search(r'src="(http*:)*//www.youtube.com/embed/([\w-]+).*"', r)
  156. video_id = m.group(2)
  157. #http://www.youtube.com/embed/RUyQ_JJ6A84?rel=0&fs=1&wmode=transparent
  158. data2 = YouTubeVideoUrl().extract(video_id)
  159. s = util.item()
  160. s["name"] = title
  161. s["url"] = data2
  162. s["desc"] = desc
  163. s["img"] = img
  164. s["resolver"] = "lmt"
  165. return [s]
  166. elif 'src="http://cdn.tiesraides.lv/lmtstraume.lv/' in r:
  167. m = re.search('src="(http://cdn\.tiesraides\.lv/[^"]+)"',r)
  168. url = m.group(1)
  169. # src="http://cdn.tiesraides.lv/lmtstraume.lv/live-record2-ip/40?c=614127284dcd58d8a84afcf498a3ac7a&v=1405"
  170. r = self._http_request(url)
  171. #http://edge-telia2.tiesraides.lv/live-record2/lmtstraume.lv.40_1/manifest.f4m
  172. m = re.search("'(http://.+?\.m3u8)'",r)
  173. data2 = m.group(1) if m else ""
  174. s = util.item()
  175. s["name"] = title
  176. s["url"] = data2
  177. s["desc"] = desc
  178. s["img"] = img
  179. s["resolver"] = "lmt"
  180. return [s]
  181. elif "//live.tiesraides.lv/lmtstraume.lv" in r:
  182. m = re.search('<div class="single-video-item video-picture">.+?src="([^"]+)"', r, re.DOTALL)
  183. url = "http:" + m.group(1)
  184. # src="http://cdn.tiesraides.lv/lmtstraume.lv/live-record2-ip/40?c=614127284dcd58d8a84afcf498a3ac7a&v=1405"
  185. r = self._http_request(url)
  186. #http://edge-telia2.tiesraides.lv/live-record2/lmtstraume.lv.40_1/manifest.f4m
  187. m = re.search("'(http://.+?\.m3u8)'",r)
  188. data2 = m.group(1) if m else ""
  189. s = util.item()
  190. s["name"] = title
  191. s["url"] = data2
  192. s["desc"] = desc
  193. s["img"] = img
  194. s["resolver"] = "lmt"
  195. return [s]
  196. elif "embed.php?salt=" in r:
  197. m = re.search('embed\.php\?salt=([^"]+)"', r)
  198. url = "http://g1.delphi.lv/vfe/data.php?video_id=%s" % m.group(1)
  199. r2 = self._http_request(url)
  200. js = json.loads(r2)
  201. s = util.item()
  202. s["name"] = js["data"]["title"]
  203. s["desc"] = re.sub("<.+?>", "", js["data"]["description"])
  204. s["img"] = js["data"]["poster"]
  205. s["resolver"] = "lmt"
  206. vers = js["data"]["versions"].keys()
  207. ver = "HD" if "HD" in vers else "SD" if "SD" in vers else None
  208. if not ver:
  209. raise Exception("No stream found")
  210. sdata = js["data"]["versions"][ver]
  211. s["url"] = "http:" + sdata[1]["src"]
  212. return [s]
  213. elif re.search('src="(https://www.facebook.com/plugins/video.php[^"]+)', r):
  214. m = re.search('src="(https://www.facebook.com/plugins/video.php[^"]+)', r)
  215. url = m.group(1)
  216. r2 = self._http_request(url)
  217. if not r2:
  218. raise Exception("No stream found")
  219. streams = []
  220. for it in re.findall('"(sd|hd)_src":"([^"]+)', r2):
  221. s = util.item()
  222. s["name"] = tite
  223. s["desc"] = desc
  224. s["img"] = img
  225. s["resolver"] = "lmt"
  226. s["url"] = it[1].replace("\\", "")
  227. s["quality"] = it[0]
  228. return [s]
  229. else:
  230. raise Exception("No stream found")
  231. # streams = resolver.resolve(url)
  232. # for s in streams:
  233. # s["name"] = title
  234. # s["desc"] = desc
  235. # s["img"] = img
  236. # streams.append(s)
  237. # return streams
  238. if __name__ == "__main__":
  239. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  240. import run
  241. source = Source()
  242. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  243. if len(sys.argv) > 2:
  244. run.run_cli(source, data)
  245. else:
  246. run.run(source, data)
  247. sys.exit()