diff options
author | ComradeCrow <comradecrow@vivaldi.net> | 2023-08-30 19:12:22 -0700 |
---|---|---|
committer | ComradeCrow <comradecrow@vivaldi.net> | 2023-08-30 19:12:22 -0700 |
commit | 12df82c1a90208a822ebdf3d5f4994d55ef7cbea (patch) | |
tree | f4e55f053778a637803dd492ae1ac0dcd21ec309 /src | |
parent | fd350c39ce8dab1ebb6bca94abf25e2e782086b3 (diff) | |
download | ytcpp-12df82c1a90208a822ebdf3d5f4994d55ef7cbea.tar.gz |
update
Diffstat (limited to 'src')
-rw-r--r-- | src/invapi.cpp | 38 | ||||
-rw-r--r-- | src/main.cpp | 94 | ||||
-rw-r--r-- | src/sqliteinterface.cpp | 10 | ||||
-rw-r--r-- | src/ytcpp.cpp | 48 |
4 files changed, 96 insertions, 94 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() { diff --git a/src/main.cpp b/src/main.cpp index 178575b..42ad541 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,9 @@ -// #include <cpr/cpr.h> -#include <nlohmann/json.hpp> -// #include <pybind11/embed.h> -// #include <sqlite3.h> -// #include <string> #include <iostream> -// #include <exception> -// #include <vector> -#include <fstream> -#include <filesystem> #include <stdlib.h> #include <cstring> -#ifdef _WIN32 -#include <windows.h> -#elif (__unix || __posix) -#include <unistd.h> -#include <spawn.h> -#include <sys/wait.h> -#endif +#include <map> +#include <ytcpp.hpp> #include <tui.hpp> #include <video.hpp> #include <invapi.hpp> @@ -30,77 +16,13 @@ using namespace std; using json = nlohmann::json; namespace fs = std::filesystem; -void validateStructConversions() { - fs::path here("info.json"); - cout << fs::absolute(here) << endl; - - ifstream ifs; - ifs.open(fs::absolute(here).c_str(), ifstream::in); - - json info = json::parse(ifs); - info = info["entries"][0]; - - Video::video v = info.get<Video::video>(); - json j = v; - - for (auto& i: j.items()) { - - if (i.key() != "formats") { - - assert(i.value() == info.at(i.key())); - cout << i.key() << ": " << i.value() << endl; - } - } - - for (auto i: j["formats"]) { - - for (auto& k: i.items()) { - - if (k.key() == "quality" || k.key() == "resolution" || k.key() == "url") cout << k.key() << ": " << k.value() << endl; - } - } -} - -void spawn(const char* url, bool wait) { - - #ifdef _WIN32 - - start mpv - bool CreateProcessA() - #elif (__unix || __posix) - - extern char **environ; - pid_t pid{0}; - char *const mpvArgs[] = {strdup("mpv"), strdup(url), nullptr}; - int result = posix_spawnp(&pid, mpvArgs[0], NULL, NULL, mpvArgs, environ); - if (result != 0) { - - cout << "ERROR RUNNING " << mpvArgs[0] << "! Error code " << result << ": " << strerror(result) << endl; - } else { - - cout << "pid: " << pid << endl; - if (wait) { - - do { - if (waitpid(pid, &result, 0) != -1) { - - cout << "Child status: " << WEXITSTATUS(result) << endl; - } else { - - perror("waitpid"); - exit(1); - } - } while (!WIFEXITED(result) && !WIFSIGNALED(result)); - } - } - #endif -} void parseSysArgs(int argc, char **argv) { - + multimap <string, string> args; string currentArg; for (int i{0}; i < argc; ++i) { + if (strncmp(argv[i], "-", 1) == 0) { if (strcmp(argv[i], "--version") == 0) { @@ -141,9 +63,9 @@ int main(int argc, char **argv) { // InvidiousApi invapi("https://httpbin.org/get"); // invapi.test(); // InvidiousApi::saveInstancesToDb(sqldb); - // vector<string> instances = InvidiousApi::getInstances(); - // for (string i: instances) { + vector<InvidiousApi::instance> instances = InvidiousApi::getInstances(); + for (InvidiousApi::instance i: instances) { - // cout << i << endl; - // } + cout << i.url << " " << i.health << " " << i.location << endl; + } } \ No newline at end of file diff --git a/src/sqliteinterface.cpp b/src/sqliteinterface.cpp index 7cf14dc..97a13e6 100644 --- a/src/sqliteinterface.cpp +++ b/src/sqliteinterface.cpp @@ -76,6 +76,16 @@ void SqliteInterface::saveInstance(const char *uri, float health, const char * } } +void SqliteInterface::saveInstance(const InvidiousApi::instance& inst) { + + char *errmsg = 0; + int rc = sqlite3_exec(db, sqlite3_mprintf("REPLACE INTO INSTANCES VALUES(%Q, '%.3f', %Q);", inst.url, inst.health, inst.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/ytcpp.cpp b/src/ytcpp.cpp new file mode 100644 index 0000000..346f94e --- /dev/null +++ b/src/ytcpp.cpp @@ -0,0 +1,48 @@ +#include <iostream> +#include <stdlib.h> +#include <cstring> +#ifdef _WIN32 +#include <windows.h> +#elif (__unix || __posix) +#include <unistd.h> +#include <spawn.h> +#include <sys/wait.h> +#endif + +using namespace std; + +void spawn(const char* url, bool wait) { + + #ifdef _WIN32 + + start mpv + bool CreateProcessA() + #elif (__unix || __posix) + + extern char **environ; + pid_t pid{0}; + char *const mpvArgs[] = {strdup("mpv"), strdup(url), nullptr}; + + int result = posix_spawnp(&pid, mpvArgs[0], NULL, NULL, mpvArgs, environ); + if (result != 0) { + + cout << "ERROR RUNNING " << mpvArgs[0] << "! Error code " << result << ": " << strerror(result) << endl; + } else { + + cout << "pid: " << pid << endl; + if (wait) { + + do { + if (waitpid(pid, &result, 0) != -1) { + + cout << "Child status: " << WEXITSTATUS(result) << endl; + } else { + + perror("waitpid"); + exit(1); + } + } while (!WIFEXITED(result) && !WIFSIGNALED(result)); + } + } + #endif +} \ No newline at end of file |