2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
from pathlib import Path
|
2019-04-27 21:26:24 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-05-01 03:13:04 +00:00
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveOutput, ArchiveError
|
2020-06-30 05:12:06 +00:00
|
|
|
from ..system import run, chmod_file, atomic_write
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..util import (
|
|
|
|
enforce_types,
|
|
|
|
is_static_file,
|
|
|
|
chrome_args,
|
2023-08-28 15:27:03 +00:00
|
|
|
chrome_cleanup,
|
2019-04-27 21:26:24 +00:00
|
|
|
)
|
|
|
|
from ..config import (
|
|
|
|
TIMEOUT,
|
|
|
|
SAVE_DOM,
|
|
|
|
CHROME_VERSION,
|
|
|
|
)
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import TimedProgress
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
def get_output_path():
|
|
|
|
return 'output.html'
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2021-01-21 21:45:11 +00:00
|
|
|
def should_save_dom(link: Link, out_dir: Optional[Path]=None, overwrite: Optional[bool]=False) -> bool:
|
2019-04-27 21:26:24 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
2021-01-21 21:45:11 +00:00
|
|
|
|
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
if not overwrite and (out_dir / get_output_path()).exists():
|
|
|
|
if (out_dir / get_output_path()).stat().st_size > 1:
|
2023-03-13 10:49:26 +00:00
|
|
|
return False
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
return SAVE_DOM
|
2021-01-21 21:45:11 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
@enforce_types
|
2020-09-15 19:05:48 +00:00
|
|
|
def save_dom(link: Link, out_dir: Optional[Path]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
2019-04-27 21:26:24 +00:00
|
|
|
"""print HTML of site to file using chrome --dump-html"""
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
output: ArchiveOutput = get_output_path()
|
2020-09-15 19:05:48 +00:00
|
|
|
output_path = out_dir / output
|
2019-04-27 21:26:24 +00:00
|
|
|
cmd = [
|
2023-03-14 11:29:41 +00:00
|
|
|
*chrome_args(),
|
2019-04-27 21:26:24 +00:00
|
|
|
'--dump-dom',
|
|
|
|
link.url
|
|
|
|
]
|
|
|
|
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-06-30 05:12:06 +00:00
|
|
|
atomic_write(output_path, result.stdout)
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
if result.returncode:
|
|
|
|
hints = result.stderr.decode()
|
|
|
|
raise ArchiveError('Failed to save DOM', hints)
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
chmod_file(output, cwd=str(out_dir))
|
2019-04-27 21:26:24 +00:00
|
|
|
except Exception as err:
|
|
|
|
status = 'failed'
|
|
|
|
output = err
|
2023-08-28 15:27:03 +00:00
|
|
|
chrome_cleanup()
|
2019-04-27 21:26:24 +00:00
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-15 19:05:48 +00:00
|
|
|
pwd=str(out_dir),
|
2019-04-27 21:26:24 +00:00
|
|
|
cmd_version=CHROME_VERSION,
|
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|