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):