Mercurial > audlegacy-plugins
comparison src/lastfm/lastfm.c @ 979:e31c4566feb6 trunk
[svn] - disable all use of puts() unless -DDEBUG is defined.
author | nenolod |
---|---|
date | Tue, 24 Apr 2007 02:53:37 -0700 |
parents | 3513bc2fd738 |
children | 7b0d16a9a92f |
comparison
equal
deleted
inserted
replaced
978:3513bc2fd738 | 979:e31c4566feb6 |
---|---|
34 { | 34 { |
35 VFSFile *proxy_fd; | 35 VFSFile *proxy_fd; |
36 gchar *lastfm_session_id; | 36 gchar *lastfm_session_id; |
37 gchar *lastfm_mp3_stream_url; | 37 gchar *lastfm_mp3_stream_url; |
38 gchar *lastfm_station_name; | 38 gchar *lastfm_station_name; |
39 gchar *lastfm_artist; | 39 gchar *lastfm_artist; |
40 gchar *lastfm_title; | 40 gchar *lastfm_title; |
41 gchar *lastfm_album; | 41 gchar *lastfm_album; |
42 gchar *lastfm_cover; | 42 gchar *lastfm_cover; |
43 int lastfm_duration; | 43 int lastfm_duration; |
44 int login_count; | 44 int login_count; |
45 } LastFM; | 45 } LastFM; |
46 | 46 |
47 LastFM *LastFMGlobalData; | 47 LastFM *LastFMGlobalData; |
48 /*this keeps the login data in a global place | 48 /*this keeps the login data in a global place |
49 since we cannot login on every fopen call | 49 since we cannot login on every fopen call |
50 if anyone has a better solution to this any help is welcome */ | 50 if anyone has a better solution to this any help is welcome */ |
51 | 51 |
52 static size_t lastfm_store_res(void *ptr, size_t size, size_t nmemb, void *udata) | 52 static size_t lastfm_store_res(void *ptr, size_t size, size_t nmemb, void *udata) |
53 { | 53 { |
54 GString *data = (GString *) udata; | 54 GString *data = (GString *) udata; |
55 g_string_append_len(data, ptr, nmemb); | 55 g_string_append_len(data, ptr, nmemb); |
58 | 58 |
59 | 59 |
60 static gboolean lastfm_login() | 60 static gboolean lastfm_login() |
61 { | 61 { |
62 /*gets the session ID in lastfm_session_id and returns the URL to be played | 62 /*gets the session ID in lastfm_session_id and returns the URL to be played |
63 * read http://gabistapler.de/blog/index.php?/archives/268-Play-last.fm-streams-without-the-player.html for more info | 63 * read http://gabistapler.de/blog/index.php?/archives/268-Play-last.fm-streams-without-the-player.html for more info |
64 */ | 64 */ |
65 gint status, i; | 65 gint status, i; |
66 gchar buf[4096], **split = NULL; | 66 gchar buf[4096], **split = NULL; |
67 GString *res = g_string_new(NULL); | 67 GString *res = g_string_new(NULL); |
68 ConfigDb *cfgfile = NULL; | 68 ConfigDb *cfgfile = NULL; |
81 g_free(username); | 81 g_free(username); |
82 } | 82 } |
83 else | 83 else |
84 return FALSE; | 84 return FALSE; |
85 | 85 |
86 puts("preparing curl"); | 86 g_print("preparing curl\n"); |
87 curl = curl_easy_init(); | 87 curl = curl_easy_init(); |
88 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); | 88 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); |
89 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); | 89 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); |
90 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); | 90 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); |
91 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | 91 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
94 curl_easy_setopt(curl, CURLOPT_URL, buf); | 94 curl_easy_setopt(curl, CURLOPT_URL, buf); |
95 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); | 95 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); |
96 status = curl_easy_perform(curl); | 96 status = curl_easy_perform(curl); |
97 curl_easy_cleanup(curl); | 97 curl_easy_cleanup(curl); |
98 | 98 |
99 puts("curl is done"); | 99 g_print("curl is done\n"); |
100 #ifdef DEBUG | |
100 puts(buf); | 101 puts(buf); |
101 puts(res->str); | 102 puts(res->str); |
103 #endif | |
102 if (status == CURLE_OK) | 104 if (status == CURLE_OK) |
103 { | 105 { |
104 split = g_strsplit(res->str, "\n", 7); | 106 split = g_strsplit(res->str, "\n", 7); |
105 | 107 |
106 for (i = 0; split && split[i]; i++) | 108 for (i = 0; split && split[i]; i++) |
132 GString *res = g_string_new(NULL); | 134 GString *res = g_string_new(NULL); |
133 CURL *curl; | 135 CURL *curl; |
134 if (LastFMGlobalData->lastfm_session_id == NULL) | 136 if (LastFMGlobalData->lastfm_session_id == NULL) |
135 return FALSE; | 137 return FALSE; |
136 snprintf(tmp, sizeof(tmp), LASTFM_ADJUST_URL, LastFMGlobalData->lastfm_session_id, url); | 138 snprintf(tmp, sizeof(tmp), LASTFM_ADJUST_URL, LastFMGlobalData->lastfm_session_id, url); |
139 #ifdef DEBUG | |
137 puts("test1"); | 140 puts("test1"); |
138 | 141 #endif |
139 | |
140 curl = curl_easy_init(); | 142 curl = curl_easy_init(); |
141 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); | 143 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); |
142 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); | 144 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); |
143 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); | 145 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); |
144 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | 146 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
146 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT); | 148 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT); |
147 curl_easy_setopt(curl, CURLOPT_URL, tmp); | 149 curl_easy_setopt(curl, CURLOPT_URL, tmp); |
148 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); | 150 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); |
149 status = curl_easy_perform(curl); | 151 status = curl_easy_perform(curl); |
150 curl_easy_cleanup(curl); | 152 curl_easy_cleanup(curl); |
151 | 153 #ifdef DEBUG |
152 | |
153 puts("Adjust received data:"); | 154 puts("Adjust received data:"); |
154 puts(res->str); | 155 puts(res->str); |
155 | 156 #endif |
156 if (status == CURLE_OK) | 157 if (status == CURLE_OK) |
157 { | 158 { |
158 split = g_strsplit(res->str, "\n", 3); | 159 split = g_strsplit(res->str, "\n", 3); |
159 | 160 |
160 for (i = 0; split && split[i]; i++) | 161 for (i = 0; split && split[i]; i++) |
162 if (g_str_has_prefix(split[i], "response=OK")) | 163 if (g_str_has_prefix(split[i], "response=OK")) |
163 ret = TRUE; | 164 ret = TRUE; |
164 if (g_str_has_prefix(split[i], "stationname=")) | 165 if (g_str_has_prefix(split[i], "stationname=")) |
165 { | 166 { |
166 LastFMGlobalData->lastfm_station_name = g_strdup(split[i] + 12); | 167 LastFMGlobalData->lastfm_station_name = g_strdup(split[i] + 12); |
168 #ifdef DEBUG | |
167 puts("StationnName:"); | 169 puts("StationnName:"); |
168 puts(LastFMGlobalData->lastfm_station_name); | 170 puts(LastFMGlobalData->lastfm_station_name); |
171 #endif | |
169 } | 172 } |
170 } | 173 } |
171 | 174 |
172 g_strfreev(split); | 175 g_strfreev(split); |
173 } | 176 } |
175 | 178 |
176 return ret; | 179 return ret; |
177 } | 180 } |
178 | 181 |
179 | 182 |
180 static gboolean lastfm_get_metadata( LastFM *handle) | 183 static gboolean lastfm_get_metadata(LastFM * handle) |
181 { | 184 { |
182 | 185 |
183 gint status, i; | 186 gint status, i; |
184 gchar tmp[4096], **split = NULL; | 187 gchar tmp[4096], **split = NULL; |
185 gboolean ret = FALSE; | 188 gboolean ret = FALSE; |
186 GString *res = g_string_new(NULL); | 189 GString *res = g_string_new(NULL); |
187 CURL *curl; | 190 CURL *curl; |
188 if (handle->lastfm_session_id == NULL) | 191 if (handle->lastfm_session_id == NULL) |
189 return FALSE; | 192 return FALSE; |
190 snprintf(tmp, sizeof(tmp), LASTFM_METADATA_URL, handle->lastfm_session_id); | 193 snprintf(tmp, sizeof(tmp), LASTFM_METADATA_URL, handle->lastfm_session_id); |
191 | 194 |
192 curl = curl_easy_init(); | 195 curl = curl_easy_init(); |
193 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); | 196 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); |
194 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); | 197 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); |
195 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); | 198 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); |
196 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | 199 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
197 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); | 200 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); |
199 curl_easy_setopt(curl, CURLOPT_URL, tmp); | 202 curl_easy_setopt(curl, CURLOPT_URL, tmp); |
200 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); | 203 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); |
201 status = curl_easy_perform(curl); | 204 status = curl_easy_perform(curl); |
202 curl_easy_cleanup(curl); | 205 curl_easy_cleanup(curl); |
203 | 206 |
204 | 207 #ifdef DEBUG |
205 puts("Received metadata:"); | 208 puts("Received metadata:"); |
206 puts(res->str); | 209 puts(res->str); |
210 #endif | |
207 | 211 |
208 if (status == CURLE_OK) | 212 if (status == CURLE_OK) |
209 { | 213 { |
210 split = g_strsplit(res->str, "\n", 20); | 214 split = g_strsplit(res->str, "\n", 20); |
211 | 215 |
212 for (i = 0; split && split[i]; i++) | 216 for (i = 0; split && split[i]; i++) |
213 { | 217 { |
214 if (g_str_has_prefix(split[i], "artist=")) | 218 if (g_str_has_prefix(split[i], "artist=")) |
215 handle->lastfm_artist=g_strdup(split[i] + 7); | 219 handle->lastfm_artist = g_strdup(split[i] + 7); |
216 if (g_str_has_prefix(split[i], "title=")) | 220 if (g_str_has_prefix(split[i], "title=")) |
217 handle->lastfm_title=g_strdup(split[i] + 6); | 221 handle->lastfm_title = g_strdup(split[i] + 6); |
218 if (g_str_has_prefix(split[i], "album=")) | 222 if (g_str_has_prefix(split[i], "album=")) |
219 handle->lastfm_album=g_strdup(split[i] + 6); | 223 handle->lastfm_album = g_strdup(split[i] + 6); |
220 if (g_str_has_prefix(split[i], "albumcover_medium=")) | 224 if (g_str_has_prefix(split[i], "albumcover_medium=")) |
221 handle->lastfm_cover=g_strdup(split[i] + 18); | 225 handle->lastfm_cover = g_strdup(split[i] + 18); |
222 if (g_str_has_prefix(split[i], "trackduration=")) | 226 if (g_str_has_prefix(split[i], "trackduration=")) |
223 handle->lastfm_duration=atoi(g_strdup(split[i] + 14)); | 227 handle->lastfm_duration = atoi(g_strdup(split[i] + 14)); |
224 if (g_str_has_prefix(split[i], "station=")) | 228 if (g_str_has_prefix(split[i], "station=")) |
225 { | 229 { |
226 handle->lastfm_station_name = g_strdup(split[i] + 8); | 230 handle->lastfm_station_name = g_strdup(split[i] + 8); |
227 puts("StationnName:"); | 231 printf("Station Name: %s\n", handle->lastfm_station_name); |
228 puts(handle->lastfm_station_name); | |
229 } | 232 } |
230 } | 233 } |
231 | 234 |
232 g_strfreev(split); | 235 g_strfreev(split); |
233 } | 236 } |
234 g_string_erase(res, 0, -1); | 237 g_string_erase(res, 0, -1); |
235 | 238 |
236 return TRUE; | 239 return TRUE; |
237 } | 240 } |
238 | |
239 | |
240 | |
241 | |
242 | 241 |
243 VFSFile *lastfm_vfs_fopen_impl(const gchar * path, const gchar * mode) | 242 VFSFile *lastfm_vfs_fopen_impl(const gchar * path, const gchar * mode) |
244 { | 243 { |
245 VFSFile *file; | 244 VFSFile *file; |
246 LastFM *handle; | 245 LastFM *handle; |
247 file = g_new0(VFSFile, 1); | 246 file = g_new0(VFSFile, 1); |
248 handle = g_new0(LastFM, 1); | 247 handle = g_new0(LastFM, 1); |
249 | 248 #ifdef DEBUG |
250 puts("Starting fopen"); | 249 puts("Starting fopen"); |
251 | 250 #endif |
252 while ((LastFMGlobalData->lastfm_mp3_stream_url == NULL) && (LastFMGlobalData->login_count <= 3)) | 251 while ((LastFMGlobalData->lastfm_mp3_stream_url == NULL) && (LastFMGlobalData->login_count <= 3)) |
253 { | 252 { |
254 printf("Login try count: %d\n", LastFMGlobalData->login_count++); | 253 printf("Login try count: %d\n", LastFMGlobalData->login_count++); |
255 lastfm_login(); | 254 lastfm_login(); |
256 if (LastFMGlobalData->lastfm_mp3_stream_url == NULL) | 255 if (LastFMGlobalData->lastfm_mp3_stream_url == NULL) |
257 sleep(5); | 256 sleep(5); |
258 | 257 } |
259 } | 258 |
260 | 259 if (LastFMGlobalData->lastfm_mp3_stream_url == NULL) |
261 | 260 return NULL; |
262 | 261 |
263 handle->lastfm_mp3_stream_url = g_strdup(LastFMGlobalData->lastfm_mp3_stream_url); | 262 handle->lastfm_mp3_stream_url = g_strdup(LastFMGlobalData->lastfm_mp3_stream_url); |
264 handle->lastfm_session_id = g_strdup(LastFMGlobalData->lastfm_session_id); | 263 handle->lastfm_session_id = g_strdup(LastFMGlobalData->lastfm_session_id); |
265 handle->lastfm_station_name = g_strdup(LastFMGlobalData->lastfm_station_name); | 264 handle->lastfm_station_name = g_strdup(LastFMGlobalData->lastfm_station_name); |
266 | 265 |
267 | |
268 | |
269 if (lastfm_adjust(path)) | 266 if (lastfm_adjust(path)) |
270 { | 267 { |
271 printf("Tuning completed OK, getting metadata\n"); | 268 gint ret; |
272 if(lastfm_get_metadata(handle)) | 269 printf("Tuning completed OK, getting metadata\n"); |
273 puts("Successfully fetched the metadata"); | 270 ret = lastfm_get_metadata(handle); |
274 else | 271 |
275 puts("Errors were encountered while fetching the metadata"); | 272 #ifdef DEBUG |
276 } | 273 if (ret) |
274 puts("Successfully fetched the metadata"); | |
275 else | |
276 puts("Errors were encountered while fetching the metadata"); | |
277 #endif | |
278 } | |
279 #ifdef DEBUG | |
277 else | 280 else |
278 puts("Cannot tune to given channel"); | 281 puts("Cannot tune to given channel"); |
282 #endif | |
279 | 283 |
280 handle->proxy_fd = vfs_fopen(handle->lastfm_mp3_stream_url, mode); | 284 handle->proxy_fd = vfs_fopen(handle->lastfm_mp3_stream_url, mode); |
281 file->handle = handle; | 285 file->handle = handle; |
282 | 286 |
283 | 287 #ifdef DEBUG |
284 puts("Returning from fopen"); | 288 puts("Returning from fopen"); |
289 #endif | |
285 return file; | 290 return file; |
286 } | 291 } |
287 | 292 |
288 gint lastfm_vfs_fclose_impl(VFSFile * file) | 293 gint lastfm_vfs_fclose_impl(VFSFile * file) |
289 { | 294 { |
361 } | 366 } |
362 | 367 |
363 gchar *lastfm_vfs_metadata_impl(VFSFile * file, const gchar * field) | 368 gchar *lastfm_vfs_metadata_impl(VFSFile * file, const gchar * field) |
364 { | 369 { |
365 LastFM *handle = file->handle; | 370 LastFM *handle = file->handle; |
366 | 371 |
367 puts("Interesting metadata:"); | 372 #ifdef DEBUG |
368 puts(handle->lastfm_station_name); | 373 puts("Interesting metadata:"); |
369 puts(handle->lastfm_artist); | 374 |
370 puts(handle->lastfm_title); | 375 if (handle->lastfm_station_name != NULL) |
371 printf("%d\n\n",handle->lastfm_duration); | 376 puts(handle->lastfm_station_name); |
372 | 377 |
373 | 378 if (handle->lastfm_artist != NULL) |
374 if (!strcmp(field, "stream-name")&& (handle->lastfm_station_name != NULL)) | 379 puts(handle->lastfm_artist); |
375 return g_strdup(handle->lastfm_station_name); | 380 |
376 if (!strcmp(field, "track-name") && (handle->lastfm_title != NULL) && (handle->lastfm_artist != NULL)) | 381 if (handle->lastfm_title != NULL) |
377 return g_strdup_printf("%s - %s", handle->lastfm_artist, handle->lastfm_title); | 382 puts(handle->lastfm_title); |
378 if (!strcmp(field, "content-length")) | 383 |
379 return g_strdup_printf("%ld", handle->lastfm_duration); | 384 printf("%d\n\n", handle->lastfm_duration); |
385 #endif | |
386 | |
387 if (!strcmp(field, "stream-name") && (handle->lastfm_station_name != NULL)) | |
388 return g_strdup(handle->lastfm_station_name); | |
389 if (!strcmp(field, "track-name") && (handle->lastfm_title != NULL) && (handle->lastfm_artist != NULL)) | |
390 return g_strdup_printf("%s - %s", handle->lastfm_artist, handle->lastfm_title); | |
380 | 391 |
381 return NULL; | 392 return NULL; |
382 } | 393 } |
383 | 394 |
384 VFSConstructor lastfm_const = { | 395 VFSConstructor lastfm_const = { |
405 } | 416 } |
406 | 417 |
407 static void cleanup(void) | 418 static void cleanup(void) |
408 { | 419 { |
409 g_free(LastFMGlobalData); | 420 g_free(LastFMGlobalData); |
410 #if 0 | 421 #ifdef DEBUG |
411 vfs_unregister_transport(&default_const); | 422 vfs_unregister_transport(&default_const); |
412 vfs_unregister_transport(&file_const); | 423 vfs_unregister_transport(&file_const); |
413 #endif | 424 #endif |
414 } | 425 } |
415 | 426 |