precompile regex hls

This commit is contained in:
Roy Olav Purser 2021-05-28 17:33:40 +02:00
parent 6cd4acd9c3
commit 1cff970ecf
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

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