mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-20 09:55:10 -04:00
feat: Initial version of readability extractor
This commit is contained in:
parent
b52306054d
commit
7e2b249388
3 changed files with 99 additions and 0 deletions
|
@ -76,6 +76,7 @@ CONFIG_DEFAULTS: Dict[str, ConfigDefaultDict] = {
|
||||||
'SAVE_WGET': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET',)},
|
'SAVE_WGET': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET',)},
|
||||||
'SAVE_WGET_REQUISITES': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET_REQUISITES',)},
|
'SAVE_WGET_REQUISITES': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET_REQUISITES',)},
|
||||||
'SAVE_SINGLEFILE': {'type': bool, 'default': True, 'aliases': ('FETCH_SINGLEFILE',)},
|
'SAVE_SINGLEFILE': {'type': bool, 'default': True, 'aliases': ('FETCH_SINGLEFILE',)},
|
||||||
|
'SAVE_READABILITY': {'type': bool, 'default': True, 'aliases': ('FETCH_READABILITY',)},
|
||||||
'SAVE_PDF': {'type': bool, 'default': True, 'aliases': ('FETCH_PDF',)},
|
'SAVE_PDF': {'type': bool, 'default': True, 'aliases': ('FETCH_PDF',)},
|
||||||
'SAVE_SCREENSHOT': {'type': bool, 'default': True, 'aliases': ('FETCH_SCREENSHOT',)},
|
'SAVE_SCREENSHOT': {'type': bool, 'default': True, 'aliases': ('FETCH_SCREENSHOT',)},
|
||||||
'SAVE_DOM': {'type': bool, 'default': True, 'aliases': ('FETCH_DOM',)},
|
'SAVE_DOM': {'type': bool, 'default': True, 'aliases': ('FETCH_DOM',)},
|
||||||
|
@ -107,6 +108,7 @@ CONFIG_DEFAULTS: Dict[str, ConfigDefaultDict] = {
|
||||||
'USE_CURL': {'type': bool, 'default': True},
|
'USE_CURL': {'type': bool, 'default': True},
|
||||||
'USE_WGET': {'type': bool, 'default': True},
|
'USE_WGET': {'type': bool, 'default': True},
|
||||||
'USE_SINGLEFILE': {'type': bool, 'default': True},
|
'USE_SINGLEFILE': {'type': bool, 'default': True},
|
||||||
|
'USE_READABILITY': {'type': bool, 'default': True},
|
||||||
'USE_GIT': {'type': bool, 'default': True},
|
'USE_GIT': {'type': bool, 'default': True},
|
||||||
'USE_CHROME': {'type': bool, 'default': True},
|
'USE_CHROME': {'type': bool, 'default': True},
|
||||||
'USE_YOUTUBEDL': {'type': bool, 'default': True},
|
'USE_YOUTUBEDL': {'type': bool, 'default': True},
|
||||||
|
@ -115,6 +117,7 @@ CONFIG_DEFAULTS: Dict[str, ConfigDefaultDict] = {
|
||||||
'GIT_BINARY': {'type': str, 'default': 'git'},
|
'GIT_BINARY': {'type': str, 'default': 'git'},
|
||||||
'WGET_BINARY': {'type': str, 'default': 'wget'},
|
'WGET_BINARY': {'type': str, 'default': 'wget'},
|
||||||
'SINGLEFILE_BINARY': {'type': str, 'default': 'single-file'},
|
'SINGLEFILE_BINARY': {'type': str, 'default': 'single-file'},
|
||||||
|
'READABILITY_BINARY': {'type': str, 'default': 'readability-extractor'},
|
||||||
'YOUTUBEDL_BINARY': {'type': str, 'default': 'youtube-dl'},
|
'YOUTUBEDL_BINARY': {'type': str, 'default': 'youtube-dl'},
|
||||||
'CHROME_BINARY': {'type': str, 'default': None},
|
'CHROME_BINARY': {'type': str, 'default': None},
|
||||||
},
|
},
|
||||||
|
@ -256,6 +259,9 @@ DERIVED_CONFIG_DEFAULTS: ConfigDefaultDict = {
|
||||||
'USE_SINGLEFILE': {'default': lambda c: c['USE_SINGLEFILE'] and c['SAVE_SINGLEFILE']},
|
'USE_SINGLEFILE': {'default': lambda c: c['USE_SINGLEFILE'] and c['SAVE_SINGLEFILE']},
|
||||||
'SINGLEFILE_VERSION': {'default': lambda c: bin_version(c['SINGLEFILE_BINARY']) if c['USE_SINGLEFILE'] else None},
|
'SINGLEFILE_VERSION': {'default': lambda c: bin_version(c['SINGLEFILE_BINARY']) if c['USE_SINGLEFILE'] else None},
|
||||||
|
|
||||||
|
'USE_READABILITY': {'default': lambda c: c['USE_READABILITY'] and c['SAVE_READABILITY']},
|
||||||
|
'READABILITY_VERSION': {'default': lambda c: bin_version(c['READABILITY_BINARY']) if c['USE_READABILITY'] else None},
|
||||||
|
|
||||||
'USE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
|
'USE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
|
||||||
'GIT_VERSION': {'default': lambda c: bin_version(c['GIT_BINARY']) if c['USE_GIT'] else None},
|
'GIT_VERSION': {'default': lambda c: bin_version(c['GIT_BINARY']) if c['USE_GIT'] else None},
|
||||||
'SAVE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
|
'SAVE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
|
||||||
|
@ -272,6 +278,7 @@ DERIVED_CONFIG_DEFAULTS: ConfigDefaultDict = {
|
||||||
'SAVE_SCREENSHOT': {'default': lambda c: c['USE_CHROME'] and c['SAVE_SCREENSHOT']},
|
'SAVE_SCREENSHOT': {'default': lambda c: c['USE_CHROME'] and c['SAVE_SCREENSHOT']},
|
||||||
'SAVE_DOM': {'default': lambda c: c['USE_CHROME'] and c['SAVE_DOM']},
|
'SAVE_DOM': {'default': lambda c: c['USE_CHROME'] and c['SAVE_DOM']},
|
||||||
'SAVE_SINGLEFILE': {'default': lambda c: c['USE_CHROME'] and c['USE_SINGLEFILE']},
|
'SAVE_SINGLEFILE': {'default': lambda c: c['USE_CHROME'] and c['USE_SINGLEFILE']},
|
||||||
|
'SAVE_READABILITY': {'default': lambda c: c['USE_READABILITY']},
|
||||||
|
|
||||||
'DEPENDENCIES': {'default': lambda c: get_dependency_info(c)},
|
'DEPENDENCIES': {'default': lambda c: get_dependency_info(c)},
|
||||||
'CODE_LOCATIONS': {'default': lambda c: get_code_locations(c)},
|
'CODE_LOCATIONS': {'default': lambda c: get_code_locations(c)},
|
||||||
|
@ -689,6 +696,13 @@ def get_dependency_info(config: ConfigDict) -> ConfigValue:
|
||||||
'enabled': config['USE_SINGLEFILE'],
|
'enabled': config['USE_SINGLEFILE'],
|
||||||
'is_valid': bool(config['SINGLEFILE_VERSION']),
|
'is_valid': bool(config['SINGLEFILE_VERSION']),
|
||||||
},
|
},
|
||||||
|
'READABILITY_BINARY': {
|
||||||
|
'path': bin_path(config['READABILITY_BINARY']),
|
||||||
|
'version': config['READABILITY_VERSION'],
|
||||||
|
'hash': bin_hash(config['READABILITY_BINARY']),
|
||||||
|
'enabled': config['USE_READABILITY'],
|
||||||
|
'is_valid': bool(config['READABILITY_VERSION']),
|
||||||
|
},
|
||||||
'GIT_BINARY': {
|
'GIT_BINARY': {
|
||||||
'path': bin_path(config['GIT_BINARY']),
|
'path': bin_path(config['GIT_BINARY']),
|
||||||
'version': config['GIT_VERSION'],
|
'version': config['GIT_VERSION'],
|
||||||
|
|
|
@ -26,6 +26,7 @@ from .title import should_save_title, save_title
|
||||||
from .favicon import should_save_favicon, save_favicon
|
from .favicon import should_save_favicon, save_favicon
|
||||||
from .wget import should_save_wget, save_wget
|
from .wget import should_save_wget, save_wget
|
||||||
from .singlefile import should_save_singlefile, save_singlefile
|
from .singlefile import should_save_singlefile, save_singlefile
|
||||||
|
from .readability import should_save_readability, save_readability
|
||||||
from .pdf import should_save_pdf, save_pdf
|
from .pdf import should_save_pdf, save_pdf
|
||||||
from .screenshot import should_save_screenshot, save_screenshot
|
from .screenshot import should_save_screenshot, save_screenshot
|
||||||
from .dom import should_save_dom, save_dom
|
from .dom import should_save_dom, save_dom
|
||||||
|
@ -39,6 +40,7 @@ def get_default_archive_methods():
|
||||||
('favicon', should_save_favicon, save_favicon),
|
('favicon', should_save_favicon, save_favicon),
|
||||||
('wget', should_save_wget, save_wget),
|
('wget', should_save_wget, save_wget),
|
||||||
('singlefile', should_save_singlefile, save_singlefile),
|
('singlefile', should_save_singlefile, save_singlefile),
|
||||||
|
('readability', should_save_readability, save_readability),
|
||||||
('pdf', should_save_pdf, save_pdf),
|
('pdf', should_save_pdf, save_pdf),
|
||||||
('screenshot', should_save_screenshot, save_screenshot),
|
('screenshot', should_save_screenshot, save_screenshot),
|
||||||
('dom', should_save_dom, save_dom),
|
('dom', should_save_dom, save_dom),
|
||||||
|
|
83
archivebox/extractors/readability.py
Normal file
83
archivebox/extractors/readability.py
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
__package__ = 'archivebox.extractors'
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ..index.schema import Link, ArchiveResult, ArchiveError
|
||||||
|
from ..system import run, atomic_write
|
||||||
|
from ..util import (
|
||||||
|
enforce_types,
|
||||||
|
download_url,
|
||||||
|
|
||||||
|
)
|
||||||
|
from ..config import (
|
||||||
|
TIMEOUT,
|
||||||
|
SAVE_READABILITY,
|
||||||
|
READABILITY_BINARY,
|
||||||
|
READABILITY_VERSION,
|
||||||
|
CHROME_BINARY,
|
||||||
|
)
|
||||||
|
from ..logging_util import TimedProgress
|
||||||
|
|
||||||
|
|
||||||
|
@enforce_types
|
||||||
|
def should_save_readability(link: Link, out_dir: Optional[str]=None) -> bool:
|
||||||
|
out_dir = out_dir or link.link_dir
|
||||||
|
|
||||||
|
output = Path(out_dir or link.link_dir) / 'readability.json'
|
||||||
|
return SAVE_READABILITY and (not output.exists())
|
||||||
|
|
||||||
|
|
||||||
|
@enforce_types
|
||||||
|
def save_readability(link: Link, out_dir: Optional[str]=None, timeout: int=TIMEOUT) -> ArchiveResult:
|
||||||
|
"""download reader friendly version using @mozilla/readability"""
|
||||||
|
|
||||||
|
out_dir = out_dir or link.link_dir
|
||||||
|
output = str(Path(out_dir).absolute() / "readability.json")
|
||||||
|
|
||||||
|
document = download_url(link.url)
|
||||||
|
|
||||||
|
# SingleFile CLI Docs: https://github.com/gildas-lormeau/SingleFile/tree/master/cli
|
||||||
|
cmd = [
|
||||||
|
READABILITY_BINARY,
|
||||||
|
document
|
||||||
|
]
|
||||||
|
|
||||||
|
status = 'succeeded'
|
||||||
|
timer = TimedProgress(timeout, prefix=' ')
|
||||||
|
try:
|
||||||
|
result = run(cmd, cwd=out_dir, timeout=timeout)
|
||||||
|
result_json = json.loads(result.stdout)
|
||||||
|
atomic_write(output, result_json)
|
||||||
|
|
||||||
|
# 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 readability response code: {}.'.format(result.returncode),
|
||||||
|
*output_tail,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check for common failure cases
|
||||||
|
if (result.returncode > 0):
|
||||||
|
raise ArchiveError('Readability was not able to archive the page', hints)
|
||||||
|
except Exception as err:
|
||||||
|
status = 'failed'
|
||||||
|
output = err
|
||||||
|
finally:
|
||||||
|
timer.end()
|
||||||
|
|
||||||
|
return ArchiveResult(
|
||||||
|
cmd=cmd,
|
||||||
|
pwd=out_dir,
|
||||||
|
cmd_version=READABILITY_VERSION,
|
||||||
|
output=output,
|
||||||
|
status=status,
|
||||||
|
**timer.stats,
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue