about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-04-27 23:33:49 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-04-27 23:33:49 -0700
commita58e7083712a14f3e644b8d3badf4254191199fa (patch)
tree79a2adf67081e0525ecd59c627cf4579bcda83c2
parent42452b644f4acc7425546d7c46d627b720cbafa6 (diff)
downloadytcpp-a58e7083712a14f3e644b8d3badf4254191199fa.tar.gz
video update
seperate out video, add more conversions, add to invapi cuz it will be
needed
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/YtdlpWrapper.cpp119
-rw-r--r--src/YtdlpWrapper.hpp52
-rw-r--r--src/invapi.cpp1
-rw-r--r--src/main.cpp16
-rw-r--r--src/video.cpp149
-rw-r--r--src/video.hpp61
7 files changed, 229 insertions, 171 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index df0000a..ee4387b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,7 +48,7 @@ else()
 endif()
 
 
-add_executable(${PROJECT_NAME} src/main.cpp src/tui.cpp src/invapi.cpp src/YtdlpWrapper.cpp src/sqliteinterface.cpp)
+add_executable(${PROJECT_NAME} src/main.cpp src/tui.cpp src/invapi.cpp src/YtdlpWrapper.cpp src/sqliteinterface.cpp src/video.cpp)
 target_include_directories(${PROJECT_NAME} PRIVATE src)
 
 target_compile_definitions(${PROJECT_NAME} 
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");
diff --git a/src/YtdlpWrapper.hpp b/src/YtdlpWrapper.hpp
index 39cf4df..81857c9 100644
--- a/src/YtdlpWrapper.hpp
+++ b/src/YtdlpWrapper.hpp
@@ -1,59 +1,11 @@
 #ifndef YTDLPWRAPPER_H
 #define YTDLPWRAPPER_H
 #include <string>
+#include <vector>
+#include <iostream>
 #include <pybind11/embed.h>
 #include <nlohmann/json.hpp>
 
-namespace Video {
-
-    struct format {
-    
-        std::string format;
-        std::string url;
-        std::string vcodec;
-        std::string acodec;
-        std::string ext;
-        std::optional<float> quality;
-        std::optional<std::string> resolution;
-        std::optional<int> height;
-        std::optional<int> width;
-    };
-
-    struct thumbnail {
-
-        std::string url;
-        int preference;
-        std::string id;
-        std::optional<std::string> resolution;
-        std::optional<int> height;
-        std::optional<int> width;
-    };
-
-    struct video {
-
-        std::string id;
-        std::string title;
-        std::string url;
-        std::string channelId;
-        std::string channelUrl;
-        std::vector<format> formats;
-        std::vector<thumbnail> thumbnails;
-        std::optional<std::string> uploader;
-        std::optional<std::string> uploaderId;
-        std::optional<std::string> uploaderUrl;
-        std::optional<int> duration;
-        std::optional<int> viewcount;
-        std::optional<std::string> description;
-    };
-
-    void to_json(nlohmann::json& j, const format& f);
-    void to_json(nlohmann::json& j, const thumbnail& t);
-    void to_json(nlohmann::json& j, const video& v);
-    void from_json(const nlohmann::json& j, format& f);
-    void from_json(const nlohmann::json& j, thumbnail& t);
-    void from_json(const nlohmann::json& j, video& v);
-};
-
 class YtdlpWrapper {
 
     public:
diff --git a/src/invapi.cpp b/src/invapi.cpp
index 1433c10..3f6a7c2 100644
--- a/src/invapi.cpp
+++ b/src/invapi.cpp
@@ -6,6 +6,7 @@
 #include <nlohmann/json.hpp>
 
 #include "invapi.hpp"
+#include "video.hpp"
 
 using namespace std;
 using json = nlohmann::json;
diff --git a/src/main.cpp b/src/main.cpp
index 02afb7b..100d2b5 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,8 +8,10 @@
 // #include <vector>
 #include <fstream>
 #include <filesystem>
+#include <stdlib.h>
 
 #include "tui.hpp"
+#include "video.hpp"
 #include "invapi.hpp"
 #include "YtdlpWrapper.hpp"
 #include "sqliteinterface.hpp"
@@ -37,7 +39,7 @@ void validateStructConversions() {
         
         if (i.key() != "formats") {
             
-            assert(j.at(i.key()) == info.at(i.key()));
+            assert(i.value() == info.at(i.key()));
             cout << i.key() << ": " << i.value() << endl;
         }
     }
@@ -49,10 +51,20 @@ void validateStructConversions() {
             if (k.key() == "quality" || k.key() == "resolution") cout << k.key() << ": " << k.value() << endl;
         }
     }
-} 
+}
+
+void parseSysArgs(int argc, char **argv) {
+
+    for (int i{0}; i < argc; ++i) {
+        cout << *(argv + i) << endl;
+    }
+}
 
 int main(int argc, char **argv) {
 
+    parseSysArgs(argc, argv);
+    
+
     // validateStructConversions();
     
     // YtdlpWrapper yt;
diff --git a/src/video.cpp b/src/video.cpp
new file mode 100644
index 0000000..64bf526
--- /dev/null
+++ b/src/video.cpp
@@ -0,0 +1,149 @@
+#include "video.hpp"
+
+using json = nlohmann::json;
+
+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 std::vector<format>& v) {
+
+    j = json::array();
+
+    for (format f: v) {
+
+        j.emplace_back(json(f));
+    }
+}
+
+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 std::vector<thumbnail>& v) {
+
+    j = json::array();
+
+    for (thumbnail t: v) {
+
+        j.emplace_back(json(t));
+    }
+}
+
+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, std::vector<format>& v) {
+
+    v.clear();
+    for (json f: j) {
+
+        v.push_back(f.get<format>());
+    }
+}
+
+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, std::vector<thumbnail>& v) {
+
+    v.clear();
+    for (json t: j) {
+
+        v.push_back(t.get<thumbnail>());
+    }
+}
+
+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")) {
+
+        v.formats.push_back(f.get<format>());
+    }
+
+    for (json t: j.at("thumbnails")) {
+        
+        v.thumbnails.push_back(t.get<thumbnail>());
+    }
+}
\ No newline at end of file
diff --git a/src/video.hpp b/src/video.hpp
new file mode 100644
index 0000000..577606a
--- /dev/null
+++ b/src/video.hpp
@@ -0,0 +1,61 @@
+#ifndef VIDEO_H
+#define VIDEO_H
+#include <string>
+#include <optional>
+#include <vector>
+#include <nlohmann/json.hpp>
+
+namespace Video {
+
+    struct format {
+    
+        std::string format;
+        std::string url;
+        std::string vcodec;
+        std::string acodec;
+        std::string ext;
+        std::optional<float> quality;
+        std::optional<std::string> resolution;
+        std::optional<int> height;
+        std::optional<int> width;
+    };
+
+    struct thumbnail {
+
+        std::string url;
+        int preference;
+        std::string id;
+        std::optional<std::string> resolution;
+        std::optional<int> height;
+        std::optional<int> width;
+    };
+
+    struct video {
+
+        std::string id;
+        std::string title;
+        std::string url;
+        std::string channelId;
+        std::string channelUrl;
+        std::vector<format> formats;
+        std::vector<thumbnail> thumbnails;
+        std::optional<std::string> uploader;
+        std::optional<std::string> uploaderId;
+        std::optional<std::string> uploaderUrl;
+        std::optional<int> duration;
+        std::optional<int> viewcount;
+        std::optional<std::string> description;
+    };
+
+    void to_json(nlohmann::json& j, const format& f);
+    void to_json(nlohmann::json& j, const std::vector<format>& v);
+    void to_json(nlohmann::json& j, const thumbnail& t);
+    void to_json(nlohmann::json& j, const std::vector<thumbnail>& v);
+    void to_json(nlohmann::json& j, const video& v);
+    void from_json(const nlohmann::json& j, format& f);
+    void from_json(const nlohmann::json& j, const std::vector<format>& v);
+    void from_json(const nlohmann::json& j, thumbnail& t);
+    void from_json(const nlohmann::json& j, const std::vector<thumbnail>& v);
+    void from_json(const nlohmann::json& j, video& v);
+};
+#endif
\ No newline at end of file