From 713dd70c1228aa12f280c38757383f282b33a762 Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Wed, 24 Jan 2024 10:50:08 -0500 Subject: [PATCH] images --- Makefile | 2 +- src/csv.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/csv.h | 10 +++++++++ src/json.c | 18 +++++++++++++++- src/json.h | 1 + src/main.c | 27 +----------------------- 6 files changed, 90 insertions(+), 28 deletions(-) create mode 100644 src/csv.c create mode 100644 src/csv.h diff --git a/Makefile b/Makefile index 1ce0674..321fa65 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: src/*.c - gcc src/main.c src/json.c src/curl.c -lcurl -lcjson -o isbn -Wall + gcc src/main.c src/json.c src/curl.c src/csv.c -lcurl -lcjson -o isbn -Wall mkdir -p build mv isbn build clean: diff --git a/src/csv.c b/src/csv.c new file mode 100644 index 0000000..7d3d649 --- /dev/null +++ b/src/csv.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include + +#include "curl.h" +#include "json.h" +#include "csv.h" + +#define MAX_BUFFER_SIZE 512 + +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 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"); + 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(); + } + + // 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); +} diff --git a/src/csv.h b/src/csv.h new file mode 100644 index 0000000..ae57daf --- /dev/null +++ b/src/csv.h @@ -0,0 +1,10 @@ +#ifndef CSV_H +#define CSV_H + +#define FILE_NAME "books.csv" + +int get_next_id(); + +void write_to_file(book_t* book); + +#endif diff --git a/src/json.c b/src/json.c index 3ce42f4..d70f2c3 100644 --- a/src/json.c +++ b/src/json.c @@ -40,6 +40,22 @@ void get_authors(cJSON* bookinfo, char* authors) } } + +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 parse_json(string* s, char* isbn, book_t* book) { char authors[MAX_BUF_LEN]; @@ -63,7 +79,7 @@ void parse_json(string* s, char* isbn, book_t* 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); diff --git a/src/json.h b/src/json.h index 5d76cc7..2d6aa27 100644 --- a/src/json.h +++ b/src/json.h @@ -7,6 +7,7 @@ typedef struct book_t { char* isbn; char* title; char* authors; + char* image_url; int year_of_publication; int page_len; } book_t; diff --git a/src/main.c b/src/main.c index 9f52db9..8bff1ef 100644 --- a/src/main.c +++ b/src/main.c @@ -7,8 +7,7 @@ #include "curl.h" #include "json.h" - -#define FILE_NAME "books.csv" +#include "csv.h" #define MAX_BUF_LEN 1024 @@ -33,30 +32,6 @@ void print_book(book_t* book) printf("Page length: %d\n", book->page_len); } -void write_to_file(book_t* book) -{ - FILE* file; - int file_exists; - - /** - * 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"); - fprintf(file, "isbn,title,authors,year of publication,page length\n"); - } else { - file = fopen(FILE_NAME, "a"); - } - - // now we write the information - fprintf(file, "\"%s\",\"%s\",\"%s\",%d,%d\n", - book->isbn, book->title, book->authors, book->year_of_publication, book->page_len); - - fclose(file); -} void do_output(book_t* book, enum book_options output_type) {