2020-08-07 13:05:17 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
|
|
|
from pathlib import Path
|
2020-08-10 18:15:28 +00:00
|
|
|
from tempfile import NamedTemporaryFile
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
|
|
|
from ..system import run, atomic_write
|
|
|
|
from ..util import (
|
|
|
|
enforce_types,
|
|
|
|
download_url,
|
2020-08-11 12:40:55 +00:00
|
|
|
is_static_file,
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
)
|
|
|
|
from ..config import (
|
|
|
|
TIMEOUT,
|
2020-09-01 15:16:24 +00:00
|
|
|
CURL_BINARY,
|
2020-08-07 13:05:17 +00:00
|
|
|
SAVE_READABILITY,
|
2020-08-18 22:40:19 +00:00
|
|
|
DEPENDENCIES,
|
2020-08-07 13:05:17 +00:00
|
|
|
READABILITY_VERSION,
|
|
|
|
)
|
|
|
|
from ..logging_util import TimedProgress
|
|
|
|
|
2020-08-11 13:36:03 +00:00
|
|
|
@enforce_types
|
|
|
|
def get_html(link: Link, path: Path) -> str:
|
|
|
|
"""
|
|
|
|
Try to find wget, singlefile and then dom files.
|
|
|
|
If none is found, download the url again.
|
|
|
|
"""
|
|
|
|
canonical = link.canonical_outputs()
|
|
|
|
abs_path = path.absolute()
|
2020-08-14 16:55:22 +00:00
|
|
|
sources = [canonical["singlefile_path"], canonical["wget_path"], canonical["dom_path"]]
|
2020-08-11 13:36:03 +00:00
|
|
|
document = None
|
|
|
|
for source in sources:
|
|
|
|
try:
|
|
|
|
with open(abs_path / source, "r") as f:
|
|
|
|
document = f.read()
|
|
|
|
break
|
2020-08-11 16:15:15 +00:00
|
|
|
except (FileNotFoundError, TypeError):
|
2020-08-11 13:36:03 +00:00
|
|
|
continue
|
|
|
|
if document is None:
|
|
|
|
return download_url(link.url)
|
|
|
|
else:
|
|
|
|
return document
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def should_save_readability(link: Link, out_dir: Optional[str]=None) -> bool:
|
|
|
|
out_dir = out_dir or link.link_dir
|
2020-08-11 12:40:55 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
2020-08-07 13:05:17 +00:00
|
|
|
|
2020-08-11 16:52:43 +00:00
|
|
|
output = Path(out_dir or link.link_dir) / 'readability'
|
2020-08-18 08:38:13 +00:00
|
|
|
return SAVE_READABILITY and READABILITY_VERSION and (not output.exists())
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def save_readability(link: Link, out_dir: Optional[str]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
|
|
|
"""download reader friendly version using @mozilla/readability"""
|
|
|
|
|
2020-08-11 13:36:03 +00:00
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
|
|
|
output_folder = out_dir.absolute() / "readability"
|
2020-08-11 13:58:49 +00:00
|
|
|
output = str(output_folder)
|
2020-08-07 13:05:17 +00:00
|
|
|
|
2020-08-10 18:17:55 +00:00
|
|
|
# Readability Docs: https://github.com/mozilla/readability
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
status = 'succeeded'
|
2020-09-01 23:42:22 +00:00
|
|
|
# fake command to show the user so they have something to try debugging if get_html fails
|
2020-09-01 15:16:24 +00:00
|
|
|
cmd = [
|
|
|
|
CURL_BINARY,
|
|
|
|
link.url
|
|
|
|
]
|
2020-08-07 13:05:17 +00:00
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2020-08-17 13:34:40 +00:00
|
|
|
document = get_html(link, out_dir)
|
|
|
|
temp_doc = NamedTemporaryFile(delete=False)
|
|
|
|
temp_doc.write(document.encode("utf-8"))
|
|
|
|
temp_doc.close()
|
|
|
|
|
|
|
|
cmd = [
|
2020-08-18 22:40:19 +00:00
|
|
|
DEPENDENCIES['READABILITY_BINARY']['path'],
|
2020-08-17 13:34:40 +00:00
|
|
|
temp_doc.name
|
|
|
|
]
|
|
|
|
|
2020-08-07 13:05:17 +00:00
|
|
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
|
|
|
result_json = json.loads(result.stdout)
|
2020-08-10 18:15:28 +00:00
|
|
|
output_folder.mkdir(exist_ok=True)
|
|
|
|
atomic_write(str(output_folder / "content.html"), result_json.pop("content"))
|
|
|
|
atomic_write(str(output_folder / "content.txt"), result_json.pop("textContent"))
|
|
|
|
atomic_write(str(output_folder / "article.json"), result_json)
|
2020-08-07 13:05:17 +00:00
|
|
|
|
|
|
|
# parse out number of files downloaded from last line of stderr:
|
|
|
|
# "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)"
|
|
|
|
output_tail = [
|
|
|
|
line.strip()
|
|
|
|
for line in (result.stdout + result.stderr).decode().rsplit('\n', 3)[-3:]
|
|
|
|
if line.strip()
|
|
|
|
]
|
|
|
|
hints = (
|
|
|
|
'Got readability response code: {}.'.format(result.returncode),
|
|
|
|
*output_tail,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check for common failure cases
|
|
|
|
if (result.returncode > 0):
|
|
|
|
raise ArchiveError('Readability was not able to archive the page', hints)
|
2020-08-18 23:09:41 +00:00
|
|
|
except (Exception, OSError) as err:
|
2020-08-07 13:05:17 +00:00
|
|
|
status = 'failed'
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-08-11 13:36:03 +00:00
|
|
|
pwd=str(out_dir),
|
2020-08-07 13:05:17 +00:00
|
|
|
cmd_version=READABILITY_VERSION,
|
2020-08-11 13:58:49 +00:00
|
|
|
output=output,
|
2020-08-07 13:05:17 +00:00
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|