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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include <libytdlp/libytdlp.h>

#include <cstdlib>
#include <exception>
#include <filesystem>
#include <memory>
#include <mutex>
#include <new>
#include <string>
#include <string_view>

#include "bridge.hpp"
#include "install_dirs.hpp"
#include "jobs.hpp"
#include "runtime.hpp"

namespace {

struct LyContext {
    std::string options = "{}";
};

struct LyResult {
    std::string json;
};

struct LyError {
    ly_status code = LY_E_INTERNAL;
    std::string message;
    std::string exception_class;
    mutable std::string cached_details;
    mutable std::mutex details_mutex;
};

std::string installed_share_dir() {
    return libytdlp::staged_share_dir();
}

bool system_ytdlp() {
#ifdef LIBYTDLP_DEPLOYMENT_PROFILE
    return std::string_view(LIBYTDLP_DEPLOYMENT_PROFILE) == "SYSTEM_YTDLP_SYSTEM_PYTHON";
#else
    return false;
#endif
}

std::string bridge_dir() {
    const char* from_env = std::getenv("LIBYTDLP_BRIDGE_DIR");
    if (from_env != nullptr) {
        return from_env;
    }

    std::string share = installed_share_dir();

    if (!share.empty()) {
        std::filesystem::path bridge = std::filesystem::path(share) / "python";

        return bridge.string();
    }

#ifdef LIBYTDLP_BRIDGE_DIR
    return LIBYTDLP_BRIDGE_DIR;
#else
    return "";
#endif
}

std::string ytdlp_dir() {
    const char* from_env = std::getenv("LIBYTDLP_YTDLP_DIR");
    if (from_env != nullptr) {
        return from_env;
    }

    if (system_ytdlp()) {
        return std::string();
    }

    std::string share = installed_share_dir();

    if (!share.empty()) {
        std::filesystem::path ytdlp = std::filesystem::path(share) / "yt-dlp";

        return ytdlp.string();
    }

#ifdef LIBYTDLP_YTDLP_DIR
    return LIBYTDLP_YTDLP_DIR;
#else
    return "";
#endif
}

std::unique_ptr<LyError> make_error(
    ly_status code,
    const std::string& message,
    const std::string& exception_class = std::string()
) {
    auto error = std::make_unique<LyError>();
    error->code = code;
    error->message = message;
    error->exception_class = exception_class;
    return error;
}

bool store_error(ly_error** error, std::unique_ptr<LyError> owned) {
    if (error == nullptr) {
        return false;
    }

    *error = reinterpret_cast<ly_error*>(owned.release());
    return true;
}

bool report(ly_error** error, ly_status code, const char* message) noexcept {
    if (error == nullptr) {
        return false;
    }

    auto* slot = new (std::nothrow) LyError();
    if (slot == nullptr) {
        *error = nullptr;
        return false;
    }

    try {
        slot->message = message;
    } catch (...) {
        delete slot;
        *error = nullptr;
        return false;
    }

    slot->code = code;
    *error = reinterpret_cast<ly_error*>(slot);
    return true;
}

bool ensure_runtime(ly_error** error) {
    std::string message;
    std::string bridge = bridge_dir();
    std::string ytdlp = ytdlp_dir();
    if (libytdlp::Runtime::instance().ensure(bridge.c_str(), ytdlp.c_str(), message)) {
        return true;
    }

    store_error(error, make_error(LY_E_RUNTIME, message));
    return false;
}

const char* status_kind(ly_status status) {
    switch (status) {
    case LY_OK:
        return "ok";
    case LY_E_INVALID_ARGUMENT:
        return "invalid_argument";
    case LY_E_RUNTIME:
        return "runtime";
    case LY_E_EXTRACT:
        return "extract";
    case LY_E_NETWORK:
        return "network";
    case LY_E_CANCELLED:
        return "cancelled";
    case LY_E_BUSY:
        return "busy";
    case LY_E_INTERNAL:
        return "internal";
    }

    return "internal";
}

void append_escaped(std::string& out, const std::string& text) {
    static const char* kHex = "0123456789abcdef";
    out += '"';

    for (char step : text) {
        unsigned char c = static_cast<unsigned char>(step);

        if (c == '"' || c == '\\') {
            out += '\\';
            out += static_cast<char>(c);
            continue;
        }

        if (c < 0x20) {
            out += "\\u00";
            out += kHex[c >> 4];
            out += kHex[c & 0x0F];
            continue;
        }

        out += static_cast<char>(c);
    }

    out += '"';
}

} // namespace

