diff options
author | ComradeCrow <comradecrow@vivaldi.net> | 2023-04-27 23:33:49 -0700 |
---|---|---|
committer | ComradeCrow <comradecrow@vivaldi.net> | 2023-04-27 23:33:49 -0700 |
commit | a58e7083712a14f3e644b8d3badf4254191199fa (patch) | |
tree | 79a2adf67081e0525ecd59c627cf4579bcda83c2 /src/YtdlpWrapper.cpp | |
parent | 42452b644f4acc7425546d7c46d627b720cbafa6 (diff) | |
download | ytcpp-a58e7083712a14f3e644b8d3badf4254191199fa.tar.gz |
video update
seperate out video, add more conversions, add to invapi cuz it will be needed
Diffstat (limited to 'src/YtdlpWrapper.cpp')
-rw-r--r-- | src/YtdlpWrapper.cpp | 119 |
1 files changed, 1 insertions, 118 deletions
diff --git a/src/YtdlpWrapper.cpp b/src/YtdlpWrapper.cpp index 605b6e9..5a18ab2 100644 --- a/src/YtdlpWrapper.cpp +++ b/src/YtdlpWrapper.cpp @@ -1,127 +1,10 @@ -#include <string> -#include <vector> -#include <iostream> -#include <pybind11/embed.h> -#include <nlohmann/json.hpp> - +#include "video.hpp" #include "YtdlpWrapper.hpp" using json = nlohmann::json; namespace py = pybind11; using namespace py::literals; -void Video::to_json(json& j, const format& f) { - - j = json{{"format", f.format}, - {"url", f.url}, - {"vcodec", f.vcodec}, - {"acodec", f.acodec}, - {"ext", f.ext} - }; - - if (f.quality.has_value()) { j["quality"] = f.quality.value(); } - if (f.resolution.has_value()) { j["resolution"] = f.resolution.value(); } - if (f.height.has_value()) { j["height"] = f.height.value(); } - if (f.width.has_value()) { j["width"] = f.width.value(); } -} - -void Video::to_json(json& j, const thumbnail& t) { - - j = json{{"url", t.url}, - {"preference", t.preference}, - {"id", t.id} - }; - - if (t.resolution.has_value()) { j["resolution"] = t.resolution.value(); } - if (t.height.has_value()) { j["height"] = t.height.value(); } - if (t.width.has_value()) { j["width"] = t.width.value(); } -} - -void Video::to_json(json& j, const video& v) { - - j = json{{"id", v.id}, - {"title", v.title}, - {"webpage_url", v.url}, - {"channel_id", v.channelId}, - {"channel_url", v.channelUrl}}; - - if (v.uploader.has_value()) { j["uploader"] = v.uploader.value(); } - if (v.uploaderId.has_value()) { j["uploader_id"] = v.uploaderId.value(); } - if (v.uploaderUrl.has_value()) { j["uploader_url"] = v.uploaderUrl.value(); } - if (v.duration.has_value()) { j["duration"] = v.duration.value(); } - if (v.viewcount.has_value()) { j["view_count"] = v.viewcount.value(); } - if (v.description.has_value()) { j["description"] = v.description.value(); } - - j["formats"] = json::array(); - - for (format f: v.formats) { - - j.at("formats").emplace_back(json(f)); - } - - j["thumbnails"] = json::array(); - - for (thumbnail t: v.thumbnails) { - - j.at("thumbnails").emplace_back(json(t)); - } -} - -void Video::from_json(const json& j, format& f) { - - j.at("format").get_to(f.format); - j.at("url").get_to(f.url); - j.at("vcodec").get_to(f.vcodec); - j.at("acodec").get_to(f.acodec); - j.at("ext").get_to(f.ext); - - 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"); } -} - -void Video::from_json(const json& j, thumbnail& t) { - - j.at("url").get_to(t.url); - j.at("preference").get_to(t.preference); - j.at("id").get_to(t.id); - - if (j.contains("resolution") && j.at("resolution").is_string()) { t.resolution = j.at("resolution"); } - if (j.contains("width") && j.at("width").is_number()) { t.width = j.at("width"); } - if (j.contains("height") && j.at("height").is_number()) { t.height = j.at("height"); } -} - -void Video::from_json(const json& j, video& v) { - - j.at("id").get_to(v.id); - j.at("title").get_to(v.title); - j.at("webpage_url").get_to(v.url); - j.at("channel_id").get_to(v.channelId); - j.at("channel_url").get_to(v.channelUrl); - - if (j.contains("uploader")) { v.uploader = j.at("uploader"); } - if (j.contains("uploader_id")) { v.uploaderId = j.at("uploader_id"); } - if (j.contains("uploader_url")) { v.uploaderUrl = j.at("uploader_url"); } - if (j.contains("duration")) { v.duration = j.at("duration"); } - if (j.contains("view_count")) { v.viewcount = j.at("view_count"); } - if (j.contains("description")) { v.description = j.at("description"); } - - for (json f: j.at("formats")) { - - format form; - from_json(f, form); - v.formats.push_back(form); - } - - for (json t: j.at("thumbnails")) { - - thumbnail thumb; - from_json(t, thumb); - v.thumbnails.push_back(thumb); - } -} - std::string pyDictToJsonString(const py::dict& dict) { pybind11::object json = py::module::import("json"); |