1
0
Fork 0
mirror of synced 2024-06-09 22:14:30 +12:00
ArchiveBox/archivebox/extractors/media.py

96 lines
3 KiB
Python
Raw Normal View History

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
2019-05-01 15:13:04 +12:00
from ..index.schema import Link, ArchiveResult, ArchiveOutput, ArchiveError
2020-06-26 14:14:40 +12:00
from ..system import run, chmod_file
2019-04-28 09:26:24 +12:00
from ..util import (
enforce_types,
is_static_file,
)
from ..config import (
MEDIA_TIMEOUT,
SAVE_MEDIA,
YOUTUBEDL_ARGS,
2019-04-28 09:26:24 +12:00
YOUTUBEDL_BINARY,
YOUTUBEDL_VERSION,
CHECK_SSL_VALIDITY
)
from ..logging_util import TimedProgress
2019-04-28 09:26:24 +12:00
@enforce_types
def should_save_media(link: Link, out_dir: Optional[Path]=None, overwrite: Optional[bool]=False) -> bool:
2019-04-28 09:26:24 +12:00
if is_static_file(link.url):
return False
out_dir = out_dir or Path(link.link_dir)
if not overwrite and (out_dir / 'media').exists():
2019-04-28 09:26:24 +12:00
return False
return SAVE_MEDIA
@enforce_types
2020-09-16 07:05:48 +12:00
def save_media(link: Link, out_dir: Optional[Path]=None, timeout: int=MEDIA_TIMEOUT) -> ArchiveResult:
2019-04-28 09:26:24 +12:00
"""Download playlists or individual video, audio, and subtitles using youtube-dl"""
2020-09-16 07:05:48 +12:00
out_dir = out_dir or Path(link.link_dir)
2019-04-28 09:26:24 +12:00
output: ArchiveOutput = 'media'
2020-09-16 07:05:48 +12:00
output_path = out_dir / output
output_path.mkdir(exist_ok=True)
2019-04-28 09:26:24 +12:00
cmd = [
YOUTUBEDL_BINARY,
*YOUTUBEDL_ARGS,
2019-04-28 09:26:24 +12:00
*([] if CHECK_SSL_VALIDITY else ['--no-check-certificate']),
# TODO: add --cookies-from-browser={CHROME_USER_DATA_DIR}
2019-04-28 09:26:24 +12:00
link.url,
]
status = 'succeeded'
timer = TimedProgress(timeout, prefix=' ')
try:
2020-09-16 07:05:48 +12:00
result = run(cmd, cwd=str(output_path), timeout=timeout + 1)
chmod_file(output, cwd=str(out_dir))
2019-04-28 09:26:24 +12:00
if result.returncode:
if (b'ERROR: Unsupported URL' in result.stderr
or b'HTTP Error 404' in result.stderr
or b'HTTP Error 403' in result.stderr
or b'URL could be a direct video link' in result.stderr
or b'Unable to extract container ID' in result.stderr):
# These happen too frequently on non-media pages to warrant printing to console
pass
else:
hints = (
'Got youtube-dl response code: {}.'.format(result.returncode),
*result.stderr.decode().split('\n'),
)
raise ArchiveError('Failed to save media', hints)
except Exception as err:
status = 'failed'
output = err
finally:
timer.end()
# add video description and subtitles to full-text index
2022-09-13 08:34:02 +12:00
# Let's try a few different
index_texts = [
text_file.read_text(encoding='utf-8').strip()
for text_file in (
*output_path.glob('*.description'),
*output_path.glob('*.srt'),
*output_path.glob('*.vtt'),
*output_path.glob('*.lrc'),
*output_path.glob('*.lrc'),
)
]
2019-04-28 09:26:24 +12:00
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=YOUTUBEDL_VERSION,
output=output,
status=status,
index_texts=index_texts,
2019-04-28 09:26:24 +12:00
**timer.stats,
)