2020-07-30 18:23:10 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from typing import Optional
|
2020-08-03 18:19:47 +00:00
|
|
|
import json
|
2020-07-30 18:23:10 +00:00
|
|
|
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
|
|
|
from ..system import run, chmod_file
|
|
|
|
from ..util import (
|
|
|
|
enforce_types,
|
2020-08-10 17:42:20 +00:00
|
|
|
is_static_file,
|
|
|
|
chrome_args,
|
2020-07-30 18:23:10 +00:00
|
|
|
)
|
|
|
|
from ..config import (
|
|
|
|
TIMEOUT,
|
|
|
|
SAVE_SINGLEFILE,
|
2020-08-18 22:40:19 +00:00
|
|
|
DEPENDENCIES,
|
2020-07-30 18:23:10 +00:00
|
|
|
SINGLEFILE_VERSION,
|
|
|
|
CHROME_BINARY,
|
|
|
|
)
|
|
|
|
from ..logging_util import TimedProgress
|
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2020-09-15 19:05:48 +00:00
|
|
|
def should_save_singlefile(link: Link, out_dir: Optional[Path]=None) -> bool:
|
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2020-08-10 17:42:20 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
2020-07-30 18:23:10 +00:00
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
output = out_dir / 'singlefile.html'
|
2020-08-18 08:38:13 +00:00
|
|
|
return SAVE_SINGLEFILE and SINGLEFILE_VERSION and (not output.exists())
|
2020-07-30 18:23:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2020-09-15 19:05:48 +00:00
|
|
|
def save_singlefile(link: Link, out_dir: Optional[Path]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
2020-07-30 18:23:10 +00:00
|
|
|
"""download full site using single-file"""
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
|
|
|
output = str(out_dir.absolute() / "singlefile.html")
|
2020-07-30 18:23:10 +00:00
|
|
|
|
2020-08-03 18:19:47 +00:00
|
|
|
browser_args = chrome_args(TIMEOUT=0)
|
|
|
|
|
2020-07-31 18:07:22 +00:00
|
|
|
# SingleFile CLI Docs: https://github.com/gildas-lormeau/SingleFile/tree/master/cli
|
2020-09-04 15:23:41 +00:00
|
|
|
browser_args = '--browser-args={}'.format(json.dumps(browser_args[1:]))
|
2020-07-30 18:23:10 +00:00
|
|
|
cmd = [
|
2020-08-18 22:40:19 +00:00
|
|
|
DEPENDENCIES['SINGLEFILE_BINARY']['path'],
|
2020-07-30 18:23:10 +00:00
|
|
|
'--browser-executable-path={}'.format(CHROME_BINARY),
|
2020-09-04 15:23:41 +00:00
|
|
|
browser_args,
|
2020-07-30 18:23:10 +00:00
|
|
|
link.url,
|
|
|
|
output
|
|
|
|
]
|
|
|
|
|
|
|
|
status = 'succeeded'
|
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2020-09-15 19:05:48 +00:00
|
|
|
result = run(cmd, cwd=str(out_dir), timeout=timeout)
|
2020-07-30 18:23:10 +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 single-file response code: {}.'.format(result.returncode),
|
|
|
|
*output_tail,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check for common failure cases
|
|
|
|
if (result.returncode > 0):
|
|
|
|
raise ArchiveError('SingleFile was not able to archive the page', hints)
|
|
|
|
chmod_file(output)
|
2020-08-18 23:09:41 +00:00
|
|
|
except (Exception, OSError) as err:
|
2020-07-30 18:23:10 +00:00
|
|
|
status = 'failed'
|
2020-09-04 15:23:41 +00:00
|
|
|
# TODO: Make this prettier. This is necessary to run the command (escape JSON internal quotes).
|
|
|
|
cmd[2] = browser_args.replace('"', "\\\"")
|
2020-07-30 18:23:10 +00:00
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-15 19:05:48 +00:00
|
|
|
pwd=str(out_dir),
|
2020-07-30 18:23:10 +00:00
|
|
|
cmd_version=SINGLEFILE_VERSION,
|
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|