about summary refs log tree commit diff stats
path: root/src/invapi.cpp
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-08-30 19:12:22 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-08-30 19:12:22 -0700
commit12df82c1a90208a822ebdf3d5f4994d55ef7cbea (patch)
treef4e55f053778a637803dd492ae1ac0dcd21ec309 /src/invapi.cpp
parentfd350c39ce8dab1ebb6bca94abf25e2e782086b3 (diff)
downloadytcpp-12df82c1a90208a822ebdf3d5f4994d55ef7cbea.tar.gz
update
Diffstat (limited to 'src/invapi.cpp')
-rw-r--r--src/invapi.cpp38
1 files changed, 30 insertions, 8 deletions
diff --git a/src/invapi.cpp b/src/invapi.cpp
index f53c06b..e778f45 100644
--- a/src/invapi.cpp
+++ b/src/invapi.cpp
@@ -12,13 +12,7 @@
 using namespace std;
 using json = nlohmann::json;
 
-InvidiousApi::InvidiousApi(const string& url) {
-
-    instanceUrl = url;
-    
-}
-
-vector<string> InvidiousApi::getInstances() {
+vector<string> InvidiousApi::getInstancesUrls() {
 
     vector<string> result;
     cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
@@ -40,6 +34,34 @@ vector<string> InvidiousApi::getInstances() {
     }
     return result;
 }
+vector<InvidiousApi::instance> InvidiousApi::getInstances() {
+
+    vector<InvidiousApi::instance> result;
+    cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
+        cpr::Parameters{{"sort_by", "health"}},
+        cpr::Timeout{5000});
+
+    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") 
+            && 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 health = i[1]["monitor"]["30dRatio"]["ratio"];
+                result.push_back( {i[1]["uri"], stof(health), i[1]["region"]} );
+        }
+    }
+    return result;
+}
 
 void InvidiousApi::saveInstancesToDb(SqliteInterface& sqldb) {
 
@@ -67,7 +89,7 @@ void InvidiousApi::saveInstancesToDb(SqliteInterface& sqldb) {
                 string region = i[1]["region"];
                 sqldb.saveInstance(uri.c_str(), stof(health), region.c_str());
         }
-    }    
+    }
 }
 
 void InvidiousApi::test() {