json stuff
This commit is contained in:
parent
d2a8f8d53d
commit
66904b589f
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
CC=gcc
|
||||
CFLAGS= -c -g -Wall
|
||||
|
||||
LDLIBS = -lcurl
|
||||
LDLIBS = -lcurl -lcjson
|
||||
|
||||
TARGET := isbn-lookup
|
||||
|
||||
|
17
build/book.json
Normal file
17
build/book.json
Normal 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
BIN
build/src/json.c.o
Normal file
Binary file not shown.
Binary file not shown.
13
src/include/db.h
Normal file
13
src/include/db.h
Normal 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
|
86
src/main.c
86
src/main.c
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user