proxy fallback

This commit is contained in:
Roy Olav Purser 2021-05-11 14:19:17 +02:00
parent be0a868861
commit 6771bb774b
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -28,8 +28,17 @@ class ProxyElem():
proxies = {}
for key in providers:
proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key] = ProxyElem(proxy)
proxies[key] = []
current = []
for i in range(0,9):
proxy = os.environ.get(f'{key}_proxy{i}'.upper())
if proxy is not None:
current.append(proxy)
if len(current) == 0:
proxies[key].append(ProxyElem(None))
else:
for proxy in current:
proxies[key].append(ProxyElem(proxy))
playlist = None
icecast_server = os.environ.get("ICECAST_SERVER")
@ -154,18 +163,23 @@ class MainHandler(tornado.web.RequestHandler):
upstream = None
proxy = None
if provider in providers.keys():
proxy = proxies.get(provider)
path = self.request.path
if provider == "youtube":
path = path.strip("/")
src = providers[provider] + self.request.path
try:
resp = requests.head(src, allow_redirects=True)
if resp is not None:
src = resp.url
except Exception as e:
print(e)
else:
proxy = None
proxy_list = proxies.get(provider)
if isinstance(proxy_list, list):
for proxy_temp in proxy_list:
try:
resp = requests.head(src, allow_redirects=True, proxies=proxy_temp.req)
if resp is not None:
src = resp.url
except Exception as e:
print(e)
else:
proxy = proxy_temp
if proxy is not None:
try:
streams = proxy.stream.streams(src)
for key in reversed(streams):
@ -174,8 +188,7 @@ class MainHandler(tornado.web.RequestHandler):
upstream = stream.url
break
except Exception as e:
print(str(e))
upstream = None
print(e)
if upstream is None:
self.set_status(404)
if write: