diff options
author | ComradeCrow <comradecrow@vivaldi.net> | 2023-04-27 00:52:55 -0700 |
---|---|---|
committer | ComradeCrow <comradecrow@vivaldi.net> | 2023-04-27 00:52:55 -0700 |
commit | f72e036610d5174ba95fe7c11793ccf02c96ee6d (patch) | |
tree | 096f21caf47bf931809ef36181c55a3f2897b169 /src | |
parent | 304f8dcd8d1e217dce56ccebb9be6f95d558ab31 (diff) | |
download | ytcpp-f72e036610d5174ba95fe7c11793ccf02c96ee6d.tar.gz |
json conversions
json conversions are working, ytdlpwrapper crashes on destruction
Diffstat (limited to 'src')
-rw-r--r-- | src/YtdlpWrapper.cpp | 108 | ||||
-rw-r--r-- | src/YtdlpWrapper.hpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 35 |
3 files changed, 36 insertions, 109 deletions
diff --git a/src/YtdlpWrapper.cpp b/src/YtdlpWrapper.cpp index 424c6a4..605b6e9 100644 --- a/src/YtdlpWrapper.cpp +++ b/src/YtdlpWrapper.cpp @@ -75,7 +75,7 @@ void Video::from_json(const json& j, format& f) { j.at("acodec").get_to(f.acodec); j.at("ext").get_to(f.ext); - if (j.contains("quality") && j.at("quality").is_string()) { f.quality = j.at("quality"); } + if (j.contains("quality") && j.at("quality").is_number()) { f.quality = j.at("quality"); } if (j.contains("resolution") && j.at("resolution").is_string()) { f.resolution = j.at("resolution"); } if (j.contains("width") && j.at("width").is_number()) { f.width = j.at("width"); } if (j.contains("height") && j.at("height").is_number()) { f.height = j.at("height"); } @@ -149,58 +149,13 @@ std::vector<Video::video> YtdlpWrapper::searchVideos(const std::string& searchTe std::vector<Video::video> resp; - json info = getJsonSearch(searchTerm, limit); + 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.at("id"), i.at("title"), i.at("original_url"), {i.at("channel_url")}}; - - for (json j: i.at("formats")) { - - Video::format form{j.at("format"), j.at("url"), j.at("vcodec"), j.at("acodec"), j.at("ext")}; - - if (j.contains("quality")) { - - form.quality = j.at("quality"); - } - - if (j.contains("resolution")) { - - form.resolution = j.at("resolution"); - } - - if (j.contains("width")) { - - form.width = j.at("width"); - } - - if (j.contains("height")) { - - form.height = j.at("height"); - } - entry.formats.push_back(form); - } - - for (json j: i.at("thumbnails")) { - - Video::thumbnail thumb{j.at("url"), j.at("preference"), j.at("id")}; - - if (j.contains("resolution")) { - - thumb.resolution = j.at("resolution"); - } - - if (j.contains("height")) { - - thumb.height = j.at("height"); - } - - if (j.contains("width")) { - - thumb.width = j.at("width"); - } - - entry.thumbnails.push_back(thumb); - } + Video::video entry = i.get<Video::video>(); resp.push_back(entry); } @@ -216,54 +171,5 @@ Video::video YtdlpWrapper::getVideoByUrl(const std::string& url) { .attr("extract_info")(url, "download"_a=py::bool_(false)) )); - Video::video resp{info.at("id"), info.at("title"), info.at("original_url"), {info.at("channel_url")}}; - - for (json j: info.at("formats")) { - - Video::format form{j.at("format"), j.at("url"), j.at("vcodec"), j.at("acodec"), j.at("ext")}; - - if (j.contains("quality")) { - - form.quality = j.at("quality"); - } - - if (j.contains("resolution")) { - - form.resolution = j.at("resolution"); - } - - if (j.contains("width")) { - - form.width = j.at("width"); - } - - if (j.contains("height")) { - - form.height = j.at("height"); - } - resp.formats.push_back(form); - } - - for (json j: info.at("thumbnails")) { - - Video::thumbnail thumb{j.at("url"), j.at("preference"), j.at("id")}; - - if (j.contains("resolution")) { - - thumb.resolution = j.at("resolution"); - } - - if (j.contains("height")) { - - thumb.height = j.at("height"); - } - - if (j.contains("width")) { - - thumb.width = j.at("width"); - } - - resp.thumbnails.push_back(thumb); - } - return resp; + return info.get<Video::video>(); } \ No newline at end of file diff --git a/src/YtdlpWrapper.hpp b/src/YtdlpWrapper.hpp index 9138346..39cf4df 100644 --- a/src/YtdlpWrapper.hpp +++ b/src/YtdlpWrapper.hpp @@ -23,7 +23,7 @@ namespace Video { std::string url; int preference; - int id; + std::string id; std::optional<std::string> resolution; std::optional<int> height; std::optional<int> width; diff --git a/src/main.cpp b/src/main.cpp index 69309c0..8153a75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,24 +21,45 @@ using json = nlohmann::json; namespace fs = std::filesystem; void validateStructConversions() { - fs::path here("raycharles.json"); + fs::path here("info.json"); cout << fs::absolute(here) << endl; ifstream ifs; ifs.open(fs::absolute(here).c_str(), ifstream::in); json info = json::parse(ifs); + info = info["entries"][0]; - Video::video v = info["entries"][0].get<Video::video>(); + Video::video v = info.get<Video::video>(); json j = v; - cout << j << endl; + + for (auto& i: j.items()) { + + if (i.key() != "formats") { + + assert(j.at(i.key()) == info.at(i.key())); + cout << i.key() << ": " << i.value() << endl; + } + } + + for (auto i: j["formats"]) { + + for (auto& k: i.items()) { + + if (k.key() == "quality" || k.key() == "resolution") cout << k.key() << ": " << k.value() << endl; + } + } } int main(int argc, char **argv) { - validateStructConversions(); + // validateStructConversions(); - // YtdlpWrapper yt; + YtdlpWrapper yt; + Video::video vid = yt.getVideoByUrl("https://youtu.be/jy5x7bDYd4o?list=OLAK5uy_kSLxuOA_vBO8SsXaI6PjJbqvsIBnBReGM"); + cout << vid.id << ", " << vid.url << endl; + yt.~YtdlpWrapper(); + // vector<Video::video> response = yt.searchVideos("factorio"); // for (Video::video i: response) { @@ -48,9 +69,9 @@ int main(int argc, char **argv) { // nlohmann::json j = yt.getJsonSearch("the very thought of you"); // cout << j << endl; - // cout << VERSION << endl; + cout << VERSION << endl; - // SqliteInterface data; + SqliteInterface data; // vector<string> instances = getInstances(); // for (auto i: instances) { |