diff --git a/stream.py b/stream.py index e5323f8..b370ef0 100755 --- a/stream.py +++ b/stream.py @@ -6,6 +6,7 @@ import re import os import base64 import logging +import asyncio import streamlink import tornado.web import tornado.routing @@ -56,8 +57,6 @@ class ProxyElem(): logger.info(e) return ctype async def proxy_url(self, current, path): - if proxy_server is None: - return data["upstream"] jdata = None data = {} data_list = [data] @@ -66,11 +65,12 @@ class ProxyElem(): else: data["upstream"] = urllib.parse.urljoin(current, path) data["proxied"] = True - ret = None if self.proxy is None: data["proxied"] = False else: data["proxy"] = self.proxy + if proxy_server is None: + return data["upstream"] try: async with self.local() as session: resp = await session.post(proxy_server, json=data_list) @@ -148,22 +148,28 @@ class UpstreamHandler(): current = proxy_list.pop() proxy_list = [current] + proxy_list session = current.session() - delays.append(AsyncSession(session, session.head(src), current_list, current)) + task = asyncio.create_task(session.head(src)) + delays.append(AsyncSession(session, task, current_list, current)) + done = False for delay in delays: - try: - resp = await delay.future - except Exception as e: - logger.info(e) + if done: + delay.future.cancel() else: - new_url = str(resp.url) - if new_url.lower().startswith("https://consent.youtube.com"): - self.upstream = src - self.upstream_safe = urllib.parse.quote(src) + try: + resp = await delay.future + except Exception as e: + logger.info(e) else: - self.upstream = new_url - self.upstream_safe = urllib.parse.quote(new_url) - self.proxy = delay.current_proxy - proxies[self.provider] = delay.current_proxies + done = True + new_url = str(resp.url) + if new_url.lower().startswith("https://consent.youtube.com"): + self.upstream = src + self.upstream_safe = urllib.parse.quote(src) + else: + self.upstream = new_url + self.upstream_safe = urllib.parse.quote(new_url) + self.proxy = delay.current_proxy + proxies[self.provider] = delay.current_proxies for delay in delays: try: await delay.session.close()