add proxy object

This commit is contained in:
Roy Olav Purser 2021-05-11 14:00:43 +02:00
parent 060e1807ef
commit be0a868861
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -11,23 +11,25 @@ import re
providers = {} providers = {}
providers["nrk"] = "https://tv.nrk.no" providers["nrk"] = "https://tv.nrk.no"
providers["svt"] = "https://svtplay.se" providers["svt"] = "https://svtplay.se"
providers["youtube"] = "https://youtu.be" providers["youtube"] = "https://www.youtube.com/watch?v="
providers["twitch"] = "https://twitch.tv" providers["twitch"] = "https://twitch.tv"
proxies = {} class ProxyElem():
proxies_raw = {} def __init__(self, proxy):
proxies_req = {} self.stream = streamlink.Streamlink()
self.proxy = proxy
for key in providers: self.req = {}
proxies[key] = streamlink.Streamlink() self.stream.set_option("http-timeout", 2.0)
proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key].set_option("http-timeout", 2.0)
if proxy is not None: if proxy is not None:
proxies[key].set_option("https-proxy", "socks5://" + proxy) self.stream.set_option("https-proxy", "socks5://" + proxy)
proxies_raw[key] = proxy self.stream.set_option("http-proxy", "socks5://" + proxy)
proxies_req[key] = {} self.req["http"] = "socks5://" + proxy
proxies_req[key]["http"] = "socks5://" + proxy self.req["https"] = "socks5://" + proxy
proxies_req[key]["https"] = "socks5://" + proxy
proxies = {}
for key in providers:
proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key] = ProxyElem(proxy)
playlist = None playlist = None
icecast_server = os.environ.get("ICECAST_SERVER") icecast_server = os.environ.get("ICECAST_SERVER")
@ -74,19 +76,17 @@ def get_proxy_url(proxy, current, path):
return presp.text return presp.text
def rewrite(current, provider): def rewrite(current, provider):
proxy_req = proxies_req.get(provider) proxy = proxies.get(provider)
proxy = proxies_raw.get(provider) resp = requests.head(current, proxies=proxy.req)
resp = requests.head(current, proxies=proxy_req)
ctype = resp.headers.get("Content-Type") ctype = resp.headers.get("Content-Type")
if ctype is None: if ctype is None:
return None return None
else: else:
if "mpegurl" not in ctype.lower(): if "mpegurl" not in ctype.lower():
return None return None
resp = requests.get(current, proxies=proxy_req) resp = requests.get(current, proxies=proxy.req)
ndata = None ndata = None
if resp.text is not None: if resp.text is not None:
ndata = ""
links = [] links = []
for line in resp.text.splitlines(): for line in resp.text.splitlines():
if line.startswith("#EXT-X-KEY:METHOD="): if line.startswith("#EXT-X-KEY:METHOD="):
@ -94,17 +94,19 @@ def rewrite(current, provider):
if len(matches) == 1: if len(matches) == 1:
ldata = {} ldata = {}
ldata["upstream"] = urllib.parse.urljoin(current, matches[0]) ldata["upstream"] = urllib.parse.urljoin(current, matches[0])
ldata["proxy"] = proxy ldata["proxy"] = proxy.proxy
ldata["proxied"] = isinstance(proxy, str) ldata["proxied"] = isinstance(proxy.proxy, str)
links.append(ldata) links.append(ldata)
elif line.startswith("#"): elif line.startswith("#"):
pass pass
else: else:
ldata = {} ldata = {}
ldata["upstream"] = urllib.parse.urljoin(current, line) ldata["upstream"] = urllib.parse.urljoin(current, line)
ldata["proxy"] = proxy ldata["proxy"] = proxy.proxy
ldata["proxied"] = isinstance(proxy, str) ldata["proxied"] = isinstance(proxy.proxy, str)
links.append(ldata) links.append(ldata)
if isinstance(proxy_server, str):
ndata = ""
presp = requests.post(proxy_server, json=links) presp = requests.post(proxy_server, json=links)
if isinstance(presp.text, str): if isinstance(presp.text, str):
print(presp.text) print(presp.text)
@ -153,19 +155,19 @@ class MainHandler(tornado.web.RequestHandler):
proxy = None proxy = None
if provider in providers.keys(): if provider in providers.keys():
proxy = proxies.get(provider) proxy = proxies.get(provider)
path = self.request.path
if provider == "youtube":
path = path.strip("/")
src = providers[provider] + self.request.path src = providers[provider] + self.request.path
try: try:
resp = requests.head(src, allow_redirects=True) resp = requests.head(src, allow_redirects=True)
if resp is not None: if resp is not None:
src = resp.url src = resp.url
if isinstance(src, str) and "consent.youtube.com" in src:
video_id = self.request.path.strip("/")
src = f'https://www.youtube.com/watch?v={video_id}'
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
try: try:
streams = proxy.streams(src) streams = proxy.stream.streams(src)
for key in reversed(streams): for key in reversed(streams):
stream = streams.get(key) stream = streams.get(key)
if hasattr(stream, "url"): if hasattr(stream, "url"):