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

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