searpent/searpent.c

107 lines
2.7 KiB
C

/*
* Copyright (c) 2023 Emma Tebibyte
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <curl/curl.h>
#include "jsmn.h"
typedef struct curl_read_data_t {
size_t len;
char* data;
} curl_read_data_t;
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
curl_read_data_t *data = (curl_read_data_t *)userp;
printf("recv'd %d\n", nmemb);
if (data->len == 0) {
data->len = nmemb;
data->data = (char *) malloc(nmemb);
memcpy(data->data, buffer, nmemb);
} else {
size_t old_len = data->len;
data->len += nmemb;
data->data = (char *) realloc(data->data, data->len);
memcpy(data->data + old_len, buffer, nmemb);
}
return nmemb;
}
curl_read_data_t search(char *term) {
char *url = "https://searx.mxchange.org/";
size_t buf_size = strlen(url) + strlen(term) + 32;
char *search_url = malloc(buf_size);
snprintf(search_url, buf_size, "%ssearch?q=%s&format=json", url, term);
curl_read_data_t json;
json.len = 0;
json.data = NULL;
CURL *handle = curl_easy_init();
if (handle) {
CURLcode res;
char error[CURL_ERROR_SIZE];
curl_easy_setopt(handle, CURLOPT_URL, search_url);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &json);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error);
res = curl_easy_perform(handle);
curl_easy_cleanup(handle);
puts(error);
} else {
puts("failed to init curl\n");
}
return json;
}
void parse_json(curl_read_data_t json) {
jsmn_parser parser;
jsmntok_t tokens[json.len];
jsmn_init(&parser);
int tokens_parsed = jsmn_parse(
&parser,
json.data,
json.len,
tokens,
json.len
);
}
int main(int argc, char *argv[]) {
if (argc < 1) {
printf("Usage: %s terms...", argv[0]);
return EX_USAGE;
}
for (int i = 1; i < argc; i++) {
curl_read_data_t json = search(argv[i]);
printf("total recv'd: %d\n", json.len);
write(1, json.data, json.len);
parse_json(json);
}
return EX_OK;
}