mirror of
https://github.com/riwiwa/muzi.git
synced 2025-12-30 12:45:26 -08:00
new methods to get json file paths, error handling
This commit is contained in:
88
muzi.c
88
muzi.c
@@ -12,22 +12,67 @@
|
|||||||
#define MAX_FILENAME_SIZE 255
|
#define MAX_FILENAME_SIZE 255
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - for each json file in the directory {
|
|
||||||
// - for each json entry {
|
// - for each json entry {
|
||||||
// - add entry to postgresql db
|
// - add entry to postgresql db
|
||||||
// }
|
// }
|
||||||
// }
|
|
||||||
//
|
//
|
||||||
// - web ui
|
// - web ui
|
||||||
// - sql tables: "full history", "artists", "songs", "albums" (see ipad)
|
// - sql tables: "full history", "artists", "songs", "albums" (see ipad)
|
||||||
//
|
//
|
||||||
|
|
||||||
int extract(const char *path, const char *target);
|
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 import_spotify(void);
|
||||||
|
int add_dir_to_db(const char *path);
|
||||||
|
int add_to_db(const char *json_file);
|
||||||
|
|
||||||
int get_artist_plays(void) {
|
int add_to_db(const char *json_file) {
|
||||||
FILE *fp = fopen("test.json", "r");
|
// 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) {
|
if (fp == NULL) {
|
||||||
printf("Error while opening file\n");
|
printf("Error while opening file\n");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -55,7 +100,6 @@ int get_artist_plays(void) {
|
|||||||
|
|
||||||
cJSON *track = NULL;
|
cJSON *track = NULL;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char artist[] = "";
|
|
||||||
cJSON_ArrayForEach(track, json) {
|
cJSON_ArrayForEach(track, json) {
|
||||||
cJSON *trackName = cJSON_GetObjectItemCaseSensitive(
|
cJSON *trackName = cJSON_GetObjectItemCaseSensitive(
|
||||||
track, "master_metadata_album_artist_name");
|
track, "master_metadata_album_artist_name");
|
||||||
@@ -95,7 +139,10 @@ int extract(const char *path, const char *target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char file_target[MAX_FILENAME_SIZE];
|
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, '/');
|
char *search = strchr(st.name, '/');
|
||||||
if (search != NULL) {
|
if (search != NULL) {
|
||||||
@@ -108,24 +155,23 @@ int extract(const char *path, const char *target) {
|
|||||||
}
|
}
|
||||||
dir[end + 1] = '\0';
|
dir[end + 1] = '\0';
|
||||||
char dir_target[MAX_FILENAME_SIZE];
|
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);
|
mkdir(dir_target, 0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle directories
|
|
||||||
if (file_target[strlen(file_target) - 1] == '/') {
|
if (file_target[strlen(file_target) - 1] == '/') {
|
||||||
mkdir(file_target, 0777); // Create directory if it doesn't exist
|
mkdir(file_target, 0777);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open file in archive
|
|
||||||
zip_file_t *zf = zip_fopen_index(za, i, 0);
|
zip_file_t *zf = zip_fopen_index(za, i, 0);
|
||||||
if (!zf) {
|
if (!zf) {
|
||||||
fprintf(stderr, "Error opening file in zip: %s\n", file_target);
|
fprintf(stderr, "Error opening file in zip: %s\n", file_target);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create output file
|
|
||||||
FILE *outfile = fopen(file_target, "w+");
|
FILE *outfile = fopen(file_target, "w+");
|
||||||
if (!outfile) {
|
if (!outfile) {
|
||||||
fprintf(stderr, "Error creating output file: %s\n", file_target);
|
fprintf(stderr, "Error creating output file: %s\n", file_target);
|
||||||
@@ -133,14 +179,12 @@ int extract(const char *path, const char *target) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and write data
|
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
zip_int64_t bytes_read;
|
zip_int64_t bytes_read;
|
||||||
while ((bytes_read = zip_fread(zf, buffer, sizeof(buffer))) > 0) {
|
while ((bytes_read = zip_fread(zf, buffer, sizeof(buffer))) > 0) {
|
||||||
fwrite(buffer, 1, bytes_read, outfile);
|
fwrite(buffer, 1, bytes_read, outfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close files
|
|
||||||
fclose(outfile);
|
fclose(outfile);
|
||||||
zip_fclose(zf);
|
zip_fclose(zf);
|
||||||
}
|
}
|
||||||
@@ -158,7 +202,10 @@ int import_spotify(void) {
|
|||||||
struct dirent *entry = NULL;
|
struct dirent *entry = NULL;
|
||||||
while ((entry = readdir(dir)) != NULL) {
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
char full_name[MAX_FILENAME_SIZE];
|
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) {
|
if (entry->d_type != DT_DIR) {
|
||||||
char *ext = strrchr(entry->d_name, '.');
|
char *ext = strrchr(entry->d_name, '.');
|
||||||
@@ -167,14 +214,21 @@ int import_spotify(void) {
|
|||||||
int len = strlen(zip_dir_name);
|
int len = strlen(zip_dir_name);
|
||||||
zip_dir_name[len - 4] = '\0';
|
zip_dir_name[len - 4] = '\0';
|
||||||
char target[MAX_FILENAME_SIZE];
|
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);
|
extract(full_name, target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
add_dir_to_db(target_base);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) { import_spotify(); }
|
int main(void) {
|
||||||
|
// import_spotify();
|
||||||
|
// add_dir_to_db("./spotify-data/extracted");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user