Compare commits
10 Commits
25d4b373ce
...
testing
Author | SHA1 | Date | |
---|---|---|---|
4851e19159 | |||
3d6f78e29a | |||
3513858264 | |||
a2d4e27d73 | |||
2052137673 | |||
7d8c8c6a80 | |||
66c9c45c93 | |||
5027238879 | |||
5392d6c936 | |||
e879a947d8 |
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS= -c -g -Wall
|
CFLAGS= -c -g -Wall
|
||||||
|
|
||||||
LDLIBS = -lcurl -lcjson
|
LDLIBS = -lcurl -lcjson -lsqlite3 -lm
|
||||||
|
|
||||||
TARGET := isbn-lookup
|
TARGET := isbn-lookup
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
#include "include/curl.h"
|
#include "include/curl.h"
|
||||||
|
|
||||||
#define API_URL "https://openlibrary.org/search.json?q="
|
#define API_URL "https://openlibrary.org/search.json?page=1&q="
|
||||||
#define API_URL_LEN 38
|
#define API_URL_LEN 45
|
||||||
|
|
||||||
static void setup_api_link(const char* isbn, char** buf)
|
static void setup_api_link(const char* isbn, char** buf)
|
||||||
{
|
{
|
||||||
|
164
src/db.c
164
src/db.c
@ -1,3 +1,167 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "include/db.h"
|
#include "include/db.h"
|
||||||
|
|
||||||
|
static void open_db(sqlite3** db)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = sqlite3_open("books.db", db);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "Cannot open db: %s\n", sqlite3_errmsg(*db));
|
||||||
|
sqlite3_close(*db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_num_rows(sqlite3* db)
|
||||||
|
{
|
||||||
|
int rows = 0;
|
||||||
|
|
||||||
|
sqlite3_stmt* msg;
|
||||||
|
sqlite3_prepare(db, "SELECT * FROM books;", -1, &msg, NULL);
|
||||||
|
while (sqlite3_step(msg) == SQLITE_ROW)
|
||||||
|
rows++;
|
||||||
|
sqlite3_finalize(msg);
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_db(sqlite3* db)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char* err_buf = 0;
|
||||||
|
|
||||||
|
open_db(&db);
|
||||||
|
|
||||||
|
err = sqlite3_exec(db,
|
||||||
|
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
|
||||||
|
title TEXT, subtitle TEXT, isbn TEXT, authors TEXT, subjects TEXT, \
|
||||||
|
langs TEXT, date_added TEXT, pub_date TEXT, \
|
||||||
|
image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
|
||||||
|
0, 0, &err_buf);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "sqlite3_exec: %s\n", err_buf);
|
||||||
|
sqlite3_free(err_buf);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_str_entry(char** dst, char** src, bool last)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
len = strlen(*src) + strlen(*dst) + 2 + 1 + (last ? 0 : 2);
|
||||||
|
|
||||||
|
*dst = realloc(*dst, sizeof(char) * len);
|
||||||
|
|
||||||
|
*dst = strcat(*dst, "\"");
|
||||||
|
*dst = strcat(*dst, *src);
|
||||||
|
*dst = strcat(*dst, "\"");
|
||||||
|
if (!last)
|
||||||
|
*dst = strcat(*dst, ", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_int_entry(char** dst, int src, bool last)
|
||||||
|
{
|
||||||
|
size_t len_int, len;
|
||||||
|
char* num;
|
||||||
|
|
||||||
|
len_int = (src == 0) ? 1 : floor(log10(abs(src))) + 1;
|
||||||
|
|
||||||
|
num = malloc(sizeof(char) * (len_int + 1));
|
||||||
|
snprintf(num, len_int + 1, "%d", src);
|
||||||
|
|
||||||
|
len = strlen(*dst) + len_int + 1 + (last ? 0 : 2);
|
||||||
|
*dst = realloc(*dst, sizeof(char) * len);
|
||||||
|
*dst = strcat(*dst, num);
|
||||||
|
|
||||||
|
if (!last)
|
||||||
|
strcat(*dst, ", ");
|
||||||
|
|
||||||
|
free(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void string_cat_vals(char** dst, book_t* book, int id)
|
||||||
|
{
|
||||||
|
add_int_entry(dst, id, false);
|
||||||
|
add_str_entry(dst, &(book->title), false);
|
||||||
|
add_str_entry(dst, &(book->subtitle), false);
|
||||||
|
add_str_entry(dst, &(book->isbn), false);
|
||||||
|
add_str_entry(dst, &(book->authors), false);
|
||||||
|
add_str_entry(dst, &(book->subjects), false);
|
||||||
|
add_str_entry(dst, &(book->langs), false);
|
||||||
|
add_str_entry(dst, &(book->date_added), false);
|
||||||
|
add_str_entry(dst, &(book->pub_date), false);
|
||||||
|
add_str_entry(dst, &(book->image_code), false);
|
||||||
|
add_int_entry(dst, book->page_len, true);
|
||||||
|
|
||||||
|
*dst = realloc(*dst, sizeof(char) * (strlen(*dst) + 3));
|
||||||
|
strcat(*dst, ");");
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, pub_date, image_code, page_len) VALUES("
|
||||||
|
|
||||||
|
void add_to_db(book_t* book, sqlite3* db)
|
||||||
|
{
|
||||||
|
int id, err;
|
||||||
|
char* cmd;
|
||||||
|
char* err_msg = 0;
|
||||||
|
|
||||||
|
open_db(&db);
|
||||||
|
|
||||||
|
id = get_num_rows(db) + 4;
|
||||||
|
|
||||||
|
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
|
||||||
|
snprintf(cmd, strlen(INSERT_STR) + 1, "%s", INSERT_STR);
|
||||||
|
|
||||||
|
string_cat_vals(&cmd, book, id);
|
||||||
|
|
||||||
|
printf("%s\n", cmd);
|
||||||
|
|
||||||
|
err = sqlite3_exec(db, cmd, 0, 0, &err_msg);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "sqlite3_exec: %s\n", err_msg);
|
||||||
|
sqlite3_free(err_msg);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
#undef INSERT_STR
|
||||||
|
|
||||||
|
#define DEL_STR "DELETE FROM books WHERE id = "
|
||||||
|
void remove_from_db(int id, sqlite3* db)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char *cmd, *err_msg;
|
||||||
|
|
||||||
|
open_db(&db);
|
||||||
|
|
||||||
|
cmd = malloc(sizeof(char) * (strlen(DEL_STR) + 1));
|
||||||
|
snprintf(cmd, strlen(DEL_STR), "%s", DEL_STR);
|
||||||
|
|
||||||
|
add_int_entry(&cmd, id, true);
|
||||||
|
|
||||||
|
err = sqlite3_exec(db, cmd, 0, 0, &err_msg);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "sqlite3_exec: %s\n", err_msg);
|
||||||
|
sqlite3_free(err_msg);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef DEL_STR
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
#ifndef DB_H
|
#ifndef DB_H
|
||||||
#define DB_H
|
#define DB_H
|
||||||
|
|
||||||
@ -5,9 +7,16 @@ typedef struct {
|
|||||||
char *title, *subtitle, *isbn;
|
char *title, *subtitle, *isbn;
|
||||||
char *authors, *subjects, *langs;
|
char *authors, *subjects, *langs;
|
||||||
char *date_added, *pub_date;
|
char *date_added, *pub_date;
|
||||||
char *image_url;
|
char *image_code;
|
||||||
|
|
||||||
int year_of_pub, page_len;
|
int page_len;
|
||||||
} book_t;
|
} book_t;
|
||||||
|
|
||||||
|
void setup_db(sqlite3* db);
|
||||||
|
|
||||||
|
void add_to_db(book_t* book, sqlite3* db);
|
||||||
|
|
||||||
|
void remove_from_db(int id, sqlite3* db);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
37
src/main.c
37
src/main.c
@ -4,9 +4,11 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "include/curl.h"
|
#include "include/curl.h"
|
||||||
|
#include "include/db.h"
|
||||||
#include "include/json.h"
|
#include "include/json.h"
|
||||||
|
|
||||||
struct flags {
|
struct flags {
|
||||||
|
char* isbn;
|
||||||
bool json, db, print;
|
bool json, db, print;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -17,8 +19,8 @@ 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 doc[] = "Lookup details of a book from the ISBN. Using OpenLibrary API.";
|
||||||
static char args_doc[] = "<ISBN>";
|
static char args_doc[] = "<ISBN>";
|
||||||
static struct argp_option options[] = {
|
static struct argp_option options[] = {
|
||||||
{0, 'j', "json", 0, "output to json", 0},
|
{"json", 'j', 0, 0, "output to json", 0},
|
||||||
{0, 'd', "db", 0, "output to sqlite3 database", 0},
|
{"db", 'd', 0, 0, "output to sqlite3 database", 0},
|
||||||
{0, 'p', 0, 0, "print the full json response", 0},
|
{0, 'p', 0, 0, "print the full json response", 0},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
@ -44,7 +46,12 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state)
|
|||||||
case 'p':
|
case 'p':
|
||||||
flags->print = true;
|
flags->print = true;
|
||||||
break;
|
break;
|
||||||
|
case ARGP_KEY_ARG:
|
||||||
|
flags->isbn = arg;
|
||||||
|
break;
|
||||||
case ARGP_KEY_END:
|
case ARGP_KEY_END:
|
||||||
|
if (state->arg_num < 1)
|
||||||
|
argp_usage(state);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return ARGP_ERR_UNKNOWN;
|
return ARGP_ERR_UNKNOWN;
|
||||||
@ -63,7 +70,9 @@ int main(int argc, char* argv[])
|
|||||||
init_flags(&flags);
|
init_flags(&flags);
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &flags);
|
argp_parse(&argp, argc, argv, 0, 0, &flags);
|
||||||
|
|
||||||
book_api_call("9780762437818", &resp);
|
/*
|
||||||
|
//book_api_call("9780762437818", &resp);
|
||||||
|
book_api_call(flags.isbn, &resp);
|
||||||
|
|
||||||
if (flags.print)
|
if (flags.print)
|
||||||
puts(resp.buf);
|
puts(resp.buf);
|
||||||
@ -74,8 +83,28 @@ int main(int argc, char* argv[])
|
|||||||
if (flags.json)
|
if (flags.json)
|
||||||
json_out(&query);
|
json_out(&query);
|
||||||
else
|
else
|
||||||
// db out;
|
; // db out;
|
||||||
|
*/
|
||||||
|
|
||||||
|
book_t book;
|
||||||
|
|
||||||
|
book.title = "hi";
|
||||||
|
book.subtitle = "test";
|
||||||
|
book.isbn = "1234";
|
||||||
|
book.authors = "test";
|
||||||
|
book.subjects = "test";
|
||||||
|
book.langs = "eng";
|
||||||
|
book.date_added = "test";
|
||||||
|
book.pub_date = "test";
|
||||||
|
book.image_code = "test";
|
||||||
|
|
||||||
|
book.page_len = 1;
|
||||||
|
|
||||||
|
sqlite3* db;
|
||||||
|
|
||||||
|
setup_db(db);
|
||||||
|
|
||||||
|
add_to_db(&book, db);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user