about summary refs log tree commit diff stats
path: root/src/invapi.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/invapi.cpp')
-rw-r--r--src/invapi.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/invapi.cpp b/src/invapi.cpp
index 2b63c7e..3fd6632 100644
--- a/src/invapi.cpp
+++ b/src/invapi.cpp
@@ -7,6 +7,7 @@
 
 #include "invapi.hpp"
 #include "video.hpp"
+#include "sqliteinterface.hpp"
 
 using namespace std;
 using json = nlohmann::json;
@@ -20,7 +21,7 @@ vector<string> InvidiousApi::getInstances() {
 
     vector<string> result;
     cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
-        cpr::Parameters{{"sort_by", "health"}});
+        cpr::Parameters{{"sort_by", "health"}, {"fields", "uri,api"}});
 
     if (r.status_code > 299) {
         
@@ -38,6 +39,26 @@ vector<string> InvidiousApi::getInstances() {
     return result;
 }
 
+void InvidiousApi::saveInstancesToDb(SqliteInterface& sqldb) {
+
+    cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
+        cpr::Parameters{{"sort_by", "health"}, {"fields", "uri,api,health,location"}});
+
+    if (r.status_code > 299) {
+        
+        cerr << "invidious.io returned error code " << r.status_code << "!!" << endl;
+        throw runtime_error("bad response");
+    }
+
+    json j = json::parse(r.text);
+    for ( json i: j ) {
+        if ( i.at(1).at("api") != json::value_t::null && i.at(1).at("api")) {
+
+            sqldb.saveInstance(i[1]["uri"], i[1]["health"], i[1]["location"]);
+        }
+    }    
+}
+
 std::string InvidiousApi::getInstance() {
 
     return instanceUrl;