From c8d51d2e3d7b642007e9fe53471c21da857fa147 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 14 May 2021 16:26:31 +0200 Subject: [PATCH] live stream test --- stream.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/stream.py b/stream.py index 07ce605..6ef818d 100755 --- a/stream.py +++ b/stream.py @@ -220,10 +220,12 @@ try: except Exception as e: logger.info(e) -def rewrite(current, provider, proxy): - resp = requests.get(current, proxies=proxy.req) +async def rewrite(current, provider, proxy): + async with proxy.session() as session: + resp = await session.get(current) + text = await resp.text() ndata = None - if resp.text is not None: + if text is not None: links = [] for line in resp.text.splitlines(): if line.startswith("#EXT-X-KEY:METHOD="): @@ -244,19 +246,21 @@ def rewrite(current, provider, proxy): links.append(ldata) if isinstance(proxy_server, str): ndata = "" - presp = requests.post(proxy_server, json=links) - if isinstance(presp.text, str): - 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 += links.pop(0) + async with proxy.session() as session: + resp = await session.post(proxy_server, json=links) + link_text = await resp.text() + if isinstance(link_text, str): + links = json.loads(link_text) + for line in 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 += links.pop(0) ndata += "\n" return ndata class MainHandler(tornado.web.RequestHandler):