libytdlp-sys 0.6.0

Raw FFI bindings to the libytdlp C ABI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import json
import os
import subprocess
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "python"))

import libytdlp_bridge
from libytdlp_bridge import extract_json, versions


def check_versions():
    info = json.loads(versions())
    assert set(("bridge", "yt_dlp", "python")) <= set(info), info
    assert info["bridge"] == libytdlp_bridge.__version__
    assert info["yt_dlp"], "empty yt-dlp version"
    print("versions:", info)


def check_dumps_round_trip():
    from libytdlp_bridge.serialize import dumps

    text = dumps({"id": "x", "title": "ü", "tags": ("a", "b"), "n": None})
    parsed = json.loads(text)
    assert parsed["id"] == "x"
    assert parsed["title"] == "ü"
    assert text.isascii(), "bridge output must stay ascii-safe"
    print("dumps round trip ok")


def check_rejects_empty_url():
    try:
        extract_json("", "{}")
    except ValueError:
        print("empty url rejected ok")
        return
    raise AssertionError("empty url was accepted")


def check_rejects_non_object_options():
    try:
        extract_json("ly-bogus://example", "[1, 2]")
    except ValueError:
        print("non-object options rejected ok")
        return
    raise AssertionError("non-object options were accepted")


def check_runtime_info():
    from libytdlp_bridge import runtime_info

    info = json.loads(runtime_info("FULL_BUNDLED", "abc123", 1, False))
    assert info["backend"] == "embedded"
    assert info["deployment_profile"] == "FULL_BUNDLED"
    assert info["ytdlp_commit"] == "abc123"
    assert info["abi"] == 1
    assert info["host_owned"] is False
    assert info["yt_dlp"], "empty yt-dlp version"
    print("runtime info ok")


def check_cachedir_defaults_off():
    from libytdlp_bridge.download import _params as download_params
    from libytdlp_bridge.extract import _params as extract_params

    assert extract_params({})["cachedir"] is False
    assert download_params({}, [], None)["cachedir"] is False
    assert extract_params({"cachedir": "/tmp/x"})["cachedir"] == "/tmp/x"

    hook = download_params({}, [], None)["progress_hooks"]
    custom = download_params({"progress_hooks": ["user"]}, [], None)["progress_hooks"]
    assert len(hook) == 1 and callable(hook[0])
    assert custom[0] == "user" and len(custom) == 2 and callable(custom[1])
    print("cachedir defaults ok")


def check_unsupported_url():
    try:
        extract_json("ly-bogus://no-such-scheme-xyz", "{}")
    except Exception as caught:
        assert str(caught), "exception without a message"
        print("unsupported url rejected ok:", type(caught).__name__)
        return
    raise AssertionError("unsupported url was accepted")


def check_extractor_inventory():
    from libytdlp_bridge import extractor_names

    names = json.loads(extractor_names())
    assert isinstance(names, list), type(names)
    assert names == sorted(names), "extractor list must be sorted"
    assert len(names) > 1000, f"expected a full registry, got {len(names)}"
    assert "generic" in names, "generic extractor missing"
    assert "youtube" in names, "youtube extractor missing"
    print(f"extractor inventory ok: {len(names)} extractors")


def check_extractor_args_passthrough():
    from libytdlp_bridge.extract import _params
    from yt_dlp import YoutubeDL

    options = {"extractor_args": {"youtube": {"player_client": ["ios"]}}}

    params = _params(options)

    assert params["extractor_args"] == {"youtube": {"player_client": ["ios"]}}

    with YoutubeDL(params) as ydl:
        assert ydl.params["extractor_args"] == {"youtube": {"player_client": ["ios"]}}

    print("extractor args passthrough ok")


