2023-06-14 23:03:15 -06:00
|
|
|
/*
|
|
|
|
* 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>
|
2023-06-15 13:20:05 -06:00
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
#define SEARX_QUERY_JSON "search?format=json&q="
|
2023-06-14 23:03:15 -06:00
|
|
|
|
|
|
|
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;
|
2023-06-15 13:20:05 -06:00
|
|
|
size_t old_len;
|
2023-06-14 23:03:15 -06:00
|
|
|
|
|
|
|
if (data->len == 0) {
|
|
|
|
data->len = nmemb;
|
|
|
|
data->data = (char *) malloc(nmemb);
|
|
|
|
memcpy(data->data, buffer, nmemb);
|
|
|
|
} else {
|
2023-06-15 13:20:05 -06:00
|
|
|
old_len = data->len;
|
2023-06-14 23:03:15 -06:00
|
|
|
data->len += nmemb;
|
|
|
|
data->data = (char *) realloc(data->data, data->len);
|
|
|
|
memcpy(data->data + old_len, buffer, nmemb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nmemb;
|
|
|
|
}
|
|
|
|
|
2023-06-15 13:20:05 -06:00
|
|
|
curl_read_data_t search(char *argv[], char *term) {
|
2023-06-14 23:03:15 -06:00
|
|
|
char *url = "https://searx.mxchange.org/";
|
2023-06-15 13:20:05 -06:00
|
|
|
size_t buf_size = sizeof(SEARX_QUERY_JSON) + strlen(url) + strlen(term);
|
2023-06-14 23:03:15 -06:00
|
|
|
char *search_url = malloc(buf_size);
|
2023-06-15 13:20:05 -06:00
|
|
|
char *error;
|
|
|
|
CURL *handle;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
snprintf(search_url, buf_size, "%s%s%s", url, SEARX_QUERY_JSON, term);
|
2023-06-14 23:03:15 -06:00
|
|
|
|
|
|
|
curl_read_data_t json;
|
|
|
|
json.len = 0;
|
|
|
|
json.data = NULL;
|
|
|
|
|
2023-06-15 13:20:05 -06:00
|
|
|
handle = curl_easy_init();
|
2023-06-14 23:03:15 -06:00
|
|
|
|
|
|
|
if (handle) {
|
2023-06-15 13:20:05 -06:00
|
|
|
error[CURL_ERROR_SIZE];
|
2023-06-14 23:03:15 -06:00
|
|
|
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);
|
2023-06-15 13:20:05 -06:00
|
|
|
fprintf(stderr, "%s: %s", argv[0], error);
|
2023-06-14 23:03:15 -06:00
|
|
|
} else {
|
2023-06-15 13:20:05 -06:00
|
|
|
fprintf(stderr, "%s: failed to initialize curl.\n");
|
2023-06-14 23:03:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2023-06-15 13:20:05 -06:00
|
|
|
void parse_json(char *argv[], char* json) {
|
|
|
|
int out;
|
|
|
|
json_error_t *error;
|
|
|
|
json_t *json_val = json_loads(json, JSON_DECODE_ANY, error);
|
|
|
|
|
|
|
|
out = json_unpack(json_val, "{s:o}");
|
2023-06-14 23:03:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2023-06-15 13:20:05 -06:00
|
|
|
curl_read_data_t json;
|
2023-06-14 23:03:15 -06:00
|
|
|
|
|
|
|
if (argc < 1) {
|
2023-06-15 13:20:05 -06:00
|
|
|
fprintf(stderr, "Usage: %s terms...", argv[0]);
|
2023-06-14 23:03:15 -06:00
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
2023-06-15 13:20:05 -06:00
|
|
|
json = search(argv, argv[i]);
|
2023-06-14 23:03:15 -06:00
|
|
|
write(1, json.data, json.len);
|
2023-06-15 13:20:05 -06:00
|
|
|
parse_json(argv, json.data);
|
2023-06-14 23:03:15 -06:00
|
|
|
}
|
|
|
|
return EX_OK;
|
|
|
|
}
|