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

lmt.py 8.6KB

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