diff --git a/backend/stream.py b/backend/stream.py index dd52d77..3dfac48 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -218,7 +218,6 @@ except Exception as e: logger.info(e) async def rewrite(upstream, current, proxy): - rewrite_start = (time.time_ns() // 1_000_000) ndata = None text = None try: @@ -230,9 +229,11 @@ async def rewrite(upstream, current, proxy): if text is not None: lines = text.splitlines() links = [] + pattern_match = re.compile(r'(?<=URI=").+(?=")') + pattern_sub = re.compile(r'URI=".+"') for line in lines: if line.startswith("#EXT-X-KEY:METHOD="): - matches = re.findall(r'(?<=URI=").+(?=")', line) + matches = pattern_match.findall(line) if len(matches) == 1: ldata = {} ldata["upstream"] = urllib.parse.urljoin(current, matches[0]) @@ -260,17 +261,15 @@ async def rewrite(upstream, current, proxy): links = json.loads(link_text) for line in lines: if line.startswith("#EXT-X-KEY:METHOD="): - matches = re.findall(r'(?<=URI=").+(?=")', line) + matches = pattern_match.findall(line) if len(matches) == 1: new_url = links.pop(0) - ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line) + ndata += pattern_sub.sub(f'URI="{new_url}"', line) elif line.startswith("#"): ndata += line else: ndata += links.pop(0) ndata += "\n" - rewrite_stop = (time.time_ns() // 1_000_000) - logger.info(str(rewrite_stop - rewrite_start)) return ndata class MainHandler(tornado.web.RequestHandler):