def check_merge_options():
    from libytdlp_bridge.options import merge_options

    assert merge_options("{}", "{}") == {}
    assert merge_options("", "") == {}
    assert merge_options("", "{}") == {}
    assert merge_options("{}", "") == {}

    merged = merge_options('{"a": 1, "b": 2}', '{"b": 3, "c": 4}')
    assert merged == {"a": 1, "b": 3, "c": 4}, merged

    assert merge_options('{"a": 1}', "") == {"a": 1}

    for context_options, job_options in [
        ("[1, 2]", "{}"),
        ("{}", "[1, 2]"),
        ("42", "{}"),
        ("{}", "null"),
        ('"x"', "{}"),
    ]:
        try:
            merge_options(context_options, job_options)
        except ValueError:
            pass
        else:
            raise AssertionError(
                f"non-object options were accepted: {context_options!r} {job_options!r}"
            )

    print("merge options ok")


def check_download_sandbox():
    import tempfile

    from libytdlp_bridge.download import _params as download_params
    from yt_dlp import YoutubeDL

    root = tempfile.mkdtemp()
    outside = tempfile.mkdtemp()

    try:
        params = download_params({"outtmpl": os.path.join(outside, "%(id)s.%(ext)s")}, [], None)

        assert params["outtmpl"].startswith(outside)
        assert "libytdlp" not in params

        with YoutubeDL(params):
            pass

        try:
            download_params(
                {
                    "libytdlp": {"restrict_to": "relative/dir"},
                    "outtmpl": os.path.join(root, "%(id)s.%(ext)s"),
                },
                [],
                None,
            )
        except ValueError:
            pass
        else:
            raise AssertionError("relative restrict_to was accepted")

        try:
            download_params(
                {
                    "libytdlp": {"restrict_to": root},
                    "outtmpl": os.path.join(root, "..", "escape-%(id)s.%(ext)s"),
                },
                [],
                None,
            )
        except ValueError:
            pass
        else:
            raise AssertionError("escaping outtmpl was accepted")

        try:
            download_params(
                {
                    "libytdlp": {"restrict_to": root},
                    "outtmpl": os.path.join(outside, "out-%(id)s.%(ext)s"),
                },
                [],
                None,
            )
        except ValueError:
            pass
        else:
            raise AssertionError("outside outtmpl was accepted")

        try:
            download_params(
                {
                    "libytdlp": {"restrict_to": root},
                    "outtmpl": os.path.join(root, "%(id)s.%(ext)s"),
                    "paths": {"home": outside},
                },
                [],
                None,
            )
        except ValueError:
            pass
        else:
            raise AssertionError("outside paths home was accepted")

        try:
            download_params({"libytdlp": {"restrict_to": root}}, [], None)
        except ValueError:
            pass
        else:
            raise AssertionError("restriction without outtmpl or paths was accepted")

        params = download_params(
            {
                "libytdlp": {"restrict_to": root},
                "outtmpl": os.path.join(root, "%(id)s.%(ext)s"),
            },
            [],
            None,
        )

        assert params["outtmpl"].startswith(root)
        assert "libytdlp" not in params

        with YoutubeDL(params):
            pass

        print("download sandbox ok")
    finally:
        os.rmdir(root)
        os.rmdir(outside)


def fixture_script():
    here = os.path.dirname(os.path.abspath(__file__))

    return os.path.normpath(os.path.join(here, "..", "fixtures", "slow_server.py"))


def start_fixture_server(payload_bytes, rate_bytes_per_sec):
    process = subprocess.Popen(
        [sys.executable, fixture_script(), "0", str(payload_bytes), str(rate_bytes_per_sec)],
        stdout=subprocess.PIPE,
        text=True,
    )

    line = process.stdout.readline()

    if not line.startswith("PORT "):
        stop_fixture_server(process)

        raise AssertionError(f"fixture server did not report a port: {line!r}")

    port = int(line.removeprefix("PORT ").strip())

    return process, port


def stop_fixture_server(process):
    if process.poll() is None:
        process.terminate()

    try:
        process.wait(timeout=10)
    except subprocess.TimeoutExpired:
        process.kill()
        process.wait(timeout=10)

    if process.stdout is not None:
        process.stdout.close()


