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

serialguru.py 11KB

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