yt-dlp-bot/app_worker/worker/utils.py

31 lines
900 B
Python
Raw Normal View History

2024-04-26 04:09:50 +08:00
from pathlib import Path
2023-07-07 04:27:43 +08:00
import yt_dlp
2024-04-26 04:09:50 +08:00
_COOKIES_FILEPATH = Path('/app/cookies/cookies.txt')
2023-07-07 04:27:43 +08:00
def cli_to_api(opts: list) -> dict:
"""Convert yt-dlp CLI options to internal API ones."""
default = yt_dlp.parse_options([]).ydl_opts
diff = {
k: v for k, v in yt_dlp.parse_options(opts).ydl_opts.items() if default[k] != v
}
if 'postprocessors' in diff:
diff['postprocessors'] = [
pp for pp in diff['postprocessors'] if pp not in default['postprocessors']
]
return diff
2023-07-07 04:27:43 +08:00
2024-04-26 04:09:50 +08:00
def is_file_empty(filepath: Path) -> bool:
2023-07-07 04:27:43 +08:00
"""Check whether the file is empty."""
2024-04-26 04:09:50 +08:00
return filepath.is_file() and filepath.stat().st_size == 0
2023-07-07 04:27:43 +08:00
def get_cookies_opts_if_not_empty() -> list[str]:
"""Return yt-dlp cookies option with cookies filepath."""
2024-04-26 04:09:50 +08:00
if is_file_empty(_COOKIES_FILEPATH):
return []
return ['--cookies', str(_COOKIES_FILEPATH)]