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

filmix.py 19KB

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