summaryrefslogtreecommitdiff
path: root/plugins/artwork/lastfm.c
diff options
context:
space:
mode:
authorGravatar Alexey Yakovenko <wakeroid@gmail.com>2010-04-04 19:10:55 +0200
committerGravatar Alexey Yakovenko <wakeroid@gmail.com>2010-04-04 19:10:55 +0200
commit1bd73696a3c8484729460cad8d8e6a44e3649c16 (patch)
tree92a227530be85bfcee351c2959459a489129a27e /plugins/artwork/lastfm.c
parent6d6612b67a136e27427b2b2b40bae6fbed7e9f80 (diff)
switched artwork downloading to use vfs_curl
Diffstat (limited to 'plugins/artwork/lastfm.c')
-rw-r--r--plugins/artwork/lastfm.c81
1 files changed, 65 insertions, 16 deletions
diff --git a/plugins/artwork/lastfm.c b/plugins/artwork/lastfm.c
index e64af10b..df08c2b9 100644
--- a/plugins/artwork/lastfm.c
+++ b/plugins/artwork/lastfm.c
@@ -2,39 +2,88 @@
#include <string.h>
#include <curl/curl.h>
#include <stdlib.h>
+#include <unistd.h>
#include "artwork.h"
#define BASE_URL "http://ws.audioscrobbler.com/2.0/"
#define API_KEY "b25b959554ed76058ac220b7b2e0a026"
+#define trace(...) { fprintf(stderr, __VA_ARGS__); }
+//#define trace(...)
+
+extern DB_functions_t *deadbeef;
+
int
fetch_from_lastfm (const char *artist, const char *album, const char *dest)
{
char url [1024];
char *artist_url = curl_easy_escape (NULL, artist, 0);
char *album_url = curl_easy_escape (NULL, album, 0);
- snprintf (url, sizeof (url), BASE_URL "?method=album.getinfo&api_key=" API_KEY "&artist=%s&album=%s", artist_url, album_url );
+ snprintf (url, sizeof (url), BASE_URL "?method=album.getinfo&api_key=" API_KEY "&artist=%s&album=%s", artist_url, album_url);
curl_free (artist_url);
curl_free (album_url);
- char *response = fetch (url);
-// printf ("%s\n", response);
- char *img = strstr (response, "<image size=\"extralarge\">");
- if (!img)
- {
- free (response);
- return 0;
+ DB_FILE *fp = deadbeef->fopen (url);
+ if (!fp) {
+ trace ("fetch_from_lastfm: failed to open %s\n", url);
+ return -1;
}
- img += 25;
+
+ const char searchstr[] = "<image size=\"extralarge\">";
+ char buffer[1000];
+ char *img = NULL;
+ int size = deadbeef->fread (buffer, 1, sizeof (buffer), fp);
+ if (size > 0) {
+ img = strstr (buffer, searchstr);
+ }
+ deadbeef->fclose (fp);
+
+ if (!img) {
+ trace ("fetch_from_lastfm: image url not found in response from %s\n", url);
+ return -1;
+ }
+
+ img += sizeof (searchstr)-1;
+
char *end = strstr (img, "</image>");
- if (!end || (end == img))
- {
- free (response);
- return 0;
+ if (!end || end == img) {
+ trace ("fetch_from_lastfm: bad xml from %s\n", url);
+ return -1;
}
+
*end = 0;
- int res = fetch_to_file (img, dest);
- free (response);
- return res;
+
+ fp = deadbeef->fopen (img);
+ if (!fp) {
+ trace ("fetch_from_lastfm: failed to open %s\n", img);
+ return -1;
+ }
+
+ FILE *out = fopen (dest, "w+b");
+ if (!out) {
+ trace ("fetch_from_lastfm: failed to open %s for writing\n", dest);
+ deadbeef->fclose (fp);
+ return -1;
+ }
+
+ char *writebuffer[4096];
+ int len;
+ int error = 0;
+ while ((len = deadbeef->fread (writebuffer, 1, sizeof (writebuffer), fp)) > 0) {
+ if (fwrite (writebuffer, 1, len, out) != len) {
+ trace ("fetch_from_lastfm: failed to write to %s\n", dest);
+ error = 1;
+ break;
+ }
+ }
+
+ fclose (out);
+ deadbeef->fclose (fp);
+
+ if (error) {
+ unlink (dest);
+ return -1;
+ }
+ return 0;
}