mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 13:19:58 -05:00
Creating Generate Screenshot button for Archives which are missing a screenshot.
This commit is contained in:
parent
41e1ed6be6
commit
1ed0d63bac
11 changed files with 508 additions and 4 deletions
|
|
@ -10,6 +10,7 @@ import logging
|
|||
import os.path
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import traceback
|
||||
from json import JSONDecodeError
|
||||
|
|
@ -33,6 +34,12 @@ if not SINGLEFILE_PATH.is_file():
|
|||
if not SINGLEFILE_PATH.is_file():
|
||||
raise FileNotFoundError("Can't find single-file executable!")
|
||||
|
||||
BROWSER_EXEC = pathlib.Path('/usr/bin/google-chrome')
|
||||
if not BROWSER_EXEC.is_file():
|
||||
print('Unable to find browser!', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# Increase response timeout, archiving can take several minutes.
|
||||
RESPONSE_TIMEOUT = 10 * 60
|
||||
config = {
|
||||
|
|
@ -117,7 +124,7 @@ async def extract_readability(path: str, url: str) -> dict:
|
|||
|
||||
|
||||
async def take_screenshot(url: str) -> bytes:
|
||||
cmd = '/usr/bin/google-chrome' \
|
||||
cmd = f'{BROWSER_EXEC}' \
|
||||
' --headless' \
|
||||
' --disable-gpu' \
|
||||
' --no-sandbox' \
|
||||
|
|
@ -155,6 +162,61 @@ def prepare_bytes(b: bytes) -> str:
|
|||
return b
|
||||
|
||||
|
||||
@app.post('/screenshot')
|
||||
async def post_screenshot(request: Request):
|
||||
"""Generate a screenshot for the provided singlefile."""
|
||||
url = request.json['url']
|
||||
singlefile = request.json.get('singlefile')
|
||||
|
||||
try:
|
||||
logger.info(f'Generating screenshot for {url}')
|
||||
|
||||
# Decode and decompress the singlefile
|
||||
if singlefile:
|
||||
singlefile = base64.b64decode(singlefile)
|
||||
singlefile = gzip.decompress(singlefile)
|
||||
|
||||
if not singlefile:
|
||||
raise ValueError(f'No singlefile provided for {url}')
|
||||
|
||||
# Write singlefile to temp file and screenshot it
|
||||
# Use html suffix so chrome screenshot recognizes it as an HTML file
|
||||
with tempfile.NamedTemporaryFile('wb', suffix='.html') as fh:
|
||||
fh.write(singlefile)
|
||||
fh.flush()
|
||||
|
||||
screenshot = None
|
||||
try:
|
||||
# Screenshot the local singlefile
|
||||
screenshot = await take_screenshot(f'file://{fh.name}')
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to take screenshot of {fh.name}', exc_info=e)
|
||||
|
||||
# Fall back to URL if local screenshot failed
|
||||
if not screenshot:
|
||||
logger.warning(f'Failed to screenshot local singlefile, attempting to screenshot URL: {url}')
|
||||
try:
|
||||
screenshot = await take_screenshot(url)
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to take screenshot of {url}', exc_info=e)
|
||||
|
||||
if not screenshot:
|
||||
raise ValueError(f'Failed to generate screenshot for {url}')
|
||||
|
||||
# Compress for smaller response
|
||||
screenshot = prepare_bytes(screenshot)
|
||||
|
||||
ret = dict(
|
||||
url=url,
|
||||
screenshot=screenshot,
|
||||
)
|
||||
return response.json(ret)
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to generate screenshot for {url}', exc_info=e)
|
||||
error = str(traceback.format_exc())
|
||||
return response.json({'error': f'Failed to generate screenshot for {url} traceback is below... \n\n {error}'})
|
||||
|
||||
|
||||
@app.post('/json')
|
||||
async def post_archive(request: Request):
|
||||
url = request.json['url']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue