about summary refs log tree commit diff stats
path: root/src/YtdlpWrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/YtdlpWrapper.cpp')
-rw-r--r--src/YtdlpWrapper.cpp108
1 files changed, 7 insertions, 101 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