Creating new /api/archive/upload which accepts a file from the SingleFile browser extension and converts it to an Archive.

This commit is contained in:
Roland 2025-01-26 12:44:16 -07:00
parent 01338e4d89
commit 531ec3d65e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
62 changed files with 1000 additions and 562 deletions

View file

@ -1,21 +1,21 @@
# Keep older singlefile version so we can keep screenshots at specific ratio.
FROM capsulecode/singlefile:v0.3.32
FROM browserless/chrome:1.61.1-chrome-stable
USER root
WORKDIR /app
# Install Python for main.py
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 python3-dev curl && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN apt update && apt install -y python3 python3-dev python3-venv curl && ln -sf python3 /usr/bin/python
RUN python3 -m venv /app/venv
RUN . /app/venv/bin/activate
# Install a newer singlefile and Readability.
RUN npm install -g single-file-cli@2.0.73 readability-extractor@0.0.6
# Install the Python wrapper and it's requirements.
COPY docker/archive/requirements.txt /app/requirements.txt
RUN pip3 install -r /app/requirements.txt
RUN /app/venv/bin/pip3 install -r /app/requirements.txt
COPY docker/archive /app
# Install a newer singlefile and Readability.
RUN npm install -g single-file-cli@1.1.54 readability-extractor@0.0.6
ENTRYPOINT [ "python3", "/app/main.py"]
# Launch Archive app in the virtual environment.
ENTRYPOINT [ "/app/venv/bin/python3", "/app/main.py"]

View file

@ -28,6 +28,8 @@ logger.addHandler(ch)
SINGLEFILE_PATH = pathlib.Path('/usr/src/app/node_modules/single-file-cli/single-file')
if not SINGLEFILE_PATH.is_file():
SINGLEFILE_PATH = pathlib.Path('/usr/src/app/node_modules/single-file/cli/single-file')
if not SINGLEFILE_PATH.is_file():
SINGLEFILE_PATH = pathlib.Path('/usr/src/.nvm/versions/node/v18.19.0/bin/single-file')
if not SINGLEFILE_PATH.is_file():
raise FileNotFoundError("Can't find single-file executable!")
@ -77,8 +79,6 @@ async def call_single_file(url) -> bytes:
"""
logger.info(f'archiving {url}')
cmd = f'{SINGLEFILE_PATH}' \
r' --browser-executable-path /usr/bin/chromium-browser' \
r' --browser-args [\"--no-sandbox\"]' \
r' --dump-content ' \
f' "{url}"'
logger.debug(f'archive cmd: {cmd}')
@ -86,7 +86,7 @@ async def call_single_file(url) -> bytes:
if return_code != 0 or not stdout:
if stderr:
for line in stderr.splitlines():
logger.error(line)
logger.error(line.decode())
raise RuntimeError(f'Failed to single-file {url} got the following error: {stderr}')
logger.debug(f'done archiving for {url}')
return stdout
@ -112,7 +112,7 @@ async def extract_readability(path: str, url: str) -> dict:
async def take_screenshot(url: str) -> bytes:
cmd = '/usr/bin/chromium-browser' \
cmd = '/usr/bin/google-chrome' \
' --headless' \
' --disable-gpu' \
' --no-sandbox' \
@ -153,8 +153,15 @@ def prepare_bytes(b: bytes) -> str:
@app.post('/json')
async def post_archive(request: Request):
url = request.json['url']
try:
singlefile = await call_single_file(url)
# May have been passed the singlefile contents, do the extractions and return.
singlefile = request.json.get('singlefile')
singlefile = base64.b64decode(singlefile) if singlefile else None
# Use provided singlefile, or fetch and create from the internet.
singlefile = singlefile or await call_single_file(url)
# Use html suffix so chrome screenshot recognizes it as an HTML file.
with tempfile.NamedTemporaryFile('wb', suffix='.html') as fh:
fh.write(singlefile)

View file

@ -1 +1 @@
sanic==23.3.0
sanic==24.12.0