From 86ec02fa985112b3ba72ac64f14f452281f29719 Mon Sep 17 00:00:00 2001 From: ComradeCrow Date: Sat, 6 May 2023 23:48:40 -0700 Subject: working on inv again --- src/invapi.cpp | 17 ++++++++- src/invapi.hpp | 13 ++++++- src/main.cpp | 96 +++++++++++++++++++++++++++++++------------------ src/sqliteinterface.cpp | 44 +++++++++++++++++++---- src/sqliteinterface.hpp | 1 + 5 files changed, 128 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/invapi.cpp b/src/invapi.cpp index 3f6a7c2..2b63c7e 100644 --- a/src/invapi.cpp +++ b/src/invapi.cpp @@ -11,7 +11,12 @@ using namespace std; using json = nlohmann::json; -vector getInstances() { +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"}, @@ -31,4 +36,14 @@ vector getInstances() { } } return result; +} + +std::string InvidiousApi::getInstance() { + + return instanceUrl; +} + +void InvidiousApi::setInstance(const std::string& newUrl) { + + instanceUrl = newUrl; } \ No newline at end of file diff --git a/src/invapi.hpp b/src/invapi.hpp index 2d3357c..b7a7d8d 100644 --- a/src/invapi.hpp +++ b/src/invapi.hpp @@ -2,7 +2,18 @@ #define INVAPI_H #include #include +class InvidiousApi { + + public: + + InvidiousApi(const string& url); + static std::vector getInstances(); + std::string getInstance(); + void setInstance(const std::string& newUrl); + private: + + std::string instanceUrl; +}; -std::vector getInstances(); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3ee071a..ec86677 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,14 +9,13 @@ #include #include #include -#ifdef __unix -#include -#include -#elif __posix +#include +#ifdef _WIN32 +#include +#elif (__unix || __posix) #include #include -#elif _WIN32 -#include +#include #endif #include "tui.hpp" @@ -62,39 +61,17 @@ void validateStructConversions() { } } -void parseSysArgs(int argc, char **argv) { - - for (int i{0}; i < argc; ++i) { - cout << *(argv + i) << endl; - } -} - - - -int main(int argc, char **argv) { +void spawn(const char* url, bool wait) { - parseSysArgs(argc, argv); #ifdef _WIN32 + start mpv bool CreateProcessA() - #elif __unix - extern char **environ; - - int i{0}; - while ((environ[i]) != nullptr) { + #elif (__unix || __posix) - cout << environ[i++] << endl; - } + extern char **environ; pid_t pid{0}; - fs::path vidFile("It's Time to Celebrate [K3qjudqERKU].webm"); - char *const mpvArgs[] = {strdup("mpv"), strdup(fs::absolute(vidFile).c_str()), nullptr}; - - i = 0; - while ((mpvArgs[i]) != nullptr) { - - cout << mpvArgs[i++] << " "; - } - cout << endl; + char *const mpvArgs[] = {strdup("mpv"), strdup(url), nullptr}; int result = posix_spawnp(&pid, mpvArgs[0], NULL, NULL, mpvArgs, environ); if (result != 0) { @@ -103,8 +80,59 @@ int main(int argc, char **argv) { } 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) { + + for (int i{0}; i < argc; ++i) { + cout << argv[i] << " "; + } cout << endl; + + string currentArg; + for (int i{0}; i < argc; ++i) { + if (strncmp(argv[i], "-", 1) == 0) { + + currentArg = argv[i]; + } + else { + + if (currentArg == "") { + + cout << argv[i] << endl; + if (strncmp(argv[i], "http://", 7) == 0 || strncmp(argv[i], "https://", 8) == 0) { + + spawn(argv[i], true); + } + } else if (currentArg == "-S" || currentArg == "--search") { + + cout << "searching for: " << argv[i] << endl; + currentArg = ""; + } else { + + cout << currentArg << ": " << argv[i] << endl; + } + } + } +} + +int main(int argc, char **argv) { + + parseSysArgs(argc, argv); + } \ No newline at end of file diff --git a/src/sqliteinterface.cpp b/src/sqliteinterface.cpp index 328a44c..87cb854 100644 --- a/src/sqliteinterface.cpp +++ b/src/sqliteinterface.cpp @@ -8,14 +8,17 @@ namespace fs = std::filesystem; using namespace std; -void SqliteInterface::openDB() { +void SqliteInterface::openDB() +{ fs::path appdata = DEF_APPDATA; - if ( ! fs::exists(appdata) ) { + if (!fs::exists(appdata)) + { error_code ec; fs::create_directories(appdata, ec); - if (ec.value() != 0) { + if (ec.value() != 0) + { char errmsg[] = "Can't create folder: "; strcat(errmsg, ec.message().c_str()); @@ -26,7 +29,8 @@ void SqliteInterface::openDB() { fs::path filename = appdata / "ytcpp.db"; int rc = sqlite3_open(filename.c_str(), &db); - if( rc ) { + if (rc) + { char errmsg[] = "Can't open database: "; strcat(errmsg, sqlite3_errmsg(db)); @@ -34,17 +38,43 @@ void SqliteInterface::openDB() { } } -void SqliteInterface::closeDB() { +void SqliteInterface::closeDB() +{ sqlite3_close(db); } -SqliteInterface::SqliteInterface() { +static int callback(void *NotUsed, int argc, char **argv, char **azColName) { + int i; + for(i = 0; i