From 204ab98bea93754beef914fe93ee249e346ee628 Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Mon, 7 Aug 2023 22:52:47 +0800 Subject: hinata: support specifying cookies in curl_easy We use tomlc99 to parse strings. To use cookies for higher resolution video downloading, add a config.toml in the executable path. e.g. ```filename: config.toml cookie="SESSDATA=xxx; some_more_cookie=xxx" ``` --- src/main.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index e9d22f1..ecd9381 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,12 @@ #include #include #include +#include #include #include #include +#include "toml.h" #define MAX_VERTEX_BUFFER 512 * 1024 #define MAX_ELEMENT_BUFFER 128 * 1024 #define NK_INCLUDE_FIXED_TYPES @@ -29,6 +31,7 @@ #include "unifont.h" extern int win_width, win_height; +static char *cookie; extern void load_ui(struct ui_struct *); static void error_callback(int e, const char *d) { printf("Error %d: %s\n", e, d); @@ -40,8 +43,34 @@ int main(void) { /* Set locale*/ setlocale(LC_ALL, ".UTF-8"); + /* Parse config */ + // NOTICE: string parsed from toml should always be freed afterwards!!! + { + FILE *fp; + const char *config_file = "config.toml"; + char errbuf[UCHAR_MAX]; + + fp = fopen(config_file, "r"); + if (!fp) { + fprintf(stderr, "[tomlc99] Cannot open %s, applying default config...\n", + config_file); + } else { + toml_table_t *conf = toml_parse_file(fp, errbuf, sizeof(errbuf)); + fclose(fp); + + if (!conf) { + fprintf(stderr, "[tomlc99] Cannot parse %s\n", config_file); + } + toml_datum_t cookie_datum = toml_string_in(conf, "cookie"); + if (cookie_datum.ok) { + printf("[tomlc99] Found cookie string in config.toml, applying...\n"); + cookie = cookie_datum.u.s; + } + } + } + /* Curl */ - curl_init(); + curl_init(cookie); /* GLFW */ struct nk_glfw glfw = {0}; -- cgit v1.2.3