From 9f8a966edb6456bfe6242cef29e691c0396646cc Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Mon, 10 May 2021 16:24:31 +0200 Subject: [PATCH] create all links at once --- stream.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/stream.py b/stream.py index e0f5333..92a4a45 100755 --- a/stream.py +++ b/stream.py @@ -84,18 +84,40 @@ def rewrite(current, provider): ndata = None if resp.text is not None: ndata = "" + links = [] for line in resp.text.splitlines(): if line.startswith("#EXT-X-KEY:METHOD="): matches = re.findall(r'(?<=URI=").+(?=")', line) if len(matches) == 1: - new_url = get_proxy_url(proxy, current, matches[0]) + ldata = {} + ldata["upstream"] = urllib.parse.urljoin(current, matches[0]) + ldata["proxy"] = proxy + ldata["proxied"] = isinstance(proxy, str) + links.append(ldata) + elif line.startswith("#"): + pass + else: + ldata = {} + ldata["upstream"] = urllib.parse.urljoin(current, line) + ldata["proxy"] = proxy + ldata["proxied"] = isinstance(proxy, str) + links.append(ldata) + presp = requests.post(proxy_server, json=links) + if isinstance(presp.text, str): + print(presp.text) + links = json.loads(presp.text) + for line in resp.text.splitlines(): + if line.startswith("#EXT-X-KEY:METHOD="): + matches = re.findall(r'(?<=URI=").+(?=")', line) + if len(matches) == 1: + new_url = links.pop(0) ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line) elif line.startswith("#"): ndata += line else: - ndata += get_proxy_url(proxy, current, line) + ndata += links.pop(0) ndata += "\n" - return ndata + return ndata class MainHandler(tornado.web.RequestHandler): def handle_any(self, write):