Adds a bunch of new DB columns and fills them with data

This commit is contained in:
SuperNovaa41 2024-02-01 23:02:19 -05:00
parent f97049c26a
commit 6d81d205a0
3 changed files with 82 additions and 31 deletions

View File

@ -47,11 +47,13 @@ void setup_db(sqlite3* db)
{ {
int rc; int rc;
char* err_msg = 0; char* err_msg = 0;
char* sql;
sql = "CREATE TABLE IF NOT EXISTS books (isbn TEXT, title TEXT, authors TEXT, imageurl TEXT, year_of_publication YEAR, page_length UNSIGNED INT);"; rc = sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
rc = sqlite3_exec(db, sql, 0, 0, &err_msg); 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) { if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg); fprintf(stderr, "SQL error: %s\n", err_msg);
@ -67,10 +69,24 @@ void setup_db(sqlite3* db)
void add_to_db(book_t* book, sqlite3* db) void add_to_db(book_t* book, sqlite3* db)
{ {
char* sql; char* sql;
int asp_err, rc; int asp_err, rc, entries = 0;
char* err_msg = 0; char* err_msg = 0;
asp_err = asprintf(&sql, "INSERT INTO books (isbn, title, authors, imageurl, year_of_publication, page_length) VALUES(\"%s\", \"%s\", \"%s\", \"%s\", %d, %d);", book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, book->page_len); 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) { if (-1 == asp_err) {
fprintf(stderr, "asprintf failed!\n"); fprintf(stderr, "asprintf failed!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "curl.h" #include "curl.h"
#include "json.h" #include "json.h"
@ -17,26 +18,26 @@ void check_valid_query(cJSON* numfound)
exit(EXIT_FAILURE); 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; 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); snprintf(in_str, strlen(str_arr->valuestring) + 1, "%s", str_arr->valuestring);
author_arr = author_arr->next; 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 ", " // 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); temp_str = malloc(sizeof(char) * new_len);
snprintf(temp_author, new_len, "%s, %s", authors, author_arr->valuestring); snprintf(temp_str, new_len, "%s, %s", in_str, str_arr->valuestring);
memcpy(authors, temp_author, new_len); 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; int image_id = cJSON_GetObjectItemCaseSensitive(bookinfo, "cover_i")->valueint;
const char* begin = "https://covers.openlibrary.org/b/id/"; const char* begin = "https://covers.openlibrary.org/b/id/";
const char* end = "-L.jpg"; const char* end = "-L.jpg";
book->image_url = malloc(sizeof(char) * MAX_BUF_LEN); book->image_url = malloc(sizeof(char) * MAX_BUF_LEN);
sprintf(book->image_url, "%s%d%s", begin, image_id, end); 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) void parse_json(string* s, char* isbn, book_t* book)
{ {
char authors[MAX_BUF_LEN];
cJSON* json = cJSON_Parse(s->buf); cJSON* json = cJSON_Parse(s->buf);
if (NULL == json) { if (NULL == json) {
const char* error_ptr = cJSON_GetErrorPtr(); 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; cJSON* bookinfo = cJSON_GetObjectItemCaseSensitive(json, "docs")->child;
book->isbn = isbn; set_current_date(book);
book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring; set_actual_values(bookinfo, book, isbn);
book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint; set_list_values(bookinfo, book);
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);
} }

View File

@ -10,6 +10,11 @@ typedef struct book_t {
char* image_url; char* image_url;
int year_of_publication; int year_of_publication;
int page_len; int page_len;
char* subjects;
char* date_added;
char* publication_date;
char* subtitle;
} book_t; } book_t;
/** /**