about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-05-17 23:25:10 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-05-17 23:25:10 -0700
commitdd9811703ad623e6b3ddc51e58ef5cc801f41951 (patch)
tree7c10fbd421919cbf2ff19fd3454445790102e6c1
parent8b4fa2a3b6b1a3d75fd4ae5a61266e159d22e095 (diff)
downloadytcpp-dd9811703ad623e6b3ddc51e58ef5cc801f41951.tar.gz
save to db is working
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/sqliteinterface.hpp2
-rw-r--r--src/invapi.cpp18
-rw-r--r--src/main.cpp6
-rw-r--r--src/sqliteinterface.cpp5
5 files changed, 24 insertions, 8 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 033e0b0..0618420 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -86,4 +86,5 @@ target_link_libraries(${PROJECT_NAME}
 set(CPACK_PACKAGE_VENDOR "ComradeCrow")
 set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE.txt)
 set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md)
+set(PROJECT_SOURCE_DIR "src")
 include(CPack)
\ No newline at end of file
diff --git a/include/sqliteinterface.hpp b/include/sqliteinterface.hpp
index cc4adb4..5c7de4d 100644
--- a/include/sqliteinterface.hpp
+++ b/include/sqliteinterface.hpp
@@ -13,7 +13,7 @@ class SqliteInterface {
         void closeDB();
         void createTables();
         void saveVideo(const Video::video&);
-        void saveInstance(const std::string& uri, float health, const std::string& location);
+        void saveInstance(const char *uri, float health, const char *location);
     private:
         sqlite3* db;
 };
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);