diff --git a/Makefile b/Makefile index c1b4188..9114482 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS= -c -g -Wall -LDLIBS = -lcurl +LDLIBS = -lcurl -lcjson TARGET := isbn-lookup diff --git a/build/book.json b/build/book.json new file mode 100644 index 0000000..4144a2c --- /dev/null +++ b/build/book.json @@ -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" +} \ No newline at end of file diff --git a/build/isbn-lookup b/build/isbn-lookup index 9d675a7..14bcd9f 100755 Binary files a/build/isbn-lookup and b/build/isbn-lookup differ diff --git a/build/src/curl.c.o b/build/src/curl.c.o index 4e83f40..863b0c9 100644 Binary files a/build/src/curl.c.o and b/build/src/curl.c.o differ diff --git a/build/src/json.c.o b/build/src/json.c.o new file mode 100644 index 0000000..8fbdc1d Binary files /dev/null and b/build/src/json.c.o differ diff --git a/build/src/main.c.o b/build/src/main.c.o index 3a8acfd..cb0967b 100644 Binary files a/build/src/main.c.o and b/build/src/main.c.o differ diff --git a/src/db.c b/src/db.c new file mode 100644 index 0000000..092f319 --- /dev/null +++ b/src/db.c @@ -0,0 +1,3 @@ +#include "include/db.h" + + diff --git a/src/include/db.h b/src/include/db.h new file mode 100644 index 0000000..75a2196 --- /dev/null +++ b/src/include/db.h @@ -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 diff --git a/src/main.c b/src/main.c index 3e18e60..486154e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,82 @@ -#include +#include +#include +#include +#include #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[] = ""; +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; }