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

playstreamproxy.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. for h in r.headers:
  85. if h in ("user-agent","server"):continue
  86. if h=="connection":
  87. if DEBUG: print h," skipped"
  88. continue
  89. self.send_header(h, r.headers[h])
  90. if DEBUG:print h,"=",r.headers[h]
  91. self.end_headers()
  92. CHUNK_SIZE = 4 * 1024
  93. if code == 200:
  94. #while True:
  95. #chunk = r.read(CHUNK_SIZE)
  96. #if not chunk:
  97. #break
  98. #wfile.write(chunk)
  99. #pass
  100. #wfile.close()
  101. for chunk in r.iter_content(1024):
  102. try:
  103. #print "#",
  104. wfile.write(chunk)
  105. except Exception as e:
  106. print "Exception: ", str(e)
  107. return
  108. if DEBUG: print " = file downloaded = "
  109. time.sleep(2)
  110. #self.wfile.close()
  111. else:
  112. print code
  113. self.fetch_offline(wfile)
  114. pass
  115. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  116. """Handle requests in a separate thread."""
  117. def start():
  118. httpd = ThreadedHTTPServer((HOST_NAME, PORT_NUMBER), StreamHandler)
  119. print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  120. try:
  121. httpd.serve_forever()
  122. except KeyboardInterrupt:
  123. pass
  124. httpd.server_close()
  125. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
  126. class Daemon:
  127. """
  128. A generic daemon class.
  129. Usage: subclass the Daemon class and override the run() method
  130. """
  131. def __init__(self, pidfile, stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
  132. self.stdin = stdin
  133. self.stdout = stdout
  134. self.stderr = stderr
  135. self.pidfile = pidfile
  136. def daemonize(self):
  137. """
  138. do the UNIX double-fork magic, see Stevens' "Advanced
  139. Programming in the UNIX Environment" for details (ISBN 0201563177)
  140. http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
  141. """
  142. try:
  143. pid = os.fork()
  144. if pid > 0:
  145. # exit first parent
  146. sys.exit(0)
  147. except OSError, e:
  148. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  149. sys.exit(1)
  150. # decouple from parent environment
  151. os.chdir("/")
  152. os.setsid()
  153. os.umask(0)
  154. # do second fork
  155. try:
  156. pid = os.fork()
  157. if pid > 0:
  158. # exit from second parent
  159. sys.exit(0)
  160. except OSError, e:
  161. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  162. sys.exit(1)
  163. # redirect standard file descriptors
  164. sys.stdout.flush()
  165. sys.stderr.flush()
  166. si = file(self.stdin, "r")
  167. so = file(self.stdout, "a+")
  168. se = file(self.stderr, "a+", 0)
  169. os.dup2(si.fileno(), sys.stdin.fileno())
  170. os.dup2(so.fileno(), sys.stdout.fileno())
  171. os.dup2(se.fileno(), sys.stderr.fileno())
  172. # write pidfile
  173. atexit.register(self.delpid)
  174. pid = str(os.getpid())
  175. file(self.pidfile,"w+").write("%s\n" % pid)
  176. def delpid(self):
  177. os.remove(self.pidfile)
  178. def start(self):
  179. """
  180. Start the daemon
  181. """
  182. # Check for a pidfile to see if the daemon already runs
  183. try:
  184. pf = file(self.pidfile,"r")
  185. pid = int(pf.read().strip())
  186. pf.close()
  187. except IOError:
  188. pid = None
  189. if pid:
  190. message = "pidfile %s already exist. Daemon already running?\n"
  191. sys.stderr.write(message % self.pidfile)
  192. sys.exit(1)
  193. # Start the daemon
  194. self.daemonize()
  195. self.run()
  196. def stop(self):
  197. """
  198. Stop the daemon
  199. """
  200. # Get the pid from the pidfile
  201. try:
  202. pf = file(self.pidfile,"r")
  203. pid = int(pf.read().strip())
  204. pf.close()
  205. except IOError:
  206. pid = None
  207. if not pid:
  208. message = "pidfile %s does not exist. Daemon not running?\n"
  209. sys.stderr.write(message % self.pidfile)
  210. return # not an error in a restart
  211. # Try killing the daemon process
  212. try:
  213. while 1:
  214. os.kill(pid, SIGTERM)
  215. time.sleep(0.1)
  216. except OSError, err:
  217. err = str(err)
  218. if err.find("No such process") > 0:
  219. if os.path.exists(self.pidfile):
  220. os.remove(self.pidfile)
  221. else:
  222. print str(err)
  223. sys.exit(1)
  224. def restart(self):
  225. """
  226. Restart the daemon
  227. """
  228. self.stop()
  229. self.start()
  230. def run(self):
  231. """
  232. You should override this method when you subclass Daemon. It will be called after the process has been
  233. daemonized by start() or restart().
  234. """
  235. class ProxyDaemon(Daemon):
  236. def run(self):
  237. start()
  238. if __name__ == "__main__":
  239. daemon = ProxyDaemon("/var/run/streamproxy.pid")
  240. if len(sys.argv) == 2:
  241. if "start" == sys.argv[1]:
  242. daemon.start()
  243. elif "stop" == sys.argv[1]:
  244. daemon.stop()
  245. elif "restart" == sys.argv[1]:
  246. daemon.restart()
  247. elif "manualstart" == sys.argv[1]:
  248. start()
  249. else:
  250. print "Unknown command"
  251. sys.exit(2)
  252. sys.exit(0)
  253. else:
  254. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  255. sys.exit(2)