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
|
|
|
|
2024-10-01 00:13:55 +00:00
|
|
|
from archivebox.misc.system import chmod_file, run
|
2024-10-01 06:21:34 +00:00
|
|
|
from archivebox.misc.util import enforce_types, domain, dedupe
|
|
|
|
from archivebox.plugins_extractor.favicon.apps import FAVICON_CONFIG
|
|
|
|
from archivebox.plugins_extractor.curl.apps import CURL_CONFIG, CURL_BINARY
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveOutput
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import TimedProgress
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2024-08-21 01:31:21 +00:00
|
|
|
def should_save_favicon(link: Link, out_dir: str | Path | None=None, overwrite: bool=False) -> bool:
|
|
|
|
assert link.link_dir
|
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
2021-01-21 21:45:11 +00:00
|
|
|
if not overwrite and (out_dir / 'favicon.ico').exists():
|
2019-04-27 21:26:24 +00:00
|
|
|
return False
|
|
|
|
|
2024-10-01 06:21:34 +00:00
|
|
|
return FAVICON_CONFIG.SAVE_FAVICON
|
2021-01-21 21:45:11 +00:00
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
@enforce_types
|
|
|
|
def get_output_path():
|
|
|
|
return 'favicon.ico'
|
|
|
|
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
@enforce_types
|
2024-10-01 06:21:34 +00:00
|
|
|
def save_favicon(link: Link, out_dir: str | Path | None=None, timeout: int=CURL_CONFIG.CURL_TIMEOUT) -> ArchiveResult:
|
2019-04-27 21:26:24 +00:00
|
|
|
"""download site favicon from google's favicon api"""
|
|
|
|
|
2024-10-01 06:21:34 +00:00
|
|
|
curl_binary = CURL_BINARY.load()
|
|
|
|
assert curl_binary.abspath and curl_binary.version
|
|
|
|
|
2024-08-21 01:31:21 +00:00
|
|
|
out_dir = Path(out_dir or link.link_dir)
|
|
|
|
assert out_dir.exists()
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
output: ArchiveOutput = 'favicon.ico'
|
2024-03-01 20:50:32 +00:00
|
|
|
# later options take precedence
|
2024-02-21 21:13:06 +00:00
|
|
|
options = [
|
2024-10-01 06:21:34 +00:00
|
|
|
*CURL_CONFIG.CURL_ARGS,
|
|
|
|
*CURL_CONFIG.CURL_EXTRA_ARGS,
|
2019-04-27 21:26:24 +00:00
|
|
|
'--max-time', str(timeout),
|
|
|
|
'--output', str(output),
|
2024-10-01 06:21:34 +00:00
|
|
|
*(['--user-agent', '{}'.format(CURL_CONFIG.CURL_USER_AGENT)] if CURL_CONFIG.CURL_USER_AGENT else []),
|
|
|
|
*([] if CURL_CONFIG.CURL_CHECK_SSL_VALIDITY else ['--insecure']),
|
2024-02-21 21:13:06 +00:00
|
|
|
]
|
|
|
|
cmd = [
|
2024-10-01 06:21:34 +00:00
|
|
|
str(curl_binary.abspath),
|
2024-03-06 03:13:45 +00:00
|
|
|
*dedupe(options),
|
2024-10-01 06:21:34 +00:00
|
|
|
FAVICON_CONFIG.FAVICON_PROVIDER.format(domain(link.url)),
|
2019-04-27 21:26:24 +00:00
|
|
|
]
|
2021-01-30 11:07:35 +00:00
|
|
|
status = 'failed'
|
2019-04-27 21:26:24 +00:00
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2020-09-15 19:05:48 +00:00
|
|
|
run(cmd, cwd=str(out_dir), timeout=timeout)
|
|
|
|
chmod_file(output, cwd=str(out_dir))
|
2020-06-26 01:30:29 +00:00
|
|
|
status = 'succeeded'
|
2019-04-27 21:26:24 +00:00
|
|
|
except Exception as err:
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-15 19:05:48 +00:00
|
|
|
pwd=str(out_dir),
|
2024-10-01 06:21:34 +00:00
|
|
|
cmd_version=str(curl_binary.version),
|
2019-04-27 21:26:24 +00:00
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|