From 5a0ffa95fea62f8e4054a979d5254e9bcccc22d6 Mon Sep 17 00:00:00 2001 From: Micheal Smith Date: Sun, 21 Dec 2025 13:54:37 -0600 Subject: [PATCH] Image works, and looks okay. Added more output. --- image.cpp | 7 +++--- meson.build | 2 +- omdb.c | 62 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/image.cpp b/image.cpp index 2009744..b979119 100644 --- a/image.cpp +++ b/image.cpp @@ -169,11 +169,12 @@ static void render_and_print_image(unsigned char *data, int width, int height, if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { strerror_r(errno, err_buf, sizeof err_buf); std::cerr << "Couldn't get terminal dimensions: " << err_buf << std::endl; - } else { - max_width = w.ws_col * 4; - max_height = w.ws_row * 8; } + // TODO: Maybe make this configuratble, but quality suffers at smaller sizes. + max_width = w.ws_col * 4; + max_height = w.ws_row * 8; + unsigned char *output_data = data; int output_width = width; int output_height = height; diff --git a/meson.build b/meson.build index eb4cbd9..3095d31 100644 --- a/meson.build +++ b/meson.build @@ -36,4 +36,4 @@ exe = executable( install : true, ) -test('basic', exe) +test('basic', exe, args : [ '-k', 'API_KEY', 'Jack Frost' ]) diff --git a/omdb.c b/omdb.c index 41f2b67..edde50b 100644 --- a/omdb.c +++ b/omdb.c @@ -17,18 +17,18 @@ #define PROJECT_NAME "omdb" const char *TITLE_LOOKUP_KEYS[] = { - "Actors", "Awards", "BoxOffice", "Country", "DVD", "Director", - "Genre", "Language", "Metascore", "Plot", "Production", - "Rated", "Ratings", "Released", "Response", "Runtime", "Title", - "Type", "Website", "Writer", "Year", "imdbID", "imdbRating", - "imdbVotes", NULL}; + "Actors", "Awards", "BoxOffice", "Country", "DVD", + "Director", "Genre", "Language", "Metascore", "Plot", + "Production", "Rated", "Ratings", "Released", "Response", + "Runtime", "Title", "Type", "Website", "Writer", + "Year", "imdbID", "imdbRating", "imdbVotes", NULL}; /* Default to plain output. */ static enum _output_format { OUTPUT_JSON, OUTPUT_PLAIN, OUTPUT_FANCY -} output_format = OUTPUT_PLAIN; +} output_format = OUTPUT_FANCY; static struct option opts[] = {{"api-key", required_argument, NULL, 'k'}, {"id", no_argument, NULL, 'i'}, @@ -113,15 +113,37 @@ int verify_title_fields(const cJSON *restrict json) { const char **key = TITLE_LOOKUP_KEYS; while (*key != NULL) { - + if (cJSON_GetObjectItem(json, *key) == NULL) { fprintf(stderr, "Missing field '%s' in JSON.\n", *key); return -1; } - key++; - } - + key++; + } + + return 0; +} + +int print_ratings(const cJSON *restrict ratings_obj) { + cJSON *rating = NULL; + + if ((ratings_obj == NULL) || !cJSON_IsArray(ratings_obj)) { + fputs("Ratings either doesn't exist, or can't be parsed.", stderr); + return -1; + } + + cJSON_ArrayForEach(rating, ratings_obj) { + cJSON *r_source = cJSON_GetObjectItem(rating, "Source"); + cJSON *r_rating = cJSON_GetObjectItem(rating, "Value"); + + if ((r_source == NULL) || (r_rating == NULL)) + continue; + + fprintf(stderr, "Rated %s by %s\n", r_rating->valuestring, + r_source->valuestring); + } + return 0; } @@ -137,21 +159,29 @@ int print_title_plain(const cJSON *restrict json) { printf("%s [%s]\n", title->valuestring, rated->valuestring); printf("Released on %s\n", released->valuestring); + print_ratings(cJSON_GetObjectItem(json, "Ratings")); + + cJSON *plot = cJSON_GetObjectItem(json, "Plot"); + printf("\n%s\n", plot->valuestring); + + cJSON *actors = cJSON_GetObjectItem(json, "Actors"); + printf("\nStarring %s\n", actors->valuestring); + return 0; } int print_title_fancy(const cJSON *restrict json) { - /* Bad looking, but attempts to fetch, and show the poster if possible. */ + /* Bad looking, but attempts to fetch, and show the poster if possible. */ cJSON *poster = cJSON_GetObjectItem(json, "Poster"); if (poster != NULL) { - + struct fetch *f = fetch_init(); CURLU *url = init_curl_url(poster->valuestring); if (url != NULL) { struct response *resp = fetch(f, url); - + if (resp != NULL) { process_image_mem((const unsigned char *)resp->data, resp->size); response_cleanup(resp); @@ -159,7 +189,7 @@ int print_title_fancy(const cJSON *restrict json) { } } } - + return print_title_plain(json); } @@ -190,7 +220,7 @@ int handle_response(struct response *restrict resp) { break; case OUTPUT_FANCY: /* Just print_title + image planned here for now */ - print_title_plain(json); + print_title_fancy(json); break; } @@ -215,7 +245,7 @@ int main(int argc, char **argv) { if (url == NULL) { fputs("Can't set url for retrieval.\n", stderr); exit(EXIT_FAILURE); - } + } while ((ch = getopt_long(argc, argv, "hijk:ft:y:", opts, NULL)) != -1) { switch (ch) {