rayohx-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rayohx_cli/__init__.py +3 -0
- rayohx_cli/__main__.py +6 -0
- rayohx_cli/cli/__init__.py +26 -0
- rayohx_cli/cli/clean.py +59 -0
- rayohx_cli/cli/common.py +108 -0
- rayohx_cli/cli/config.py +82 -0
- rayohx_cli/cli/download.py +262 -0
- rayohx_cli/cli/info.py +121 -0
- rayohx_cli/cli/lifecycle.py +190 -0
- rayohx_cli/cli/root.py +105 -0
- rayohx_cli/events.py +40 -0
- rayohx_cli/format.py +34 -0
- rayohx_cli/output.py +119 -0
- rayohx_cli/params/__init__.py +147 -0
- rayohx_cli/params/display.py +35 -0
- rayohx_cli/params/engine.py +106 -0
- rayohx_cli/params/network.py +227 -0
- rayohx_cli/params/output.py +77 -0
- rayohx_cli/params/panels.py +7 -0
- rayohx_cli/params/selection.py +187 -0
- rayohx_cli/progress/__init__.py +24 -0
- rayohx_cli/progress/_factory.py +20 -0
- rayohx_cli/progress/_text.py +47 -0
- rayohx_cli/progress/reporters/__init__.py +8 -0
- rayohx_cli/progress/reporters/bar.py +60 -0
- rayohx_cli/progress/reporters/base.py +19 -0
- rayohx_cli/progress/reporters/null.py +16 -0
- rayohx_cli/progress/reporters/plain.py +38 -0
- rayohx_cli/py.typed +0 -0
- rayohx_cli/render.py +122 -0
- rayohx_cli/settings.py +78 -0
- rayohx_cli/values.py +284 -0
- rayohx_cli-0.1.0.dist-info/METADATA +92 -0
- rayohx_cli-0.1.0.dist-info/RECORD +37 -0
- rayohx_cli-0.1.0.dist-info/WHEEL +4 -0
- rayohx_cli-0.1.0.dist-info/entry_points.txt +2 -0
- rayohx_cli-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""The ways to show a download's events."""
|
|
2
|
+
|
|
3
|
+
from rayohx_cli.progress.reporters.bar import BarReporter
|
|
4
|
+
from rayohx_cli.progress.reporters.base import Reporter
|
|
5
|
+
from rayohx_cli.progress.reporters.null import NullReporter
|
|
6
|
+
from rayohx_cli.progress.reporters.plain import PlainReporter
|
|
7
|
+
|
|
8
|
+
__all__ = ["BarReporter", "NullReporter", "PlainReporter", "Reporter"]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""The reporter that draws a live progress bar."""
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
from rayohx import EventType
|
|
6
|
+
from rayohx.events import DownloadEvent
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.text import Text
|
|
9
|
+
|
|
10
|
+
from rayohx_cli.progress._text import detail, narrate, percent
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BarReporter:
|
|
14
|
+
"""A live progress bar with size, speed and time left."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, console: Console, *, verbose: bool = False) -> None:
|
|
17
|
+
from rich.progress import (
|
|
18
|
+
BarColumn,
|
|
19
|
+
Progress,
|
|
20
|
+
SpinnerColumn,
|
|
21
|
+
TaskProgressColumn,
|
|
22
|
+
TextColumn,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
self._verbose = verbose
|
|
26
|
+
self._console = console
|
|
27
|
+
self._progress = Progress(
|
|
28
|
+
SpinnerColumn(),
|
|
29
|
+
TextColumn("{task.description}"),
|
|
30
|
+
BarColumn(),
|
|
31
|
+
TaskProgressColumn(),
|
|
32
|
+
TextColumn("{task.fields[detail]}"),
|
|
33
|
+
console=console,
|
|
34
|
+
transient=True,
|
|
35
|
+
)
|
|
36
|
+
self._task = self._progress.add_task("Downloading", total=1.0, detail="")
|
|
37
|
+
self._lock = threading.Lock()
|
|
38
|
+
self._running = False
|
|
39
|
+
|
|
40
|
+
def start(self) -> None:
|
|
41
|
+
with self._lock:
|
|
42
|
+
self._progress.start()
|
|
43
|
+
self._running = True
|
|
44
|
+
|
|
45
|
+
def __call__(self, event: DownloadEvent) -> None:
|
|
46
|
+
with self._lock:
|
|
47
|
+
for line in narrate(event, verbose=self._verbose):
|
|
48
|
+
self._progress.console.print(Text(line), soft_wrap=True)
|
|
49
|
+
if event.type is EventType.DOWNLOAD_PROGRESS and event.progress is not None:
|
|
50
|
+
self._progress.update(
|
|
51
|
+
self._task, completed=percent(event.progress), detail=detail(event.progress)
|
|
52
|
+
)
|
|
53
|
+
elif event.type is EventType.FINALIZATION_STARTED:
|
|
54
|
+
self._progress.update(self._task, description="Finalizing", detail="")
|
|
55
|
+
|
|
56
|
+
def close(self) -> None:
|
|
57
|
+
with self._lock:
|
|
58
|
+
if self._running:
|
|
59
|
+
self._progress.stop()
|
|
60
|
+
self._running = False
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""The shape every reporter has."""
|
|
2
|
+
|
|
3
|
+
from typing import Protocol
|
|
4
|
+
|
|
5
|
+
from rayohx.events import DownloadEvent
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Reporter(Protocol):
|
|
9
|
+
"""Receives events from a running download."""
|
|
10
|
+
|
|
11
|
+
def __call__(self, event: DownloadEvent) -> None: ...
|
|
12
|
+
|
|
13
|
+
def start(self) -> None:
|
|
14
|
+
"""Called before the download starts."""
|
|
15
|
+
...
|
|
16
|
+
|
|
17
|
+
def close(self) -> None:
|
|
18
|
+
"""Called after it ends, however it ended."""
|
|
19
|
+
...
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""The reporter that shows nothing."""
|
|
2
|
+
|
|
3
|
+
from rayohx.events import DownloadEvent
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NullReporter:
|
|
7
|
+
"""Shows nothing."""
|
|
8
|
+
|
|
9
|
+
def __call__(self, event: DownloadEvent) -> None:
|
|
10
|
+
return None
|
|
11
|
+
|
|
12
|
+
def start(self) -> None:
|
|
13
|
+
return None
|
|
14
|
+
|
|
15
|
+
def close(self) -> None:
|
|
16
|
+
return None
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""The reporter that prints plain lines."""
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
from rayohx import EventType
|
|
6
|
+
from rayohx.events import DownloadEvent
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.text import Text
|
|
9
|
+
|
|
10
|
+
from rayohx_cli.progress._text import detail, narrate, percent
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PlainReporter:
|
|
14
|
+
"""One line per milestone, for logs and pipes: every 10 percent, and each step."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, console: Console, *, verbose: bool = False) -> None:
|
|
17
|
+
self._console = console
|
|
18
|
+
self._verbose = verbose
|
|
19
|
+
self._last = -1
|
|
20
|
+
self._lock = threading.Lock()
|
|
21
|
+
|
|
22
|
+
def __call__(self, event: DownloadEvent) -> None:
|
|
23
|
+
with self._lock:
|
|
24
|
+
for line in narrate(event, verbose=self._verbose):
|
|
25
|
+
self._console.print(Text(line), soft_wrap=True)
|
|
26
|
+
if event.type is EventType.DOWNLOAD_PROGRESS and event.progress is not None:
|
|
27
|
+
step = int(percent(event.progress) * 10)
|
|
28
|
+
if step > self._last:
|
|
29
|
+
self._last = step
|
|
30
|
+
self._console.print(
|
|
31
|
+
Text(f"{step * 10:3d}% {detail(event.progress)}"), soft_wrap=True
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def start(self) -> None:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
def close(self) -> None:
|
|
38
|
+
return None
|
rayohx_cli/py.typed
ADDED
|
File without changes
|
rayohx_cli/render.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Showing what ``info`` found, as tables."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from rich.table import Table
|
|
6
|
+
from rich.text import Text
|
|
7
|
+
|
|
8
|
+
from rayohx_cli import format as fmt
|
|
9
|
+
from rayohx_cli import output
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def show(data: dict[str, Any]) -> None:
|
|
13
|
+
"""Print a described playlist (see ``rayohx.playlist_to_dict``) to stdout."""
|
|
14
|
+
if data["type"] == "master":
|
|
15
|
+
_master(data)
|
|
16
|
+
else:
|
|
17
|
+
_media(data)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _yes(value: object) -> str:
|
|
21
|
+
return "yes" if value else ""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _master(data: dict[str, Any]) -> None:
|
|
25
|
+
output.data(f"Master playlist: {data['url']}")
|
|
26
|
+
selection = data.get("selection") or {}
|
|
27
|
+
variants = data["variants"]
|
|
28
|
+
# Columns nobody fills in only take up room, so they are left out.
|
|
29
|
+
optional = {
|
|
30
|
+
"Average": any(v["average_bandwidth"] for v in variants),
|
|
31
|
+
"FPS": any(v["frame_rate"] for v in variants),
|
|
32
|
+
"Audio": any(v["audio_group"] for v in variants),
|
|
33
|
+
"Subtitles": any(v["subtitle_group"] for v in variants),
|
|
34
|
+
}
|
|
35
|
+
table = Table(title="Streams", title_justify="left")
|
|
36
|
+
table.add_column("", no_wrap=True)
|
|
37
|
+
table.add_column("#", no_wrap=True)
|
|
38
|
+
table.add_column("Size", no_wrap=True)
|
|
39
|
+
table.add_column("Bit rate", no_wrap=True)
|
|
40
|
+
for name, used in optional.items():
|
|
41
|
+
if used:
|
|
42
|
+
table.add_column(name, no_wrap=True)
|
|
43
|
+
table.add_column("Codecs", overflow="fold")
|
|
44
|
+
for variant in variants:
|
|
45
|
+
row = [
|
|
46
|
+
"*" if selection.get("variant") == variant["index"] else "",
|
|
47
|
+
str(variant["index"]),
|
|
48
|
+
variant["resolution"] or "audio only",
|
|
49
|
+
fmt.bitrate(variant["bandwidth"]),
|
|
50
|
+
]
|
|
51
|
+
values = {
|
|
52
|
+
"Average": fmt.bitrate(variant["average_bandwidth"])
|
|
53
|
+
if variant["average_bandwidth"]
|
|
54
|
+
else "",
|
|
55
|
+
"FPS": f"{variant['frame_rate']:g}" if variant["frame_rate"] else "",
|
|
56
|
+
"Audio": variant["audio_group"] or "",
|
|
57
|
+
"Subtitles": variant["subtitle_group"] or "",
|
|
58
|
+
}
|
|
59
|
+
row += [values[name] for name, used in optional.items() if used]
|
|
60
|
+
row.append(", ".join(variant["codecs"]))
|
|
61
|
+
table.add_row(*row)
|
|
62
|
+
output.console.print(table)
|
|
63
|
+
_tracks(data["audio_tracks"], "Audio tracks", with_channels=True)
|
|
64
|
+
_tracks(data["subtitle_tracks"], "Subtitle tracks", with_channels=False)
|
|
65
|
+
if selection:
|
|
66
|
+
_selection(data, selection)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _tracks(tracks: list[dict[str, Any]], title: str, *, with_channels: bool) -> None:
|
|
70
|
+
if not tracks:
|
|
71
|
+
return
|
|
72
|
+
show_channels = with_channels and any(track.get("channels") for track in tracks)
|
|
73
|
+
show_forced = any(track.get("forced") for track in tracks)
|
|
74
|
+
table = Table(title=title, title_justify="left")
|
|
75
|
+
columns = ["Group", "Name", "Language", "Default"]
|
|
76
|
+
if show_forced:
|
|
77
|
+
columns.append("Forced")
|
|
78
|
+
if show_channels:
|
|
79
|
+
columns.append("Channels")
|
|
80
|
+
columns.append("Separate")
|
|
81
|
+
for column in columns:
|
|
82
|
+
table.add_column(column, no_wrap=column != "Name")
|
|
83
|
+
for track in tracks:
|
|
84
|
+
row = [track["group"], track["name"], track.get("language") or "", _yes(track["default"])]
|
|
85
|
+
if show_forced:
|
|
86
|
+
row.append(_yes(track.get("forced")))
|
|
87
|
+
if show_channels:
|
|
88
|
+
row.append(track.get("channels") or "")
|
|
89
|
+
row.append(_yes(track["separate"]))
|
|
90
|
+
table.add_row(*row)
|
|
91
|
+
output.console.print(table)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _selection(data: dict[str, Any], selection: dict[str, Any]) -> None:
|
|
95
|
+
variant = data["variants"][selection["variant"] - 1]
|
|
96
|
+
size = variant["resolution"] or "audio only"
|
|
97
|
+
lines = [
|
|
98
|
+
"With these settings a download would take:",
|
|
99
|
+
f" stream {selection['variant']}: {size}, {fmt.bitrate(variant['bandwidth'])}",
|
|
100
|
+
" audio: " + (", ".join(selection["audio"]) or "the audio in the stream"),
|
|
101
|
+
" subtitles: " + (", ".join(selection["subtitles"]) or "none"),
|
|
102
|
+
]
|
|
103
|
+
lines += [f" note: {note}" for note in selection["notes"]]
|
|
104
|
+
for line in lines:
|
|
105
|
+
output.console.print(Text(line), soft_wrap=True)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _media(data: dict[str, Any]) -> None:
|
|
109
|
+
output.data(f"Media playlist: {data['url']}")
|
|
110
|
+
table = Table(show_header=False)
|
|
111
|
+
rows = [
|
|
112
|
+
("Segments", str(data["segments"])),
|
|
113
|
+
("Duration", fmt.duration(data["duration"])),
|
|
114
|
+
("Format", data["container"]),
|
|
115
|
+
("Complete", "yes" if data["complete"] else "no (a live stream)"),
|
|
116
|
+
("Encryption", ", ".join(data["encryption"]) or "none"),
|
|
117
|
+
("Byte ranges", _yes(data["byte_ranges"]) or "no"),
|
|
118
|
+
("Discontinuities", str(data["discontinuities"])),
|
|
119
|
+
]
|
|
120
|
+
for key, value in rows:
|
|
121
|
+
table.add_row(key, value)
|
|
122
|
+
output.console.print(table)
|
rayohx_cli/settings.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""The settings file: defaults for the options, so common ones need not be typed every time.
|
|
2
|
+
|
|
3
|
+
Precedence, highest first: the command line, then ``RAYOHX_*`` environment variables, then the
|
|
4
|
+
settings file, then the built-in defaults. The file is TOML whose keys are option names
|
|
5
|
+
(``quality = "1080p"``, ``header = ["Referer: https://example.com/"]``). A key that is not an
|
|
6
|
+
option is an error, so a typo never silently does nothing.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import difflib
|
|
10
|
+
import os
|
|
11
|
+
import tomllib
|
|
12
|
+
from collections.abc import Collection, Mapping
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from rayohx.errors import ConfigurationError
|
|
17
|
+
|
|
18
|
+
FILE_NAME = "rayohx.toml"
|
|
19
|
+
#: Options that make no sense as a default.
|
|
20
|
+
EXCLUDED = frozenset({"url", "urls", "input_file", "base_url", "output", "help", "json"})
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def user_config_file(environ: Mapping[str, str] | None = None) -> Path:
|
|
24
|
+
"""Where the per-user settings file lives on this system."""
|
|
25
|
+
env = os.environ if environ is None else environ
|
|
26
|
+
if os.name == "nt":
|
|
27
|
+
base = Path(env.get("APPDATA") or Path.home() / "AppData" / "Roaming")
|
|
28
|
+
else:
|
|
29
|
+
base = Path(env.get("XDG_CONFIG_HOME") or Path.home() / ".config")
|
|
30
|
+
return base / "rayohx" / "config.toml"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def find_config(
|
|
34
|
+
explicit: Path | None, *, cwd: Path | None = None, environ: Mapping[str, str] | None = None
|
|
35
|
+
) -> Path | None:
|
|
36
|
+
"""The settings file to use: the one named, else ``rayohx.toml`` here, else the user's.
|
|
37
|
+
|
|
38
|
+
Raises:
|
|
39
|
+
ConfigurationError: If a file was named and does not exist.
|
|
40
|
+
"""
|
|
41
|
+
if explicit is not None:
|
|
42
|
+
if not explicit.is_file():
|
|
43
|
+
raise ConfigurationError(f"The settings file {explicit} does not exist.")
|
|
44
|
+
return explicit
|
|
45
|
+
local = (cwd or Path.cwd()) / FILE_NAME
|
|
46
|
+
if local.is_file():
|
|
47
|
+
return local
|
|
48
|
+
personal = user_config_file(environ)
|
|
49
|
+
return personal if personal.is_file() else None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def load_config(path: Path, valid: Collection[str]) -> dict[str, Any]:
|
|
53
|
+
"""Read a settings file and check its keys against the option names in ``valid``.
|
|
54
|
+
|
|
55
|
+
Keys may use dashes or underscores. The result uses underscores, as the options do.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
ConfigurationError: If the file is not valid TOML or has a key that is not an option.
|
|
59
|
+
"""
|
|
60
|
+
try:
|
|
61
|
+
raw = tomllib.loads(path.read_text(encoding="utf-8"))
|
|
62
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
63
|
+
raise ConfigurationError(f"The settings file {path} cannot be read.") from exc
|
|
64
|
+
except tomllib.TOMLDecodeError as exc:
|
|
65
|
+
raise ConfigurationError(f"The settings file {path} is not valid TOML: {exc}") from exc
|
|
66
|
+
allowed = set(valid) - EXCLUDED
|
|
67
|
+
settings: dict[str, Any] = {}
|
|
68
|
+
for key, value in raw.items():
|
|
69
|
+
name = key.replace("-", "_")
|
|
70
|
+
if name not in allowed:
|
|
71
|
+
close = difflib.get_close_matches(name, sorted(allowed), n=1)
|
|
72
|
+
hint = f"Did you mean {close[0].replace('_', '-')!r}?" if close else None
|
|
73
|
+
raise ConfigurationError(
|
|
74
|
+
f"{key!r} in {path} is not a setting.",
|
|
75
|
+
hint=hint or "Use the names of the command line options.",
|
|
76
|
+
)
|
|
77
|
+
settings[name] = value
|
|
78
|
+
return settings
|
rayohx_cli/values.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Turning what the user typed into engine settings. Pure functions, no output."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from rayohx import OutputFormat, RemuxBackend, StorageStrategy
|
|
8
|
+
from rayohx.config import (
|
|
9
|
+
DownloadConfig,
|
|
10
|
+
NetworkConfig,
|
|
11
|
+
OutputConfig,
|
|
12
|
+
ProxyConfig,
|
|
13
|
+
SelectionConfig,
|
|
14
|
+
StorageConfig,
|
|
15
|
+
)
|
|
16
|
+
from rayohx.errors import ConfigurationError
|
|
17
|
+
|
|
18
|
+
_SIZE = re.compile(r"^\s*(\d+(?:\.\d+)?)\s*([kmgt]?)(?:i?b)?\s*$", re.IGNORECASE)
|
|
19
|
+
_BINARY = {"": 1, "k": 1024, "m": 1024**2, "g": 1024**3, "t": 1024**4}
|
|
20
|
+
_DECIMAL = {"": 1, "k": 1000, "m": 1000**2, "g": 1000**3, "t": 1000**4}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def parse_size(text: str, *, name: str = "size", decimal: bool = False) -> int:
|
|
24
|
+
"""Read ``512``, ``64k``, ``2M`` or ``1.5G``.
|
|
25
|
+
|
|
26
|
+
Suffixes are powers of 1024 (``2M`` is 2 MiB), or powers of 1000 with ``decimal`` (used for
|
|
27
|
+
bit rates, where ``3M`` means 3 million bits per second).
|
|
28
|
+
|
|
29
|
+
Raises:
|
|
30
|
+
ConfigurationError: If ``text`` is not a size.
|
|
31
|
+
"""
|
|
32
|
+
match = _SIZE.match(text)
|
|
33
|
+
if not match:
|
|
34
|
+
raise ConfigurationError(
|
|
35
|
+
f"{name} {text!r} is not a size.", hint="Use a number with an optional k, M or G."
|
|
36
|
+
)
|
|
37
|
+
scale = (_DECIMAL if decimal else _BINARY)[match.group(2).lower()]
|
|
38
|
+
value = int(float(match.group(1)) * scale)
|
|
39
|
+
if value <= 0:
|
|
40
|
+
raise ConfigurationError(f"{name} must be greater than zero.")
|
|
41
|
+
return value
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def read_values(values: Sequence[str], *, what: str) -> list[str]:
|
|
45
|
+
"""Expand ``@file`` entries to the lines of that file, so secrets need not be typed.
|
|
46
|
+
|
|
47
|
+
Blank lines and lines starting with ``#`` are skipped.
|
|
48
|
+
|
|
49
|
+
Raises:
|
|
50
|
+
ConfigurationError: If a file cannot be read.
|
|
51
|
+
"""
|
|
52
|
+
expanded: list[str] = []
|
|
53
|
+
for value in values:
|
|
54
|
+
if not value.startswith("@"):
|
|
55
|
+
expanded.append(value)
|
|
56
|
+
continue
|
|
57
|
+
path = Path(value[1:]).expanduser()
|
|
58
|
+
try:
|
|
59
|
+
lines = path.read_text(encoding="utf-8").splitlines()
|
|
60
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
61
|
+
raise ConfigurationError(f"The {what} file {path} cannot be read.") from exc
|
|
62
|
+
expanded += [line.strip() for line in lines if line.strip() and not line.startswith("#")]
|
|
63
|
+
return expanded
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def parse_headers(values: Sequence[str]) -> dict[str, str]:
|
|
67
|
+
"""Read ``Name: value`` entries (or ``@file``) into a dictionary. Later ones win."""
|
|
68
|
+
headers: dict[str, str] = {}
|
|
69
|
+
for entry in read_values(values, what="header"):
|
|
70
|
+
name, separator, value = entry.partition(":")
|
|
71
|
+
if not separator or not name.strip():
|
|
72
|
+
raise ConfigurationError(
|
|
73
|
+
f"The header {entry!r} is not written as 'Name: value'.",
|
|
74
|
+
)
|
|
75
|
+
for existing in [key for key in headers if key.lower() == name.strip().lower()]:
|
|
76
|
+
del headers[existing]
|
|
77
|
+
headers[name.strip()] = value.strip()
|
|
78
|
+
return headers
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def parse_cookies(values: Sequence[str]) -> dict[str, str]:
|
|
82
|
+
"""Read ``name=value`` entries (several may share one with ``;``, or come from ``@file``)."""
|
|
83
|
+
cookies: dict[str, str] = {}
|
|
84
|
+
for entry in read_values(values, what="cookie"):
|
|
85
|
+
for part in entry.split(";"):
|
|
86
|
+
if not part.strip():
|
|
87
|
+
continue
|
|
88
|
+
name, separator, value = part.partition("=")
|
|
89
|
+
if not separator or not name.strip():
|
|
90
|
+
raise ConfigurationError(
|
|
91
|
+
f"The cookie {part.strip()!r} is not written as name=value."
|
|
92
|
+
)
|
|
93
|
+
cookies[name.strip()] = value.strip()
|
|
94
|
+
return cookies
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def parse_cookie_file(path: Path) -> dict[str, str]:
|
|
98
|
+
"""Read a Netscape ``cookies.txt`` (seven tab-separated fields per line) into name -> value.
|
|
99
|
+
|
|
100
|
+
``#HttpOnly_`` lines count; other ``#`` lines are comments. The domain, path and expiry are
|
|
101
|
+
not used: the engine scopes cookies by host rules of its own.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
ConfigurationError: If the file cannot be read or has no cookie in it.
|
|
105
|
+
"""
|
|
106
|
+
try:
|
|
107
|
+
text = path.expanduser().read_text(encoding="utf-8")
|
|
108
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
109
|
+
raise ConfigurationError(f"The cookie file {path} cannot be read.") from exc
|
|
110
|
+
cookies: dict[str, str] = {}
|
|
111
|
+
for raw in text.splitlines():
|
|
112
|
+
line = raw.removeprefix("#HttpOnly_")
|
|
113
|
+
if not line.strip() or line.lstrip().startswith("#"):
|
|
114
|
+
continue
|
|
115
|
+
fields = line.split("\t")
|
|
116
|
+
if len(fields) < 7 or not fields[5].strip():
|
|
117
|
+
raise ConfigurationError(
|
|
118
|
+
f"The cookie file {path} is not in the Netscape cookies.txt format.",
|
|
119
|
+
hint="Each line needs seven tab-separated fields.",
|
|
120
|
+
)
|
|
121
|
+
cookies[fields[5].strip()] = fields[6].strip()
|
|
122
|
+
return cookies
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def build_config(
|
|
126
|
+
*,
|
|
127
|
+
quality: str = "best",
|
|
128
|
+
max_resolution: str | None = None,
|
|
129
|
+
max_bitrate: str | None = None,
|
|
130
|
+
variant: int | None = None,
|
|
131
|
+
resolution: str | None = None,
|
|
132
|
+
bandwidth: str | None = None,
|
|
133
|
+
variant_name: str | None = None,
|
|
134
|
+
codec: str | None = None,
|
|
135
|
+
video_range: str | None = None,
|
|
136
|
+
max_frame_rate: float | None = None,
|
|
137
|
+
audio: Sequence[str] = (),
|
|
138
|
+
audio_name: Sequence[str] = (),
|
|
139
|
+
subtitles: Sequence[str] = (),
|
|
140
|
+
subtitle_name: Sequence[str] = (),
|
|
141
|
+
video_only: bool = False,
|
|
142
|
+
header: Sequence[str] = (),
|
|
143
|
+
cookie: Sequence[str] = (),
|
|
144
|
+
user_agent: str | None = None,
|
|
145
|
+
referer: str | None = None,
|
|
146
|
+
origin: str | None = None,
|
|
147
|
+
proxy: str | None = None,
|
|
148
|
+
proxy_username: str | None = None,
|
|
149
|
+
proxy_password: str | None = None,
|
|
150
|
+
backoff: float | None = None,
|
|
151
|
+
max_redirects: int | None = None,
|
|
152
|
+
cookie_file: Path | None = None,
|
|
153
|
+
insecure: bool = False,
|
|
154
|
+
ca_bundle: Path | None = None,
|
|
155
|
+
env_proxy: bool = False,
|
|
156
|
+
client_cert: Path | None = None,
|
|
157
|
+
client_key: Path | None = None,
|
|
158
|
+
client_key_password: str | None = None,
|
|
159
|
+
connect_timeout: float | None = None,
|
|
160
|
+
read_timeout: float | None = None,
|
|
161
|
+
timeout: float | None = None,
|
|
162
|
+
retries: int | None = None,
|
|
163
|
+
rate_limit: str | None = None,
|
|
164
|
+
credential_host: Sequence[str] = (),
|
|
165
|
+
concurrency: int | None = None,
|
|
166
|
+
resume: bool = True,
|
|
167
|
+
storage: StorageStrategy = StorageStrategy.AUTO,
|
|
168
|
+
workspace: Path | None = None,
|
|
169
|
+
buffer_size: str | None = None,
|
|
170
|
+
max_temp_size: str | None = None,
|
|
171
|
+
space_check: bool = True,
|
|
172
|
+
reserve_space: str | None = None,
|
|
173
|
+
keep_state: bool = True,
|
|
174
|
+
directory: Path | None = None,
|
|
175
|
+
allow_file_keys: bool = False,
|
|
176
|
+
format: OutputFormat = OutputFormat.AUTO,
|
|
177
|
+
overwrite: bool = False,
|
|
178
|
+
validate: bool = True,
|
|
179
|
+
remux_backend: RemuxBackend = RemuxBackend.AUTO,
|
|
180
|
+
ffmpeg: Path | None = None,
|
|
181
|
+
) -> DownloadConfig:
|
|
182
|
+
"""Build the engine's settings from the command line's options.
|
|
183
|
+
|
|
184
|
+
Raises:
|
|
185
|
+
ConfigurationError: If any value is invalid. The message says which and how to fix it.
|
|
186
|
+
"""
|
|
187
|
+
headers: dict[str, str] = {}
|
|
188
|
+
if referer:
|
|
189
|
+
headers["Referer"] = referer
|
|
190
|
+
if origin:
|
|
191
|
+
headers["Origin"] = origin
|
|
192
|
+
for name, value in parse_headers(header).items():
|
|
193
|
+
# The user's own headers win over the shortcuts. Names differ only in case at times.
|
|
194
|
+
for existing in [key for key in headers if key.lower() == name.lower()]:
|
|
195
|
+
del headers[existing]
|
|
196
|
+
headers[name] = value
|
|
197
|
+
|
|
198
|
+
network_options: dict[str, object] = {
|
|
199
|
+
"headers": headers,
|
|
200
|
+
"cookies": {
|
|
201
|
+
**(parse_cookie_file(cookie_file) if cookie_file else {}),
|
|
202
|
+
**parse_cookies(cookie),
|
|
203
|
+
},
|
|
204
|
+
"verify_tls": False if insecure else (ca_bundle if ca_bundle else True),
|
|
205
|
+
"credential_hosts": tuple(credential_host),
|
|
206
|
+
"proxy_from_env": env_proxy,
|
|
207
|
+
"client_cert": client_cert,
|
|
208
|
+
"client_key": client_key,
|
|
209
|
+
"client_key_password": client_key_password,
|
|
210
|
+
}
|
|
211
|
+
optional: tuple[tuple[str, object], ...] = (
|
|
212
|
+
("user_agent", user_agent),
|
|
213
|
+
("connect_timeout", connect_timeout),
|
|
214
|
+
("read_timeout", read_timeout),
|
|
215
|
+
("total_timeout", timeout),
|
|
216
|
+
("retries", retries),
|
|
217
|
+
("backoff_factor", backoff),
|
|
218
|
+
("max_redirects", max_redirects),
|
|
219
|
+
)
|
|
220
|
+
for key, chosen in optional:
|
|
221
|
+
if chosen is not None:
|
|
222
|
+
network_options[key] = chosen
|
|
223
|
+
if (proxy_username or proxy_password) and not proxy:
|
|
224
|
+
raise ConfigurationError(
|
|
225
|
+
"A proxy user name or password was given without a proxy.", hint="Add --proxy."
|
|
226
|
+
)
|
|
227
|
+
if proxy:
|
|
228
|
+
network_options["proxy"] = ProxyConfig(
|
|
229
|
+
url=proxy, username=proxy_username, password=proxy_password
|
|
230
|
+
)
|
|
231
|
+
if rate_limit:
|
|
232
|
+
network_options["rate_limit"] = parse_size(rate_limit, name="rate limit")
|
|
233
|
+
storage_options: dict[str, object] = {
|
|
234
|
+
"strategy": storage,
|
|
235
|
+
"workspace_dir": workspace,
|
|
236
|
+
"check_space": space_check,
|
|
237
|
+
"keep_state_on_failure": keep_state,
|
|
238
|
+
}
|
|
239
|
+
if reserve_space is not None:
|
|
240
|
+
storage_options["reserve_bytes"] = (
|
|
241
|
+
0 if reserve_space.strip() == "0" else parse_size(reserve_space, name="reserve space")
|
|
242
|
+
)
|
|
243
|
+
if buffer_size:
|
|
244
|
+
storage_options["buffer_bytes"] = parse_size(buffer_size, name="buffer size")
|
|
245
|
+
if max_temp_size:
|
|
246
|
+
storage_options["max_temp_bytes"] = parse_size(max_temp_size, name="temporary size limit")
|
|
247
|
+
|
|
248
|
+
selection_options: dict[str, object] = {
|
|
249
|
+
"quality": quality,
|
|
250
|
+
"max_resolution": max_resolution,
|
|
251
|
+
"audio_languages": tuple(audio),
|
|
252
|
+
"audio_names": tuple(audio_name),
|
|
253
|
+
"subtitle_languages": tuple(subtitles),
|
|
254
|
+
"subtitle_names": tuple(subtitle_name),
|
|
255
|
+
"video_only": video_only,
|
|
256
|
+
"variant": variant,
|
|
257
|
+
"resolution": resolution,
|
|
258
|
+
"variant_name": variant_name,
|
|
259
|
+
"codec": codec,
|
|
260
|
+
"video_range": video_range,
|
|
261
|
+
"max_frame_rate": max_frame_rate,
|
|
262
|
+
}
|
|
263
|
+
if bandwidth:
|
|
264
|
+
selection_options["bandwidth"] = parse_size(bandwidth, name="bandwidth", decimal=True)
|
|
265
|
+
if max_bitrate:
|
|
266
|
+
selection_options["max_bitrate"] = parse_size(max_bitrate, name="bitrate", decimal=True)
|
|
267
|
+
|
|
268
|
+
config_options: dict[str, object] = {"resume": resume, "allow_file_keys": allow_file_keys}
|
|
269
|
+
if concurrency is not None:
|
|
270
|
+
config_options["concurrency"] = concurrency
|
|
271
|
+
return DownloadConfig(
|
|
272
|
+
network=NetworkConfig(**network_options), # type: ignore[arg-type]
|
|
273
|
+
storage=StorageConfig(**storage_options), # type: ignore[arg-type]
|
|
274
|
+
output=OutputConfig(
|
|
275
|
+
format=format,
|
|
276
|
+
overwrite=overwrite,
|
|
277
|
+
directory=directory,
|
|
278
|
+
validate=validate,
|
|
279
|
+
remux_backend=remux_backend,
|
|
280
|
+
ffmpeg_path=ffmpeg,
|
|
281
|
+
),
|
|
282
|
+
selection=SelectionConfig(**selection_options), # type: ignore[arg-type]
|
|
283
|
+
**config_options, # type: ignore[arg-type]
|
|
284
|
+
)
|