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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Playstreamproxy daemon (based on Livestream daemon)
  5. Provides API to ContetSources + stream serving to play via m3u8 playlists
  6. """
  7. import os, sys, time, re, json
  8. import ConfigParser
  9. import atexit
  10. from signal import SIGTERM
  11. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  12. from SocketServer import ThreadingMixIn
  13. import time, threading, socket, SocketServer
  14. from urllib import unquote, quote
  15. import urllib,urlparse
  16. import requests
  17. #import cookielib,urllib2
  18. from ContentSources import ContentSources
  19. from sources.SourceBase import stream_type
  20. import util
  21. from util import streamproxy_decode3, streamproxy_encode3
  22. try:
  23. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  24. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  25. except:
  26. pass
  27. HOST_NAME = "0.0.0.0"
  28. PORT_NUMBER = 8880
  29. REDIRECT = True
  30. MULTITHREAD = False
  31. WORKERS = 10
  32. KEY = ""
  33. DEBUG = False
  34. DEBUG2 = False
  35. SPLIT_CHAR = "~"
  36. SPLIT_CODE = "%7E"
  37. EQ_CODE = "%3D"
  38. COL_CODE = "%3A"
  39. cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
  40. cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
  41. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  42. headers0 = headers2dict("""
  43. User-Agent: GStreamer souphttpsrc libsoup/2.52.2
  44. icy-metadata: 1
  45. """)
  46. headers0_ = headers2dict("""
  47. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  48. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  49. """)
  50. cur_directory = os.path.dirname(os.path.realpath(__file__))
  51. slinks = {}
  52. sessions = {}
  53. cfg_file = "streams.cfg"
  54. sources = ContentSources("", cfg_file)
  55. config = ConfigParser.ConfigParser()
  56. proxy_cfg_file = os.path.join(cur_directory, "playstreamproxy.cfg")
  57. if not os.path.exists(proxy_cfg_file):
  58. config.add_section("playstreamproxy")
  59. config.set("playstreamproxy", "debug", DEBUG)
  60. config.set("playstreamproxy", "host", HOST_NAME)
  61. config.set("playstreamproxy", "port", PORT_NUMBER)
  62. config.set("playstreamproxy", "redirect", REDIRECT)
  63. config.set("playstreamproxy", "key", KEY)
  64. config.set("playstreamproxy", "multithread", MULTITHREAD)
  65. config.set("playstreamproxy", "workers", WORKERS)
  66. config.set("playstreamproxy", "wsgi", "wsgiref")
  67. config.write(open(proxy_cfg_file, "w"))
  68. else:
  69. config.read(proxy_cfg_file)
  70. DEBUG = config.getboolean("playstreamproxy", "debug")
  71. HOST_NAME = config.get("playstreamproxy", "host")
  72. PORT_NUMBER = config.getint("playstreamproxy", "port")
  73. REDIRECT = config.getboolean("playstreamproxy", "redirect")
  74. KEY = config.get("playstreamproxy", "key")
  75. MULTITHREAD = config.getboolean("playstreamproxy", "multithread")
  76. WORKERS = config.getint("playstreamproxy", "workers")
  77. pass
  78. class StreamHandler(BaseHTTPRequestHandler):
  79. def do_HEAD(self):
  80. print "**get_head"
  81. self.send_response(200)
  82. self.send_header("Server", "playstreamproxy")
  83. if ".m3u8" in self.path.lower():
  84. ct = "application/vnd.apple.mpegurl"
  85. elif ".ts" in self.path.lower():
  86. ct = "video/MP2T"
  87. elif ".mp4" in self.path.lower():
  88. ct = "video/mp4"
  89. else:
  90. ct = "text/html"
  91. self.send_header("Content-type", ct)
  92. self.end_headers()
  93. def do_GET(self):
  94. """Respond to a GET request"""
  95. #
  96. print "\n\n"+40*"#"+"\nget_url: \n%s" % self.path
  97. cmd, data, headers, qs = streamproxy_decode3(self.path)
  98. print "cmd=", cmd,
  99. print " data=", data
  100. print_headers(qs)
  101. if KEY:
  102. key = qs["key"] if "key" in qs else ""
  103. if key <> KEY:
  104. print "No key provided"
  105. #self.send_response(404)
  106. self.write_error(404)
  107. return
  108. if DEBUG:
  109. print "=== Headers:"
  110. print_headers(self.headers.dict)
  111. self.protocol_version = 'HTTP/1.1'
  112. try:
  113. if cmd == "playstream":
  114. self.fetch_source( self.path)
  115. elif cmd in ["get_content", "get_streams", "get_info", "is_video", "options_read", "options_write"]:
  116. if cmd == "get_content":
  117. content = sources.get_content(data)
  118. elif cmd == "get_streams":
  119. content = sources.get_streams(data)
  120. elif cmd == "get_info":
  121. content = sources.get_info(data)
  122. elif cmd == "is_video":
  123. content = sources.is_video(data)
  124. elif cmd == "options_read":
  125. content = sources.options_read(data)
  126. else:
  127. content = []
  128. txt = json.dumps(content)
  129. self.send_response(200)
  130. self.send_header("Server", "playstreamproxy")
  131. self.send_header("Content-type", "application/json")
  132. self.end_headers()
  133. self.wfile.write(txt)
  134. self.wfile.close()
  135. else:
  136. self.write_error(404)
  137. except Exception as e:
  138. print "Got Exception: ", str(e)
  139. import traceback
  140. traceback.print_exc()
  141. ### Remote server request procedures ###
  142. def fetch_offline(self):
  143. print "** Fetch offline"
  144. self.send_response(200)
  145. self.send_header("Server", "playstreamproxy")
  146. self.send_header("Content-type", "video/mp4")
  147. self.end_headers()
  148. self.wfile.write(open("offline.mp4", "rb").read())
  149. #self.wfile.close()
  150. def redirect_source(self, urlp):
  151. cmd, data, headers, qs = streamproxy_decode3(urlp)
  152. streams = sources.get_streams(data)
  153. if not streams:
  154. self.write_error(500) # TODO
  155. return
  156. stream = streams[0]
  157. url = stream["url"]
  158. headers = stream["headers"] if "headers" in stream else headers0
  159. self.send_response(307)
  160. self.send_header("Location", url)
  161. self.end_headers()
  162. def fetch_source(self, urlp):
  163. cmd, data, headers, qs = streamproxy_decode3(urlp)
  164. if DEBUG:
  165. print "\n***********************************************************"
  166. print "fetch_source: \n%s"%urlp
  167. base_data = hls_base(urlp)
  168. if DEBUG:
  169. print "base_data=", base_data
  170. print "data=", data
  171. if not base_data in slinks:
  172. streams = sources.get_streams(data)
  173. if not streams:
  174. self.write_error(500) # TODO
  175. return
  176. stream = streams[0]
  177. url = stream["url"]
  178. headers = stream["headers"] if "headers" in stream else headers0
  179. base_url = hls_base2(url)
  180. if DEBUG: print "New link, base_url=",base_url
  181. ses = requests.Session()
  182. ses.trust_env = False
  183. slinks[base_data] = {"data": data, "urlp":urlp,"url": url, "base_url": base_url,"session":ses}
  184. else:
  185. ses = slinks[base_data]["session"]
  186. if urlp == slinks[base_data]["urlp"]:
  187. url = slinks[base_data]["url"]
  188. if DEBUG: print "Existing base link", url
  189. else:
  190. url = urlp.replace(base_data, slinks[base_data]["base_url"])
  191. if DEBUG: print "Existing new link", url
  192. if REDIRECT:
  193. print "-->redirect to: " + url
  194. self.send_response(307)
  195. self.send_header("Location", url)
  196. self.end_headers()
  197. #self.wfile.close()
  198. return
  199. headers2 = headers if headers else self.headers.dict
  200. headers2 = del_headers(headers2, ["host"])
  201. # if ".ts" in url:
  202. # print url
  203. r = self.get_page_ses(url,ses,True,headers = headers2)
  204. code = r.status_code
  205. if not code in (200,206): # TODO 206 apstrāde!
  206. self.write_error(code)
  207. return
  208. if code == 206:
  209. print "Code=206"
  210. self.send_response(code)
  211. #headers2 = del_headers(r.headers, ["Content-Encoding"])
  212. self.send_headers(r.headers)
  213. CHUNK_SIZE = 1024 *4
  214. while True:
  215. chunk = r.raw.read(CHUNK_SIZE, decode_content=False)
  216. if not chunk:
  217. break
  218. try:
  219. self.wfile.write(chunk)
  220. except Exception as e:
  221. print "Exception: ", str(e)
  222. self.wfile.close()
  223. return
  224. if DEBUG: print "**File downloaded"
  225. #if "connection" in r.headers and r.headers["connection"] <> "keep-alive":
  226. self.wfile.close()
  227. return
  228. def send_headers(self,headers):
  229. #if DEBUG:
  230. #print "**Return headers: "
  231. #print_headers(headers)
  232. for h in headers:
  233. self.send_header(h, headers[h])
  234. self.end_headers()
  235. def write_error(self,code):
  236. print "***Error, code=%s" % code
  237. self.send_response(code)
  238. #self.send_headers(r.headers)
  239. self.wfile.close() # TODO?
  240. # self.fetch_offline()
  241. def get_page_ses(self,url,ses,stream=True, headers=None):
  242. headers= headers if headers else headers0
  243. ses.headers.update(headers)
  244. if DEBUG:
  245. print "\n\n====================================================\n**get_page_ses\n%s"%url
  246. print "**Server request headers: "
  247. print_headers(ses.headers)
  248. r = ses.get(url, stream=stream, verify=False)
  249. if DEBUG:
  250. print "**Server response:", r.status_code
  251. print "**Server response headers: "
  252. print_headers(r.headers)
  253. return r
  254. def get_page(self,url,headers=None):
  255. if not headers:
  256. headers = headers0
  257. if DEBUG:
  258. print "\n\n====================================================\n**get_page\n%s"%url
  259. print "**Server request headers: "
  260. print_headers(headers)
  261. r = requests.get(url, headers=headers,stream=True)
  262. if DEBUG:
  263. print "**Server response:", r.status_code
  264. print "**Server response headers: "
  265. print_headers(r.headers)
  266. return r
  267. def address_string(self):
  268. host, port = self.client_address[:2]
  269. #return socket.getfqdn(host)
  270. return host
  271. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  272. """Handle requests in a separate thread."""
  273. def start(host = HOST_NAME, port = PORT_NUMBER, redirect=REDIRECT, key=KEY):
  274. global REDIRECT,KEY
  275. if redirect:
  276. REDIRECT = redirect
  277. if key:
  278. KEY = key
  279. httpd = ThreadedHTTPServer((host, port), StreamHandler)
  280. print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  281. try:
  282. httpd.serve_forever()
  283. except KeyboardInterrupt:
  284. pass
  285. httpd.server_close()
  286. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
  287. def start2(host = HOST_NAME, port = PORT_NUMBER, redirect=REDIRECT, key=KEY, workers=WORKERS):
  288. global REDIRECT,KEY
  289. if redirect:
  290. REDIRECT = redirect
  291. if key:
  292. KEY = key
  293. print time.asctime(), "Server Starts - %s:%s" % (host, port)
  294. addr = ('', port)
  295. sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  296. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  297. sock.bind(addr)
  298. sock.listen(5)
  299. # Launch listener threads.
  300. class Thread(threading.Thread):
  301. def __init__(self, i):
  302. threading.Thread.__init__(self)
  303. self.i = i
  304. self.daemon = True
  305. self.start()
  306. def run(self):
  307. httpd = HTTPServer(addr, StreamHandler, False)
  308. # Prevent the HTTP server from re-binding every handler.
  309. # https://stackoverflow.com/questions/46210672/
  310. httpd.socket = sock
  311. httpd.server_bind = self.server_close = lambda self: None
  312. httpd.serve_forever()
  313. [Thread(i) for i in range(workers)]
  314. time.sleep(9e5)
  315. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
  316. class Daemon:
  317. """
  318. A generic daemon class.
  319. Usage: subclass the Daemon class and override the run() method
  320. """
  321. def __init__(self, pidfile, stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
  322. self.stdin = stdin
  323. self.stdout = stdout
  324. self.stderr = stderr
  325. self.pidfile = pidfile
  326. def daemonize(self):
  327. """
  328. do the UNIX double-fork magic, see Stevens' "Advanced
  329. Programming in the UNIX Environment" for details (ISBN 0201563177)
  330. http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
  331. """
  332. try:
  333. pid = os.fork()
  334. if pid > 0:
  335. # exit first parent
  336. sys.exit(0)
  337. except OSError, e:
  338. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  339. sys.exit(1)
  340. # decouple from parent environment
  341. os.chdir("/")
  342. os.setsid()
  343. os.umask(0)
  344. # do second fork
  345. try:
  346. pid = os.fork()
  347. if pid > 0:
  348. # exit from second parent
  349. sys.exit(0)
  350. except OSError, e:
  351. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  352. sys.exit(1)
  353. # redirect standard file descriptors
  354. sys.stdout.flush()
  355. sys.stderr.flush()
  356. si = file(self.stdin, "r")
  357. so = file(self.stdout, "a+")
  358. se = file(self.stderr, "a+", 0)
  359. os.dup2(si.fileno(), sys.stdin.fileno())
  360. os.dup2(so.fileno(), sys.stdout.fileno())
  361. os.dup2(se.fileno(), sys.stderr.fileno())
  362. # write pidfile
  363. atexit.register(self.delpid)
  364. pid = str(os.getpid())
  365. file(self.pidfile,"w+").write("%s\n" % pid)
  366. def delpid(self):
  367. os.remove(self.pidfile)
  368. def start(self):
  369. """
  370. Start the daemon
  371. """
  372. # Check for a pidfile to see if the daemon already runs
  373. try:
  374. pf = file(self.pidfile,"r")
  375. pid = int(pf.read().strip())
  376. pf.close()
  377. except IOError:
  378. pid = None
  379. if pid:
  380. message = "pidfile %s already exist. Daemon already running?\n"
  381. sys.stderr.write(message % self.pidfile)
  382. sys.exit(1)
  383. # Start the daemon
  384. self.daemonize()
  385. self.run()
  386. def stop(self):
  387. """
  388. Stop the daemon
  389. """
  390. # Get the pid from the pidfile
  391. try:
  392. pf = file(self.pidfile,"r")
  393. pid = int(pf.read().strip())
  394. pf.close()
  395. except IOError:
  396. pid = None
  397. if not pid:
  398. message = "pidfile %s does not exist. Daemon not running?\n"
  399. sys.stderr.write(message % self.pidfile)
  400. return # not an error in a restart
  401. # Try killing the daemon process
  402. try:
  403. while 1:
  404. os.kill(pid, SIGTERM)
  405. time.sleep(0.1)
  406. except OSError, err:
  407. err = str(err)
  408. if err.find("No such process") > 0:
  409. if os.path.exists(self.pidfile):
  410. os.remove(self.pidfile)
  411. else:
  412. print str(err)
  413. sys.exit(1)
  414. def restart(self):
  415. """
  416. Restart the daemon
  417. """
  418. self.stop()
  419. self.start()
  420. def run(self):
  421. """
  422. You should override this method when you subclass Daemon. It will be called after the process has been
  423. daemonized by start() or restart().
  424. """
  425. class ProxyDaemon(Daemon):
  426. def run(self):
  427. print "Daemon start - %s" % MULTITHREAD
  428. if MULTITHREAD:
  429. start2()
  430. else:
  431. start()
  432. def print_headers(headers):
  433. for h in headers:
  434. print "%s: %s"%(h,headers[h])
  435. def del_headers(headers0,tags):
  436. headers = headers0.copy()
  437. for t in tags:
  438. if t in headers:
  439. del headers[t]
  440. if t.lower() in headers:
  441. del headers[t.lower()]
  442. return headers
  443. def hls_base(url):
  444. base = url.split("?")[0]
  445. base = "/".join(base.split("/")[0:3])+ "/"
  446. rest = url.replace(base, "")
  447. return base
  448. def hls_base2(url):
  449. base = url.split("?")[0]
  450. base = "/".join(base.split("/")[0:-1])+ "/"
  451. rest = url.replace(base, "")
  452. return base
  453. if __name__ == "__main__":
  454. daemon = ProxyDaemon("/var/run/playstreamproxy.pid")
  455. if len(sys.argv) == 2:
  456. if "start" == sys.argv[1]:
  457. daemon.start()
  458. elif "stop" == sys.argv[1]:
  459. daemon.stop()
  460. elif "restart" == sys.argv[1]:
  461. daemon.restart()
  462. elif "manualstart" == sys.argv[1]:
  463. daemon.run()
  464. elif "multithread" == sys.argv[1]:
  465. start2()
  466. else:
  467. print "Unknown command"
  468. sys.exit(2)
  469. sys.exit(0)
  470. else:
  471. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  472. sys.exit(2)