add proxy
This commit is contained in:
parent
3fc31165e5
commit
c1ab35efa3
36
stream.py
36
stream.py
@ -5,6 +5,7 @@ import tornado.routing
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
providers = {}
|
providers = {}
|
||||||
providers["nrk"] = "https://tv.nrk.no"
|
providers["nrk"] = "https://tv.nrk.no"
|
||||||
@ -13,20 +14,22 @@ providers["youtube"] = "https://youtu.be"
|
|||||||
providers["twitch"] = "https://twitch.tv"
|
providers["twitch"] = "https://twitch.tv"
|
||||||
|
|
||||||
proxies = {}
|
proxies = {}
|
||||||
proxies["nrk"] = streamlink.Streamlink()
|
proxies_raw = {}
|
||||||
proxies["svt"] = streamlink.Streamlink()
|
|
||||||
proxies["youtube"] = streamlink.Streamlink()
|
|
||||||
proxies["twitch"] = streamlink.Streamlink()
|
|
||||||
|
|
||||||
for key in proxies:
|
for key in providers:
|
||||||
|
proxies[key] = streamlink.Streamlink()
|
||||||
proxy = os.environ.get(key.upper() + "_PROXY")
|
proxy = os.environ.get(key.upper() + "_PROXY")
|
||||||
proxies[key].set_option("http-timeout", 2.0)
|
proxies[key].set_option("http-timeout", 2.0)
|
||||||
if proxy is not None:
|
if proxy is not None:
|
||||||
proxies[key].set_option("https-proxy", proxy)
|
#proxies[key].set_option("https-proxy", proxy)
|
||||||
|
splitted = proxy.split(":", 1)
|
||||||
|
if len(splitted) == 2:
|
||||||
|
proxies_raw[key] = splitted[1].lstrip("/")
|
||||||
|
|
||||||
playlist = None
|
playlist = None
|
||||||
icecast_server = os.environ.get("ICECAST_SERVER")
|
icecast_server = os.environ.get("ICECAST_SERVER")
|
||||||
stream_server = os.environ.get("STREAM_SERVER")
|
stream_server = os.environ.get("STREAM_SERVER")
|
||||||
|
proxy_server = os.environ.get("PROXY_SERVER")
|
||||||
|
|
||||||
if icecast_server is not None and stream_server is not None:
|
if icecast_server is not None and stream_server is not None:
|
||||||
with open("/app/sources.json", "r") as f:
|
with open("/app/sources.json", "r") as f:
|
||||||
@ -43,12 +46,32 @@ if icecast_server is not None and stream_server is not None:
|
|||||||
playlist += f'#EXTINF:0 radio="false", {name}\n'
|
playlist += f'#EXTINF:0 radio="false", {name}\n'
|
||||||
playlist += stream_server + key + "\n"
|
playlist += stream_server + key + "\n"
|
||||||
|
|
||||||
|
def rewrite(current, endpoint, proxy):
|
||||||
|
resp = requests.get(current)
|
||||||
|
ndata = resp.text
|
||||||
|
if resp.text is not None and proxy_server is not None:
|
||||||
|
ndata = ""
|
||||||
|
for line in resp.text.splitlines():
|
||||||
|
if line.startswith("#"):
|
||||||
|
ndata += line
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
data["upstream"] = urllib.parse.urljoin(current, line)
|
||||||
|
data["proxy"] = proxy
|
||||||
|
presp = requests.post(proxy_server, json=data)
|
||||||
|
ndata += presp.text
|
||||||
|
ndata += "\n"
|
||||||
|
print(ndata)
|
||||||
|
|
||||||
class MainHandler(tornado.web.RequestHandler):
|
class MainHandler(tornado.web.RequestHandler):
|
||||||
def handle_any(self, write):
|
def handle_any(self, write):
|
||||||
provider = self.get_query_argument("provider", None)
|
provider = self.get_query_argument("provider", None)
|
||||||
endpoint = None
|
endpoint = None
|
||||||
|
proxy = None
|
||||||
|
proxy_raw = None
|
||||||
if provider is not None and provider in providers.keys():
|
if provider is not None and provider in providers.keys():
|
||||||
proxy = proxies.get(provider)
|
proxy = proxies.get(provider)
|
||||||
|
proxy_raw = proxies_raw.get(provider)
|
||||||
src = providers[provider] + self.request.uri
|
src = providers[provider] + self.request.uri
|
||||||
try:
|
try:
|
||||||
resp = requests.get(src)
|
resp = requests.get(src)
|
||||||
@ -72,6 +95,7 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
if write:
|
if write:
|
||||||
self.write("stream not found")
|
self.write("stream not found")
|
||||||
else:
|
else:
|
||||||
|
rewrite(endpoint, "", proxy_raw)
|
||||||
self.redirect(endpoint, status=303)
|
self.redirect(endpoint, status=303)
|
||||||
def get(self):
|
def get(self):
|
||||||
self.handle_any(True)
|
self.handle_any(True)
|
||||||
|
Loading…
Reference in New Issue
Block a user