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

playstreamproxy.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/python
  2. """
  3. StreamProxy daemon (based on Livestream daemon)
  4. Ensures persistent cookies, User-Agents and others tricks to play protected HLS/DASH streams
  5. """
  6. import os
  7. import sys
  8. import time
  9. import atexit
  10. import re
  11. from signal import SIGTERM
  12. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  13. from SocketServer import ThreadingMixIn
  14. from urllib import unquote, quote
  15. import urllib,urlparse
  16. #import cookielib,urllib2
  17. import requests
  18. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  19. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  20. HOST_NAME = ""
  21. PORT_NUMBER = 88
  22. DEBUG = True
  23. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  24. sessions = {}
  25. class StreamHandler(BaseHTTPRequestHandler):
  26. def do_HEAD(self):
  27. self.send_response(200)
  28. self.send_header("Server", "StreamProxy")
  29. self.send_header("Content-type", "text/html")
  30. self.end_headers()
  31. def do_GET(self):
  32. """Respond to a GET request."""
  33. SPLIT_CHAR = "~"
  34. SPLIT_CODE = "%7E"
  35. EQ_CODE = "%3D"
  36. COL_CODE = "%3A"
  37. p = self.path.split("~")
  38. url = urllib.unquote(p[0][1:])
  39. url = url.replace(COL_CODE, ":")
  40. headers = headers2dict("""
  41. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4
  42. """)
  43. if len(p)>1:
  44. for h in p[1:]:
  45. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  46. #self.fetch_offline(self.wfile)
  47. try:
  48. self.fetch_url2(self.wfile, url, headers)
  49. except Exception as e:
  50. print "Got Exception: ", str(e)
  51. def fetch_offline(self,wfile):
  52. self.send_response(200)
  53. self.send_header("Server", "StreamProxy")
  54. self.send_header("Content-type", "video/mp4")
  55. self.end_headers()
  56. self.wfile.write(open("offline.mp4", "rb").read())
  57. self.wfile.close()
  58. def fetch_url2(self, wfile, url, headers):
  59. if DEBUG: print "\n***********************************************************"
  60. self.log_message("fetch_url: %s", url)
  61. #self.log_message("headers: %s", headers)
  62. base_url = "/".join(url.split("/")[0:-1])
  63. if base_url not in sessions:
  64. sessions[base_url] = requests.Session()
  65. #cj = cookielib.CookieJar()
  66. #sessions[base_url] = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  67. else:
  68. pass
  69. ses = sessions[base_url]
  70. if DEBUG: print "**Request headers: "
  71. ses.headers.update(headers)
  72. #ses.addheaders=[]
  73. for h in ses.headers:
  74. #ses.addheaders.append((h,headers[h]))
  75. if DEBUG: print h,"=",ses.headers[h]
  76. r = ses.get(url, stream=True,verify=False)
  77. #r = ses.open(url)
  78. code = r.status_code #r.status_code
  79. if DEBUG: print "**Response:", code #r.status_code
  80. if DEBUG: print "**Response headers: "
  81. for h in r.headers:
  82. if DEBUG: print h,"=",r.headers[h]
  83. self.send_response(code)
  84. if DEBUG: print "**Return headers:"
  85. for h in r.headers:
  86. if h.lower() in ("user-agent","server"):continue
  87. if h.lower()=="connection":
  88. if DEBUG: print h," skipped"
  89. continue
  90. self.send_header(h, r.headers[h])
  91. if DEBUG:print h,"=",r.headers[h]
  92. self.end_headers()
  93. CHUNK_SIZE = 4 * 1024
  94. if code == 200:
  95. #while True:
  96. #chunk = r.read(CHUNK_SIZE)
  97. #if not chunk:
  98. #break
  99. #wfile.write(chunk)
  100. #pass
  101. #wfile.close()
  102. for chunk in r.iter_content(1024):
  103. try:
  104. #print "#",
  105. wfile.write(chunk)
  106. except Exception as e:
  107. print "Exception: ", str(e)
  108. return
  109. if DEBUG: print " = file downloaded = "
  110. time.sleep(2)
  111. #self.wfile.close()
  112. else:
  113. print code
  114. self.fetch_offline(wfile)
  115. pass
  116. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  117. """Handle requests in a separate thread."""
  118. def start():
  119. httpd = ThreadedHTTPServer((HOST_NAME, PORT_NUMBER), StreamHandler)
  120. print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  121. try:
  122. httpd.serve_forever()
  123. except KeyboardInterrupt:
  124. pass
  125. httpd.server_close()
  126. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
  127. class Daemon:
  128. """
  129. A generic daemon class.
  130. Usage: subclass the Daemon class and override the run() method
  131. """
  132. def __init__(self, pidfile, stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
  133. self.stdin = stdin
  134. self.stdout = stdout
  135. self.stderr = stderr
  136. self.pidfile = pidfile
  137. def daemonize(self):
  138. """
  139. do the UNIX double-fork magic, see Stevens' "Advanced
  140. Programming in the UNIX Environment" for details (ISBN 0201563177)
  141. http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
  142. """
  143. try:
  144. pid = os.fork()
  145. if pid > 0:
  146. # exit first parent
  147. sys.exit(0)
  148. except OSError, e:
  149. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  150. sys.exit(1)
  151. # decouple from parent environment
  152. os.chdir("/")
  153. os.setsid()
  154. os.umask(0)
  155. # do second fork
  156. try:
  157. pid = os.fork()
  158. if pid > 0:
  159. # exit from second parent
  160. sys.exit(0)
  161. except OSError, e:
  162. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  163. sys.exit(1)
  164. # redirect standard file descriptors
  165. sys.stdout.flush()
  166. sys.stderr.flush()
  167. si = file(self.stdin, "r")
  168. so = file(self.stdout, "a+")
  169. se = file(self.stderr, "a+", 0)
  170. os.dup2(si.fileno(), sys.stdin.fileno())
  171. os.dup2(so.fileno(), sys.stdout.fileno())
  172. os.dup2(se.fileno(), sys.stderr.fileno())
  173. # write pidfile
  174. atexit.register(self.delpid)
  175. pid = str(os.getpid())
  176. file(self.pidfile,"w+").write("%s\n" % pid)
  177. def delpid(self):
  178. os.remove(self.pidfile)
  179. def start(self):
  180. """
  181. Start the daemon
  182. """
  183. # Check for a pidfile to see if the daemon already runs
  184. try:
  185. pf = file(self.pidfile,"r")
  186. pid = int(pf.read().strip())
  187. pf.close()
  188. except IOError:
  189. pid = None
  190. if pid:
  191. message = "pidfile %s already exist. Daemon already running?\n"
  192. sys.stderr.write(message % self.pidfile)
  193. sys.exit(1)
  194. # Start the daemon
  195. self.daemonize()
  196. self.run()
  197. def stop(self):
  198. """
  199. Stop the daemon
  200. """
  201. # Get the pid from the pidfile
  202. try:
  203. pf = file(self.pidfile,"r")
  204. pid = int(pf.read().strip())
  205. pf.close()
  206. except IOError:
  207. pid = None
  208. if not pid:
  209. message = "pidfile %s does not exist. Daemon not running?\n"
  210. sys.stderr.write(message % self.pidfile)
  211. return # not an error in a restart
  212. # Try killing the daemon process
  213. try:
  214. while 1:
  215. os.kill(pid, SIGTERM)
  216. time.sleep(0.1)
  217. except OSError, err:
  218. err = str(err)
  219. if err.find("No such process") > 0:
  220. if os.path.exists(self.pidfile):
  221. os.remove(self.pidfile)
  222. else:
  223. print str(err)
  224. sys.exit(1)
  225. def restart(self):
  226. """
  227. Restart the daemon
  228. """
  229. self.stop()
  230. self.start()
  231. def run(self):
  232. """
  233. You should override this method when you subclass Daemon. It will be called after the process has been
  234. daemonized by start() or restart().
  235. """
  236. class ProxyDaemon(Daemon):
  237. def run(self):
  238. start()
  239. if __name__ == "__main__":
  240. daemon = ProxyDaemon("/var/run/streamproxy.pid")
  241. if len(sys.argv) == 2:
  242. if "start" == sys.argv[1]:
  243. daemon.start()
  244. elif "stop" == sys.argv[1]:
  245. daemon.stop()
  246. elif "restart" == sys.argv[1]:
  247. daemon.restart()
  248. elif "manualstart" == sys.argv[1]:
  249. start()
  250. else:
  251. print "Unknown command"
  252. sys.exit(2)
  253. sys.exit(0)
  254. else:
  255. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  256. sys.exit(2)