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

serialguru.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 urllib2, urllib
  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. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  20. import HTMLParser
  21. h = HTMLParser.HTMLParser()
  22. class Source(SourceBase):
  23. def __init__(self,country="",cfg_path=None):
  24. self.name = "serialguru"
  25. self.title = "SerialGURU.ru"
  26. self.img = "http://serialguru.ru/images/xlogo_new.png.pagespeed.ic.0sre2_2OJN.png"
  27. self.desc = "Serialguru.ru portāla satura skatīšanās"
  28. self.country=country
  29. self.headers = headers2dict("""
  30. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36
  31. Referer: http://serialguru.ru/
  32. """)
  33. self.headers2 = headers2dict("""
  34. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36
  35. X-Requested-With: XMLHttpRequest
  36. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  37. Referer: http://serialguru.ru/
  38. """)
  39. self.url = "http://serialguru.ru/"
  40. #self.login()
  41. def login(self,user="",password=""):
  42. return True
  43. def get_content(self, data):
  44. print "[tvdom] get_content:", data
  45. if "::" in data:
  46. data = data.split("::")[1]
  47. path = data.split("?")[0]
  48. clist = path.split("/")[0]
  49. params = data[data.find("?"):] if "?" in data else ""
  50. qs = dict(map(lambda x:x.split("="),re.findall("[%\w]+=\w+",params)))
  51. lang = qs["lang"] if "lang" in qs else self.country
  52. content=[]
  53. content.append(("..return", "back","","Return back"))
  54. if clist=="home":
  55. content.extend([
  56. ("Search", "serialguru::search/{0}","","Search content"),
  57. ("Last", "serialguru::last","","Last series"),
  58. ("Series", "serialguru::serials","","TV Series"),
  59. ("Shows", "serialguru::tv","","TV Shows"),
  60. ("Animations", "serialguru::mult","","Animation series"),
  61. #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
  62. ])
  63. return content
  64. elif data == "last":
  65. r = self.call("")
  66. for item in re.findall(r'<li><a href="(http://serialguru\.ru/[^"]+)"><i>([^<]+)</i> <i>([^<]+)</i> <b>([^<]+)</b></a></li>', r, re.DOTALL):
  67. title = item[1] + " - " + item[2]+"/"+item[3]
  68. img = ""
  69. data2 = item[0].replace(self.url, "")
  70. desc = title
  71. content.append((title, self.name+"::"+data2, img, desc))
  72. return content
  73. elif data=="serials":
  74. content.extend([
  75. ("All", "serialguru::serials?o=0&t=S","","All series"),
  76. ("Russian", "serialguru::serials?c%5B%5D=53&c%5B%5D=61&c%5B%5D=33&c%5B%5D=42&c%5B%5D=31&o=0&t=S","","Russian countries series"),
  77. ("English", "serialguru::serials?c%5B%5D=27&c%5B%5D=26&c%5B%5D=43&c%5B%5D=30&c%5B%5D=34&c%5B%5D=25&o=0&t=S","","English countries series"),
  78. ("Europe", "serialguru::serials?c%5B%5D=29&c%5B%5D=66&c%5B%5D=44&c%5B%5D=28&c%5B%5D=51&c%5B%5D=65&c%5B%5D=62&c%5B%5D=40&c%5B%5D=45&c%5B%5D=68&c%5B%5D=59&c%5B%5D=39&c%5B%5D=35&c%5B%5D=47&o=0&t=S","","European countries series"),
  79. ("Other", "serialguru::serials?c%5B%5D=36&c%5B%5D=32&c%5B%5D=67&c%5B%5D=63&c%5B%5D=60&c%5B%5D=64&c%5B%5D=38&c%5B%5D=52&c%5B%5D=41&c%5B%5D=58&c%5B%5D=57&c%5B%5D=37&c%5B%5D=50&c%5B%5D=46&o=0&t=S","","Other countries series"),
  80. #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
  81. ])
  82. return content
  83. elif data=="tv":
  84. content.extend([
  85. ("All", "serialguru::tv?o=0&t=S","","All series"),
  86. ("Russian", "serialguru::tv?c%5B%5D=53&c%5B%5D=61&c%5B%5D=33&c%5B%5D=42&c%5B%5D=31&o=0&t=P","","Russian countries TV shows"),
  87. ("English", "serialguru::tv?c%5B%5D=27&c%5B%5D=26&c%5B%5D=43&c%5B%5D=30&c%5B%5D=34&c%5B%5D=25&o=0&t=P","","English countries TV shows"),
  88. ("Europe", "serialguru::tv?c%5B%5D=29&c%5B%5D=66&c%5B%5D=44&c%5B%5D=28&c%5B%5D=51&c%5B%5D=65&c%5B%5D=62&c%5B%5D=40&c%5B%5D=45&c%5B%5D=68&c%5B%5D=59&c%5B%5D=39&c%5B%5D=35&c%5B%5D=47&o=0&t=P","","European countries TV shows series"),
  89. ("Other", "serialguru::tv?c%5B%5D=36&c%5B%5D=32&c%5B%5D=67&c%5B%5D=63&c%5B%5D=60&c%5B%5D=64&c%5B%5D=38&c%5B%5D=52&c%5B%5D=41&c%5B%5D=58&c%5B%5D=57&c%5B%5D=37&c%5B%5D=50&c%5B%5D=46&o=0&t=P","","Other countries TV shows"),
  90. #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
  91. ])
  92. return content
  93. elif data=="mult":
  94. content.extend([
  95. ("All", "serialguru::mult?o=0&t=S","","All series"),
  96. ("Russian", "serialguru::mult?c%5B%5D=53&c%5B%5D=61&c%5B%5D=33&c%5B%5D=42&c%5B%5D=31&o=0&t=M","","Russian countries animantions"),
  97. ("English", "serialguru::mult?c%5B%5D=27&c%5B%5D=26&c%5B%5D=43&c%5B%5D=30&c%5B%5D=34&c%5B%5D=25&o=0&t=M","","English countries animantions"),
  98. ("Europe", "serialguru::mult?c%5B%5D=29&c%5B%5D=66&c%5B%5D=44&c%5B%5D=28&c%5B%5D=51&c%5B%5D=65&c%5B%5D=62&c%5B%5D=40&c%5B%5D=45&c%5B%5D=68&c%5B%5D=59&c%5B%5D=39&c%5B%5D=35&c%5B%5D=47&o=0&t=M","","European countries animantions"),
  99. ("Other", "serialguru::mult?c%5B%5D=36&c%5B%5D=32&c%5B%5D=67&c%5B%5D=63&c%5B%5D=60&c%5B%5D=64&c%5B%5D=38&c%5B%5D=52&c%5B%5D=41&c%5B%5D=58&c%5B%5D=57&c%5B%5D=37&c%5B%5D=50&c%5B%5D=46&o=0&t=M","","Other countries animantions"),
  100. #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
  101. ])
  102. return content
  103. elif clist=="search":
  104. if data.split("/")>1:
  105. term = data.split("/")[1]
  106. else:
  107. return content
  108. r = self.call("main/autocomplete?term=%s"%(term))
  109. if r=="null":
  110. return content
  111. js = json.loads(r)
  112. for item in js:
  113. title = item["name"].encode("utf8")
  114. data2 = item["url"].encode("utf8")
  115. img = "http://serialguru.ru/uploads/cover/"+item["image_s"].replace("_s","")+".jpg"
  116. rating = item["rating"].encode("utf8") if item["rating"] else ""
  117. desc = title +"\nRating:%s (%s+/%s-)"%(rating,item["plus_cnt"].encode("utf8"),item["minus_cnt"].encode("utf8"))
  118. content.append((title,self.name+"::"+data2,img,desc))
  119. return content
  120. elif path=="serials" or path=="tv" or path=="mult":
  121. if path=="serials" and not "cat%5B%5D" in data:
  122. #content.append(("All", "serialguru::"+data+"&cat%5B%5D=","","All series"))
  123. categories = self.get_categories(path)
  124. for c in categories:
  125. content.append((c[1], "serialguru::"+data+"&cat%5B%5D="+c[0],"",c[1]))
  126. return content
  127. else:
  128. r = self.call("main/load", params[1:], headers=self.headers2)
  129. for item in re.findall('<li><a href="([^"]+)"><div>.*?<img src="([^"]+)" alt="([^"]+)"[^<]*?><p>([^<]+)<i><span class="r">([^<]+)</span> <span class="plus">([^<]+)</span> <span class="minus">([^<]+)</span></i></p></div>([^<]+)</a></li>', r, re.DOTALL):
  130. title = "%s (%s)"%(item[2],item[3])
  131. img = item[1].replace("_s.jpg","_l.jpg")
  132. data2 = item[0].replace(self.url,"")
  133. desc = title +"\nRating:%s (%s+/%s-)"%(item[4],item[5],item[6])
  134. content.append((title,self.name+"::"+data2,img,desc))
  135. page=int(re.search("o=(\d+)",data).group(1))
  136. data2 = re.sub("o=(\d+)","o=%s"%(page+15),data)
  137. content.append(("Next page",self.name+"::"+data2,"","Go to next page"))
  138. return content
  139. ### Pārraide
  140. else:
  141. r = self.call(clist)
  142. title0=re.search('<h2>(.+?)</h2>',r,re.DOTALL).group(1)
  143. m=re.search('<div class="description">(.+?)</div>',r,re.DOTALL)
  144. desc0=m.group(1) if m else ""
  145. desc0=desc0.replace("<p>","").replace("</p>","\n").replace('<a href="#">ПОКАЗАТЬ ПОЛНОСТЬЮ</a>',"")
  146. desc0=title0+"\n"+desc0.strip()
  147. img0=""
  148. m = re.search("http://serialguru.ru/main/playlist/\d+",r)
  149. if m:
  150. url = m.group()
  151. else:
  152. raise Exception ("No stream found")
  153. r = self._http_request(url)
  154. js = json.loads(r,"utf8")
  155. if not "/" in data: # sezonas
  156. for i,item in enumerate(js["playlist"]):
  157. title = title0 + " - " + item["comment"].encode("utf8")
  158. img = img0
  159. data2 = "%s/%s"%(data,i)
  160. desc = desc0
  161. content.append((title,self.name+"::"+data2,img,desc))
  162. else:
  163. snum = int(data.split("/")[1])
  164. title1 = js["playlist"][snum]["comment"].encode('utf8')
  165. for i,item in enumerate(js["playlist"][snum]["playlist"]):
  166. title = title0 + " - " + title1+"/"+item["comment"].encode("utf8")
  167. img = img0
  168. data2 = item["file"].encode("utf8")
  169. desc = desc0
  170. content.append((title,data2,img,desc))
  171. return content
  172. def is_video(self,data):
  173. if "::" in data:
  174. data = data.split("::")[1]
  175. if "live/view" in data:
  176. return True
  177. else:
  178. return False
  179. def get_categories(self,data):
  180. r = self.call(data)
  181. r2 = re.search('<td class="category">(.+?)</td>', r, re.DOTALL).group(1)
  182. items = re.findall(r'<a href="#" data-id="(\d+)">([^<]+)</a>', r2, re.DOTALL)
  183. return items
  184. def call(self, data,params = None, headers=None):
  185. if not headers: headers = self.headers
  186. #if not lang: lang = self.country
  187. url = self.url + data
  188. content = self._http_request(url,params, headers)
  189. return content
  190. if __name__ == "__main__":
  191. country= "lv"
  192. c = Source(country)
  193. if len(sys.argv)>1:
  194. data= sys.argv[1]
  195. else:
  196. data = "home"
  197. content = c.get_content(data)
  198. for item in content:
  199. print item
  200. #cat = api.get_categories(country)
  201. #chan = api.get_channels("lv")
  202. #prog = api.get_programs(channel=6400)
  203. #prog = api.get_programs(category=55)
  204. #seas = api.get_seasons(program=6453)
  205. #str = api.get_streams(660243)
  206. #res = api.get_videos(802)
  207. #formats = api.getAllFormats()
  208. #det = api.detailed("1516")
  209. #vid = api.getVideos("13170")
  210. pass