2019-04-28 09:26:24 +12:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
2020-09-16 07:05:48 +12:00
|
|
|
from pathlib import Path
|
2019-04-28 09:26:24 +12:00
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveOutput
|
2020-06-26 14:14:40 +12:00
|
|
|
from ..system import chmod_file, run
|
2019-05-01 15:13:04 +12:00
|
|
|
from ..util import enforce_types, domain
|
2019-04-28 09:26:24 +12:00
|
|
|
from ..config import (
|
|
|
|
TIMEOUT,
|
|
|
|
SAVE_FAVICON,
|
|
|
|
CURL_BINARY,
|
2020-10-16 02:49:54 +13:00
|
|
|
CURL_ARGS,
|
2019-04-28 09:26:24 +12:00
|
|
|
CURL_VERSION,
|
|
|
|
CHECK_SSL_VALIDITY,
|
2020-06-26 13:30:29 +12:00
|
|
|
CURL_USER_AGENT,
|
2019-04-28 09:26:24 +12:00
|
|
|
)
|
2020-07-23 04:02:13 +12:00
|
|
|
from ..logging_util import TimedProgress
|
2019-04-28 09:26:24 +12:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2021-01-22 10:45:11 +13:00
|
|
|
def should_save_favicon(link: Link, out_dir: Optional[str]=None, overwrite: Optional[bool]=False) -> bool:
|
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
|
|
|
if not overwrite and (out_dir / 'favicon.ico').exists():
|
2019-04-28 09:26:24 +12:00
|
|
|
return False
|
|
|
|
|
|
|
|
return SAVE_FAVICON
|
2021-01-22 10:45:11 +13:00
|
|
|
|
2019-04-28 09:26:24 +12:00
|
|
|
@enforce_types
|
2020-09-16 07:05:48 +12:00
|
|
|
def save_favicon(link: Link, out_dir: Optional[Path]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
2019-04-28 09:26:24 +12:00
|
|
|
"""download site favicon from google's favicon api"""
|
|
|
|
|
|
|
|
out_dir = out_dir or link.link_dir
|
|
|
|
output: ArchiveOutput = 'favicon.ico'
|
|
|
|
cmd = [
|
|
|
|
CURL_BINARY,
|
2020-10-16 02:49:54 +13:00
|
|
|
*CURL_ARGS,
|
2019-04-28 09:26:24 +12:00
|
|
|
'--max-time', str(timeout),
|
|
|
|
'--output', str(output),
|
2020-06-26 14:14:40 +12:00
|
|
|
*(['--user-agent', '{}'.format(CURL_USER_AGENT)] if CURL_USER_AGENT else []),
|
2019-04-28 09:26:24 +12:00
|
|
|
*([] if CHECK_SSL_VALIDITY else ['--insecure']),
|
|
|
|
'https://www.google.com/s2/favicons?domain={}'.format(domain(link.url)),
|
|
|
|
]
|
2021-01-31 00:07:35 +13:00
|
|
|
status = 'failed'
|
2019-04-28 09:26:24 +12:00
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2020-09-16 07:05:48 +12:00
|
|
|
run(cmd, cwd=str(out_dir), timeout=timeout)
|
|
|
|
chmod_file(output, cwd=str(out_dir))
|
2020-06-26 13:30:29 +12:00
|
|
|
status = 'succeeded'
|
2019-04-28 09:26:24 +12:00
|
|
|
except Exception as err:
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-16 07:05:48 +12:00
|
|
|
pwd=str(out_dir),
|
2019-04-28 09:26:24 +12:00
|
|
|
cmd_version=CURL_VERSION,
|
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|