about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-05-06 23:48:40 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-05-06 23:48:40 -0700
commit86ec02fa985112b3ba72ac64f14f452281f29719 (patch)
tree6cde7e9d947f65f4552260838140c36d32d2ed34
parent3212791b7ac63f01da8bb115e68c43a36c3b67a7 (diff)
downloadytcpp-86ec02fa985112b3ba72ac64f14f452281f29719.tar.gz
working on inv again
-rw-r--r--src/invapi.cpp17
-rw-r--r--src/invapi.hpp13
-rw-r--r--src/main.cpp96
-rw-r--r--src/sqliteinterface.cpp44
-rw-r--r--src/sqliteinterface.hpp1
5 files changed, 128 insertions, 43 deletions
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<string> getInstances() {
+InvidiousApi::InvidiousApi(const string& url) {
+
+    instanceUrl = url;
+}
+
+vector<string> InvidiousApi::getInstances() {
 
     vector<string> result;
     cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
@@ -31,4 +36,14 @@ vector<string> 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 <string>
 #include <vector>
+class InvidiousApi {
+
+    public:
+
+        InvidiousApi(const string& url);
+        static std::vector<std::string> getInstances();
+        std::string getInstance();
+        void setInstance(const std::string& newUrl);
+    private:
+
+        std::string instanceUrl;
+};
 
-std::vector<std::string> 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 <fstream>
 #include <filesystem>
 #include <stdlib.h>
-#ifdef __unix
-#include <unistd.h>
-#include <spawn.h>
-#elif __posix
+#include <cstring>
+#ifdef _WIN32
+#include <windows.h>
+#elif (__unix || __posix)
 #include <unistd.h>
 #include <spawn.h>
-#elif _WIN32
-#include <windows.h>
+#include <sys/wait.h>
 #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<argc; i++) {
+        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    printf("\n");
+    return 0;
+}
+
+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);
+    if( rc != SQLITE_OK ){
+        fprintf(stderr, "SQL error: %s\n", errmsg);
+        sqlite3_free(errmsg);
+   } else {
+        fprintf(stdout, "Table created successfully\n");
+   }
+
+}
+
+SqliteInterface::SqliteInterface()
+{
 
     openDB();
 }
 
-SqliteInterface::~SqliteInterface() {
+SqliteInterface::~SqliteInterface()
+{
 
     closeDB();
 }
\ No newline at end of file
diff --git a/src/sqliteinterface.hpp b/src/sqliteinterface.hpp
index 5dc25d3..5be355e 100644
--- a/src/sqliteinterface.hpp
+++ b/src/sqliteinterface.hpp
@@ -9,6 +9,7 @@ class SqliteInterface {
         ~SqliteInterface();
         void openDB();
         void closeDB();
+        void createTables();
     private:
         sqlite3* db;
 };