From 52052dd51f7ccc2aad8f4ecf65cb36672b01a4b5 Mon Sep 17 00:00:00 2001 From: rmuxnet Date: Tue, 9 Jun 2026 01:59:27 +0200 Subject: [PATCH] feat: add GET /api/status, GET /api/search; add LICENSE (MIT 2026 rmuxnet) --- LICENSE | 21 ++++++++++ http/handlers.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ http/handlers.h | 2 + http/server.c | 2 + 4 files changed, 128 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2f5051f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 rmuxnet + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/http/handlers.c b/http/handlers.c index 49e1aca..5c5b7f7 100644 --- a/http/handlers.c +++ b/http/handlers.c @@ -452,3 +452,106 @@ void handle_api_notifications_dismiss(int fd, const char *body) { int found = notify_dismiss(ts); send_json(fd, found ? "{\"ok\":1}" : "{\"ok\":0}"); } + +/* ── GET /api/status ───────────────────────────────────────────── */ +void handle_api_status(int fd) { + /* queue depth */ + int queued = 0, active = 0; + pthread_mutex_lock(&g_dl_mutex); + for (int i = 0; i < g_dl_count; i++) { + if (strcmp(g_downloads[i].status, "queued") == 0) queued++; + if (strcmp(g_downloads[i].status, "downloading") == 0) active++; + } + pthread_mutex_unlock(&g_dl_mutex); + + /* active download name + progress */ + char active_name[256] = ""; int active_pct = 0; long active_speed = 0; + pthread_mutex_lock(&g_dl_mutex); + for (int i = 0; i < g_dl_count; i++) { + if (strcmp(g_downloads[i].status, "downloading") == 0) { + strncpy(active_name, g_downloads[i].name, sizeof(active_name)-1); + active_pct = g_downloads[i].total > 0 + ? (int)(g_downloads[i].downloaded * 100 / g_downloads[i].total) : 0; + active_speed = (long)g_downloads[i].speed_bps; + break; + } + } + pthread_mutex_unlock(&g_dl_mutex); + + /* escape name for JSON */ + char safe[512]; int si = 0; + for (const char *p = active_name; *p && si < (int)sizeof(safe)-2; p++) { + if (*p == '"' || *p == '\\') safe[si++] = '\\'; + safe[si++] = *p; + } + safe[si] = 0; + + Buf b; buf_init(&b); + buf_fmt(&b, + "{\"version\":\"2.0\"," + "\"queue_depth\":%d," + "\"active_downloads\":%d," + "\"total_history\":%d," + "\"active_name\":\"%s\"," + "\"active_pct\":%d," + "\"active_speed_bps\":%ld}", + queued, active, g_dl_count, safe, active_pct, active_speed); + send_json_buf(fd, &b); + buf_free(&b); +} + +/* ── GET /api/search?q=...&type=series|movies ──────────────────── */ +void handle_api_search(int fd, const char *qs) { + char *q = qparam(qs, "q"); + char *type = qparam(qs, "type"); + int is_series = !type || strcmp(type, "movies") != 0; + + if (!q || !q[0]) { + send_json(fd, "{\"error\":\"missing q\"}"); + free(q); free(type); return; + } + + char *json = is_series ? api_get("get_series", "") : api_get("get_vod_streams", ""); + int n; char **arr = json_array(json, &n); + + Buf b; buf_init(&b); + buf_str(&b, "["); + int first = 1; + for (int i = 0; i < n; i++) { + char *name = json_str(arr[i], "name"); + if (!str_icontains(name, q)) { free(name); free(arr[i]); continue; } + if (!first) buf_str(&b, ","); + first = 0; + if (is_series) { + char *sid = json_str(arr[i], "series_id"); + char *cover = json_str(arr[i], "cover"); + buf_fmt(&b, "{\"id\":\"%s\",\"name\":", sid?sid:""); + /* JSON-escape name */ + buf_str(&b, "\""); + for (const char *p = name; *p; p++) { + if (*p=='"'||*p=='\\') buf_str(&b,"\\"); + buf_append(&b, p, 1); + } + buf_fmt(&b, "\",\"type\":\"series\",\"cover\":\"%s\"}", cover?cover:""); + free(sid); free(cover); + } else { + char *vid = json_str(arr[i], "stream_id"); + char *ext = json_str(arr[i], "container_extension"); + char *icon = json_str(arr[i], "stream_icon"); + buf_fmt(&b, "{\"id\":\"%s\",\"name\":", vid?vid:""); + buf_str(&b, "\""); + for (const char *p = name; *p; p++) { + if (*p=='"'||*p=='\\') buf_str(&b,"\\"); + buf_append(&b, p, 1); + } + buf_fmt(&b, "\",\"type\":\"movie\",\"ext\":\"%s\",\"cover\":\"%s\"}", + ext?ext:"mp4", icon?icon:""); + free(vid); free(ext); free(icon); + } + free(name); free(arr[i]); + } + buf_str(&b, "]"); + free(arr); free(json); free(q); free(type); + send_json_buf(fd, &b); + buf_free(&b); +} diff --git a/http/handlers.h b/http/handlers.h index bde760f..44de486 100644 --- a/http/handlers.h +++ b/http/handlers.h @@ -22,3 +22,5 @@ void handle_api_download_movie(int fd, const char *body); void handle_api_notifications(int fd); void handle_api_notifications_test(int fd); void handle_api_notifications_dismiss(int fd, const char *body); +void handle_api_status(int fd); +void handle_api_search(int fd, const char *qs); diff --git a/http/server.c b/http/server.c index fd6bcc2..580d00a 100644 --- a/http/server.c +++ b/http/server.c @@ -22,6 +22,8 @@ void *handle_conn(void *arg) { else if (!strcmp(req.path,"/downloads")) handle_downloads(fd); else if (!strcmp(req.path,"/api/downloads")) handle_api_downloads(fd); else if (!strcmp(req.path,"/api/notifications")) handle_api_notifications(fd); + else if (!strcmp(req.path,"/api/status")) handle_api_status(fd); + else if (!strcmp(req.path,"/api/search")) handle_api_search(fd, req.query); else { /* try static JS files */ send_static_js(fd, req.path);