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 | |
parent | fd350c39ce8dab1ebb6bca94abf25e2e782086b3 (diff) | |
download | ytcpp-12df82c1a90208a822ebdf3d5f4994d55ef7cbea.tar.gz |
update
-rw-r--r-- | CMakeLists.txt | 13 | ||||
-rw-r--r-- | description | 1 | ||||
-rw-r--r-- | include/invapi.hpp | 12 | ||||
-rw-r--r-- | include/sqliteinterface.hpp | 2 | ||||
-rw-r--r-- | include/ytcpp.hpp | 1 | ||||
-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 |
9 files changed, 117 insertions, 102 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0618420..41e5488 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,10 +10,12 @@ if(PROJECT_IS_TOP_LEVEL) include(CTest) enable_testing() endif() -# set(PROJECT_SOURCE_DIR "src") +set(ytcpp_HOME_DIR ${PROJECT_SOURCE_DIR}) +set(PROJECT_SOURCE_DIR "src") + set(PROJECT_INCLUDE_DIR "include") -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake) +set(EXECUTABLE_OUTPUT_PATH ${ytcpp_HOME_DIR}/bin) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ytcpp_HOME_DIR}/cmake) if(WIN32) # Install dlls in the same directory as the executable on Windows set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -84,7 +86,6 @@ target_link_libraries(${PROJECT_NAME} # set(CPACK_PROJECT_NAME ${PROJECT_NAME}) # set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) 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") +set(CPACK_RESOURCE_FILE_LICENSE ${ytcpp_HOME_DIR}/LICENSE.txt) +set(CPACK_RESOURCE_FILE_README ${ytcpp_HOME_DIR}/README.md) include(CPack) \ No newline at end of file diff --git a/description b/description new file mode 100644 index 0000000..6ccb18d --- /dev/null +++ b/description @@ -0,0 +1 @@ +a small test with CPP, yt-dlp, and invidious.io diff --git a/include/invapi.hpp b/include/invapi.hpp index 3fb4c05..bcd5eef 100644 --- a/include/invapi.hpp +++ b/include/invapi.hpp @@ -9,10 +9,18 @@ class InvidiousApi { public: + struct instance { - InvidiousApi(const std::string& url); - static std::vector<std::string> getInstances(); + std::string url; + float health; + std::string location; + }; + + InvidiousApi(const std::string& url) : instanceUrl(url) {}; + static std::vector<std::string> getInstancesUrls(); + static std::vector<instance> getInstances(); static void saveInstancesToDb(SqliteInterface& sqldb); + static std::string getInstanceUrlFromDb(SqliteInterface& sqldb); std::string getInstance(); void setInstance(const std::string& newUrl); diff --git a/include/sqliteinterface.hpp b/include/sqliteinterface.hpp index 5c7de4d..42965d2 100644 --- a/include/sqliteinterface.hpp +++ b/include/sqliteinterface.hpp @@ -3,6 +3,7 @@ #include <string> #include <sqlite3.h> #include "video.hpp" +#include "invapi.hpp" class SqliteInterface { @@ -14,6 +15,7 @@ class SqliteInterface { void createTables(); void saveVideo(const Video::video&); void saveInstance(const char *uri, float health, const char *location); + void saveInstance(const InvidiousApi::instance&); private: sqlite3* db; }; diff --git a/include/ytcpp.hpp b/include/ytcpp.hpp new file mode 100644 index 0000000..2e32ecc --- /dev/null +++ b/include/ytcpp.hpp @@ -0,0 +1 @@ +void spawn(const char* url, bool wait); \ No newline at end of file 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 |