diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | cmake/FindYtdlp.cmake | 11 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | src/YtdlpWrapper.cpp | 12 | ||||
-rw-r--r-- | src/YtdlpWrapper.hpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 15 | ||||
-rw-r--r-- | src/tui.cpp | 3 |
7 files changed, 30 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bf5b4da..f6a31de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ project(ytcpp ) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake) if(WIN32) # Install dlls in the same directory as the executable on Windows set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -37,6 +38,8 @@ find_package(SQLite3 REQUIRED) # find_package(pybind11 REQUIRED) add_subdirectory(extern/pybind11) +find_package(Ytdlp REQUIRED RUNTIME) + add_executable(${PROJECT_NAME} src/main.cpp) target_include_directories(${PROJECT_NAME} PRIVATE src) diff --git a/cmake/FindYtdlp.cmake b/cmake/FindYtdlp.cmake new file mode 100644 index 0000000..a1d779d --- /dev/null +++ b/cmake/FindYtdlp.cmake @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im> +# +# SPDX-License-Identifier: BSD-2-Clause + +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +execute_process(COMMAND ${Python3_EXECUTABLE} -c "import yt_dlp" RESULT_VARIABLE YTDLP_CHECK_RESULT) + +if (${YTDLP_CHECK_RESULT} EQUAL 0) + set(Ytdlp_FOUND TRUE) +endif() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b21e42f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +yt-dlp \ No newline at end of file diff --git a/src/YtdlpWrapper.cpp b/src/YtdlpWrapper.cpp index e0598a0..eca1cbc 100644 --- a/src/YtdlpWrapper.cpp +++ b/src/YtdlpWrapper.cpp @@ -8,14 +8,18 @@ using json = nlohmann::json; namespace py = pybind11; using namespace py::literals; -YtdlpWrapper::YtdlpWrapper() { +py::object YtdlpWrapper::get_ytdl() { + + if (ytdl.is_none()) { + ytdl = py::module::import("yt_dlp").attr("YoutubeDL")(py::dict("ignoreerrors"_a=py::bool_(true))); + } - ytdl = py::module::import("yt_dlp").attr("YoutubeDL")(py::dict({"ignoreerrors": true})); + return ytdl; } json YtdlpWrapper::getJsonSearch(const string& searchTerm) { - const auto info = ytdl.attr("extract_info")("ytsearch:"+searchTerm, "download"_a=py::bool_(false)); - return json::parse(static_cast<std::string>(info)); + py::object info = get_ytdl().attr("extract_info")("ytsearch:"+searchTerm, "download"_a=py::bool_(false)); + return json::parse(info.cast<std::string>()); } diff --git a/src/YtdlpWrapper.hpp b/src/YtdlpWrapper.hpp index a9522ea..6cc045d 100644 --- a/src/YtdlpWrapper.hpp +++ b/src/YtdlpWrapper.hpp @@ -6,10 +6,10 @@ class YtdlpWrapper { public: - YtdlpWrapper(); nlohmann::json getJsonSearch(const string& searchTerm); private: - pybind11::object ytdl; + pybind11::object get_ytdl(); + pybind11::object ytdl = pybind11::none(); }; diff --git a/src/main.cpp b/src/main.cpp index 4267a73..42cba4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,23 +8,16 @@ #include "tui.cpp" #include "invapi.cpp" -// #include "YtdlpWrapper.cpp" +#include "YtdlpWrapper.cpp" using namespace std; int main() { - Tui tui; - string searchTerm; - int searchingFor; - tui.renderSearchBar(searchTerm, searchingFor); - if (tui.isCancelled()) { - - cerr << "cancelled!" << endl; - return 0; - } - cout << searchTerm << "," << searchingFor << endl; + YtdlpWrapper yt; + nlohmann::json j = yt.getJsonSearch("All Quiet on the Western Front Ost"); + cout << j << endl; // vector<string> instances = getInstances(); // for (auto i: instances) { diff --git a/src/tui.cpp b/src/tui.cpp index 601f468..61ba95e 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -15,7 +15,6 @@ using namespace std; bool Tui::renderSearchBar(string& searchTerm, int& searchingFor) { - cancel = false; auto screen = ftxui::ScreenInteractive::Fullscreen(); @@ -57,7 +56,7 @@ bool Tui::renderSearchBar(string& searchTerm, int& searchingFor) { }) | ftxui::border; }); - + cancel = false; screen.Loop(renderer); return true; |