diff --git a/src/httpd_jsonapi.c b/src/httpd_jsonapi.c index 87e7ae2e..f78dc49f 100644 --- a/src/httpd_jsonapi.c +++ b/src/httpd_jsonapi.c @@ -113,7 +113,7 @@ safe_json_add_int_from_string(json_object *obj, const char *key, const char *val } static inline void -safe_json_add_time_from_string(json_object *obj, const char *key, const char *value, bool with_time) +safe_json_add_time_from_string(json_object *obj, const char *key, const char *value) { uint32_t tmp; time_t timestamp; @@ -139,10 +139,39 @@ safe_json_add_time_from_string(json_object *obj, const char *key, const char *va return; } - if (with_time) - strftime(result, sizeof(result), "%FT%TZ", &tm); - else - strftime(result, sizeof(result), "%F", &tm); + strftime(result, sizeof(result), "%FT%TZ", &tm); + + json_object_object_add(obj, key, json_object_new_string(result)); +} + +static inline void +safe_json_add_date_from_string(json_object *obj, const char *key, const char *value) +{ + uint32_t tmp; + time_t timestamp; + struct tm tm; + char result[32]; + + if (!value) + return; + + if (safe_atou32(value, &tmp) != 0) + { + DPRINTF(E_LOG, L_WEB, "Error converting timestamp to uint32_t: %s\n", value); + return; + } + + if (!tmp) + return; + + timestamp = tmp; + if (localtime_r(×tamp, &tm) == NULL) + { + DPRINTF(E_LOG, L_WEB, "Error converting timestamp to localtime: %s\n", value); + return; + } + + strftime(result, sizeof(result), "%F", &tm); json_object_object_add(obj, key, json_object_new_string(result)); } @@ -165,8 +194,8 @@ artist_to_json(struct db_group_info *dbgri) safe_json_add_int_from_string(item, "track_count", dbgri->itemcount); safe_json_add_int_from_string(item, "length_ms", dbgri->song_length); - safe_json_add_time_from_string(item, "time_played", dbgri->time_played, true); - safe_json_add_time_from_string(item, "time_added", dbgri->time_added, true); + safe_json_add_time_from_string(item, "time_played", dbgri->time_played); + safe_json_add_time_from_string(item, "time_added", dbgri->time_added); ret = safe_atoi32(dbgri->seek, &intval); if (ret == 0) @@ -210,8 +239,8 @@ album_to_json(struct db_group_info *dbgri) safe_json_add_int_from_string(item, "track_count", dbgri->itemcount); safe_json_add_int_from_string(item, "length_ms", dbgri->song_length); - safe_json_add_time_from_string(item, "time_played", dbgri->time_played, true); - safe_json_add_time_from_string(item, "time_added", dbgri->time_added, true); + safe_json_add_time_from_string(item, "time_played", dbgri->time_played); + safe_json_add_time_from_string(item, "time_added", dbgri->time_added); ret = safe_atoi32(dbgri->seek, &intval); if (ret == 0) @@ -225,7 +254,7 @@ album_to_json(struct db_group_info *dbgri) if (ret == 0) safe_json_add_string(item, "data_kind", db_data_kind_label(intval)); - safe_json_add_time_from_string(item, "date_released", dbgri->date_released, false); + safe_json_add_date_from_string(item, "date_released", dbgri->date_released); safe_json_add_int_from_string(item, "year", dbgri->year); ret = snprintf(uri, sizeof(uri), "%s:%s:%s", "library", "album", dbgri->persistentid); @@ -271,10 +300,10 @@ track_to_json(struct db_media_file_info *dbmfi) safe_json_add_int_from_string(item, "rating", dbmfi->rating); safe_json_add_int_from_string(item, "play_count", dbmfi->play_count); safe_json_add_int_from_string(item, "skip_count", dbmfi->skip_count); - safe_json_add_time_from_string(item, "time_played", dbmfi->time_played, true); - safe_json_add_time_from_string(item, "time_skipped", dbmfi->time_skipped, true); - safe_json_add_time_from_string(item, "time_added", dbmfi->time_added, true); - safe_json_add_time_from_string(item, "date_released", dbmfi->date_released, false); + safe_json_add_time_from_string(item, "time_played", dbmfi->time_played); + safe_json_add_time_from_string(item, "time_skipped", dbmfi->time_skipped); + safe_json_add_time_from_string(item, "time_added", dbmfi->time_added); + safe_json_add_date_from_string(item, "date_released", dbmfi->date_released); safe_json_add_int_from_string(item, "seek_ms", dbmfi->seek); safe_json_add_string(item, "type", dbmfi->type); @@ -1113,14 +1142,14 @@ jsonapi_reply_library(struct httpd_request *hreq) ret = db_admin_get(&s, DB_ADMIN_START_TIME); if (ret == 0) { - safe_json_add_time_from_string(jreply, "started_at", s, true); + safe_json_add_time_from_string(jreply, "started_at", s); free(s); } ret = db_admin_get(&s, DB_ADMIN_DB_UPDATE); if (ret == 0) { - safe_json_add_time_from_string(jreply, "updated_at", s, true); + safe_json_add_time_from_string(jreply, "updated_at", s); free(s); } diff --git a/src/spotify_webapi.c b/src/spotify_webapi.c index 9ace4d1f..c9f4d966 100644 --- a/src/spotify_webapi.c +++ b/src/spotify_webapi.c @@ -52,6 +52,7 @@ struct spotify_album const char *name; const char *release_date; const char *release_date_precision; + time_t release_date_time; int release_year; const char *uri; const char *artwork_url; @@ -72,6 +73,10 @@ struct spotify_track const char *id; const char *name; int track_number; + const char *release_date; + const char *release_date_precision; + time_t release_date_time; + int release_year; const char *uri; const char *artwork_url; @@ -728,6 +733,8 @@ parse_metadata_album(json_object *jsonalbum, struct spotify_album *album, int ma album->release_date = jparse_str_from_obj(jsonalbum, "release_date"); album->release_date_precision = jparse_str_from_obj(jsonalbum, "release_date_precision"); + if (album->release_date_precision && strcmp(album->release_date_precision, "day") == 0) + album->release_date_time = jparse_time_from_obj(jsonalbum, "release_date"); album->release_year = get_year_from_date(album->release_date); if (max_w > 0) @@ -1383,12 +1390,13 @@ map_track_to_mfi(struct media_file_info *mfi, const struct spotify_track *track, mfi->time_modified = track->mtime; mfi->time_added = track->mtime; - if (album) + if (album && album->uri) { mfi->album_artist = safe_strdup(album->artist); mfi->album = safe_strdup(album->name); mfi->genre = safe_strdup(album->genre); mfi->compilation = album->is_compilation; + mfi->date_released = album->release_date_time; mfi->year = album->release_year; } else @@ -1445,7 +1453,7 @@ track_add(struct spotify_track *track, struct spotify_album *album, const char * spotify_uri_register(track->uri); - if (album) + if (album && album->uri) cache_artwork_ping(track->uri, album->mtime, 0); else cache_artwork_ping(track->uri, 1, 0); @@ -1560,7 +1568,9 @@ static int saved_playlist_tracks_add(json_object *item, int index, int total, void *arg) { struct spotify_track track; + struct spotify_album album; json_object *jsontrack; + json_object *jsonalbum; int *plid; int dir_id; int ret; @@ -1583,8 +1593,17 @@ saved_playlist_tracks_add(json_object *item, int index, int total, void *arg) return 0; } + if (json_object_object_get_ex(jsontrack, "album", &jsonalbum)) + { + parse_metadata_album(jsonalbum, &album, 0); + } + else + { + memset(&album, 0, sizeof(struct spotify_album)); + } + dir_id = prepare_directories(track.album_artist, track.album); - ret = track_add(&track, NULL, NULL, dir_id); + ret = track_add(&track, &album, NULL, dir_id); if (ret == 0) db_pl_add_item_bypath(*plid, track.uri);