about summary refs log tree commit diff stats
path: root/src/invapi.cpp
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-05-15 16:43:57 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-05-15 16:43:57 -0700
commitef43068ce29233115cff2bdac11fc882a71ed107 (patch)
treea5fae21897a3065804a615936b0c2e730b3ffd2e /src/invapi.cpp
parent0bde4573e5301f682d438856c4622209549718c9 (diff)
downloadytcpp-ef43068ce29233115cff2bdac11fc882a71ed107.tar.gz
db updates
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;