From a7d481c40866016676b74b6a007cda9f2192779f Mon Sep 17 00:00:00 2001 From: riwiwa Date: Tue, 18 Nov 2025 04:13:07 -0800 Subject: [PATCH] new methods to get json file paths, error handling --- muzi.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 17 deletions(-) diff --git a/muzi.c b/muzi.c index b518d84..3c79907 100644 --- a/muzi.c +++ b/muzi.c @@ -12,22 +12,67 @@ #define MAX_FILENAME_SIZE 255 // TODO: -// - for each json file in the directory { // - for each json entry { // - add entry to postgresql db // } -// } // // - web ui // - sql tables: "full history", "artists", "songs", "albums" (see ipad) // int extract(const char *path, const char *target); -int get_artist_plays(void); +int get_artist_plays(const char *json_file, const char *artist); int import_spotify(void); +int add_dir_to_db(const char *path); +int add_to_db(const char *json_file); -int get_artist_plays(void) { - FILE *fp = fopen("test.json", "r"); +int add_to_db(const char *json_file) { + // for json_entry in json_file { + // add to database + // } + printf("Adding to database: %s\n", json_file); + return 0; +} + +int add_dir_to_db(const char *path) { + DIR *dir = opendir(path); + struct dirent *data_dir = NULL; + + while ((data_dir = readdir(dir)) != NULL) { + if (data_dir->d_type == DT_DIR) { + if ((strcmp(data_dir->d_name, ".") != 0) && + (strcmp(data_dir->d_name, "..") != 0)) { + char data_dir_path[MAX_FILENAME_SIZE]; + if (snprintf(data_dir_path, MAX_FILENAME_SIZE, "%s/%s/%s", path, + data_dir->d_name, + "Spotify Extended Streaming History") < 0) { + return 1; + } + + DIR *json_dir = opendir(data_dir_path); + struct dirent *json_file = NULL; + while ((json_file = readdir(json_dir)) != NULL) { + if (json_file->d_type != DT_DIR) { + char *ext = strrchr(json_file->d_name, '.'); + if (strcmp(ext, ".json") == 0) { + char json_file_path[MAX_FILENAME_SIZE]; + if (snprintf(json_file_path, MAX_FILENAME_SIZE, "%s/%s", + data_dir_path, json_file->d_name) < 0) { + return 1; + } + + add_to_db(json_file_path); + } + } + } + } + } + } + return 0; +} + +int get_artist_plays(const char *json_file, const char *artist) { + FILE *fp = fopen(json_file, "r"); if (fp == NULL) { printf("Error while opening file\n"); return 1; @@ -55,7 +100,6 @@ int get_artist_plays(void) { cJSON *track = NULL; int i = 0; - char artist[] = ""; cJSON_ArrayForEach(track, json) { cJSON *trackName = cJSON_GetObjectItemCaseSensitive( track, "master_metadata_album_artist_name"); @@ -95,7 +139,10 @@ int extract(const char *path, const char *target) { } char file_target[MAX_FILENAME_SIZE]; - snprintf(file_target, MAX_FILENAME_SIZE, "%s/%s", target, st.name); + if (snprintf(file_target, MAX_FILENAME_SIZE, "%s/%s", target, st.name) < + 0) { + return 1; + } char *search = strchr(st.name, '/'); if (search != NULL) { @@ -108,24 +155,23 @@ int extract(const char *path, const char *target) { } dir[end + 1] = '\0'; char dir_target[MAX_FILENAME_SIZE]; - snprintf(dir_target, MAX_FILENAME_SIZE, "%s/%s", target, dir); + if (snprintf(dir_target, MAX_FILENAME_SIZE, "%s/%s", target, dir) < 0) { + return 1; + } mkdir(dir_target, 0777); } - // Handle directories if (file_target[strlen(file_target) - 1] == '/') { - mkdir(file_target, 0777); // Create directory if it doesn't exist + mkdir(file_target, 0777); continue; } - // Open file in archive zip_file_t *zf = zip_fopen_index(za, i, 0); if (!zf) { fprintf(stderr, "Error opening file in zip: %s\n", file_target); continue; } - // Create output file FILE *outfile = fopen(file_target, "w+"); if (!outfile) { fprintf(stderr, "Error creating output file: %s\n", file_target); @@ -133,14 +179,12 @@ int extract(const char *path, const char *target) { continue; } - // Read and write data char buffer[4096]; zip_int64_t bytes_read; while ((bytes_read = zip_fread(zf, buffer, sizeof(buffer))) > 0) { fwrite(buffer, 1, bytes_read, outfile); } - // Close files fclose(outfile); zip_fclose(zf); } @@ -158,7 +202,10 @@ int import_spotify(void) { struct dirent *entry = NULL; while ((entry = readdir(dir)) != NULL) { char full_name[MAX_FILENAME_SIZE]; - snprintf(full_name, MAX_FILENAME_SIZE, "%s/%s", path, entry->d_name); + if (snprintf(full_name, MAX_FILENAME_SIZE, "%s/%s", path, entry->d_name) < + 0) { + return 1; + } if (entry->d_type != DT_DIR) { char *ext = strrchr(entry->d_name, '.'); @@ -167,14 +214,21 @@ int import_spotify(void) { int len = strlen(zip_dir_name); zip_dir_name[len - 4] = '\0'; char target[MAX_FILENAME_SIZE]; - snprintf(target, MAX_FILENAME_SIZE, "%s/%s", target_base, zip_dir_name); + if (snprintf(target, MAX_FILENAME_SIZE, "%s/%s", target_base, + zip_dir_name) < 0) { + return 1; + } extract(full_name, target); } } } closedir(dir); + add_dir_to_db(target_base); return 0; } -int main(void) { import_spotify(); } +int main(void) { + // import_spotify(); + // add_dir_to_db("./spotify-data/extracted"); +}