From e4707db254a8dc7055f6b88fefcae4671b246262 Mon Sep 17 00:00:00 2001 From: riwiwa Date: Sun, 29 Jun 2025 16:55:08 -0700 Subject: [PATCH] parse and print strings from spotify json data --- README.md | 5 ++++- muzi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 muzi.c diff --git a/README.md b/README.md index e3e7a22..5c0f905 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ Self hosted music listening statistics +### Dependencies: +- cJSON (https://github.com/DaveGamble/cJSON) + ### plans: - Ability to import all listening statistics and scrobbles from lastfm, spotify, apple music - daily, weekly, monthly, yearly, lifetime presets for listening reports @@ -13,4 +16,4 @@ Self hosted music listening statistics - webUI - ability to "sync" scrobbles (send from a device to the server) - live scrobbling to the server -- batch scrobble editor \ No newline at end of file +- batch scrobble editor diff --git a/muzi.c b/muzi.c new file mode 100644 index 0000000..1a78aa6 --- /dev/null +++ b/muzi.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +// TODO: +// - unzip given .zip archives of spotify data +// - web ui +// - enter all json data into postgresql db automatically +// - sql tables: "full history", "artists", "songs", "albums" (see ipad) + +int main(void) { + FILE *fp = fopen("test.json", "r"); + if(fp == NULL) { + printf("Error while opening file\n"); + return 1; + } + + fseek(fp, 0, SEEK_END); + long fileSize = ftell(fp); + fseek(fp, 0, SEEK_SET); + + char *buffer = (char *)malloc(fileSize + 1); + fread(buffer, 1, fileSize, fp); + buffer[fileSize] = '\0'; + fclose(fp); + + cJSON *json = cJSON_Parse(buffer); + if(json == NULL) { + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) { + printf("Error: %s\n", error_ptr); + } + cJSON_Delete(json); + free(buffer); + return 1; + } + + cJSON *track = NULL; + int i = 0; + char artist[] = "Test"; + cJSON_ArrayForEach(track, json) { + cJSON *trackName = cJSON_GetObjectItemCaseSensitive(track, "master_metadata_album_artist_name"); + if(cJSON_IsString(trackName)) { + if(strcasecmp(artist, (trackName->valuestring)) == 0) { + i++; + } + } + } + printf("\"%s\" count: %d\n", artist, i); + + + cJSON_Delete(json); + free(buffer); + return 0; +}