about summary refs log tree commit diff stats
path: root/src/YtdlpWrapper.cpp
blob: 9e27a1c27bcc846289baaf204f6a99b4ae437bfe (plain) (blame)
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
#include <string>
#include <pybind11/embed.h>
#include <nlohmann/json.hpp>

#include "YtdlpWrapper.hpp"

using json = nlohmann::json;
namespace py = pybind11;
using namespace py::literals;

std::string pyDictToJsonString(const py::dict& dict) {

    pybind11::object json = py::module::import("json");
    return json.attr("dumps")(dict).cast<std::string>();
}

py::object YtdlpWrapper::get_ytdl() {
    
    if (ytdl.is_none()) {
        ytdl = py::module::import("yt_dlp");
        ytdl = ytdl.attr("YoutubeDL")(py::dict("ignoreerrors"_a=py::bool_(true)));
    }

    return ytdl;
}

json YtdlpWrapper::getJsonSearch(const std::string& searchTerm, int limit) {

    py::dict info = get_ytdl().attr("extract_info")("ytsearch"+std::to_string(limit)+":"+searchTerm, "download"_a=py::bool_(false));
    return json::parse(pyDictToJsonString(info));
}