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
self.req = {}
self.stream.set_option("http-timeout", 2.0)
if proxy is not None:
self.stream.set_option("https-proxy", "socks5://" + proxy)
self.stream.set_option("http-proxy", "socks5://" + proxy)
self.req["http"] = "socks5://" + proxy
self.req["https"] = "socks5://" + proxy
proxies = {}
for key in providers: 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] = ProxyElem(proxy)
if proxy is not None:
proxies[key].set_option("https-proxy", "socks5://" + proxy)
proxies_raw[key] = proxy
proxies_req[key] = {}
proxies_req[key]["http"] = "socks5://" + proxy
proxies_req[key]["https"] = "socks5://" + 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,32 +94,34 @@ 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)
presp = requests.post(proxy_server, json=links) if isinstance(proxy_server, str):
if isinstance(presp.text, str): ndata = ""
print(presp.text) presp = requests.post(proxy_server, json=links)
links = json.loads(presp.text) if isinstance(presp.text, str):
for line in resp.text.splitlines(): print(presp.text)
if line.startswith("#EXT-X-KEY:METHOD="): links = json.loads(presp.text)
matches = re.findall(r'(?<=URI=").+(?=")', line) for line in resp.text.splitlines():
if len(matches) == 1: if line.startswith("#EXT-X-KEY:METHOD="):
new_url = links.pop(0) matches = re.findall(r'(?<=URI=").+(?=")', line)
ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line) if len(matches) == 1:
elif line.startswith("#"): new_url = links.pop(0)
ndata += line ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line)
else: elif line.startswith("#"):
ndata += links.pop(0) ndata += line
ndata += "\n" else:
ndata += links.pop(0)
ndata += "\n"
return ndata return ndata
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
@ -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"):