diff options
Diffstat (limited to 'plugins/artwork/lastfm.c')
-rw-r--r-- | plugins/artwork/lastfm.c | 82 |
1 files changed, 66 insertions, 16 deletions
diff --git a/plugins/artwork/lastfm.c b/plugins/artwork/lastfm.c index e64af10b..6f429bf7 100644 --- a/plugins/artwork/lastfm.c +++ b/plugins/artwork/lastfm.c @@ -2,39 +2,89 @@ #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]; + memset (buffer, 0, sizeof (buffer)); + 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; } |