annotate src/deezer.c @ 11:e6a594f16403

*: huge refactor the config file has changed drastically, moving to an ini file from that custom format; i *would* have used the win32 functions for those, but they were barely functional, so I decided on using ini.h which is lightweight enough. additionally, I've added Deezer support so album art will be displayed! unfortunately though winhttp is a pain in the ass so if I send a request with any form of unicode chars in it it just returns a "bad request" error. I've tried debugging this but I could never really come up with anything: my hypothesis is that deezer expects their characters in percent-encoded UTF-8, but winhttp is sending them in some other encoding. the config dialog was moved out of config.c (overdue) and many more options are given in the config as well. main.c has been renamed to plugin.c to better differentiate it from... everything else.
author Paper <paper@paper.us.eu.org>
date Thu, 14 Mar 2024 20:25:37 -0400
parents
children dd427b7cc459
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "json.h"
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include "utils.h"
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include <windef.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <minwinbase.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <winhttp.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <winuser.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 HINTERNET session = NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 HINTERNET connection = NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 /* preferably we would use some other API for this, but
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 * meh
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 static int init_winhttp(void) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 session = WinHttpOpen(
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 NULL,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 WINHTTP_NO_PROXY_NAME,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 WINHTTP_NO_PROXY_BYPASS,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 0
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 );
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 return !!session;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 static int init_connect(void) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 if (!session)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 connection = WinHttpConnect(
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 session,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 L"api.deezer.com",
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 /* require HTTPS, we aren't in 2001 anymore */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 INTERNET_DEFAULT_HTTPS_PORT,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 0
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 );
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 return !!connection;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 /* do this on exit */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 void close_open_http_handles(void) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 if (session) WinHttpCloseHandle(session);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 if (connection) WinHttpCloseHandle(connection);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 /* return MUST be free'd */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 static LPCWSTR deezer_get_thumbnail_build_query(LPCWSTR restrict artist, LPCWSTR restrict album) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 static LPCWSTR begin = L"/search/track?strict=on&q=artist:\"";
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 static LPCWSTR album_query = L" album:\"";
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 size_t len = wcslen(begin) + wcslen(artist) + 1 /* quote */;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 if (album && album[0]) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 len += 1 /* space */ + wcslen(album_query) + wcslen(album) + 1 /* quote */;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 LPWSTR final = calloc(len + 1, sizeof(WCHAR));
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 wcscpy(final, begin);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 wcscat(final, artist);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 wcscat(final, L"\"");
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 if (album && album[0]) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 wcscat(final, album_query);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 wcscat(final, album);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 wcscat(final, L"\"");
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 return final;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 static int deezer_album_object_get_cover(cJSON* restrict album, const char* restrict name, char** restrict url) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 if (!url) return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 cJSON* cover = cJSON_GetObjectItemCaseSensitive(album, name);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 if (cover) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 if (*url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 free(*url);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 size_t len = strlen(cover->valuestring);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 *url = malloc((len + 1) * sizeof(char));
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 (*url)[len] = '\0';
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 strncpy(*url, cover->valuestring, len);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 return 1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 } else return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 static int deezer_get_thumbnail_download_url(LPCWSTR restrict query, char** restrict data, size_t* restrict size) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 if (!data || !size)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 HINTERNET request = WinHttpOpenRequest(
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 connection,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 L"GET",
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 query,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 NULL,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 WINHTTP_NO_REFERER,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 WINHTTP_DEFAULT_ACCEPT_TYPES,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 WINHTTP_FLAG_SECURE | WINHTTP_FLAG_ESCAPE_PERCENT
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 );
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 if (!request)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 BOOL result = WinHttpSendRequest(
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 request,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 L"Content-Type: application/json; charset=utf-8",
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 0,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 WINHTTP_NO_REQUEST_DATA,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 0,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 0,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 0
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 );
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 if (!result) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 WinHttpCloseHandle(request);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 result = WinHttpReceiveResponse(request, NULL);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 if (!result) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 WinHttpCloseHandle(request);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 DWORD data_available = 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 DWORD data_downloaded = 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 do {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 data_available = 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 if (!WinHttpQueryDataAvailable(request, &data_available)) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 WinHttpCloseHandle(request);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 if (!data_available)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 break;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 *data = realloc(*data, *size + data_available + 1);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 (*data)[*size + data_available] = '\0';
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138 if (!WinHttpReadData(request, *data + *size, data_available, &data_downloaded)) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139 WinHttpCloseHandle(request);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142 *size += data_downloaded;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143 } while (data_available > 0);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 WinHttpCloseHandle(request);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 enum deezer_thumbnail_state {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 ERROR = 0,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 ALBUM,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152 ARTIST,
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 };
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 static enum deezer_thumbnail_state deezer_get_thumbnail_parse_search_result(cJSON* json, char** restrict cover_url, int get_artist) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 cJSON* album = cJSON_GetObjectItemCaseSensitive(json, "album");
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158 if (cJSON_IsObject(album)) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 if (deezer_album_object_get_cover(album, "cover_medium", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160 || deezer_album_object_get_cover(album, "cover_large", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161 || deezer_album_object_get_cover(album, "cover_small", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 || deezer_album_object_get_cover(album, "cover_xl", cover_url))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 return ALBUM;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164 } else if (get_artist) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165 cJSON* artist = cJSON_GetObjectItemCaseSensitive(json, "artist");
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 if (!cJSON_IsObject(artist))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 return ERROR;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 /* treat artist pictures as a fallback and only retrieve them once */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170 if (deezer_album_object_get_cover(artist, "picture_medium", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171 || deezer_album_object_get_cover(artist, "picture_large", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 || deezer_album_object_get_cover(artist, "picture_small", cover_url)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 || deezer_album_object_get_cover(artist, "picture_xl", cover_url))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174 return ARTIST;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 return ERROR;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180 static int deezer_get_thumbnail_parse_json(char** restrict cover_url, const char* restrict data, size_t size) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 cJSON* json = cJSON_ParseWithLength(data, size);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 if (!json) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 const char* err_ptr = cJSON_GetErrorPtr();
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184 if (err_ptr)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185 MessageBoxA(NULL, err_ptr, "wgsdk: Error parsing Deezer JSON!", MB_ICONERROR | MB_OK);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186 cJSON_Delete(json);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 cJSON* json_data = cJSON_GetObjectItemCaseSensitive(json, "data");
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 size_t json_data_size;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 if (!cJSON_IsArray(json_data) || !(json_data_size = cJSON_GetArraySize(json_data)))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195 int have_artist = 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196 for (size_t i = 0; i < json_data_size; i++) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 cJSON* result = cJSON_GetArrayItem(json_data, i);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198 if (!cJSON_IsObject(result))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199 return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201 enum deezer_thumbnail_state state = deezer_get_thumbnail_parse_search_result(result, cover_url, have_artist);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202 switch (state) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 case ERROR: return -1;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204 case ARTIST: have_artist = 1; break;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 case ALBUM: return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 cJSON_Delete(json);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 return !have_artist;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 }
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 char* deezer_get_thumbnail(LPCWSTR restrict artist, LPCWSTR restrict album) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 char* response_data = NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 size_t response_size = 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 LPCWSTR query = NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217 char* cover_url = NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219 /* make sure everything is OK */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 if (!(session || init_winhttp()) || !(connection || init_connect()) || (!artist || !artist[0]))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 return NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223 query = deezer_get_thumbnail_build_query(artist, album);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 if (!query)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 return NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 if (deezer_get_thumbnail_download_url(query, &response_data, &response_size))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228 return NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 if (deezer_get_thumbnail_parse_json(&cover_url, response_data, response_size))
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231 return NULL;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233 return cover_url;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 }