#include "video.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(); } py::object YtdlpWrapper::get_ytdl() { // pybind11::object ytdl = pybind11::none(); // if (ytdl.is_none()) { // ytdl = py::module::import("yt_dlp"); // } return py::module::import("yt_dlp"); } json YtdlpWrapper::getJsonSearch(const std::string& searchTerm, int limit) { pybind11::scoped_interpreter guard{}; py::dict info = get_ytdl() .attr("YoutubeDL")(py::dict("ignoreerrors"_a=py::bool_(true))) .attr("extract_info")("ytsearch"+std::to_string(limit)+":"+searchTerm, "download"_a=py::bool_(false)); return json::parse(pyDictToJsonString(info)); } std::vector YtdlpWrapper::searchVideos(const std::string& searchTerm, int limit) { pybind11::scoped_interpreter guard{}; std::vector resp; json info = json::parse(pyDictToJsonString( get_ytdl().attr("YoutubeDL")(py::dict("ignoreerrors"_a=py::bool_(true))) .attr("extract_info")("ytsearch"+std::to_string(limit)+":"+searchTerm, "download"_a=py::bool_(false)) )); for (json i: info.at("entries")) { Video::video entry = i.get(); resp.push_back(entry); } return resp; } Video::video YtdlpWrapper::getVideoByUrl(const std::string& url) { pybind11::scoped_interpreter guard{}; json info = json::parse(pyDictToJsonString( get_ytdl().attr("YoutubeDL")(py::dict("ignoreerrors"_a=py::bool_(true), "noplaylist"_a=py::bool_(true))) .attr("extract_info")(url, "download"_a=py::bool_(false)) )); return info.get(); }