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

playstreamproxy2.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. PlayStreamProxy server
  5. Provides API to ContetSources + stream serving to play via m3u8 playlists
  6. """
  7. import os, sys, time
  8. import urllib,urlparse
  9. from urllib import unquote, quote
  10. #import cookielib,urllib2
  11. import requests
  12. import re, json
  13. from diskcache import Cache
  14. import bottle
  15. from bottle import Bottle, hook, response, route, request, run
  16. #if not sys.platform == 'win32':
  17. import daemonize
  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. # Default arguments
  28. SERVER = "wsgiref"
  29. PORT_NUMBER = 8880
  30. DEBUG = False
  31. cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
  32. cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
  33. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  34. headers0 = headers2dict("""
  35. User-Agent: GStreamer souphttpsrc libsoup/2.52.2
  36. icy-metadata: 1
  37. """)
  38. headers0_ = headers2dict("""
  39. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  40. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  41. """)
  42. cur_directory = os.path.dirname(os.path.realpath(__file__))
  43. slinks = {}
  44. sessions = {}
  45. cfg_file = "streams.cfg"
  46. sources0 = ContentSources("", cfg_file)
  47. cache_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "cache")
  48. s0 = Cache(cache_dir)
  49. s0["n"] = 0
  50. s0["sources"] = sources0
  51. app = Bottle()
  52. @app.hook('before_request')
  53. def set_globals():
  54. global s, sources, cmd, data, headers, qs
  55. cmd, data, headers, qs = streamproxy_decode3(request.url)
  56. cache_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "cache")
  57. s = Cache(cache_dir)
  58. sources = s["sources"]
  59. # http://localhost:8880/playstream/ltc%3A%3Acontent%2Flive-streams%2F101%3Finclude%3Dquality/
  60. #@app.route('/')
  61. @app.route('/playstream/<url:re:.*>')
  62. def fetch_source(url):
  63. global s, sources, cmd, data, headers, qs
  64. return 'cmd=%s, data=%s, qs=%s'% (cmd, data, str(qs))
  65. ####################################################################
  66. # Run WSGI server
  67. def start(server,port):
  68. options = {}
  69. if server == "mtwsgi":
  70. import mtwsgi
  71. server = mtwsgi.MTServer
  72. options = {"thread_count": 3,}
  73. run(app=app,server=server, host='0.0.0.0',
  74. port=port,
  75. #reloader=True,
  76. quiet=False,
  77. plugins=None,
  78. debug=True,
  79. #thread_count=3,
  80. config=None,
  81. **options)
  82. def print_headers(headers):
  83. for h in headers:
  84. print "%s: %s"%(h,headers[h])
  85. def del_headers(headers0,tags):
  86. headers = headers0.copy()
  87. for t in tags:
  88. if t in headers:
  89. del headers[t]
  90. if t.lower() in headers:
  91. del headers[t.lower()]
  92. return headers
  93. def hls_base(url):
  94. base = url.split("?")[0]
  95. base = "/".join(base.split("/")[0:3])+ "/"
  96. rest = url.replace(base, "")
  97. return base
  98. def hls_base2(url):
  99. base = url.split("?")[0]
  100. base = "/".join(base.split("/")[0:-1])+ "/"
  101. rest = url.replace(base, "")
  102. return base
  103. if __name__ == '__main__':
  104. if len(sys.argv) > 1:
  105. pid = "/var/run/playstreamproxy2.pid"
  106. daemon = daemonize.Daemon(start, pid)
  107. server = sys.argv[2] if len(sys.argv) > 2 else SERVER
  108. port = sys.argv[3] if len(sys.argv) > 3 else PORT_NUMBER
  109. if "start" == sys.argv[1]:
  110. daemon.start(server,port)
  111. daemon.is_running()
  112. elif "stop" == sys.argv[1]:
  113. daemon.stop()
  114. elif "restart" == sys.argv[1]:
  115. daemon.restart()
  116. daemon.is_running()
  117. elif "manualstart" == sys.argv[1]:
  118. start(server,port)
  119. elif "status" == sys.argv[1]:
  120. daemon.is_running()
  121. else:
  122. print "Unknown command"
  123. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  124. sys.exit(2)
  125. sys.exit(0)
  126. else:
  127. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  128. sys.exit(2)