Image works, and looks okay. Added more output.

This commit is contained in:
Micheal Smith 2025-12-21 13:54:37 -06:00
parent b0e34a58ea
commit 5a0ffa95fe
No known key found for this signature in database
GPG key ID: E40750BFE6702504
3 changed files with 51 additions and 20 deletions

View file

@ -169,11 +169,12 @@ static void render_and_print_image(unsigned char *data, int width, int height,
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
strerror_r(errno, err_buf, sizeof err_buf); strerror_r(errno, err_buf, sizeof err_buf);
std::cerr << "Couldn't get terminal dimensions: " << err_buf << std::endl; 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; unsigned char *output_data = data;
int output_width = width; int output_width = width;
int output_height = height; int output_height = height;

View file

@ -36,4 +36,4 @@ exe = executable(
install : true, install : true,
) )
test('basic', exe) test('basic', exe, args : [ '-k', 'API_KEY', 'Jack Frost' ])

62
omdb.c
View file

@ -17,18 +17,18 @@
#define PROJECT_NAME "omdb" #define PROJECT_NAME "omdb"
const char *TITLE_LOOKUP_KEYS[] = { const char *TITLE_LOOKUP_KEYS[] = {
"Actors", "Awards", "BoxOffice", "Country", "DVD", "Director", "Actors", "Awards", "BoxOffice", "Country", "DVD",
"Genre", "Language", "Metascore", "Plot", "Production", "Director", "Genre", "Language", "Metascore", "Plot",
"Rated", "Ratings", "Released", "Response", "Runtime", "Title", "Production", "Rated", "Ratings", "Released", "Response",
"Type", "Website", "Writer", "Year", "imdbID", "imdbRating", "Runtime", "Title", "Type", "Website", "Writer",
"imdbVotes", NULL}; "Year", "imdbID", "imdbRating", "imdbVotes", NULL};
/* Default to plain output. */ /* Default to plain output. */
static enum _output_format { static enum _output_format {
OUTPUT_JSON, OUTPUT_JSON,
OUTPUT_PLAIN, OUTPUT_PLAIN,
OUTPUT_FANCY OUTPUT_FANCY
} output_format = OUTPUT_PLAIN; } output_format = OUTPUT_FANCY;
static struct option opts[] = {{"api-key", required_argument, NULL, 'k'}, static struct option opts[] = {{"api-key", required_argument, NULL, 'k'},
{"id", no_argument, NULL, 'i'}, {"id", no_argument, NULL, 'i'},
@ -113,15 +113,37 @@ int verify_title_fields(const cJSON *restrict json) {
const char **key = TITLE_LOOKUP_KEYS; const char **key = TITLE_LOOKUP_KEYS;
while (*key != NULL) { while (*key != NULL) {
if (cJSON_GetObjectItem(json, *key) == NULL) { if (cJSON_GetObjectItem(json, *key) == NULL) {
fprintf(stderr, "Missing field '%s' in JSON.\n", *key); fprintf(stderr, "Missing field '%s' in JSON.\n", *key);
return -1; 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; return 0;
} }
@ -137,21 +159,29 @@ int print_title_plain(const cJSON *restrict json) {
printf("%s [%s]\n", title->valuestring, rated->valuestring); printf("%s [%s]\n", title->valuestring, rated->valuestring);
printf("Released on %s\n", released->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; return 0;
} }
int print_title_fancy(const cJSON *restrict json) { 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"); cJSON *poster = cJSON_GetObjectItem(json, "Poster");
if (poster != NULL) { if (poster != NULL) {
struct fetch *f = fetch_init(); struct fetch *f = fetch_init();
CURLU *url = init_curl_url(poster->valuestring); CURLU *url = init_curl_url(poster->valuestring);
if (url != NULL) { if (url != NULL) {
struct response *resp = fetch(f, url); struct response *resp = fetch(f, url);
if (resp != NULL) { if (resp != NULL) {
process_image_mem((const unsigned char *)resp->data, resp->size); process_image_mem((const unsigned char *)resp->data, resp->size);
response_cleanup(resp); response_cleanup(resp);
@ -159,7 +189,7 @@ int print_title_fancy(const cJSON *restrict json) {
} }
} }
} }
return print_title_plain(json); return print_title_plain(json);
} }
@ -190,7 +220,7 @@ int handle_response(struct response *restrict resp) {
break; break;
case OUTPUT_FANCY: case OUTPUT_FANCY:
/* Just print_title + image planned here for now */ /* Just print_title + image planned here for now */
print_title_plain(json); print_title_fancy(json);
break; break;
} }
@ -215,7 +245,7 @@ int main(int argc, char **argv) {
if (url == NULL) { if (url == NULL) {
fputs("Can't set url for retrieval.\n", stderr); fputs("Can't set url for retrieval.\n", stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while ((ch = getopt_long(argc, argv, "hijk:ft:y:", opts, NULL)) != -1) { while ((ch = getopt_long(argc, argv, "hijk:ft:y:", opts, NULL)) != -1) {
switch (ch) { switch (ch) {