Kodi plugin to to play various online streams (mostly Latvian)

lmt.py 7.4KB

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