mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2026-01-03 01:15:57 +10:00
This implements the hook concurrency plan from TODO_hook_concurrency.md: ## Schema Changes - Add Snapshot.current_step (IntegerField 0-9, default=0) - Create migration 0034_snapshot_current_step.py - Fix uuid_compat imports in migrations 0032 and 0003 ## Core Logic - Add extract_step(hook_name) utility - extracts step from __XX_ pattern - Add is_background_hook(hook_name) utility - checks for .bg. suffix - Update Snapshot.create_pending_archiveresults() to create one AR per hook - Update ArchiveResult.run() to handle hook_name field - Add Snapshot.advance_step_if_ready() method for step advancement - Integrate with SnapshotMachine.is_finished() to call advance_step_if_ready() ## Worker Coordination - Update ArchiveResultWorker.get_queue() for step-based filtering - ARs are only claimable when their step <= snapshot.current_step ## Hook Renumbering - Step 5 (DOM extraction): singlefile→50, screenshot→51, pdf→52, dom→53, title→54, readability→55, headers→55, mercury→56, htmltotext→57 - Step 6 (post-DOM): wget→61, git→62, media→63.bg, gallerydl→64.bg, forumdl→65.bg, papersdl→66.bg - Step 7 (URL extraction): parse_* hooks moved to 70-75 Background hooks (.bg suffix) don't block step advancement, enabling long-running downloads to continue while other hooks proceed.
229 lines
7.5 KiB
Python
229 lines
7.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Download media from a URL using yt-dlp.
|
|
|
|
Usage: on_Snapshot__media.py --url=<url> --snapshot-id=<uuid>
|
|
Output: Downloads media files to $PWD/media/
|
|
|
|
Environment variables:
|
|
YTDLP_BINARY: Path to yt-dlp binary
|
|
YTDLP_TIMEOUT: Timeout in seconds (default: 3600 for large media)
|
|
YTDLP_CHECK_SSL_VALIDITY: Whether to check SSL certificates (default: True)
|
|
YTDLP_EXTRA_ARGS: Extra arguments for yt-dlp (space-separated)
|
|
|
|
# Media feature toggles
|
|
USE_YTDLP: Enable yt-dlp media extraction (default: True)
|
|
SAVE_MEDIA: Alias for USE_YTDLP
|
|
|
|
# Media size limits
|
|
MEDIA_MAX_SIZE: Maximum media file size (default: 750m)
|
|
|
|
# Fallback to ARCHIVING_CONFIG values if YTDLP_* not set:
|
|
MEDIA_TIMEOUT: Fallback timeout for media
|
|
TIMEOUT: Fallback timeout
|
|
CHECK_SSL_VALIDITY: Fallback SSL check
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import rich_click as click
|
|
|
|
|
|
# Extractor metadata
|
|
PLUGIN_NAME = 'media'
|
|
BIN_NAME = 'yt-dlp'
|
|
BIN_PROVIDERS = 'pip,apt,brew,env'
|
|
OUTPUT_DIR = '.'
|
|
|
|
|
|
def get_env(name: str, default: str = '') -> str:
|
|
return os.environ.get(name, default).strip()
|
|
|
|
|
|
def get_env_bool(name: str, default: bool = False) -> bool:
|
|
val = get_env(name, '').lower()
|
|
if val in ('true', '1', 'yes', 'on'):
|
|
return True
|
|
if val in ('false', '0', 'no', 'off'):
|
|
return False
|
|
return default
|
|
|
|
|
|
def get_env_int(name: str, default: int = 0) -> int:
|
|
try:
|
|
return int(get_env(name, str(default)))
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
STATICFILE_DIR = '../staticfile'
|
|
|
|
def has_staticfile_output() -> bool:
|
|
"""Check if staticfile extractor already downloaded this URL."""
|
|
staticfile_dir = Path(STATICFILE_DIR)
|
|
return staticfile_dir.exists() and any(staticfile_dir.iterdir())
|
|
|
|
|
|
# Default yt-dlp args (from old YTDLP_CONFIG)
|
|
def get_ytdlp_default_args(media_max_size: str = '750m') -> list[str]:
|
|
"""Build default yt-dlp arguments."""
|
|
return [
|
|
'--restrict-filenames',
|
|
'--trim-filenames', '128',
|
|
'--write-description',
|
|
'--write-info-json',
|
|
'--write-annotations',
|
|
'--write-thumbnail',
|
|
'--no-call-home',
|
|
'--write-sub',
|
|
'--write-auto-subs',
|
|
'--convert-subs=srt',
|
|
'--yes-playlist',
|
|
'--continue',
|
|
'--no-abort-on-error',
|
|
'--ignore-errors',
|
|
'--geo-bypass',
|
|
'--add-metadata',
|
|
f'--format=(bv*+ba/b)[filesize<={media_max_size}][filesize_approx<=?{media_max_size}]/(bv*+ba/b)',
|
|
]
|
|
|
|
|
|
def save_media(url: str, binary: str) -> tuple[bool, str | None, str]:
|
|
"""
|
|
Download media using yt-dlp.
|
|
|
|
Returns: (success, output_path, error_message)
|
|
"""
|
|
# Get config from env (with YTDLP_ prefix or fallback to ARCHIVING_CONFIG style)
|
|
timeout = get_env_int('YTDLP_TIMEOUT') or get_env_int('MEDIA_TIMEOUT') or get_env_int('TIMEOUT', 3600)
|
|
check_ssl = get_env_bool('YTDLP_CHECK_SSL_VALIDITY', get_env_bool('CHECK_SSL_VALIDITY', True))
|
|
extra_args = get_env('YTDLP_EXTRA_ARGS') or get_env('YOUTUBEDL_EXTRA_ARGS', '')
|
|
media_max_size = get_env('MEDIA_MAX_SIZE', '750m')
|
|
|
|
# Output directory is current directory (hook already runs in output dir)
|
|
output_dir = Path(OUTPUT_DIR)
|
|
|
|
# Build command (later options take precedence)
|
|
cmd = [
|
|
binary,
|
|
*get_ytdlp_default_args(media_max_size),
|
|
'--no-progress',
|
|
'-o', f'{OUTPUT_DIR}/%(title)s.%(ext)s',
|
|
]
|
|
|
|
if not check_ssl:
|
|
cmd.append('--no-check-certificate')
|
|
|
|
if extra_args:
|
|
cmd.extend(extra_args.split())
|
|
|
|
cmd.append(url)
|
|
|
|
try:
|
|
result = subprocess.run(cmd, capture_output=True, timeout=timeout, text=True)
|
|
|
|
# Check if any media files were downloaded
|
|
media_extensions = (
|
|
'.mp4', '.webm', '.mkv', '.avi', '.mov', '.flv', '.wmv', '.m4v',
|
|
'.mp3', '.m4a', '.ogg', '.wav', '.flac', '.aac', '.opus',
|
|
'.json', '.jpg', '.png', '.webp', '.jpeg',
|
|
'.vtt', '.srt', '.ass', '.lrc',
|
|
'.description',
|
|
)
|
|
|
|
downloaded_files = [
|
|
f for f in output_dir.glob('*')
|
|
if f.is_file() and f.suffix.lower() in media_extensions
|
|
]
|
|
|
|
if downloaded_files:
|
|
# Return first video/audio file, or first file if no media
|
|
video_audio = [
|
|
f for f in downloaded_files
|
|
if f.suffix.lower() in ('.mp4', '.webm', '.mkv', '.avi', '.mov', '.mp3', '.m4a', '.ogg', '.wav', '.flac')
|
|
]
|
|
output = str(video_audio[0]) if video_audio else str(downloaded_files[0])
|
|
return True, output, ''
|
|
else:
|
|
stderr = result.stderr
|
|
|
|
# These are NOT errors - page simply has no downloadable media
|
|
# Return success with no output (legitimate "nothing to download")
|
|
if 'ERROR: Unsupported URL' in stderr:
|
|
return True, None, '' # Not a media site - success, no output
|
|
if 'URL could be a direct video link' in stderr:
|
|
return True, None, '' # Not a supported media URL - success, no output
|
|
if result.returncode == 0:
|
|
return True, None, '' # yt-dlp exited cleanly, just no media - success
|
|
|
|
# These ARE errors - something went wrong
|
|
if 'HTTP Error 404' in stderr:
|
|
return False, None, '404 Not Found'
|
|
if 'HTTP Error 403' in stderr:
|
|
return False, None, '403 Forbidden'
|
|
if 'Unable to extract' in stderr:
|
|
return False, None, 'Unable to extract media info'
|
|
|
|
return False, None, f'yt-dlp error: {stderr[:200]}'
|
|
|
|
except subprocess.TimeoutExpired:
|
|
return False, None, f'Timed out after {timeout} seconds'
|
|
except Exception as e:
|
|
return False, None, f'{type(e).__name__}: {e}'
|
|
|
|
|
|
@click.command()
|
|
@click.option('--url', required=True, help='URL to download media from')
|
|
@click.option('--snapshot-id', required=True, help='Snapshot UUID')
|
|
def main(url: str, snapshot_id: str):
|
|
"""Download media from a URL using yt-dlp."""
|
|
|
|
output = None
|
|
status = 'failed'
|
|
error = ''
|
|
|
|
try:
|
|
# Check if yt-dlp is enabled
|
|
if not (get_env_bool('USE_YTDLP', True) and get_env_bool('SAVE_MEDIA', True)):
|
|
print('Skipping media (USE_YTDLP=False or SAVE_MEDIA=False)', file=sys.stderr)
|
|
print(json.dumps({'type': 'ArchiveResult', 'status': 'skipped', 'output_str': 'USE_YTDLP=False'}))
|
|
sys.exit(0)
|
|
|
|
# Check if staticfile extractor already handled this (permanent skip)
|
|
if has_staticfile_output():
|
|
print('Skipping media - staticfile extractor already downloaded this', file=sys.stderr)
|
|
print(json.dumps({'type': 'ArchiveResult', 'status': 'skipped', 'output_str': 'staticfile already exists'}))
|
|
sys.exit(0)
|
|
|
|
# Get binary from environment
|
|
binary = get_env('YTDLP_BINARY') or get_env('YOUTUBEDL_BINARY', 'yt-dlp')
|
|
|
|
# Run extraction
|
|
success, output, error = save_media(url, binary)
|
|
status = 'succeeded' if success else 'failed'
|
|
|
|
except Exception as e:
|
|
error = f'{type(e).__name__}: {e}'
|
|
status = 'failed'
|
|
|
|
if error:
|
|
print(f'ERROR: {error}', file=sys.stderr)
|
|
|
|
# Output clean JSONL (no RESULT_JSON= prefix)
|
|
result = {
|
|
'type': 'ArchiveResult',
|
|
'status': status,
|
|
'output_str': output or error or '',
|
|
}
|
|
print(json.dumps(result))
|
|
|
|
sys.exit(0 if status == 'succeeded' else 1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|