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

lmt.py 8.5KB

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 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,self.img,"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. data2 = m.group(1)
  143. stream = util.item()
  144. stream["name"] = title
  145. stream["url"] = data2
  146. stream["img"] = img
  147. stream["desc"] = desc
  148. stream["resolver"] = "lmt"
  149. return [stream]
  150. elif re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r):
  151. m = re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r)
  152. video_id = m.group(1)
  153. #http://www.youtube.com/embed/RUyQ_JJ6A84?rel=0&fs=1&wmode=transparent
  154. data2 = YouTubeVideoUrl().extract(video_id)
  155. s = util.item()
  156. s["name"] = title
  157. s["url"] = data2
  158. s["desc"] = desc
  159. s["img"] = img
  160. s["resolver"] = "lmt"
  161. return [s]
  162. elif 'src="http://cdn.tiesraides.lv/lmtstraume.lv/' in r:
  163. m = re.search('src="(http://cdn\.tiesraides\.lv/[^"]+)"',r)
  164. url = m.group(1)
  165. # src="http://cdn.tiesraides.lv/lmtstraume.lv/live-record2-ip/40?c=614127284dcd58d8a84afcf498a3ac7a&v=1405"
  166. r = self._http_request(url)
  167. #http://edge-telia2.tiesraides.lv/live-record2/lmtstraume.lv.40_1/manifest.f4m
  168. m = re.search("'(http://.+?\.m3u8)'",r)
  169. data2 = m.group(1) if m else ""
  170. s = util.item()
  171. s["name"] = title
  172. s["url"] = data2
  173. s["desc"] = desc
  174. s["img"] = img
  175. s["resolver"] = "lmt"
  176. return [s]
  177. else:
  178. raise Exception("No stream found")
  179. # streams = resolver.resolve(url)
  180. # for s in streams:
  181. # s["name"] = title
  182. # s["desc"] = desc
  183. # s["img"] = img
  184. # streams.append(s)
  185. # return streams
  186. if __name__ == "__main__":
  187. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  188. import run
  189. source = Source()
  190. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  191. if len(sys.argv) > 2:
  192. run.run_cli(source, data)
  193. else:
  194. run.run(source, data)
  195. sys.exit()