This commit is contained in:
SuperNovaa41 2024-01-24 10:50:08 -05:00
parent 36e7aa535c
commit 713dd70c12
6 changed files with 90 additions and 28 deletions

View File

@ -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:

60
src/csv.c Normal file
View File

@ -0,0 +1,60 @@
#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 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);
}

10
src/csv.h Normal file
View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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)
{