uint32_t ly_abi_version(void) noexcept {
    return LIBYTDLP_ABI_VERSION;
}

ly_status ly_context_create(
    const char* options_json,
    size_t options_len,
    ly_context** out,
    ly_error** error
) noexcept {
    try {
        if (out == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "out must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *out = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (options_json == nullptr && options_len != 0) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "options length needs a buffer"));
            return LY_E_INVALID_ARGUMENT;
        }

        auto context = std::make_unique<LyContext>();
        if (options_json != nullptr && options_len != 0) {
            context->options.assign(options_json, options_len);
        }

        *out = reinterpret_cast<ly_context*>(context.release());
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown context creation failure");
        return LY_E_INTERNAL;
    }
}

ly_status ly_context_release(ly_context* context) noexcept {
    if (context == nullptr) {
        return LY_OK;
    }

    delete reinterpret_cast<LyContext*>(context);
    return LY_OK;
}

ly_status ly_extract(
    ly_context* context,
    const char* url,
    size_t url_len,
    const char* options_json,
    size_t options_len,
    ly_result** out,
    ly_error** error
) noexcept {
    try {
        if (out == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "out must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *out = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (context == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "context must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (url == nullptr || url_len == 0) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "url must not be empty"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (options_json == nullptr && options_len != 0) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "options length needs a buffer"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (!ensure_runtime(error)) {
            return LY_E_RUNTIME;
        }

        std::string url_text(url, url_len);
        const std::string& context_options = reinterpret_cast<LyContext*>(context)->options;

        std::string call_options = "{}";

        if (options_json != nullptr && options_len != 0) {
            call_options.assign(options_json, options_len);
        }

        libytdlp::BridgeResult called =
            libytdlp::extract_json(url_text, context_options, call_options);
        if (!called.ok) {
            store_error(error, make_error(called.status, called.message, called.exception_class));
            return called.status;
        }

        auto result = std::make_unique<LyResult>();
        result->json = std::move(called.json);

        *out = reinterpret_cast<ly_result*>(result.release());
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown extraction failure");
        return LY_E_INTERNAL;
    }
}

ly_status ly_version_info(ly_result** out, ly_error** error) noexcept {
    try {
        if (out == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "out must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *out = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (!ensure_runtime(error)) {
            return LY_E_RUNTIME;
        }

        libytdlp::BridgeResult called = libytdlp::runtime_info_json();
        if (!called.ok) {
            store_error(error, make_error(called.status, called.message, called.exception_class));
            return called.status;
        }

        auto result = std::make_unique<LyResult>();
        result->json = std::move(called.json);

        *out = reinterpret_cast<ly_result*>(result.release());
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown version query failure");
        return LY_E_INTERNAL;
    }
}

ly_status ly_result_json(const ly_result* result, const char** data, size_t* len) noexcept {
    if (result == nullptr || data == nullptr || len == nullptr) {
        return LY_E_INVALID_ARGUMENT;
    }

    const std::string& json = reinterpret_cast<const LyResult*>(result)->json;
    *data = json.data();
    *len = json.size();
    return LY_OK;
}

void ly_result_release(ly_result* result) noexcept {
    delete reinterpret_cast<LyResult*>(result);
}

ly_status ly_download_start(
    ly_context* context,
    const char* url,
    size_t url_len,
    const char* options_json,
    size_t options_len,
    ly_job** out,
    ly_error** error
) noexcept {
    try {
        if (out == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "out must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *out = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (context == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "context must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (url == nullptr || url_len == 0) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "url must not be empty"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (options_json == nullptr && options_len != 0) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "options length needs a buffer"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (!ensure_runtime(error)) {
            return LY_E_RUNTIME;
        }

        std::string url_text(url, url_len);
        std::string options = reinterpret_cast<LyContext*>(context)->options;
        std::string job_options = "{}";

        if (options_json != nullptr && options_len != 0) {
            job_options.assign(options_json, options_len);
        }

        std::string failure;
        libytdlp::DownloadJob* job = libytdlp::DownloadJob::create(
            std::move(url_text),
            std::move(options),
            std::move(job_options),
            failure
        );
        if (job == nullptr) {
            store_error(error, make_error(LY_E_RUNTIME, failure));
            return LY_E_RUNTIME;
        }

        *out = reinterpret_cast<ly_job*>(job);
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown download start failure");
        return LY_E_INTERNAL;
    }
}

ly_status
ly_job_poll(ly_job* job, ly_job_state* state, ly_result** events, ly_error** error) noexcept {
    try {
        if (events == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "events must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *events = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (job == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "job must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }

        if (state == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "state must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }

        std::string events_json;
        ly_job_state current = reinterpret_cast<libytdlp::DownloadJob*>(job)->poll(events_json);

        auto result = std::make_unique<LyResult>();
        result->json = std::move(events_json);

        *events = reinterpret_cast<ly_result*>(result.release());
        *state = current;
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown job poll failure");
        return LY_E_INTERNAL;
    }
}

ly_status ly_job_wait(ly_job* job, ly_result** out, ly_error** error) noexcept {
    try {
        if (out == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "out must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }
        *out = nullptr;

        if (error != nullptr) {
            *error = nullptr;
        }

        if (job == nullptr) {
            store_error(error, make_error(LY_E_INVALID_ARGUMENT, "job must not be null"));
            return LY_E_INVALID_ARGUMENT;
        }

        std::string json;
        ly_status status = LY_OK;
        std::string message;
        std::string exception_class;
        reinterpret_cast<libytdlp::DownloadJob*>(job)->wait(json, status, message, exception_class);

        if (status != LY_OK) {
            store_error(error, make_error(status, message, exception_class));
            return status;
        }

        auto result = std::make_unique<LyResult>();
        result->json = std::move(json);

        *out = reinterpret_cast<ly_result*>(result.release());
        return LY_OK;
    } catch (const std::exception& caught) {
        report(error, LY_E_INTERNAL, caught.what());
        return LY_E_INTERNAL;
    } catch (...) {
        report(error, LY_E_INTERNAL, "unknown job wait failure");
        return LY_E_INTERNAL;
    }
}

ly_status ly_job_cancel(ly_job* job) noexcept {
    try {
        if (job == nullptr) {
            return LY_E_INVALID_ARGUMENT;
        }

        reinterpret_cast<libytdlp::DownloadJob*>(job)->cancel();
        return LY_OK;
    } catch (...) {
        return LY_E_INTERNAL;
    }
}

void ly_job_release(ly_job* job) noexcept {
    delete reinterpret_cast<libytdlp::DownloadJob*>(job);
}

ly_status ly_error_code(const ly_error* error) noexcept {
    if (error == nullptr) {
        return LY_E_INVALID_ARGUMENT;
    }

    return reinterpret_cast<const LyError*>(error)->code;
}

const char* ly_error_message(const ly_error* error) noexcept {
    if (error == nullptr) {
        return nullptr;
    }

    return reinterpret_cast<const LyError*>(error)->message.c_str();
}

bool ly_error_is_retryable(const ly_error* error) noexcept {
    if (error == nullptr) {
        return false;
    }

    switch (reinterpret_cast<const LyError*>(error)->code) {
    case LY_E_NETWORK:
    case LY_E_BUSY:
        return true;
    default:
        return false;
    }
}

void ly_error_release(ly_error* error) noexcept {
    delete reinterpret_cast<LyError*>(error);
}

ly_status ly_error_details_json(const ly_error* error, const char** data, size_t* len) noexcept {
    try {
        if (error == nullptr || data == nullptr || len == nullptr) {
            return LY_E_INVALID_ARGUMENT;
        }

        const LyError* owned = reinterpret_cast<const LyError*>(error);
        std::lock_guard<std::mutex> details_lock(owned->details_mutex);
        std::string json = "{\"code\":";

        json += std::to_string(static_cast<unsigned int>(owned->code));
        json += ",\"kind\":\"";
        json += status_kind(owned->code);
        json += "\",\"class\":";

        if (owned->exception_class.empty()) {
            json += "null";
        } else {
            append_escaped(json, owned->exception_class);
        }

        json += ",\"message\":";
        append_escaped(json, owned->message);
        json += "}";

        owned->cached_details = std::move(json);
        *data = owned->cached_details.data();
        *len = owned->cached_details.size();
        return LY_OK;
    } catch (...) {
        return LY_E_INTERNAL;
    }
}