diff options
author | ComradeCrow <comradecrow@vivaldi.net> | 2023-05-15 16:43:57 -0700 |
---|---|---|
committer | ComradeCrow <comradecrow@vivaldi.net> | 2023-05-15 16:43:57 -0700 |
commit | ef43068ce29233115cff2bdac11fc882a71ed107 (patch) | |
tree | a5fae21897a3065804a615936b0c2e730b3ffd2e | |
parent | 0bde4573e5301f682d438856c4622209549718c9 (diff) | |
download | ytcpp-ef43068ce29233115cff2bdac11fc882a71ed107.tar.gz |
db updates
-rw-r--r-- | src/invapi.cpp | 23 | ||||
-rw-r--r-- | src/invapi.hpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 7 | ||||
-rw-r--r-- | src/sqliteinterface.cpp | 14 | ||||
-rw-r--r-- | src/sqliteinterface.hpp | 1 |
5 files changed, 41 insertions, 8 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; diff --git a/src/invapi.hpp b/src/invapi.hpp index c1e14ce..3f07436 100644 --- a/src/invapi.hpp +++ b/src/invapi.hpp @@ -2,12 +2,16 @@ #define INVAPI_H #include <string> #include <vector> + +#include "sqliteinterface.hpp" + class InvidiousApi { public: InvidiousApi(const std::string& url); static std::vector<std::string> getInstances(); + static void saveInstancesToDb(SqliteInterface& sqldb); std::string getInstance(); void setInstance(const std::string& newUrl); private: diff --git a/src/main.cpp b/src/main.cpp index 2f75391..4bbd6db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,9 +99,9 @@ void spawn(const char* url, bool wait) { void parseSysArgs(int argc, char **argv) { - for (int i{0}; i < argc; ++i) { - cout << argv[i] << " "; - } cout << endl; + // for (int i{0}; i < argc; ++i) { + // cout << argv[i] << " "; + // } cout << endl; string currentArg; for (int i{0}; i < argc; ++i) { @@ -134,5 +134,6 @@ int main(int argc, char **argv) { parseSysArgs(argc, argv); SqliteInterface sqldb; + InvidiousApi::saveInstancesToDb(sqldb); } \ No newline at end of file diff --git a/src/sqliteinterface.cpp b/src/sqliteinterface.cpp index 39bdb3b..c34576b 100644 --- a/src/sqliteinterface.cpp +++ b/src/sqliteinterface.cpp @@ -29,7 +29,6 @@ void SqliteInterface::openDB() } fs::path filename = appdata / "ytcpp.db"; - cout << filename.c_str() << endl; int rc = sqlite3_open(filename.c_str(), &db); if (rc) @@ -60,14 +59,21 @@ static int callback(void *NotUsed, int argc, char **argv, char **azColName) { void SqliteInterface::createTables() { char *errmsg = 0; - int rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS INSTANCES (url TEXT PRIMARY KEY, health, location);", callback, 0, &errmsg); + int rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS INSTANCES (url TEXT PRIMARY KEY, health FLOAT, location TEXT);", callback, 0, &errmsg); if( rc != SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", errmsg); sqlite3_free(errmsg); - } else { - fprintf(stdout, "Table created successfully\n"); } +} + +void SqliteInterface::saveInstance(const string& uri, float health, const string& location) { + char *errmsg = 0; + int rc = sqlite3_exec(db, sqlite3_mprintf("INSERT INTO INSTANCES (%Q, %F.2, %Q)", uri, health, location), callback, 0, &errmsg); + if( rc != SQLITE_OK ){ + fprintf(stderr, "SQL error: %s\n", errmsg); + sqlite3_free(errmsg); + } } void SqliteInterface::saveVideo(const Video::video& vid) { diff --git a/src/sqliteinterface.hpp b/src/sqliteinterface.hpp index 8d7f016..cc4adb4 100644 --- a/src/sqliteinterface.hpp +++ b/src/sqliteinterface.hpp @@ -13,6 +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); private: sqlite3* db; }; |