#include #include #include #include #include #include "invapi.hpp" #include "video.hpp" #include "sqliteinterface.hpp" using namespace std; using json = nlohmann::json; InvidiousApi::InvidiousApi(const string& url) { instanceUrl = url; } vector InvidiousApi::getInstances() { vector result; cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, cpr::Parameters{{"sort_by", "health"}, {"fields", "uri,api"}}, 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")) { result.push_back( i[1]["uri"] ); } } 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"}}, 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")) { sqldb.saveInstance(i[1]["uri"], i[1]["health"], i[1]["location"]); } } } void InvidiousApi::test() { session.SetUrl(cpr::Url{instanceUrl}); session.SetParameters(cpr::Parameters{{"hello", "world"}}); cpr::Response new_r = session.Get(); cout << new_r.url << endl; cout << new_r.text << endl; } std::string InvidiousApi::getInstance() { return instanceUrl; } void InvidiousApi::setInstance(const std::string& newUrl) { instanceUrl = newUrl; }