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