From 213402990459eabea74963dad4245610b0931eef Mon Sep 17 00:00:00 2001 From: Micheal Smith Date: Fri, 9 Jan 2026 01:42:40 -0600 Subject: [PATCH] Moved curl convenience functions into fetch.c --- fetch.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- fetch.h | 4 ++++ omdb.c | 49 +++---------------------------------------------- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/fetch.c b/fetch.c index 3ba5c5a..5c8df81 100644 --- a/fetch.c +++ b/fetch.c @@ -7,6 +7,49 @@ static char curl_errbuf[CURL_ERROR_SIZE] = {'\0'}; +CURLU *init_curl_url(const char *restrict ustr) { + /* Just validate and add options directly to the request now. */ + CURLU *url = curl_url(); + CURLUcode ures; + + if (url == NULL) { + fputs("Out of memory.\n", stderr); + exit(EXIT_FAILURE); + } + + if ((ures = curl_url_set(url, CURLUPART_URL, ustr, 0)) != CURLUE_OK) { + fprintf(stderr, "Couldn't set url: %s\n", curl_url_strerror(ures)); + return NULL; + } + + return url; +} + +/* Set a URL query parameter. */ +void set_param(CURLU *restrict url, const char *restrict key, + char *restrict value) { + CURLUcode err; + char param[256]; + + snprintf(param, sizeof param, "%s=%s", key, value); + + if ((err = curl_url_set(url, CURLUPART_QUERY, param, + CURLU_APPENDQUERY | CURLU_URLENCODE)) != CURLUE_OK) { + fprintf(stderr, "Error setting url param '%s': %s\n", key, + curl_url_strerror(err)); + exit(EXIT_FAILURE); + } +} + +/* Just print the url if needed for debugging. */ +void print_url(CURLU *restrict url) { + char *url_string = NULL; + + curl_url_get(url, CURLUPART_URL, &url_string, 0); + printf("%s\n", url_string); + curl_free(url_string); +} + struct fetch *fetch_init(void) { CURLcode res; CURL *handle; @@ -59,7 +102,7 @@ size_t fetch_cb(void *ptr, size_t size, size_t nmemb, void *data) { exit(EXIT_FAILURE); } - printf("Wrote %zd bytes\n", real_size); + // printf("Wrote %zd bytes\n", real_size); memcpy(resp->data + resp->size, ptr, real_size); resp->size += real_size; @@ -94,7 +137,7 @@ struct response *fetch(struct fetch *restrict f, CURLU *restrict url) { &resp->content_type) != CURLE_OK) goto call_fetch_fail; - printf("CONTENT TYPE: %s\n", resp->content_type); + // printf("CONTENT TYPE: %s\n", resp->content_type); return resp; diff --git a/fetch.h b/fetch.h index 7210927..2d5158d 100644 --- a/fetch.h +++ b/fetch.h @@ -19,6 +19,10 @@ struct response { typedef size_t (*CurlWriteCallback)(void *ptr, size_t size, size_t nmemb, void *userdata); +CURLU *init_curl_url(const char *restrict ustr); +void set_param(CURLU *restrict url, const char *restrict key, + char *restrict value); +void print_url(CURLU *restrict url); struct fetch *fetch_init(void); struct response *fetch(struct fetch *restrict f, CURLU *restrict url); void fetch_cleanup(struct fetch *f); diff --git a/omdb.c b/omdb.c index edde50b..8d0b769 100644 --- a/omdb.c +++ b/omdb.c @@ -50,49 +50,6 @@ void print_usage(void) { "\t-y/--year: Year of release.\n"); } -CURLU *init_curl_url(const char *restrict ustr) { - /* Just validate and add options directly to the request now. */ - CURLU *url = curl_url(); - CURLUcode ures; - - if (url == NULL) { - fputs("Out of memory.\n", stderr); - exit(EXIT_FAILURE); - } - - if ((ures = curl_url_set(url, CURLUPART_URL, ustr, 0)) != CURLUE_OK) { - fprintf(stderr, "Couldn't set url: %s\n", curl_url_strerror(ures)); - return NULL; - } - - return url; -} - -/* Set a URL query parameter. */ -void set_param(CURLU *restrict url, const char *restrict key, - char *restrict value) { - CURLUcode err; - char param[256]; - - snprintf(param, sizeof param, "%s=%s", key, value); - - if ((err = curl_url_set(url, CURLUPART_QUERY, param, - CURLU_APPENDQUERY | CURLU_URLENCODE)) != CURLUE_OK) { - fprintf(stderr, "Error setting url param '%s': %s\n", key, - curl_url_strerror(err)); - exit(EXIT_FAILURE); - } -} - -/* Just print the url if needed for debugging. */ -void print_url(CURLU *restrict url) { - char *url_string = NULL; - - curl_url_get(url, CURLUPART_URL, &url_string, 0); - printf("%s\n", url_string); - curl_free(url_string); -} - /* Print entire JSON response as is. */ int print_title_json(const cJSON *const json) { char *json_str = cJSON_Print(json); @@ -288,16 +245,16 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - printf("Looking for: %s\n", argv[optind]); + // printf("Looking for: %s\n", argv[optind]); /* Set remaining params if any... */ set_param(url, "type", search_mode); set_param(url, "r", "json"); - /* Just testing... */ + /* Search for title... */ set_param(url, "t", argv[optind]); /* Print it... */ - print_url(url); + // print_url(url); /* * All of the call setup should be done via the URL so all that's left is to