json stuff

This commit is contained in:
2025-02-28 00:42:27 -05:00
parent d2a8f8d53d
commit 66904b589f
9 changed files with 110 additions and 11 deletions

3
src/db.c Normal file
View File

@ -0,0 +1,3 @@
#include "include/db.h"

13
src/include/db.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef DB_H
#define DB_H
typedef struct {
char *title, *subtitle, *isbn;
char *authors, *subjects, *langs;
char *date_added, *pub_date;
char *image_url;
int year_of_pub, page_len;
} book_t;
#endif

View File

@ -1,16 +1,82 @@
#include <stdio.h>
#include <cjson/cJSON.h>
#include <argp.h>
#include <stdlib.h>
#include <stdbool.h>
#include "include/curl.h"
#include "include/json.h"
int main(void)
struct flags {
bool json, db, print;
};
static void init_flags(struct flags* flags) {flags->json = false;flags->db = false;flags->print = false;}
const char* argp_program_version = "isbn-lookup 2.1";
const char* argp_program_bug_address = "supernovaa41@gmx.com";
static char doc[] = "Lookup details of a book from the ISBN. Using OpenLibrary API.";
static char args_doc[] = "<ISBN>";
static struct argp_option options[] = {
{0, 'j', "json", 0, "output to json", 0},
{0, 'd', "db", 0, "output to sqlite3 database", 0},
{0, 'p', 0, 0, "print the full json response", 0},
{0}
};
static error_t parse_opt(int key, char* arg, struct argp_state* state)
{
get_response resp;
book_api_call("9780762437818", &resp);
puts(resp.buf);
free_get_response(&resp);
struct flags* flags = state->input;
switch (key) {
case 'j':
if (flags->db) {
fprintf(stderr, "-j and -d are mutually exclusive.\n");
exit(EXIT_FAILURE);
}
flags->json = true;
break;
case 'd':
if (flags->json) {
fprintf(stderr, "-j and -d are mutually exclusive.\n");
exit(EXIT_FAILURE);
}
flags->db = true;
break;
case 'p':
flags->print = true;
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, args_doc, doc, 0, 0, 0};
struct flags flags;
int main(int argc, char* argv[])
{
get_response resp;
cJSON* query;
init_flags(&flags);
argp_parse(&argp, argc, argv, 0, 0, &flags);
book_api_call("9780762437818", &resp);
if (flags.print)
puts(resp.buf);
get_json_book(&resp, &query);
free_get_response(&resp);
if (flags.json)
json_out(&query);
else
// db out;
return 0;
}