|
@@ -2,7 +2,8 @@ __package__ = 'archivebox.extractors'
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
-from typing import Optional
|
|
|
|
|
|
|
+from subprocess import CompletedProcess
|
|
|
|
|
+from typing import Optional, Tuple, List
|
|
|
import json
|
|
import json
|
|
|
|
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
|
@@ -20,6 +21,21 @@ from ..config import (
|
|
|
)
|
|
)
|
|
|
from ..logging_util import TimedProgress
|
|
from ..logging_util import TimedProgress
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@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}).',
|
|
|
|
|
+ *(
|
|
|
|
|
+ line.strip()
|
|
|
|
|
+ for line in (result.stdout + result.stderr).decode().rsplit('\n', lines)[-lines:]
|
|
|
|
|
+ if line.strip()
|
|
|
|
|
+ ),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@enforce_types
|
|
@enforce_types
|
|
|
def should_save_mercury(link: Link, out_dir: Optional[str]=None) -> bool:
|
|
def should_save_mercury(link: Link, out_dir: Optional[str]=None) -> bool:
|
|
|
out_dir = out_dir or link.link_dir
|
|
out_dir = out_dir or link.link_dir
|
|
@@ -31,7 +47,7 @@ def should_save_mercury(link: Link, out_dir: Optional[str]=None) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
|
@enforce_types
|
|
|
-def save_mercury(link: Link, out_dir: Optional[str]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
|
|
|
|
|
|
+def save_mercury(link: Link, out_dir: Optional[Path]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
|
|
"""download reader friendly version using @postlight/mercury-parser"""
|
|
"""download reader friendly version using @postlight/mercury-parser"""
|
|
|
|
|
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
|
out_dir = Path(out_dir or link.link_dir)
|
|
@@ -41,41 +57,38 @@ def save_mercury(link: Link, out_dir: Optional[str]=None, timeout: int=TIMEOUT)
|
|
|
status = 'succeeded'
|
|
status = 'succeeded'
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
try:
|
|
try:
|
|
|
|
|
+ # Get plain text version of article
|
|
|
cmd = [
|
|
cmd = [
|
|
|
DEPENDENCIES['MERCURY_BINARY']['path'],
|
|
DEPENDENCIES['MERCURY_BINARY']['path'],
|
|
|
link.url,
|
|
link.url,
|
|
|
"--format=text"
|
|
"--format=text"
|
|
|
]
|
|
]
|
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
|
|
- txtresult_json = json.loads(result.stdout)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ article_text = json.loads(result.stdout)
|
|
|
|
|
+ except json.JSONDecodeError:
|
|
|
|
|
+ raise ShellError(cmd, result)
|
|
|
|
|
+
|
|
|
|
|
+ # Get HTML version of article
|
|
|
cmd = [
|
|
cmd = [
|
|
|
DEPENDENCIES['MERCURY_BINARY']['path'],
|
|
DEPENDENCIES['MERCURY_BINARY']['path'],
|
|
|
link.url
|
|
link.url
|
|
|
]
|
|
]
|
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
|
|
- result_json = json.loads(result.stdout)
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ article_json = json.loads(result.stdout)
|
|
|
|
|
+ except json.JSONDecodeError:
|
|
|
|
|
+ raise ShellError(cmd, result)
|
|
|
|
|
|
|
|
output_folder.mkdir(exist_ok=True)
|
|
output_folder.mkdir(exist_ok=True)
|
|
|
- atomic_write(str(output_folder / "content.html"), result_json.pop("content"))
|
|
|
|
|
- atomic_write(str(output_folder / "content.txt"), txtresult_json["content"])
|
|
|
|
|
- atomic_write(str(output_folder / "article.json"), result_json)
|
|
|
|
|
-
|
|
|
|
|
- # parse out last line of stderr
|
|
|
|
|
- output_tail = [
|
|
|
|
|
- line.strip()
|
|
|
|
|
- for line in (result.stdout + result.stderr).decode().rsplit('\n', 20)[-20:]
|
|
|
|
|
- if line.strip()
|
|
|
|
|
- ]
|
|
|
|
|
- hints = (
|
|
|
|
|
- 'Got mercury response code: {}.'.format(result.returncode),
|
|
|
|
|
- *output_tail,
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ atomic_write(str(output_folder / "content.html"), article_json.pop("content"))
|
|
|
|
|
+ atomic_write(str(output_folder / "content.txt"), article_text["content"])
|
|
|
|
|
+ atomic_write(str(output_folder / "article.json"), article_json)
|
|
|
|
|
|
|
|
# Check for common failure cases
|
|
# Check for common failure cases
|
|
|
if (result.returncode > 0):
|
|
if (result.returncode > 0):
|
|
|
- raise ArchiveError('Mercury parser was not able to archive the page', hints)
|
|
|
|
|
- except (Exception, OSError) as err:
|
|
|
|
|
|
|
+ raise ShellError(cmd, result)
|
|
|
|
|
+ except (ArchiveError, Exception, OSError) as err:
|
|
|
status = 'failed'
|
|
status = 'failed'
|
|
|
output = err
|
|
output = err
|
|
|
finally:
|
|
finally:
|