mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -05:00
Readability extraction no longer takes minutes on image-heavy pages (#838)
* Readability extraction no longer takes minutes on image-heavy pages SingleFile inlines every image as a base64 data URI, and readability-extractor (jsdom) slows to minutes on a page carrying megabytes of them, so these pages hit the readability timeout and were saved without readability text. Replace each base64 payload with a short placeholder before running readability-extractor, then restore the payloads in its output. Applied in both the archive container and the native (Raspberry Pi/Debian) path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Data URI placeholders cannot be forged by page text The placeholder was a fixed string, so a page containing placeholder-like text could index past the payloads (IndexError, failing the archive on the native path) or repeat a placeholder to copy a large image into the output many times. Each extraction now uses a random token, and text that is not a known placeholder is left as-is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Data URI placeholders survive Readability's lazy-image cleanup Readability's _fixLazyImages removes an <img> src whose base64 payload is shorter than 133 characters when another attribute names an image file (alt="cat.png", title, a leftover data-src), treating it as a lazy-load placeholder. The ~50 character placeholder triggered this, so those images lost their inline data. The token is now 160 hex characters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
06e35e1095
commit
1f07b5f944
3 changed files with 148 additions and 4 deletions
|
|
@ -11,6 +11,8 @@ import logging
|
|||
import os
|
||||
import os.path
|
||||
import pathlib
|
||||
import re
|
||||
import secrets
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
|
|
@ -187,6 +189,37 @@ async def call_single_file(url, compress: bool = False) -> (bytes, bytes):
|
|||
return singlefile, page_html
|
||||
|
||||
|
||||
# The prefix and payload of a base64 data URI. Keep in sync with modules/archive/lib.py.
|
||||
BASE64_DATA_URI = re.compile(rb'(data:[^,;\s"\'()]*(?:;[^,;\s"\'()]*)*;base64,)([A-Za-z0-9+/]+=*)')
|
||||
DATA_URI_PLACEHOLDER = re.compile(r'WROLPIDATAURI[0-9a-f]{160}N\d+X')
|
||||
|
||||
|
||||
def strip_data_uris(html: bytes) -> (bytes, dict):
|
||||
"""Replace each base64 data URI payload with a placeholder; readability-extractor takes minutes on a
|
||||
page of large inline images (SingleFile inlines every image). Returns the HTML and the payloads by
|
||||
placeholder. The placeholder holds a random token, so text in the page cannot be mistaken for one."""
|
||||
# At least 133 characters: Readability drops a shorter base64 `src` as a lazy-load placeholder when another
|
||||
# attribute names an image file (e.g. alt="cat.png").
|
||||
token = secrets.token_hex(80)
|
||||
payloads = dict()
|
||||
|
||||
def replace(match):
|
||||
placeholder = f'WROLPIDATAURI{token}N{len(payloads)}X'
|
||||
payloads[placeholder] = match.group(2)
|
||||
return match.group(1) + placeholder.encode()
|
||||
|
||||
return BASE64_DATA_URI.sub(replace, html), payloads
|
||||
|
||||
|
||||
def restore_data_uris(readability: dict, payloads: dict) -> dict:
|
||||
"""Put the payloads removed by `strip_data_uris` back into the readability output."""
|
||||
for key, value in readability.items():
|
||||
if isinstance(value, str):
|
||||
readability[key] = DATA_URI_PLACEHOLDER.sub(
|
||||
lambda m: payloads[m.group(0)].decode() if m.group(0) in payloads else m.group(0), value)
|
||||
return readability
|
||||
|
||||
|
||||
async def extract_readability(path: str, url: str) -> dict:
|
||||
"""
|
||||
Call the CLI command for readability-extractor.
|
||||
|
|
@ -194,6 +227,9 @@ async def extract_readability(path: str, url: str) -> dict:
|
|||
See https://github.com/ArchiveBox/readability-extractor
|
||||
"""
|
||||
logger.info(f'readability for {url}')
|
||||
html, data_uri_payloads = strip_data_uris(pathlib.Path(path).read_bytes())
|
||||
path = pathlib.Path(path).with_name('readability-input.html')
|
||||
path.write_bytes(html)
|
||||
cmd = f'readability-extractor {path} "{url}"'
|
||||
logger.debug(f'readability cmd: {cmd}')
|
||||
stdout, stderr, return_code = await check_output(cmd, timeout=3 * 60)
|
||||
|
|
@ -203,7 +239,7 @@ async def extract_readability(path: str, url: str) -> dict:
|
|||
stderr = stderr.decode() if stderr else None
|
||||
raise RuntimeError(f'Failed to extract readability. {stderr}') from e
|
||||
logger.debug(f'done readability for {url}')
|
||||
return output
|
||||
return restore_data_uris(output, data_uri_payloads)
|
||||
|
||||
|
||||
async def take_screenshot(url: str) -> bytes:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue