about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/invapi.cpp18
-rw-r--r--src/main.cpp6
-rw-r--r--src/sqliteinterface.cpp5
3 files changed, 22 insertions, 7 deletions
diff --git a/src/invapi.cpp b/src/invapi.cpp
index 59e91bf..f53c06b 100644
--- a/src/invapi.cpp
+++ b/src/invapi.cpp
@@ -22,7 +22,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"}, {"fields", "uri,api"}},
+        cpr::Parameters{{"sort_by", "health"}},
         cpr::Timeout{5000});
 
     if (r.status_code > 299) {
@@ -44,7 +44,7 @@ vector<string> InvidiousApi::getInstances() {
 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"}}, 
+        cpr::Parameters{{"sort_by", "health"}}, 
         cpr::Timeout{5000});
 
     if (r.status_code > 299) {
@@ -55,9 +55,17 @@ void InvidiousApi::saveInstancesToDb(SqliteInterface& sqldb) {
 
     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"]);
+        if ( i.at(1).at("api") != json::value_t::null && i.at(1).at("api") 
+            && i.at(1).at("monitor") != json::value_t::null
+            && i.at(1).at("monitor").at("30dRatio") != json::value_t::null
+            && i.at(1).at("monitor").at("30dRatio").at("ratio") != json::value_t::null
+            && i.at(1).at("uri") != json::value_t::null
+            && i.at(1).at("region") != json::value_t::null) {
+
+                string uri = i[1]["uri"];
+                string health = i[1]["monitor"]["30dRatio"]["ratio"];
+                string region = i[1]["region"];
+                sqldb.saveInstance(uri.c_str(), stof(health), region.c_str());
         }
     }    
 }
diff --git a/src/main.cpp b/src/main.cpp
index ea955f0..178575b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -140,4 +140,10 @@ int main(int argc, char **argv) {
     SqliteInterface sqldb;
     // InvidiousApi invapi("https://httpbin.org/get");
     // invapi.test();
+    // InvidiousApi::saveInstancesToDb(sqldb);
+    // vector<string> instances = InvidiousApi::getInstances();
+    // for (string i: instances) {
+
+    //     cout << i << endl;
+    // }
 }
\ No newline at end of file
diff --git a/src/sqliteinterface.cpp b/src/sqliteinterface.cpp
index c34576b..e59fd8a 100644
--- a/src/sqliteinterface.cpp
+++ b/src/sqliteinterface.cpp
@@ -66,10 +66,11 @@ void SqliteInterface::createTables() {
    }
 }
 
-void SqliteInterface::saveInstance(const string& uri, float health, const string& location) {
+void SqliteInterface::saveInstance(const  char *uri, float health, const  char *location) {
 
     char *errmsg = 0;
-    int rc = sqlite3_exec(db, sqlite3_mprintf("INSERT INTO INSTANCES (%Q, %F.2, %Q)", uri, health, location), callback, 0, &errmsg);
+    char *sqlExec = sqlite3_mprintf("REPLACE INTO INSTANCES VALUES(%Q, '%.3f', %Q);", uri, health, location);
+    int rc = sqlite3_exec(db, sqlExec, callback, 0, &errmsg);
     if( rc != SQLITE_OK ){
         fprintf(stderr, "SQL error: %s\n", errmsg);
         sqlite3_free(errmsg);