Add playwright/Dockerfile

This commit is contained in:
2024-08-24 15:21:31 -04:00
parent 3396beeba8
commit 193e82f748

89
playwright/Dockerfile Normal file
View File

@@ -0,0 +1,89 @@
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
RUN pip install playwright xvfbwrapper
RUN playwright install firefox --with-deps
RUN apt-get update && apt-get install -y \
ffmpeg \
&& rm -rf /var/lib/apt/lists/*
RUN tee main.py <<'EOF'
from playwright.async_api import async_playwright as aP
from urllib.parse import *; from os.path import *
import os, sys, asyncio, subprocess, tempfile, shutil, xvfbwrapper
async def Page(headless=True):
page = None
async def save(response):
if not response.ok: return
url, body = response.url, await response.body()
print(f'\x1b[93m{len(body)/1e6 + .05:.1f} MB\x1b[0m {url}')
if response.headers.get('content-type', '') \
== 'application/vnd.apple.mpegurl':
parse_m3u8(body.decode())
if url in page.chunks:
open(join(page.dir, split(url)[1]),
'wb').write(body)
page.chunks.remove(url)
def parse_m3u8(m3u8):
page.chunks = []
with open(join(page.dir, 'recipe.m3u8'), 'w') as f:
for line in m3u8.splitlines():
if 'https' in line:
page.chunks.append(quote(line, safe=":/"))
line = split(line)[1]
f.write(line + '\n')
if headless:
xvfbwrapper.Xvfb().start()
else:
os.environ['DISPLAY'] = ':0'
playwright = await aP().start()
browser = await playwright.firefox.launch(headless=False)
context = await browser.new_context()
context.on('response', save)
context.set_default_timeout(0)
page = await context.new_page()
page.dir = tempfile.mkdtemp()
page.chunks = [None]
return page
async def main(url):
page = await Page()
await page.goto(url)
iframe = page.frame_locator('iframe')
await iframe.locator('.video-js').evaluate('''
(element) => {
element.player.play();
element.player.playbackRate(64);
}
''')
while len(page.chunks): await asyncio.sleep(1)
print('\x1b[94m')
subprocess.run([
'ffmpeg', '-y',
'-allowed_extensions', 'ALL',
'-i', join(page.dir, 'recipe.m3u8'),
'-c', 'copy',
'baked.mp4'
])
print('\x1b[92;1m')
os.system('ls -alh ./baked.mp4')
print('\x1b[0m')
os._exit(0)
asyncio.run(main(sys.argv[1]))
EOF
ENTRYPOINT ["python", "main.py"]
CMD ["https://yauk.tv/282"]