From b0b0055d756d058e8f7ecac175c6dc0e9c53aa28 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 14 May 2021 19:13:18 +0200 Subject: [PATCH] future stuff --- stream.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) 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()