Compare commits
10 Commits
8e78e89f3b
...
6d81d205a0
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6d81d205a0 | ||
![]() |
f97049c26a | ||
![]() |
d703a32c2d | ||
![]() |
e449bdd2c5 | ||
![]() |
ba2acb23ec | ||
![]() |
43e69fa342 | ||
![]() |
e9014258a8 | ||
![]() |
5c06dbca6c | ||
![]() |
397549386e | ||
![]() |
e0e8634dd1 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1 @@
|
||||
isbn
|
||||
books.csv
|
||||
build/
|
||||
|
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
all: src/*.c
|
||||
gcc src/main.c src/json.c src/curl.c src/csv.c -lcurl -lcjson -o isbn -Wall
|
||||
gcc src/main.c src/json.c src/curl.c src/db.c -lsqlite3 -lcurl -lcjson -o isbn -Wall
|
||||
mkdir -p build
|
||||
mv isbn build
|
||||
clean:
|
||||
|
126
src/csv.c
126
src/csv.c
@ -1,126 +0,0 @@
|
||||
#include <cjson/cJSON.h>
|
||||
#include <curl/curl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "curl.h"
|
||||
#include "json.h"
|
||||
#include "csv.h"
|
||||
|
||||
#define MAX_BUFFER_SIZE 1024
|
||||
|
||||
int get_next_id()
|
||||
{
|
||||
size_t i;
|
||||
char buffer[MAX_BUFFER_SIZE];
|
||||
FILE* csv;
|
||||
char id[MAX_BUFFER_SIZE];
|
||||
|
||||
csv = fopen(FILE_NAME, "r");
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), csv));
|
||||
|
||||
i = 0;
|
||||
while(buffer[i] != ',')
|
||||
i++;
|
||||
|
||||
strncpy(id, buffer, i);
|
||||
|
||||
return atoi(id) + 1;
|
||||
}
|
||||
|
||||
void update_line(char** line, int new_id)
|
||||
{
|
||||
int i;
|
||||
char* buffer;
|
||||
for (i = 0; i < MAX_BUFFER_SIZE; i++) {
|
||||
if ((*line)[i] == ',')
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == MAX_BUFFER_SIZE - 1) {
|
||||
fprintf(stderr, "There was an error in %s file!\n", FILE_NAME);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
buffer = malloc(sizeof(char) * MAX_BUFFER_SIZE);
|
||||
strncpy(buffer, (*line) + i, MAX_BUFFER_SIZE);
|
||||
|
||||
snprintf(*line, MAX_BUFFER_SIZE, "%d%s", new_id, buffer);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void remove_line_from_file(int id_to_remove)
|
||||
{
|
||||
int file_exists, line_count;
|
||||
FILE* csv;
|
||||
FILE* new_csv;
|
||||
char* line;
|
||||
size_t line_size = MAX_BUFFER_SIZE;
|
||||
|
||||
file_exists = access(FILE_NAME, F_OK);
|
||||
if (0 != file_exists) {
|
||||
fprintf(stderr, "%s does not exist!\n", FILE_NAME);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
csv = fopen(FILE_NAME, "r");
|
||||
if (NULL == csv) {
|
||||
fprintf(stderr, "Failed to open %s!\n", FILE_NAME);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
new_csv = fopen("temp.csv", "w");
|
||||
|
||||
line_count = 0;
|
||||
while(getline(&line, &line_size, csv) != -1) {
|
||||
if (id_to_remove > line_count) {
|
||||
fprintf(new_csv, "%s", line);
|
||||
} else if (id_to_remove < line_count) {
|
||||
update_line(&line, line_count - 1);
|
||||
fprintf(new_csv, "%s", line);
|
||||
}
|
||||
line_count++;
|
||||
}
|
||||
fclose(new_csv);
|
||||
fclose(csv);
|
||||
|
||||
remove(FILE_NAME);
|
||||
rename("temp.csv", FILE_NAME); // new csv is now the original file, without that line
|
||||
}
|
||||
|
||||
void write_to_file(book_t* book)
|
||||
{
|
||||
FILE* file;
|
||||
int file_exists;
|
||||
int book_id;
|
||||
|
||||
/**
|
||||
* We want to check if the file exists
|
||||
* if it doesnt, we create a new one
|
||||
* otherwise, we write to the existing one
|
||||
*/
|
||||
file_exists = access(FILE_NAME, F_OK);
|
||||
if (0 != file_exists) {
|
||||
file = fopen(FILE_NAME, "w");
|
||||
// write the csv headers to the file since we're making it
|
||||
fprintf(file, "id,isbn,title,authors,imageurl,year of publication,page length\n");
|
||||
book_id = 1;
|
||||
} else {
|
||||
file = fopen(FILE_NAME, "a");
|
||||
book_id = get_next_id();
|
||||
}
|
||||
|
||||
if (NULL == file) {
|
||||
fprintf(stderr, "Failed to open %s!\n", FILE_NAME);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// now we write the information
|
||||
fprintf(file, "%d,\"%s\",\"%s\",\"%s\",\"%s\",%d,%d\n",
|
||||
book_id, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, book->page_len);
|
||||
|
||||
fclose(file);
|
||||
}
|
38
src/csv.h
38
src/csv.h
@ -1,38 +0,0 @@
|
||||
#ifndef CSV_H
|
||||
#define CSV_H
|
||||
|
||||
#define FILE_NAME "books.csv"
|
||||
|
||||
/**
|
||||
* int get_next_id
|
||||
*
|
||||
* Returns the next available ID so that we know what to assign to the book
|
||||
*/
|
||||
int get_next_id();
|
||||
|
||||
/**
|
||||
* void write_to_file
|
||||
* boot_t* book - A pointer to the book information struct
|
||||
*
|
||||
* Writes the book information to a CSV file
|
||||
*/
|
||||
void write_to_file(book_t* book);
|
||||
|
||||
/**
|
||||
* void remove_line_from_file
|
||||
* int id_to_remove - The book ID that we don't want anymore
|
||||
*
|
||||
* Removes a book from the CSV file
|
||||
*/
|
||||
void remove_line_from_file(int id_to_remove);
|
||||
|
||||
/**
|
||||
* void update_line
|
||||
* char** line - Pointer to the book entry string
|
||||
* int new_id - The new ID that should be placed into this book entry
|
||||
*
|
||||
* Takes a book entry and changes the ID to the given one
|
||||
*/
|
||||
void update_line(char** line, int new_id);
|
||||
|
||||
#endif
|
124
src/db.c
Normal file
124
src/db.c
Normal file
@ -0,0 +1,124 @@
|
||||
#include <stdio.h>
|
||||
#include <sqlite3.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <cjson/cJSON.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
|
||||
#include "curl.h"
|
||||
#include "json.h"
|
||||
#include "db.h"
|
||||
|
||||
void do_db_entry(enum DB_OPTIONS option, ...)
|
||||
{
|
||||
int rc;
|
||||
sqlite3* db;
|
||||
va_list args;
|
||||
|
||||
rc = sqlite3_open("books.db", &db);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
setup_db(db);
|
||||
|
||||
va_start(args, option);
|
||||
switch(option) {
|
||||
case ADD:
|
||||
add_to_db(va_arg(args, book_t*), db);
|
||||
break;
|
||||
case REMOVE:
|
||||
remove_from_db(va_arg(args, int), db);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Invalid db command given!\n");
|
||||
break;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
sqlite3_close(db);
|
||||
}
|
||||
|
||||
void setup_db(sqlite3* db)
|
||||
{
|
||||
int rc;
|
||||
char* err_msg = 0;
|
||||
|
||||
rc = sqlite3_exec(db,
|
||||
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
|
||||
isbn TEXT, title TEXT, authors TEXT, imageurl TEXT, year_of_publication YEAR, \
|
||||
page_length UNSIGNED INT, subjects TEXT, date_added TEXT, date_completed TEXT, \
|
||||
progress UNSIGNED TINYINT, publication_date TEXT, subtitle TEXT);",
|
||||
0, 0, &err_msg);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
||||
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void add_to_db(book_t* book, sqlite3* db)
|
||||
{
|
||||
char* sql;
|
||||
int asp_err, rc, entries = 0;
|
||||
char* err_msg = 0;
|
||||
|
||||
sqlite3_stmt* getmsg;
|
||||
sqlite3_prepare(db, "SELECT * FROM BOOKS;", -1, &getmsg, NULL);
|
||||
while (sqlite3_step(getmsg) == SQLITE_ROW)
|
||||
entries++;
|
||||
sqlite3_finalize(getmsg);
|
||||
|
||||
entries++; // new id!!
|
||||
|
||||
asp_err = asprintf(&sql, "INSERT INTO books \
|
||||
(id, isbn, title, authors, imageurl, year_of_publication, \
|
||||
page_length, subjects, date_added, publication_date, subtitle) \
|
||||
VALUES(%d, \"%s\", \"%s\", \"%s\", \"%s\", %d, %d, \"%s\", \"%s\", \"%s\", \"%s\");",
|
||||
entries, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication,
|
||||
book->page_len, book->subjects, book->date_added, book->publication_date, book->subtitle);
|
||||
|
||||
if (-1 == asp_err) {
|
||||
fprintf(stderr, "asprintf failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void remove_from_db(int id, sqlite3* db)
|
||||
{
|
||||
char* sql;
|
||||
int asp_err, rc;
|
||||
char* err_msg = 0;
|
||||
|
||||
asp_err = asprintf(&sql, "DELETE FROM books WHERE id = %d;", id);
|
||||
if (-1 == asp_err) {
|
||||
fprintf(stderr, "asprintf failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
47
src/db.h
Normal file
47
src/db.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef DH_H
|
||||
#define DH_H
|
||||
|
||||
enum DB_OPTIONS {
|
||||
ADD,
|
||||
REMOVE
|
||||
};
|
||||
|
||||
/**
|
||||
* void do_db_entry
|
||||
*
|
||||
* enum DB_OPTIONS option - The type of transaction being made to the DB
|
||||
* VA args
|
||||
* - Expects book_t if option is ADD
|
||||
* - Expects int if option is REMOVE
|
||||
*
|
||||
* Handles the whole process of interacting with the database
|
||||
*/
|
||||
void do_db_entry(enum DB_OPTIONS option, ...);
|
||||
|
||||
/**
|
||||
* void setup_db
|
||||
* sqlite3* db - The database
|
||||
*
|
||||
* Just creates the database if it doesn't exist.
|
||||
*/
|
||||
void setup_db(sqlite3* db);
|
||||
|
||||
/**
|
||||
* void add_to_db
|
||||
* book_t* book - The struct full of book information
|
||||
* sqlite3* db - The database
|
||||
*
|
||||
* Adds the book information to the database
|
||||
*/
|
||||
void add_to_db(book_t* book, sqlite3* db);
|
||||
|
||||
/**
|
||||
* void remove_from_db
|
||||
* int id - The id of the book to remove
|
||||
* sqlite3* db - The database
|
||||
*
|
||||
* Removes the given ID (and its associated book) from the database.
|
||||
*/
|
||||
void remove_from_db(int id, sqlite3* db);
|
||||
|
||||
#endif
|
80
src/json.c
80
src/json.c
@ -3,6 +3,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "curl.h"
|
||||
#include "json.h"
|
||||
@ -17,26 +18,26 @@ void check_valid_query(cJSON* numfound)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void get_authors(cJSON* bookinfo, char* authors)
|
||||
void get_list(cJSON* bookinfo, char* in_str, char* json_value)
|
||||
{
|
||||
char* temp_author;
|
||||
char* temp_str;
|
||||
size_t new_len;
|
||||
cJSON* author_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, "author_name")->child;
|
||||
cJSON* str_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, json_value)->child;
|
||||
|
||||
snprintf(authors, strlen(author_arr->valuestring) + 1, "%s", author_arr->valuestring);
|
||||
author_arr = author_arr->next;
|
||||
snprintf(in_str, strlen(str_arr->valuestring) + 1, "%s", str_arr->valuestring);
|
||||
str_arr = str_arr->next;
|
||||
|
||||
while (NULL != author_arr) {
|
||||
while (NULL != str_arr) {
|
||||
// The plus 1 is for the \0, the plus 2 is for the ", "
|
||||
new_len = strlen(authors) + strlen(author_arr->valuestring) + 1 + 2;
|
||||
new_len = strlen(in_str) + strlen(str_arr->valuestring) + 1 + 2;
|
||||
|
||||
temp_author = malloc(sizeof(char) * new_len);
|
||||
snprintf(temp_author, new_len, "%s, %s", authors, author_arr->valuestring);
|
||||
memcpy(authors, temp_author, new_len);
|
||||
temp_str = malloc(sizeof(char) * new_len);
|
||||
snprintf(temp_str, new_len, "%s, %s", in_str, str_arr->valuestring);
|
||||
memcpy(in_str, temp_str, new_len);
|
||||
|
||||
free(temp_author);
|
||||
free(temp_str);
|
||||
|
||||
author_arr = author_arr->next;
|
||||
str_arr = str_arr->next;
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,21 +46,57 @@ void get_image_link(cJSON* bookinfo, book_t* book)
|
||||
{
|
||||
int image_id = cJSON_GetObjectItemCaseSensitive(bookinfo, "cover_i")->valueint;
|
||||
|
||||
|
||||
const char* begin = "https://covers.openlibrary.org/b/id/";
|
||||
const char* end = "-L.jpg";
|
||||
|
||||
book->image_url = malloc(sizeof(char) * MAX_BUF_LEN);
|
||||
|
||||
sprintf(book->image_url, "%s%d%s", begin, image_id, end);
|
||||
}
|
||||
|
||||
void set_list_values(cJSON* bookinfo, book_t* book)
|
||||
{
|
||||
char authors[MAX_BUF_LEN];
|
||||
char subjects[MAX_BUF_LEN];
|
||||
|
||||
get_list(bookinfo, authors, "author_name");
|
||||
get_list(bookinfo, subjects, "subject");
|
||||
|
||||
// Need to malloc, because we need to copy authors into the book struct
|
||||
book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1));
|
||||
memcpy(book->authors, authors, strlen(authors) + 1);
|
||||
|
||||
book->subjects = (char*) malloc(sizeof(char) * (strlen(subjects) + 1));
|
||||
memcpy(book->subjects, subjects, strlen(subjects) + 1);
|
||||
}
|
||||
|
||||
void set_actual_values(cJSON* bookinfo, book_t* book, char* isbn)
|
||||
{
|
||||
book->isbn = isbn;
|
||||
book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring;
|
||||
book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint;
|
||||
book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint;
|
||||
book->subtitle = cJSON_GetObjectItemCaseSensitive(bookinfo, "subtitle")->valuestring;
|
||||
book->publication_date = cJSON_GetObjectItemCaseSensitive(bookinfo, "publish_date")->child->valuestring;
|
||||
|
||||
get_image_link(bookinfo, book);
|
||||
}
|
||||
|
||||
void set_current_date(book_t* book)
|
||||
{
|
||||
char date[MAX_BUF_LEN];
|
||||
|
||||
time_t t;
|
||||
time(&t);
|
||||
|
||||
sprintf(date, "%s", ctime(&t));
|
||||
|
||||
book->date_added = (char*) malloc(sizeof(char) * (strlen(date) + 1));
|
||||
memcpy(book->date_added, date, strlen(date) + 1);
|
||||
}
|
||||
|
||||
void parse_json(string* s, char* isbn, book_t* book)
|
||||
{
|
||||
char authors[MAX_BUF_LEN];
|
||||
|
||||
cJSON* json = cJSON_Parse(s->buf);
|
||||
if (NULL == json) {
|
||||
const char* error_ptr = cJSON_GetErrorPtr();
|
||||
@ -73,14 +110,7 @@ void parse_json(string* s, char* isbn, book_t* book)
|
||||
|
||||
cJSON* bookinfo = cJSON_GetObjectItemCaseSensitive(json, "docs")->child;
|
||||
|
||||
book->isbn = isbn;
|
||||
book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring;
|
||||
book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint;
|
||||
book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint;
|
||||
|
||||
get_authors(bookinfo, authors);
|
||||
get_image_link(bookinfo, book);
|
||||
// Need to malloc, because we need to copy authors into the book struct
|
||||
book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1));
|
||||
memcpy(book->authors, authors, strlen(authors) + 1);
|
||||
set_current_date(book);
|
||||
set_actual_values(bookinfo, book, isbn);
|
||||
set_list_values(bookinfo, book);
|
||||
}
|
||||
|
@ -10,6 +10,11 @@ typedef struct book_t {
|
||||
char* image_url;
|
||||
int year_of_publication;
|
||||
int page_len;
|
||||
char* subjects;
|
||||
char* date_added;
|
||||
char* publication_date;
|
||||
char* subtitle;
|
||||
|
||||
} book_t;
|
||||
|
||||
/**
|
||||
|
36
src/main.c
36
src/main.c
@ -3,18 +3,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sqlite3.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "curl.h"
|
||||
#include "json.h"
|
||||
#include "csv.h"
|
||||
#include "db.h"
|
||||
|
||||
#define MAX_BUF_LEN 1024
|
||||
|
||||
/**
|
||||
* TODO: we need to check the csv file for duplicates
|
||||
* TODO: allow us to remove a book from the csv and update the ids
|
||||
*/
|
||||
void do_ISBN_get(char* argv[])
|
||||
{
|
||||
// want to hold a max of 14 so we can hold up to ISBN13s
|
||||
@ -49,7 +46,7 @@ void do_ISBN_get(char* argv[])
|
||||
// Now we want to parse the JSON input
|
||||
parse_json(&get_output, isbn_buf, &new_book);
|
||||
|
||||
write_to_file(&new_book);
|
||||
do_db_entry(ADD, &new_book);
|
||||
|
||||
// we need to free these strings
|
||||
free(get_output.buf);
|
||||
@ -57,18 +54,23 @@ void do_ISBN_get(char* argv[])
|
||||
free(new_book.image_url);
|
||||
}
|
||||
|
||||
void print_help_menu(char* program)
|
||||
{
|
||||
printf("%s - An ISBN lookup tool.\n", program);
|
||||
printf("Author: Nathan Singer\n");
|
||||
|
||||
puts("\n");
|
||||
|
||||
puts("--help - Shows this message.");
|
||||
puts("[isbn] -- Attempts to download a book from the given ISBN-10 or ISBN-13 input.");
|
||||
puts("remove [id] -- Removes a book with the given ID from the book database.");
|
||||
}
|
||||
|
||||
void process_args(char* argv[])
|
||||
{
|
||||
int id;
|
||||
if (0 == strcmp(argv[1], "--help")) {
|
||||
printf("%s - An ISBN lookup tool.\n", argv[0]);
|
||||
printf("Author: Nathan Singer\n");
|
||||
|
||||
puts("\n");
|
||||
|
||||
puts("--help - Shows this message.");
|
||||
puts("[isbn] -- Attempts to download a book from the given ISBN-10 or ISBN-13 input.");
|
||||
puts("remove [id] -- Removes a book with the given ID from the book database.");
|
||||
print_help_menu(argv[0]);
|
||||
} else if (0 == strcmp(argv[1], "remove")) {
|
||||
if (NULL == argv[2]) {
|
||||
printf("Not enough arguments! Try typing %s --help\n", argv[0]);
|
||||
@ -81,16 +83,13 @@ void process_args(char* argv[])
|
||||
printf("Invalid book ID given!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
remove_line_from_file(id);
|
||||
do_db_entry(REMOVE, id);
|
||||
} else {
|
||||
// lets assume its an ISBN and let the other functions fail if its not
|
||||
do_ISBN_get(argv);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (1 == argc) {
|
||||
@ -99,6 +98,7 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
process_args(argv);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user