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

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. 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 = "http://www.lob.lv/images/logo/lmt_straume_vert_rgb.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","","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. m = re.search('<span class="playlist-overlay">([^<]+)</span>', r2)
  82. overlay = m.group(1) if m else ""
  83. m = re.search('<span class="badge badge-[^>]+>([^<]+)(<[^>]+>([^<]+))*</span>', r2, re.IGNORECASE)
  84. badge = ""
  85. if m:
  86. badge = m.group(1)
  87. if m.group(3):
  88. badge = badge + m.group(3)
  89. categories = re.findall('<span class="category-title">([^<]+)</span>', r2)
  90. categories = "".join(categories)
  91. if overlay:
  92. title = "%s [%s]"%(title,overlay)
  93. if badge:
  94. title = "%s [%s]"%(title,badge)
  95. desc = title
  96. if categories:
  97. desc = desc + "\n"+ categories
  98. content.append((title,self.name+"::"+data2,img,desc))
  99. m = re.search("videoPage=(\d+)",data)
  100. if m:
  101. page = int(m.group(1))+1
  102. data2 = re.sub(r"videoPage=\d+", r"videoPage=%s"%page, data)
  103. content.append(("Next page",self.name+"::"+data2,self.img,"Next page"))
  104. #print content
  105. return content
  106. ### kaut kas neparedzets ###
  107. else:
  108. return content
  109. def is_video(self,data):
  110. source,data,path,plist,clist,params,qs = self.parse_data(data)
  111. if not clist in ("meklet","video", "straumes","video-saraksts","home"):
  112. return True
  113. def call(self, data,params=None,headers=None,lang=""):
  114. if not headers: headers = self.headers
  115. url = self.url+data
  116. r = requests.get(url,headers = headers)
  117. return r.content
  118. #result = self._http_request(url,params,headers=headers)
  119. return result
  120. def get_streams(self,data):
  121. print "[lmt] get_streams:", data
  122. if not self.is_video(data):
  123. return []
  124. source,data,path,plist,clist,params,qs = self.parse_data(data)
  125. r = self.call(path)
  126. title = re.search("<h1>(.+?)</h1", r, re.IGNORECASE).group(1)
  127. m = re.search('<a class="category-title".+?[^>]+>([^<]+)<', r, re.IGNORECASE | re.DOTALL)
  128. categories = m.group(1) if m else ""
  129. m = re.search('<span class="category-title">([^<]+)</span>.+?</p>', r, re.IGNORECASE | re.DOTALL)
  130. if m:
  131. categories = categories + m.group(1)
  132. if categories:
  133. tite = "%s [%s]"%(title,categories)
  134. img = re.search('<meta property="twitter:image" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  135. desc = title + "\n" + re.search('<meta property="og:description" content="([^"]+)">', r, re.IGNORECASE | re.DOTALL).group(1)
  136. m = re.search('file: "([^"]+)"', r, re.IGNORECASE)
  137. if m:
  138. data2 = m.group(1)
  139. stream = util.item()
  140. stream["name"] = title
  141. stream["url"] = data2
  142. stream["img"] = img
  143. stream["desc"] = desc
  144. stream["resolver"] = "lmt"
  145. return [stream]
  146. elif re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r):
  147. m = re.search('src="http*://www.youtube.com/embed/([\w-]+).*"',r)
  148. video_id = m.group(1)
  149. #http://www.youtube.com/embed/RUyQ_JJ6A84?rel=0&fs=1&wmode=transparent
  150. data2 = YouTubeVideoUrl().extract(video_id)
  151. s = util.item()
  152. s["name"] = title
  153. s["url"] = data2
  154. s["desc"] = desc
  155. s["img"] = img
  156. s["resolver"] = "lmt"
  157. return [s]
  158. elif 'src="http://cdn.tiesraides.lv/lmtstraume.lv/' in r:
  159. m = re.search('src="(http://cdn\.tiesraides\.lv/[^"]+)"',r)
  160. url = m.group(1)
  161. # src="http://cdn.tiesraides.lv/lmtstraume.lv/live-record2-ip/40?c=614127284dcd58d8a84afcf498a3ac7a&v=1405"
  162. r = self._http_request(url)
  163. #http://edge-telia2.tiesraides.lv/live-record2/lmtstraume.lv.40_1/manifest.f4m
  164. m = re.search("'(http://.+?\.m3u8)'",r)
  165. data2 = m.group(1) if m else ""
  166. s = util.item()
  167. s["name"] = title
  168. s["url"] = data2
  169. s["desc"] = desc
  170. s["img"] = img
  171. s["resolver"] = "lmt"
  172. return [s]
  173. else:
  174. raise Exception("No stream found")
  175. # streams = resolver.resolve(url)
  176. # for s in streams:
  177. # s["name"] = title
  178. # s["desc"] = desc
  179. # s["img"] = img
  180. # streams.append(s)
  181. # return streams
  182. if __name__ == "__main__":
  183. country= "lv"
  184. c = Source(country)
  185. if len(sys.argv)>1:
  186. data= sys.argv[1]
  187. else:
  188. data = "home"
  189. content = c.get_content(data)
  190. for item in content:
  191. print item
  192. #cat = api.get_categories(country)
  193. #chan = api.get_channels("lv")
  194. #prog = api.get_programs(channel=6400)
  195. #prog = api.get_programs(category=55)
  196. #seas = api.get_seasons(program=6453)
  197. #str = api.get_streams(660243)
  198. #res = api.get_videos(802)
  199. #formats = api.getAllFormats()
  200. #det = api.detailed("1516")
  201. #vid = api.getVideos("13170")
  202. pass