comparison src/deezer.c @ 12:dd427b7cc459 default tip

json: replace with nxjson library more lightweight, reduces the binary size by about 40 kb
author Paper <paper@paper.us.eu.org>
date Fri, 15 Mar 2024 20:46:18 -0400
parents e6a594f16403
children
comparison
equal deleted inserted replaced
11:e6a594f16403 12:dd427b7cc459
67 } 67 }
68 68
69 return final; 69 return final;
70 } 70 }
71 71
72 static int deezer_album_object_get_cover(cJSON* restrict album, const char* restrict name, char** restrict url) { 72 static int deezer_album_object_get_cover(const nx_json* album, const char* name, char** url) {
73 if (!url) return 0; 73 if (!url) return 0;
74 74
75 cJSON* cover = cJSON_GetObjectItemCaseSensitive(album, name); 75 const nx_json* cover = nx_json_get(album, name);
76 if (cover) { 76 if (!cover)
77 if (*url) 77 return 0;
78 free(*url); 78
79 size_t len = strlen(cover->valuestring); 79 free(*url);
80 *url = malloc((len + 1) * sizeof(char)); 80
81 (*url)[len] = '\0'; 81 size_t len = strlen(cover->text_value);
82 strncpy(*url, cover->valuestring, len); 82 *url = malloc((len + 1) * sizeof(char));
83 return 1; 83 (*url)[len] = '\0';
84 } else return 0; 84
85 strncpy(*url, cover->text_value, len);
86 return 1;
85 } 87 }
86 88
87 89
88 static int deezer_get_thumbnail_download_url(LPCWSTR restrict query, char** restrict data, size_t* restrict size) { 90 static int deezer_get_thumbnail_download_url(LPCWSTR restrict query, char** restrict data, size_t* restrict size) {
89 if (!data || !size) 91 if (!data || !size)
144 146
145 WinHttpCloseHandle(request); 147 WinHttpCloseHandle(request);
146 return 0; 148 return 0;
147 } 149 }
148 150
151 /* note: please prefix these */
149 enum deezer_thumbnail_state { 152 enum deezer_thumbnail_state {
150 ERROR = 0, 153 ERROR = 0,
151 ALBUM, 154 ALBUM,
152 ARTIST, 155 ARTIST,
153 }; 156 };
154 157
155 static enum deezer_thumbnail_state deezer_get_thumbnail_parse_search_result(cJSON* json, char** restrict cover_url, int get_artist) { 158 static enum deezer_thumbnail_state deezer_get_thumbnail_parse_search_result(const nx_json* json, char** cover_url, int get_artist) {
156 cJSON* album = cJSON_GetObjectItemCaseSensitive(json, "album"); 159 const nx_json* album = nx_json_get(json, "album");
157 160
158 if (cJSON_IsObject(album)) { 161 if (album->type == NX_JSON_OBJECT) {
159 if (deezer_album_object_get_cover(album, "cover_medium", cover_url) 162 if (deezer_album_object_get_cover(album, "cover_medium", cover_url)
160 || deezer_album_object_get_cover(album, "cover_large", cover_url) 163 || deezer_album_object_get_cover(album, "cover_large", cover_url)
161 || deezer_album_object_get_cover(album, "cover_small", cover_url) 164 || deezer_album_object_get_cover(album, "cover_small", cover_url)
162 || deezer_album_object_get_cover(album, "cover_xl", cover_url)) 165 || deezer_album_object_get_cover(album, "cover_xl", cover_url))
163 return ALBUM; 166 return ALBUM;
164 } else if (get_artist) { 167 }
165 cJSON* artist = cJSON_GetObjectItemCaseSensitive(json, "artist"); 168
166 if (!cJSON_IsObject(artist)) 169 if (get_artist) {
170 const nx_json* artist = nx_json_get(json, "artist");
171 if (artist->type != NX_JSON_OBJECT)
167 return ERROR; 172 return ERROR;
168 173
169 /* treat artist pictures as a fallback and only retrieve them once */ 174 /* treat artist pictures as a fallback and only retrieve them once */
170 if (deezer_album_object_get_cover(artist, "picture_medium", cover_url) 175 if (deezer_album_object_get_cover(artist, "picture_medium", cover_url)
171 || deezer_album_object_get_cover(artist, "picture_large", cover_url) 176 || deezer_album_object_get_cover(artist, "picture_large", cover_url)
175 } 180 }
176 181
177 return ERROR; 182 return ERROR;
178 } 183 }
179 184
180 static int deezer_get_thumbnail_parse_json(char** restrict cover_url, const char* restrict data, size_t size) { 185 /* THIS MODIFIES `data` IN PLACE!! */
181 cJSON* json = cJSON_ParseWithLength(data, size); 186 static int deezer_get_thumbnail_parse_json(char** cover_url, char* data) {
182 if (!json) { 187 const nx_json* json = nx_json_parse(data, NULL);
183 const char* err_ptr = cJSON_GetErrorPtr(); 188 if (!json)
184 if (err_ptr) 189 return -1;
185 MessageBoxA(NULL, err_ptr, "wgsdk: Error parsing Deezer JSON!", MB_ICONERROR | MB_OK); 190
186 cJSON_Delete(json); 191 const nx_json* json_data = nx_json_get(json, "data");
187 return -1; 192 if (json_data->type != NX_JSON_ARRAY || !json_data->children.length)
188 }
189
190 cJSON* json_data = cJSON_GetObjectItemCaseSensitive(json, "data");
191 size_t json_data_size;
192 if (!cJSON_IsArray(json_data) || !(json_data_size = cJSON_GetArraySize(json_data)))
193 return -1; 193 return -1;
194 194
195 int have_artist = 0; 195 int have_artist = 0;
196 for (size_t i = 0; i < json_data_size; i++) { 196 for (size_t i = 0; i < json_data->children.length; i++) {
197 cJSON* result = cJSON_GetArrayItem(json_data, i); 197 const nx_json* result = nx_json_item(json_data, i);
198 if (!cJSON_IsObject(result)) 198 if (result->type != NX_JSON_OBJECT)
199 return -1; 199 return -1;
200 200
201 enum deezer_thumbnail_state state = deezer_get_thumbnail_parse_search_result(result, cover_url, have_artist); 201 enum deezer_thumbnail_state state = deezer_get_thumbnail_parse_search_result(result, cover_url, have_artist);
202 switch (state) { 202 switch (state) {
203 case ERROR: return -1; 203 case ERROR: return -1;
204 case ARTIST: have_artist = 1; break; 204 case ARTIST: have_artist = 1; break;
205 case ALBUM: return 0; 205 case ALBUM: return 0;
206 } 206 }
207 } 207 }
208 208
209 cJSON_Delete(json); 209 nx_json_free(json);
210
210 return !have_artist; 211 return !have_artist;
211 } 212 }
212 213
213 char* deezer_get_thumbnail(LPCWSTR restrict artist, LPCWSTR restrict album) { 214 char* deezer_get_thumbnail(LPCWSTR restrict artist, LPCWSTR restrict album) {
214 char* response_data = NULL; 215 char* response_data = NULL;
225 return NULL; 226 return NULL;
226 227
227 if (deezer_get_thumbnail_download_url(query, &response_data, &response_size)) 228 if (deezer_get_thumbnail_download_url(query, &response_data, &response_size))
228 return NULL; 229 return NULL;
229 230
230 if (deezer_get_thumbnail_parse_json(&cover_url, response_data, response_size)) 231 if (deezer_get_thumbnail_parse_json(&cover_url, response_data))
231 return NULL; 232 return NULL;
233
234 free(response_data);
232 235
233 return cover_url; 236 return cover_url;
234 } 237 }