Python module (submodule repositary), which provides content (video streams) from various online stream sources to corresponding Enigma2, Kodi, Plex plugins

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. import base64
  17. from collections import OrderedDict
  18. import sys
  19. import ssl
  20. if "_create_unverified_context" in dir(ssl):
  21. ssl._create_default_https_context = ssl._create_unverified_context
  22. try:
  23. import util
  24. except:
  25. sys.path.insert(0,'..')
  26. import util
  27. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  28. class Source(SourceBase):
  29. def __init__(self,country="",cfg_path=None):
  30. self.name = "filmix"
  31. self.title = "filmix.me"
  32. self.img = "filmix.png"
  33. self.desc = "filmix.me satura skatīšanās"
  34. self.country=country
  35. self.headers = headers2dict("""
  36. Host: filmix.me
  37. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  38. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  39. Accept-Language: en-US,en;q=0.5
  40. """)
  41. self.headers2 = headers2dict("""
  42. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  43. X-Requested-With: XMLHttpRequest
  44. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  45. """)
  46. self.url = "https://filmix.me/"
  47. #self.login()
  48. def login(self,user="",password=""):
  49. return True
  50. def get_content(self, data):
  51. print "[filmix] get_content:", data
  52. source, data, path, plist, clist, params, qs = self.parse_data(data)
  53. content=[]
  54. content.append(("..return", "back","back.png","Return back"))
  55. if clist=="home":
  56. content.extend([
  57. ("Search", "filmix::search/{0}","","Search"),
  58. ("Movies", "filmix::movies","","Movies"),
  59. ("Series", "filmix::series","","TV Series"),
  60. ("Cartoons", "filmix::cartoons","","Cartoons"),
  61. ])
  62. return content
  63. elif clist=="search":
  64. if len(plist) < 2:
  65. return content
  66. import requests
  67. ses = requests.session()
  68. r = ses.get(self.url+data)
  69. url = "https://filmix.me/engine/ajax/sphinx_search.php"
  70. headers = headers2dict("""
  71. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0
  72. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  73. """)
  74. data = "scf=fx&story=%s&search_start=0&do=search&subaction=search" % (plist[1].replace(" ", "+"))
  75. r = ses.post(url, data, headers=headers)
  76. #r = r.content.decode("cp1251").encode("utf8")
  77. content = self.process_list(r.content, content)
  78. return content
  79. elif data in ("movies","series","cartoons"):
  80. r = self.call("")
  81. if not r:
  82. raise Exception("Can not read content")
  83. r = r.decode("cp1251").encode("utf8")
  84. if data == "movies":
  85. sname = "Фильмы"
  86. elif data=="series":
  87. sname = "Сериалы"
  88. else:
  89. sname = "Мультфильмы"
  90. # <span class="menu-title">Фильмы</span>
  91. m = re.search('<span class="menu-title">%s</span>(.+?)<li>\s+?<span'%sname, r, re.DOTALL|re.UNICODE)
  92. if not m: return content
  93. r2 = m.group(1)
  94. result = re.findall(r'<a .*?href="https://filmix\.me/([^"]+)".*?>([^<]+)</', r2, re.DOTALL)
  95. for item in result:
  96. if "catalog" in item[0]: continue
  97. title = item[1]
  98. data2 = item[0]
  99. img = self.img
  100. desc = title
  101. content.append((title,self.name+"::"+data2,img,desc))
  102. return content
  103. ## Seriāls
  104. elif clist=="play":
  105. r = self.call(path)
  106. r = r.decode("cp1251").encode("utf8")
  107. title = title0 = util.unescape(re.search("titlePlayer = '([^']+)'", r, re.DOTALL).group(1))
  108. m = re.search('<meta itemprop="thumbnailUrl" content="([^"]+)',r,re.DOTALL)
  109. img = m.group(1) if m else self.img
  110. m = re.search('<meta itemprop="duration" content="([^"]+)" />', r, re.DOTALL)
  111. duration = "(%s)"%m.group(1) if m else ""
  112. m = re.search('<p itemprop="description"[^>]+>([^<]+)<', r, re.DOTALL)
  113. desc = desc0 = util.unescape(m.group(1).strip()) if m else ""
  114. vid = plist[-1]
  115. m = re.search(r"meta_key = \['(\w+)', '(\w+)', '(\w+)'\]", r, re.IGNORECASE)
  116. key = m.group(3) if m else ""
  117. js = self.get_movie_info(vid,key)
  118. translations = js["message"]["translations"]["html5"]
  119. for pl in translations:
  120. if translations[pl].startswith("http"):
  121. continue
  122. pl_link = translations[pl]
  123. lang = pl.encode("utf8")
  124. break
  125. else:
  126. raise Exception("No episodes list found!")
  127. #pl_link = js["message"]["translations"]["flash"].values()[0]
  128. # TODO process several players, currently taking the first
  129. #TODO - kļuda, vairs nesradā
  130. if not pl_link.startswith("http"):
  131. pl_link = self.decode_direct_media_url(pl_link)
  132. js = self._http_request(pl_link)
  133. js = self.decode_direct_media_url(js)
  134. js = json.loads(js)
  135. if "s" in qs:
  136. s = int(qs["s"])
  137. for i,ep in enumerate(js["playlist"][s-1]["playlist"]):
  138. title = title0+" - "+js["playlist"][s-1]["playlist"][i]["comment"].encode("utf8")
  139. serie = js["playlist"][s-1]["playlist"][i]["comment"].encode("utf8")
  140. data2 = data+"&e=%s"%(i+1)
  141. desc = serie +"\n"+desc0
  142. content.append((title,self.name+"::"+data2,img,desc))
  143. else:
  144. for i,ep in enumerate(js["playlist"]):
  145. title = title0 +" - "+js["playlist"][i]["comment"].encode("utf8")
  146. serie = js["playlist"][i]["comment"].encode("utf8")
  147. if "file" in ep and ep["file"]:
  148. data2 = data+"?e=%s"%(i+1)
  149. else:
  150. data2 = data+"?s=%s"%(i+1)
  151. desc = serie +"\n"+desc0
  152. content.append((title,self.name+"::"+data2,img,desc))
  153. return content
  154. #r = self._http_request(url)
  155. ### saraksts ###
  156. else:
  157. r = self.call(data)
  158. r = r.decode("cp1251").encode("utf8")
  159. content = self.process_list(r, content)
  160. if '<div class="navigation">' in r:
  161. m = re.search(r'href="https://filmix\.me/([^"]+)" class="next icon-arowRight btn-tooltip"', r, re.DOTALL)
  162. if m:
  163. data2 = m.group(1)
  164. else:
  165. m = re.search("/page/(\d)+",data)
  166. if m:
  167. page = int(m.group(1))+1
  168. data2 = re.sub("/page/(\d)+", "/page/%s"%page, data)
  169. else:
  170. data2 = data + "/page/2"
  171. content.append(("Next page",self.name+"::"+data2,self.img,"Next page"))
  172. return content
  173. def process_list(self, r, content):
  174. for r2 in re.findall('<article class="shortstory line".+?</article>', r, re.DOTALL):
  175. #m2 = re.search(r'<a class="watch icon-play" itemprop="url" href="([^"]+)"', r2, re.DOTALL)
  176. #<a class="watch icon-play" itemprop="url" href="https://filmix.me/dramy/110957-stolik-19-2017.html"
  177. #m = re.search(r'<a href="https://filmix\.me/play/(\d+)" class="watch icon-play">', r2, re.DOTALL)
  178. m = re.search(r'<a class="watch icon-play" itemprop="url" href="https://filmix.me/\w+/(\d+)-', r2, re.DOTALL)
  179. if not m: continue
  180. vid = m.group(1)
  181. data2 = "play/%s"%vid
  182. #title = re.search('itemprop="name">([^<]+)</div>', r2, re.DOTALL).group(1)
  183. title = re.search('itemprop="name" content="([^"]+)"', r2, re.DOTALL).group(1)
  184. m = re.search('itemprop="alternativeHeadline" content="([^"]+)"', r2, re.DOTALL)
  185. if m:
  186. title = title + "/"+m.group(1)
  187. m = re.search(r'img src="(https://filmix\.me/uploads/posters/thumbs/[^"]+)"', r2)
  188. img = m.group(1) if m else self.img
  189. m = re.search(r'<a itemprop="copyrightYear".+?>(\d+)<', r2, re.DOTALL)
  190. if m:
  191. title = "%s (%s)"%(title,m.group(1))
  192. title = util.unescape(title)
  193. m = re.search('<p itemprop="description">([^<]+)</p>', r2, re.DOTALL)
  194. desc0 = util.unescape(m.group(1)) if m else ""
  195. props = []
  196. genre = re.findall('<a itemprop="genre"[^>]+?">([^<]+)</a>', r2, re.DOTALL)
  197. genre = ",".join(genre)
  198. if genre: props.append(genre)
  199. m = re.search('<div class="quality">([^<]+)</div>', r2, re.DOTALL)
  200. if m: props.append(m.group(1))
  201. m = re.search('<div class="item translate".+?class="item-content">([^<]+)<', r2)
  202. if m: props.append(m.group(1))
  203. m = re.search('itemprop="director">([^<]+)</span></div>', r2)
  204. if m: props.append(m.group(1))
  205. m = re.search('<div class="item actors">(.+?)</div>', r2)
  206. if m:
  207. result = re.findall("<span>(.+?)(&nbsp;)*</span>", m.group(1))
  208. if result:
  209. actors = []
  210. for a in zip(*result)[0]:
  211. actors.append(re.sub("<.+?>", "", a))
  212. props.append(" ".join(actors))
  213. else:
  214. x = 1
  215. pass
  216. desc="%s\n%s\n\n%s"%(title, desc0, "\n".join(props))
  217. content.append((title,self.name+"::"+data2,img,desc))
  218. return content
  219. def is_video(self,data):
  220. source,data,path,plist,clist,params,qs = self.parse_data(data)
  221. if clist == "play" and "e=" in data:
  222. return True
  223. elif clist=="play" and not params:
  224. r = self.call(path)
  225. #r = r.decode("cp1251").encode("utf8")
  226. #m = re.search('itemprop="contentUrl" content="(.+?)"', r, re.IGNORECASE | re.DOTALL)
  227. #if not m:
  228. if u"Фильм <a href=" in r.decode("cp1251"):
  229. return True
  230. else:
  231. return False
  232. else:
  233. return False
  234. def get_streams(self, data):
  235. print "[filmix] get_streams:", data
  236. source,data,path,plist,clist,params,qs = self.parse_data(data)
  237. r = self.call(path)
  238. if not r:
  239. return []
  240. streams = []
  241. r = r.decode("cp1251").encode("utf8")
  242. title = title0 = util.unescape(re.search("titlePlayer = '([^']+)'", r, re.DOTALL).group(1))
  243. m = re.search('<meta itemprop="thumbnailUrl" content="([^"]+)',r,re.DOTALL)
  244. img = m.group(1) if m else self.img
  245. m = re.search('<meta itemprop="duration" content="([^"]+)" />', r, re.DOTALL)
  246. duration = "(%s)"%m.group(1) if m else ""
  247. m = re.search('<p itemprop="description"[^>]+>([^<]+)<', r, re.DOTALL)
  248. desc = desc0 = util.unescape(m.group(1).strip()) if m else ""
  249. m = re.search('itemprop="contentUrl" content="(.+?)"', r, re.IGNORECASE | re.DOTALL)
  250. if not m:
  251. raise Exception("Can not find video link")
  252. #return []
  253. video_link = m.group(1)
  254. series = False if u"Фильм <a href=" in r.decode("utf8") else True
  255. vid = plist[1]
  256. m = re.search(r"meta_key = \['(\w+)', '(\w+)', '(\w+)'\]", r, re.IGNORECASE)
  257. key = m.group(3) if m else ""
  258. js = self.get_movie_info(vid,key)
  259. translations = js["message"]["translations"]["html5"]
  260. for pl in translations:
  261. if translations[pl].startswith("http"):
  262. continue
  263. pl_link = translations[pl]
  264. lang = pl.encode("utf8")
  265. break
  266. else:
  267. raise Exception("No episodes list found!")
  268. if not pl_link.startswith("http"):
  269. pl_link = self.decode_direct_media_url(pl_link)
  270. if not series : # Filma
  271. url0 = pl_link
  272. streams2 = self.get_streams2(url0)
  273. for st in streams2:
  274. stream = util.item()
  275. stream["url"]=st[1]
  276. stream["lang"]=lang
  277. stream["quality"]=st[0]
  278. stream["name"]= title
  279. stream["desc"]=desc
  280. streams.append(stream)
  281. return streams
  282. else: # Seriāls
  283. #pl_link = video_link
  284. js = self._http_request(pl_link)
  285. js = self.decode_direct_media_url(js)
  286. js = json.loads(js)
  287. if "e" in qs:
  288. if "s" in qs:
  289. s = int(qs["s"])
  290. else:
  291. s = None
  292. e = int(qs["e"])
  293. if s: # sezona + epizode
  294. serie = js["playlist"][s-1]["playlist"][e-1]["comment"].encode("utf8")
  295. title = title0+" - "+ serie
  296. url0 = js["playlist"][s-1]["playlist"][e-1]["file"].encode("utf8")
  297. else: # tikai epizode, nav sezonas
  298. title = title0 +" - "+js["playlist"][e-1]["comment"].encode("utf8")
  299. serie = js["playlist"][e-1]["comment"].encode("utf8")
  300. url0 = js["playlist"][e-1]["file"].encode("utf8")
  301. streams2 = self.get_streams2(url0)
  302. for st in streams2:
  303. stream = util.item()
  304. stream["url"]=st[1]
  305. stream["lang"]=lang
  306. stream["quality"]=st[0]
  307. stream["name"]= title
  308. stream["desc"]=desc
  309. streams.append(stream)
  310. return streams
  311. def call(self, data,params=None,headers=None,lang=""):
  312. if not headers: headers = self.headers
  313. url = self.url+data
  314. result = self._http_request(url,params,headers=headers)
  315. return result
  316. def get_movie_info(self,vid,key=""):
  317. headers = headers2dict("""
  318. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
  319. Accept: application/json, text/javascript, */*; q=0.01
  320. Accept-Language: en-US,en;q=0.5
  321. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  322. X-Requested-With: XMLHttpRequest
  323. Referer: https://filmix.me/play/%s
  324. Cookie: ad_win12=1;
  325. """%vid )
  326. post_data = {"post_id":vid,"key=":key}
  327. r = util.post("https://filmix.me/api/movies/player_data", data=post_data, headers = headers)
  328. if not r:
  329. raise Exception("Can not get movie info")
  330. #return []
  331. js = json.loads(r)
  332. return js
  333. def decode_base64(self, encoded_url):
  334. codec_a = ("l", "u", "T", "D", "Q", "H", "0", "3", "G", "1", "f", "M", "p", "U", "a", "I", "6", "k", "d", "s", "b", "W", "5", "e", "y", "=")
  335. codec_b = ("w", "g", "i", "Z", "c", "R", "z", "v", "x", "n", "N", "2", "8", "J", "X", "t", "9", "V", "7", "4", "B", "m", "Y", "o", "L", "h")
  336. i = 0
  337. for a in codec_a:
  338. b = codec_b[i]
  339. i += 1
  340. encoded_url = encoded_url.replace(a, '___')
  341. encoded_url = encoded_url.replace(b, a)
  342. encoded_url = encoded_url.replace('___', b)
  343. return base64.b64decode(encoded_url)
  344. def decode_unicode(self, encoded_url):
  345. from itertools import izip_longest
  346. def grouper(n, iterable, fillvalue=None):
  347. "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
  348. args = [iter(iterable)] * n
  349. return izip_longest(fillvalue=fillvalue, *args)
  350. _ = (encoded_url[1:] if encoded_url.find('#') != -1 else encoded_url)
  351. tokens = map(lambda items: '\u0'+''.join(items), grouper(3, _))
  352. return ''.join(tokens).decode('unicode_escape')
  353. def decode_direct_media_url(self, encoded_url, checkhttp=False):
  354. if(checkhttp == True and (encoded_url.find('http://') != -1 or encoded_url.find('https://') != -1)):
  355. return False
  356. try:
  357. if encoded_url.find('#') != -1:
  358. return self.decode_unicode(encoded_url)
  359. else:
  360. return self.decode_base64(encoded_url)
  361. except:
  362. return False
  363. def decode_uppod_text(self, text):
  364. Client_codec_a = ["l", "u", "T", "D", "Q", "H", "0", "3", "G", "1", "f", "M", "p", "U", "a", "I", "6", "k", "d", "s", "b", "W", "5", "e", "y", "="]
  365. Client_codec_b = ["w", "g", "i", "Z", "c", "R", "z", "v", "x", "n", "N", "2", "8", "J", "X", "t", "9", "V", "7", "4", "B", "m", "Y", "o", "L", "h"]
  366. text = text.replace("\n", "").strip()
  367. for i in range(len(Client_codec_a)):
  368. char1 = Client_codec_b[i]
  369. char2 = Client_codec_a[i]
  370. text = text.replace(char1, "___")
  371. text = text.replace(char2, char1)
  372. text = text.replace("___", char2)
  373. result = base64.b64decode(text)
  374. print result
  375. return result
  376. def get_streams2(self,url0):
  377. m = re.search("\[([\d\w,]+)\]",url0)
  378. if not m:
  379. return [("?",url0)]
  380. res = m.group(1)
  381. streams=[]
  382. for res in res.split(","):
  383. if not res: continue
  384. if res in ["1080p"]: continue #TODO fullhd only in PRO+ version
  385. url=re.sub("\[[\d\w,]+\]",res,url0)
  386. streams.append((res,url))
  387. return streams
  388. if __name__ == "__main__":
  389. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  390. import run
  391. source = Source()
  392. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  393. run.run(source, data)
  394. sys.exit()