json stuff

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

View File

@ -1,7 +1,7 @@
CC=gcc
CFLAGS= -c -g -Wall
LDLIBS = -lcurl
LDLIBS = -lcurl -lcjson
TARGET := isbn-lookup

17
build/book.json Normal file
View File

@ -0,0 +1,17 @@
{
"author_key": ["OL6823800A"],
"author_name": ["Trisha Telep"],
"cover_edition_key": "OL24447972M",
"cover_i": 8159688,
"edition_count": 2,
"first_publish_year": 2009,
"has_fulltext": true,
"ia": ["mammothbookoftim0000tele"],
"ia_collection_s": "inlibrary;internetarchivebooks;openlibrary-d-ol;printdisabled",
"key": "/works/OL15483381W",
"language": ["eng"],
"lending_edition_s": "OL24447972M",
"lending_identifier_s": "mammothbookoftim0000tele",
"public_scan_b": false,
"title": "The Mammoth Book of Time Travel Romance"
}

Binary file not shown.

Binary file not shown.

BIN
build/src/json.c.o Normal file

Binary file not shown.

Binary file not shown.

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;
}