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
|
|
|
from typing import Optional
|
|
|
|
|
2019-05-01 03:13:04 +00:00
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveOutput, ArchiveError
|
2020-06-26 02:14:40 +00:00
|
|
|
from ..system import run, chmod_file
|
2024-09-25 07:42:26 +00:00
|
|
|
from ..util import enforce_types, is_static_file, dedupe
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import TimedProgress
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
def get_output_path():
|
|
|
|
return 'media/'
|
|
|
|
|
|
|
|
def get_embed_path(archiveresult=None):
|
|
|
|
if not archiveresult:
|
|
|
|
return get_output_path()
|
|
|
|
|
|
|
|
out_dir = archiveresult.snapshot_dir / get_output_path()
|
|
|
|
try:
|
|
|
|
return get_output_path() + list(out_dir.glob('*.mp4'))[0].name
|
|
|
|
except IndexError:
|
|
|
|
return get_output_path()
|
|
|
|
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
@enforce_types
|
2021-01-21 21:45:11 +00:00
|
|
|
def should_save_media(link: Link, out_dir: Optional[Path]=None, overwrite: Optional[bool]=False) -> bool:
|
2024-09-25 07:42:26 +00:00
|
|
|
from plugins_extractor.ytdlp.apps import YTDLP_CONFIG
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
|
|
|
|
2021-01-21 21:45:11 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
if not overwrite and (out_dir / get_output_path()).exists():
|
2019-04-27 21:26:24 +00:00
|
|
|
return False
|
|
|
|
|
2024-09-25 07:42:26 +00:00
|
|
|
return YTDLP_CONFIG.USE_YTDLP
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2024-09-25 07:42:26 +00:00
|
|
|
def save_media(link: Link, out_dir: Optional[Path]=None, timeout: int=0) -> ArchiveResult:
|
2022-09-12 20:36:23 +00:00
|
|
|
"""Download playlists or individual video, audio, and subtitles using youtube-dl or yt-dlp"""
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2024-09-25 07:42:26 +00:00
|
|
|
|
|
|
|
# from plugins_extractor.chrome.apps import CHROME_CONFIG
|
|
|
|
from plugins_extractor.ytdlp.apps import YTDLP_BINARY, YTDLP_CONFIG
|
|
|
|
|
|
|
|
YTDLP_BIN = YTDLP_BINARY.load()
|
|
|
|
assert YTDLP_BIN.abspath and YTDLP_BIN.version
|
|
|
|
|
|
|
|
timeout = timeout or YTDLP_CONFIG.YTDLP_TIMEOUT
|
2020-09-15 19:05:48 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
output: ArchiveOutput = get_output_path()
|
2020-09-15 19:05:48 +00:00
|
|
|
output_path = out_dir / output
|
|
|
|
output_path.mkdir(exist_ok=True)
|
2024-03-01 20:50:32 +00:00
|
|
|
# later options take precedence
|
2024-02-23 21:40:31 +00:00
|
|
|
options = [
|
2024-09-25 07:42:26 +00:00
|
|
|
*YTDLP_CONFIG.YTDLP_EXTRA_ARGS,
|
|
|
|
*([] if YTDLP_CONFIG.YTDLP_CHECK_SSL_VALIDITY else ['--no-check-certificate']),
|
2022-04-21 14:09:17 +00:00
|
|
|
# TODO: add --cookies-from-browser={CHROME_USER_DATA_DIR}
|
2024-02-23 21:40:31 +00:00
|
|
|
]
|
|
|
|
cmd = [
|
2024-09-25 07:42:26 +00:00
|
|
|
str(YTDLP_BIN.abspath),
|
2024-03-06 03:13:45 +00:00
|
|
|
*dedupe(options),
|
2019-04-27 21:26:24 +00:00
|
|
|
link.url,
|
|
|
|
]
|
|
|
|
status = 'succeeded'
|
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2024-09-25 07:42:26 +00:00
|
|
|
result = run(cmd, cwd=str(output_path), timeout=timeout + 1, text=True)
|
2020-09-15 19:05:48 +00:00
|
|
|
chmod_file(output, cwd=str(out_dir))
|
2019-04-27 21:26:24 +00:00
|
|
|
if result.returncode:
|
2024-09-25 07:42:26 +00:00
|
|
|
if ('ERROR: Unsupported URL' in result.stderr
|
|
|
|
or 'HTTP Error 404' in result.stderr
|
|
|
|
or 'HTTP Error 403' in result.stderr
|
|
|
|
or 'URL could be a direct video link' in result.stderr
|
|
|
|
or 'Unable to extract container ID' in result.stderr):
|
2019-04-27 21:26:24 +00:00
|
|
|
# These happen too frequently on non-media pages to warrant printing to console
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
hints = (
|
2024-09-25 07:42:26 +00:00
|
|
|
'Got yt-dlp response code: {}.'.format(result.returncode),
|
|
|
|
*result.stderr.split('\n'),
|
2019-04-27 21:26:24 +00:00
|
|
|
)
|
|
|
|
raise ArchiveError('Failed to save media', hints)
|
|
|
|
except Exception as err:
|
|
|
|
status = 'failed'
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
2021-04-10 11:22:20 +00:00
|
|
|
# add video description and subtitles to full-text index
|
2022-09-12 20:34:02 +00:00
|
|
|
# Let's try a few different
|
2021-04-10 11:22:20 +00:00
|
|
|
index_texts = [
|
2022-09-12 20:40:45 +00:00
|
|
|
# errors:
|
|
|
|
# * 'strict' to raise a ValueError exception if there is an
|
|
|
|
# encoding error. The default value of None has the same effect.
|
|
|
|
# * 'ignore' ignores errors. Note that ignoring encoding errors
|
|
|
|
# can lead to data loss.
|
|
|
|
# * 'xmlcharrefreplace' is only supported when writing to a
|
|
|
|
# file. Characters not supported by the encoding are replaced with
|
|
|
|
# the appropriate XML character reference &#nnn;.
|
|
|
|
# There are a few more options described in https://docs.python.org/3/library/functions.html#open
|
|
|
|
text_file.read_text(encoding='utf-8', errors='xmlcharrefreplace').strip()
|
2021-04-10 11:22:20 +00:00
|
|
|
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-27 21:26:24 +00:00
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-15 19:05:48 +00:00
|
|
|
pwd=str(out_dir),
|
2024-09-25 07:42:26 +00:00
|
|
|
cmd_version=str(YTDLP_BIN.version),
|
2019-04-27 21:26:24 +00:00
|
|
|
output=output,
|
|
|
|
status=status,
|
2021-04-10 11:22:20 +00:00
|
|
|
index_texts=index_texts,
|
2019-04-27 21:26:24 +00:00
|
|
|
**timer.stats,
|
|
|
|
)
|