2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
from typing import Callable, Optional, Dict, List, Iterable, Union, Protocol, cast
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
import os
|
2022-09-12 20:34:02 +00:00
|
|
|
import sys
|
2020-09-15 19:05:48 +00:00
|
|
|
from pathlib import Path
|
2024-05-12 05:28:59 +00:00
|
|
|
from importlib import import_module
|
2021-04-10 08:19:30 +00:00
|
|
|
from datetime import datetime, timezone
|
2024-05-12 05:28:59 +00:00
|
|
|
|
2020-09-08 14:36:06 +00:00
|
|
|
from django.db.models import QuerySet
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2023-07-31 15:34:03 +00:00
|
|
|
from ..index.schema import ArchiveResult, Link
|
2020-11-04 20:02:54 +00:00
|
|
|
from ..index.sql import write_link_to_sql_index
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..index import (
|
|
|
|
load_link_details,
|
|
|
|
write_link_details,
|
|
|
|
)
|
2024-10-01 00:25:15 +00:00
|
|
|
from archivebox.misc.util import enforce_types
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import (
|
2020-07-13 15:26:30 +00:00
|
|
|
log_archiving_started,
|
|
|
|
log_archiving_paused,
|
|
|
|
log_archiving_finished,
|
2019-04-27 21:26:24 +00:00
|
|
|
log_link_archiving_started,
|
|
|
|
log_link_archiving_finished,
|
|
|
|
log_archive_method_started,
|
|
|
|
log_archive_method_finished,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .title import should_save_title, save_title
|
|
|
|
from .favicon import should_save_favicon, save_favicon
|
|
|
|
from .wget import should_save_wget, save_wget
|
2020-07-30 18:23:10 +00:00
|
|
|
from .singlefile import should_save_singlefile, save_singlefile
|
2020-08-07 13:05:17 +00:00
|
|
|
from .readability import should_save_readability, save_readability
|
2020-09-22 08:55:14 +00:00
|
|
|
from .mercury import should_save_mercury, save_mercury
|
2023-10-24 01:42:25 +00:00
|
|
|
from .htmltotext import should_save_htmltotext, save_htmltotext
|
2019-04-27 21:26:24 +00:00
|
|
|
from .pdf import should_save_pdf, save_pdf
|
|
|
|
from .screenshot import should_save_screenshot, save_screenshot
|
|
|
|
from .dom import should_save_dom, save_dom
|
|
|
|
from .git import should_save_git, save_git
|
|
|
|
from .media import should_save_media, save_media
|
|
|
|
from .archive_org import should_save_archive_dot_org, save_archive_dot_org
|
2020-09-11 14:06:52 +00:00
|
|
|
from .headers import should_save_headers, save_headers
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2020-11-23 18:04:38 +00:00
|
|
|
|
2023-08-02 14:56:48 +00:00
|
|
|
ShouldSaveFunction = Callable[[Link, Optional[Path], Optional[bool]], bool]
|
|
|
|
SaveFunction = Callable[[Link, Optional[Path], int], ArchiveResult]
|
|
|
|
ArchiveMethodEntry = tuple[str, ShouldSaveFunction, SaveFunction]
|
2023-07-31 15:34:03 +00:00
|
|
|
|
|
|
|
def get_default_archive_methods() -> List[ArchiveMethodEntry]:
|
2020-07-31 15:24:58 +00:00
|
|
|
return [
|
2020-08-18 08:37:54 +00:00
|
|
|
('favicon', should_save_favicon, save_favicon),
|
2021-02-16 20:52:18 +00:00
|
|
|
('headers', should_save_headers, save_headers),
|
2020-08-18 08:37:54 +00:00
|
|
|
('singlefile', should_save_singlefile, save_singlefile),
|
|
|
|
('pdf', should_save_pdf, save_pdf),
|
|
|
|
('screenshot', should_save_screenshot, save_screenshot),
|
|
|
|
('dom', should_save_dom, save_dom),
|
2021-02-16 20:52:18 +00:00
|
|
|
('wget', should_save_wget, save_wget),
|
2023-10-24 01:42:25 +00:00
|
|
|
# keep title, readability, and htmltotext below wget and singlefile, as they depend on them
|
|
|
|
('title', should_save_title, save_title),
|
2022-02-08 15:17:52 +00:00
|
|
|
('readability', should_save_readability, save_readability),
|
2020-09-22 08:55:14 +00:00
|
|
|
('mercury', should_save_mercury, save_mercury),
|
2023-10-24 01:42:25 +00:00
|
|
|
('htmltotext', should_save_htmltotext, save_htmltotext),
|
2020-08-18 08:37:54 +00:00
|
|
|
('git', should_save_git, save_git),
|
|
|
|
('media', should_save_media, save_media),
|
|
|
|
('archive_org', should_save_archive_dot_org, save_archive_dot_org),
|
|
|
|
]
|
2020-07-31 15:24:58 +00:00
|
|
|
|
2023-10-24 01:42:25 +00:00
|
|
|
ARCHIVE_METHODS_INDEXING_PRECEDENCE = [
|
|
|
|
('readability', 1),
|
|
|
|
('mercury', 2),
|
|
|
|
('htmltotext', 3),
|
|
|
|
('singlefile', 4),
|
|
|
|
('dom', 5),
|
|
|
|
('wget', 6)
|
|
|
|
]
|
2020-11-23 18:04:38 +00:00
|
|
|
|
2023-10-28 06:09:28 +00:00
|
|
|
|
2020-07-31 15:24:58 +00:00
|
|
|
@enforce_types
|
2023-07-31 15:34:03 +00:00
|
|
|
def get_archive_methods_for_link(link: Link) -> Iterable[ArchiveMethodEntry]:
|
2024-10-21 09:56:00 +00:00
|
|
|
from archivebox.config.common import ARCHIVING_CONFIG
|
|
|
|
|
2023-07-31 15:34:03 +00:00
|
|
|
DEFAULT_METHODS = get_default_archive_methods()
|
|
|
|
allowed_methods = {
|
2024-10-21 09:56:00 +00:00
|
|
|
method_name
|
|
|
|
for url_pattern, methods in ARCHIVING_CONFIG.SAVE_ALLOWLIST_PTNS.items()
|
|
|
|
for method_name in methods
|
|
|
|
if url_pattern.search(link.url)
|
|
|
|
} or { method[0] for method in DEFAULT_METHODS }
|
|
|
|
|
2023-07-31 15:34:03 +00:00
|
|
|
denied_methods = {
|
2024-10-21 09:56:00 +00:00
|
|
|
method_name
|
|
|
|
for url_pattern, methods in ARCHIVING_CONFIG.SAVE_DENYLIST_PTNS.items()
|
|
|
|
for method_name in methods
|
|
|
|
if url_pattern.search(link.url)
|
2023-07-31 15:34:03 +00:00
|
|
|
}
|
|
|
|
allowed_methods -= denied_methods
|
|
|
|
|
2024-10-21 09:56:00 +00:00
|
|
|
return [method for method in DEFAULT_METHODS if method[0] in allowed_methods]
|
2023-07-31 15:34:03 +00:00
|
|
|
|
2020-07-31 15:24:58 +00:00
|
|
|
@enforce_types
|
2023-07-31 15:34:03 +00:00
|
|
|
def ignore_methods(to_ignore: List[str]) -> Iterable[str]:
|
2020-07-31 15:24:58 +00:00
|
|
|
ARCHIVE_METHODS = get_default_archive_methods()
|
2024-10-21 09:56:00 +00:00
|
|
|
return [method[0] for method in ARCHIVE_METHODS if method[0] not in to_ignore]
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2024-08-21 02:43:07 +00:00
|
|
|
def archive_link(link: Link, overwrite: bool=False, methods: Optional[Iterable[str]]=None, out_dir: Optional[Path]=None, created_by_id: int | None=None) -> Link:
|
2019-04-27 21:26:24 +00:00
|
|
|
"""download the DOM, PDF, and a screenshot into a folder named after the link's timestamp"""
|
|
|
|
|
2024-09-25 12:10:09 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2024-09-24 09:13:01 +00:00
|
|
|
from ..search import write_search_index
|
|
|
|
|
2020-11-04 16:22:55 +00:00
|
|
|
# TODO: Remove when the input is changed to be a snapshot. Suboptimal approach.
|
2020-12-08 23:42:01 +00:00
|
|
|
from core.models import Snapshot, ArchiveResult
|
|
|
|
try:
|
|
|
|
snapshot = Snapshot.objects.get(url=link.url) # TODO: This will be unnecessary once everything is a snapshot
|
|
|
|
except Snapshot.DoesNotExist:
|
2024-08-21 02:43:07 +00:00
|
|
|
snapshot = write_link_to_sql_index(link, created_by_id=created_by_id)
|
2020-11-04 16:22:55 +00:00
|
|
|
|
2023-07-31 15:34:03 +00:00
|
|
|
active_methods = get_archive_methods_for_link(link)
|
2020-07-31 15:24:58 +00:00
|
|
|
|
2020-08-18 12:49:26 +00:00
|
|
|
if methods:
|
2023-07-31 15:34:03 +00:00
|
|
|
active_methods = [
|
|
|
|
method for method in active_methods
|
2020-08-18 12:49:26 +00:00
|
|
|
if method[0] in methods
|
2020-07-28 09:58:38 +00:00
|
|
|
]
|
2020-06-30 06:04:16 +00:00
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2019-04-27 21:26:24 +00:00
|
|
|
try:
|
2020-09-30 19:43:35 +00:00
|
|
|
is_new = not Path(out_dir).exists()
|
2019-04-27 21:26:24 +00:00
|
|
|
if is_new:
|
|
|
|
os.makedirs(out_dir)
|
|
|
|
|
|
|
|
link = load_link_details(link, out_dir=out_dir)
|
2020-12-08 23:42:01 +00:00
|
|
|
write_link_details(link, out_dir=out_dir, skip_sql_index=False)
|
2024-02-22 12:49:50 +00:00
|
|
|
log_link_archiving_started(link, str(out_dir), is_new)
|
2024-09-05 06:42:36 +00:00
|
|
|
link = link.overwrite(downloaded_at=datetime.now(timezone.utc))
|
2019-04-27 21:26:24 +00:00
|
|
|
stats = {'skipped': 0, 'succeeded': 0, 'failed': 0}
|
2021-04-10 11:52:01 +00:00
|
|
|
start_ts = datetime.now(timezone.utc)
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2023-07-31 15:34:03 +00:00
|
|
|
for method_name, should_run, method_function in active_methods:
|
2019-04-27 21:26:24 +00:00
|
|
|
try:
|
|
|
|
if method_name not in link.history:
|
|
|
|
link.history[method_name] = []
|
2020-06-30 06:04:16 +00:00
|
|
|
|
2021-01-21 21:45:11 +00:00
|
|
|
if should_run(link, out_dir, overwrite):
|
2019-04-27 21:26:24 +00:00
|
|
|
log_archive_method_started(method_name)
|
|
|
|
|
|
|
|
result = method_function(link=link, out_dir=out_dir)
|
|
|
|
|
|
|
|
link.history[method_name].append(result)
|
|
|
|
|
|
|
|
stats[result.status] += 1
|
|
|
|
log_archive_method_finished(result)
|
2020-12-08 23:42:01 +00:00
|
|
|
write_search_index(link=link, texts=result.index_texts)
|
|
|
|
ArchiveResult.objects.create(snapshot=snapshot, extractor=method_name, cmd=result.cmd, cmd_version=result.cmd_version,
|
2024-08-21 02:43:07 +00:00
|
|
|
output=result.output, pwd=result.pwd, start_ts=result.start_ts, end_ts=result.end_ts, status=result.status, created_by_id=snapshot.created_by_id)
|
2021-02-17 18:34:46 +00:00
|
|
|
|
|
|
|
|
2024-09-05 06:42:36 +00:00
|
|
|
# bump the downloaded_at time on the main Snapshot here, this is critical
|
2021-02-17 18:34:46 +00:00
|
|
|
# to be able to cache summaries of the ArchiveResults for a given
|
|
|
|
# snapshot without having to load all the results from the DB each time.
|
2024-09-05 06:42:36 +00:00
|
|
|
# (we use {Snapshot.pk}-{Snapshot.downloaded_at} as the cache key and assume
|
|
|
|
# ArchiveResults are unchanged as long as the downloaded_at timestamp is unchanged)
|
2021-02-17 18:34:46 +00:00
|
|
|
snapshot.save()
|
2019-04-27 21:26:24 +00:00
|
|
|
else:
|
2020-08-18 12:13:35 +00:00
|
|
|
# print('{black} X {}{reset}'.format(method_name, **ANSI))
|
2019-04-27 21:26:24 +00:00
|
|
|
stats['skipped'] += 1
|
|
|
|
except Exception as e:
|
2022-09-11 10:19:16 +00:00
|
|
|
# https://github.com/ArchiveBox/ArchiveBox/issues/984#issuecomment-1150541627
|
2024-09-25 12:10:09 +00:00
|
|
|
with open(settings.ERROR_LOG, "a", encoding='utf-8') as f:
|
2022-09-11 10:19:16 +00:00
|
|
|
command = ' '.join(sys.argv)
|
|
|
|
ts = datetime.now(timezone.utc).strftime('%Y-%m-%d__%H:%M:%S')
|
2022-09-12 21:32:47 +00:00
|
|
|
f.write(("\n" + 'Exception in archive_methods.save_{}(Link(url={})) command={}; ts={}'.format(
|
2022-09-11 10:19:16 +00:00
|
|
|
method_name,
|
|
|
|
link.url,
|
2022-09-12 21:32:47 +00:00
|
|
|
command,
|
|
|
|
ts
|
2023-05-04 08:43:34 +00:00
|
|
|
) + "\n" + str(e) + "\n"))
|
2022-09-12 21:31:33 +00:00
|
|
|
#f.write(f"\n> {command}; ts={ts} version={config['VERSION']} docker={config['IN_DOCKER']} is_tty={config['IS_TTY']}\n")
|
2024-09-06 09:54:22 +00:00
|
|
|
|
2024-02-22 12:49:09 +00:00
|
|
|
# print(f' ERROR: {method_name} {e.__class__.__name__}: {e} {getattr(e, "hints", "")}', ts, link.url, command)
|
2024-09-25 07:39:49 +00:00
|
|
|
raise e from Exception('Exception in archive_methods.save_{}(Link(url={}))'.format(
|
2024-02-22 12:49:09 +00:00
|
|
|
method_name,
|
|
|
|
link.url,
|
2024-09-25 07:39:49 +00:00
|
|
|
))
|
2024-02-22 12:49:09 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
# print(' ', stats)
|
|
|
|
|
2020-07-28 09:55:09 +00:00
|
|
|
try:
|
|
|
|
latest_title = link.history['title'][-1].output.strip()
|
|
|
|
if latest_title and len(latest_title) >= len(link.title or ''):
|
|
|
|
link = link.overwrite(title=latest_title)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2020-12-08 23:42:01 +00:00
|
|
|
write_link_details(link, out_dir=out_dir, skip_sql_index=False)
|
2020-06-30 06:04:16 +00:00
|
|
|
|
2023-05-29 08:03:49 +00:00
|
|
|
log_link_archiving_finished(link, out_dir, is_new, stats, start_ts)
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
try:
|
|
|
|
write_link_details(link, out_dir=link.link_dir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
print(' ! Failed to archive link: {}: {}'.format(err.__class__.__name__, err))
|
|
|
|
raise
|
|
|
|
|
|
|
|
return link
|
2020-07-13 15:26:30 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2024-08-21 02:43:07 +00:00
|
|
|
def archive_links(all_links: Union[Iterable[Link], QuerySet], overwrite: bool=False, methods: Optional[Iterable[str]]=None, out_dir: Optional[Path]=None, created_by_id: int | None=None) -> List[Link]:
|
2020-08-21 14:57:29 +00:00
|
|
|
|
2020-09-08 14:36:06 +00:00
|
|
|
if type(all_links) is QuerySet:
|
2020-08-21 14:57:29 +00:00
|
|
|
num_links: int = all_links.count()
|
2024-02-22 12:49:09 +00:00
|
|
|
get_link = lambda x: x.as_link_with_details()
|
2024-08-28 02:28:00 +00:00
|
|
|
all_links = all_links.iterator(chunk_size=500)
|
2020-09-08 14:36:06 +00:00
|
|
|
else:
|
|
|
|
num_links: int = len(all_links)
|
|
|
|
get_link = lambda x: x
|
2020-08-21 14:57:29 +00:00
|
|
|
|
|
|
|
if num_links == 0:
|
2020-07-13 15:26:30 +00:00
|
|
|
return []
|
|
|
|
|
2020-08-21 14:57:29 +00:00
|
|
|
log_archiving_started(num_links)
|
2020-07-13 15:26:30 +00:00
|
|
|
idx: int = 0
|
|
|
|
try:
|
2020-09-08 15:39:01 +00:00
|
|
|
for link in all_links:
|
2020-08-21 14:57:29 +00:00
|
|
|
idx += 1
|
|
|
|
to_archive = get_link(link)
|
2024-08-21 02:43:07 +00:00
|
|
|
archive_link(to_archive, overwrite=overwrite, methods=methods, out_dir=Path(link.link_dir), created_by_id=created_by_id)
|
2020-07-13 15:26:30 +00:00
|
|
|
except KeyboardInterrupt:
|
2020-08-21 14:57:29 +00:00
|
|
|
log_archiving_paused(num_links, idx, link.timestamp)
|
2020-07-13 15:26:30 +00:00
|
|
|
raise SystemExit(0)
|
2022-02-08 15:17:52 +00:00
|
|
|
except BaseException:
|
2020-07-13 15:26:30 +00:00
|
|
|
print()
|
|
|
|
raise
|
|
|
|
|
2020-08-21 14:57:29 +00:00
|
|
|
log_archiving_finished(num_links)
|
|
|
|
return all_links
|
2024-05-12 05:28:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EXTRACTORS_DIR = Path(__file__).parent
|
|
|
|
|
|
|
|
class ExtractorModuleProtocol(Protocol):
|
|
|
|
"""Type interface for an Extractor Module (WIP)"""
|
|
|
|
|
|
|
|
get_output_path: Callable
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# get_embed_path: Callable | None
|
|
|
|
# should_extract(Snapshot)
|
|
|
|
# extract(Snapshot)
|
|
|
|
|
|
|
|
|
|
|
|
def get_extractors(dir: Path=EXTRACTORS_DIR) -> Dict[str, ExtractorModuleProtocol]:
|
|
|
|
"""iterate through archivebox/extractors/*.py and load extractor modules"""
|
|
|
|
EXTRACTORS = {}
|
|
|
|
|
|
|
|
for filename in EXTRACTORS_DIR.glob('*.py'):
|
|
|
|
if filename.name.startswith('__'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
extractor_name = filename.name.replace('.py', '')
|
|
|
|
|
|
|
|
extractor_module = cast(ExtractorModuleProtocol, import_module(f'.{extractor_name}', package=__package__))
|
|
|
|
|
|
|
|
assert getattr(extractor_module, 'get_output_path')
|
|
|
|
EXTRACTORS[extractor_name] = extractor_module
|
|
|
|
|
|
|
|
return EXTRACTORS
|
|
|
|
|
|
|
|
EXTRACTORS = get_extractors(EXTRACTORS_DIR)
|