2020-09-22 08:46:21 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
from subprocess import CompletedProcess
|
2020-11-02 13:51:48 +00:00
|
|
|
from typing import Optional, List
|
2020-09-22 08:46:21 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
2024-10-01 00:13:55 +00:00
|
|
|
from archivebox.misc.system import run, atomic_write
|
2024-10-01 00:25:15 +00:00
|
|
|
from archivebox.misc.util import (
|
2020-09-22 08:46:21 +00:00
|
|
|
enforce_types,
|
|
|
|
is_static_file,
|
|
|
|
)
|
2024-10-01 04:43:45 +00:00
|
|
|
from archivebox.plugins_extractor.mercury.apps import MERCURY_CONFIG, MERCURY_BINARY
|
|
|
|
|
2020-09-22 08:46:21 +00:00
|
|
|
from ..logging_util import TimedProgress
|
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
def get_output_path():
|
|
|
|
return 'mercury/'
|
|
|
|
|
|
|
|
def get_embed_path(archiveresult=None):
|
|
|
|
return get_output_path() + 'content.html'
|
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def ShellError(cmd: List[str], result: CompletedProcess, lines: int=20) -> ArchiveError:
|
|
|
|
# parse out last line of stderr
|
|
|
|
return ArchiveError(
|
|
|
|
f'Got {cmd[0]} response code: {result.returncode}).',
|
2021-01-07 14:22:46 +00:00
|
|
|
" ".join(
|
2020-10-31 11:55:27 +00:00
|
|
|
line.strip()
|
|
|
|
for line in (result.stdout + result.stderr).decode().rsplit('\n', lines)[-lines:]
|
|
|
|
if line.strip()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-22 08:46:21 +00:00
|
|
|
@enforce_types
|
2021-01-21 21:45:11 +00:00
|
|
|
def should_save_mercury(link: Link, out_dir: Optional[str]=None, overwrite: Optional[bool]=False) -> bool:
|
2020-09-22 08:46:21 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
|
|
|
|
2024-10-01 04:43:45 +00:00
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
if not overwrite and (out_dir / get_output_path()).exists():
|
2021-01-21 21:45:11 +00:00
|
|
|
return False
|
|
|
|
|
2024-10-01 04:43:45 +00:00
|
|
|
return MERCURY_CONFIG.SAVE_MERCURY
|
2020-09-22 08:46:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2024-10-01 04:43:45 +00:00
|
|
|
def save_mercury(link: Link, out_dir: Optional[Path]=None, timeout: int=MERCURY_CONFIG.MERCURY_TIMEOUT) -> ArchiveResult:
|
2020-09-22 08:46:21 +00:00
|
|
|
"""download reader friendly version using @postlight/mercury-parser"""
|
|
|
|
|
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
output_folder = out_dir.absolute() / get_output_path()
|
|
|
|
output = get_output_path()
|
2024-10-01 04:43:45 +00:00
|
|
|
|
|
|
|
mercury_binary = MERCURY_BINARY.load()
|
|
|
|
assert mercury_binary.abspath and mercury_binary.version
|
2020-09-22 08:46:21 +00:00
|
|
|
|
|
|
|
status = 'succeeded'
|
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2021-02-16 11:19:16 +00:00
|
|
|
output_folder.mkdir(exist_ok=True)
|
2024-03-06 03:15:38 +00:00
|
|
|
# later options take precedence
|
|
|
|
# By default, get plain text version of article
|
2020-09-22 08:46:21 +00:00
|
|
|
cmd = [
|
2024-10-01 04:43:45 +00:00
|
|
|
str(mercury_binary.abspath),
|
|
|
|
*MERCURY_CONFIG.MERCURY_EXTRA_ARGS,
|
|
|
|
'--format=text',
|
2020-09-22 08:46:21 +00:00
|
|
|
link.url,
|
|
|
|
]
|
|
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
2020-10-31 11:55:27 +00:00
|
|
|
try:
|
|
|
|
article_text = json.loads(result.stdout)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
raise ShellError(cmd, result)
|
|
|
|
|
2021-02-16 20:53:11 +00:00
|
|
|
if article_text.get('failed'):
|
|
|
|
raise ArchiveError('Mercury was not able to get article text from the URL')
|
|
|
|
|
2021-02-16 11:19:16 +00:00
|
|
|
atomic_write(str(output_folder / "content.txt"), article_text["content"])
|
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
# Get HTML version of article
|
2020-09-22 08:46:21 +00:00
|
|
|
cmd = [
|
2024-10-01 04:43:45 +00:00
|
|
|
str(mercury_binary.abspath),
|
|
|
|
*MERCURY_CONFIG.MERCURY_EXTRA_ARGS,
|
2020-09-22 08:46:21 +00:00
|
|
|
link.url
|
|
|
|
]
|
|
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
2020-10-31 11:55:27 +00:00
|
|
|
try:
|
|
|
|
article_json = json.loads(result.stdout)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
raise ShellError(cmd, result)
|
2020-09-22 08:46:21 +00:00
|
|
|
|
2021-02-16 20:53:11 +00:00
|
|
|
if article_text.get('failed'):
|
|
|
|
raise ArchiveError('Mercury was not able to get article HTML from the URL')
|
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
atomic_write(str(output_folder / "content.html"), article_json.pop("content"))
|
|
|
|
atomic_write(str(output_folder / "article.json"), article_json)
|
2020-09-22 08:46:21 +00:00
|
|
|
|
|
|
|
# Check for common failure cases
|
|
|
|
if (result.returncode > 0):
|
2020-10-31 11:55:27 +00:00
|
|
|
raise ShellError(cmd, result)
|
|
|
|
except (ArchiveError, Exception, OSError) as err:
|
2020-09-22 08:46:21 +00:00
|
|
|
status = 'failed'
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
|
|
|
pwd=str(out_dir),
|
2024-10-01 04:43:45 +00:00
|
|
|
cmd_version=str(mercury_binary.version),
|
2020-09-22 08:46:21 +00:00
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|