def run_cli_info(url):
    return subprocess.run(
        [sys.executable, "-m", "yt_dlp", "--no-cache-dir", "-J", url],
        capture_output=True,
        text=True,
        timeout=120,
    )


def format_urls(info):
    urls = set()

    direct = info.get("url")

    if direct:
        urls.add(direct)

    for entry in info.get("formats") or []:
        candidate = entry.get("url")

        if candidate:
            urls.add(candidate)

    return urls


def check_cli_library_parity():
    # NOTE: loopback fixture must bypass any ambient proxy; stash and restore
    # the proxy env around the fixture and the CLI children that inherit it.
    scrubbed = {}
    for name in (
        "http_proxy",
        "https_proxy",
        "all_proxy",
        "HTTP_PROXY",
        "HTTPS_PROXY",
        "ALL_PROXY",
        "no_proxy",
        "NO_PROXY",
    ):
        scrubbed[name] = os.environ.pop(name, None)
    os.environ["no_proxy"] = "*"
    os.environ["NO_PROXY"] = "*"

    server, port = start_fixture_server(65536, 8000000)

    try:
        url = f"http://127.0.0.1:{port}/file.bin"

        completed = run_cli_info(url)

        assert completed.returncode == 0, completed.stderr[-2000:]

        cli_info = json.loads(completed.stdout)

        lib_info = json.loads(extract_json(url, "{}"))

        assert cli_info["id"] == lib_info["id"], (cli_info.get("id"), lib_info.get("id"))

        assert cli_info["id"] == "file", cli_info.get("id")

        assert cli_info["ext"] == lib_info["ext"], (cli_info.get("ext"), lib_info.get("ext"))

        assert format_urls(cli_info) == format_urls(lib_info)

        assert format_urls(lib_info) == {url}, format_urls(lib_info)

        bogus_cli = run_cli_info("ly-bogus://no-such-scheme-xyz")

        assert bogus_cli.returncode != 0

        try:
            extract_json("ly-bogus://no-such-scheme-xyz", "{}")
        except Exception as caught:
            assert str(caught), "exception without a message"
        else:
            raise AssertionError("bogus url was accepted by the library")

        print("cli/library parity ok")
    finally:
        stop_fixture_server(server)

        for name, value in scrubbed.items():
            if value is None:
                os.environ.pop(name, None)
            else:
                os.environ[name] = value


def check_find_executables():
    import shutil
    import tempfile

    from libytdlp_bridge.extract import find_executable, find_executables

    found = find_executable("python3", None)
    assert found, "python3 must resolve on PATH"
    assert os.path.isabs(found)
    assert os.access(found, os.X_OK)

    scratch = tempfile.mkdtemp()
    try:
        assert find_executable("python3", [scratch]) is None

        located = find_executables([scratch])
        assert located["ffmpeg"] is None
        assert located["ffprobe"] is None

        python_path = shutil.which("python3")
        assert python_path, "python3 must resolve on PATH"
        python_dir = os.path.dirname(python_path)
        if sys.platform == "win32":
            python_name = os.path.basename(python_path)
            assert find_executable(python_name, [python_dir]) == python_path
        else:
            assert find_executable("python3", [python_dir]) == python_path
    finally:
        os.rmdir(scratch)

    from libytdlp_bridge import runtime_info

    info = json.loads(runtime_info("FULL_BUNDLED", "abc123", 1, False))
    assert "ffmpeg" in info
    assert "ffprobe" in info
    assert info["ffmpeg"] is None or isinstance(info["ffmpeg"], str)
    assert info["ffprobe"] is None or isinstance(info["ffprobe"], str)
    print("find executables ok")


if __name__ == "__main__":
    check_versions()
    check_dumps_round_trip()
    check_rejects_empty_url()
    check_rejects_non_object_options()
    check_cachedir_defaults_off()
    check_extractor_inventory()
    check_extractor_args_passthrough()
    check_merge_options()
    check_download_sandbox()
    check_cli_library_parity()
    check_runtime_info()
    check_unsupported_url()
    check_find_executables()
    print("bridge ok")