summaryrefslogtreecommitdiff
path: root/plugins
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
parent6d6612b67a136e27427b2b2b40bae6fbed7e9f80 (diff)
switched artwork downloading to use vfs_curl
Diffstat (limited to 'plugins')
-rw-r--r--plugins/artwork/albumartorg.c72
-rw-r--r--plugins/artwork/artwork.c64
-rw-r--r--plugins/artwork/artwork.h9
-rw-r--r--plugins/artwork/lastfm.c81
-rw-r--r--plugins/gtkui/coverart.c9
5 files changed, 133 insertions, 102 deletions
diff --git a/plugins/artwork/albumartorg.c b/plugins/artwork/albumartorg.c
index 06247f74..74e58d6d 100644
--- a/plugins/artwork/albumartorg.c
+++ b/plugins/artwork/albumartorg.c
@@ -19,8 +19,14 @@
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
+#include <unistd.h>
#include "artwork.h"
+extern DB_functions_t *deadbeef;
+
+#define trace(...) { fprintf(stderr, __VA_ARGS__); }
+//#define trace(...)
+
int
fetch_from_albumart_org (const char *artist, const char *album, const char *dest)
{
@@ -31,24 +37,64 @@ fetch_from_albumart_org (const char *artist, const char *album, const char *dest
curl_free (artist_url);
curl_free (album_url);
- char *response = fetch (url);
-// printf ("%s\n", response);
- char *img = strstr (response, "http://ecx.images-amazon.com/images/I/");
- if (!img)
- {
- free (response);
- return 0;
+ DB_FILE *fp = deadbeef->fopen (url);
+ if (!fp) {
+ trace ("fetch_from_albumart_org: failed to open %s\n", url);
+ return -1;
+ }
+ const char searchstr[] = "http://ecx.images-amazon.com/images/I/";
+ char buffer[10000];
+ 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_albumart_org: image url not found in response from %s (%d bytes)\n", url, size);
+ return -1;
+ }
+
char *end = strstr (img, "._SL160_");
- if (!end)
+ if (!end || end == img)
{
- free (response);
- return 0;
+ trace ("fetch_from_albumart_org: bad xml from %s\n", url);
+ return -1;
}
strcpy (end, ".jpg");
- int res = fetch_to_file (img, dest);
- free (response);
- return res;
+ fp = deadbeef->fopen (img);
+ if (!fp) {
+ trace ("fetch_from_albumart_org: failed to open %s\n", img);
+ return -1;
+ }
+
+ FILE *out = fopen (dest, "w+b");
+ if (!out) {
+ trace ("fetch_from_albumart_org: 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_albumart_org: failed to write to %s\n", dest);
+ error = 1;
+ break;
+ }
+ }
+
+ fclose (out);
+ deadbeef->fclose (fp);
+
+ if (error) {
+ unlink (dest);
+ return -1;
+ }
+ return 0;
}
diff --git a/plugins/artwork/artwork.c b/plugins/artwork/artwork.c
index e2151069..284a4754 100644
--- a/plugins/artwork/artwork.c
+++ b/plugins/artwork/artwork.c
@@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <curl/curl.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
@@ -124,63 +123,6 @@ queue_pop (void) {
deadbeef->mutex_unlock (mutex);
}
-int
-fetch_to_stream (const char *url, FILE *stream)
-{
- CURL *curl = curl_easy_init();
- curl_easy_setopt (curl, CURLOPT_URL, url);
- curl_easy_setopt (curl, CURLOPT_WRITEDATA, stream);
- curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
- CURLcode ret = curl_easy_perform (curl);
- curl_easy_cleanup (curl);
- return ret;
-}
-
-int
-fetch_to_file (const char *url, const char *filename)
-{
- /**
- * Downloading files directly to its locations can cause
- * cachehits of semi-downloaded files. That's why I use
- * temporary files
- */
- char temp [1024];
- int ret;
- snprintf (temp, sizeof (temp), "%s.part", filename);
-
- FILE *stream = fopen (temp, "wb");
- if (!stream)
- {
- trace ("Could not open %s for writing\n", temp);
- return 0;
- }
- ret = fetch_to_stream (url, stream);
- if (ret != 0) {
- trace ("Failed to fetch %s\n", url);
- }
- fclose (stream);
- if (0 == ret)
- {
- ret = (0 == rename (temp, filename));
- if (!ret) {
- trace ("Could not move %s to %s: %d\n", temp, filename, errno);
- unlink (temp);
- }
- }
- return ret;
-}
-
-char*
-fetch (const char *url)
-{
- char *data;
- size_t size;
- FILE *stream = open_memstream (&data, &size);
- fetch_to_stream (url, stream);
- fclose (stream);
- return data;
-}
-
static int
check_dir (const char *dir, mode_t mode)
{
@@ -462,10 +404,10 @@ fetcher_thread (void *none)
make_cache_path (path, sizeof (path), param->album, param->artist);
- if (deadbeef->conf_get_int ("artwork.enable_lastfm", 0) && fetch_from_lastfm (param->artist, param->album, path)) {
+ if (deadbeef->conf_get_int ("artwork.enable_lastfm", 0) && !fetch_from_lastfm (param->artist, param->album, path)) {
trace ("art found on last.fm for %s %s\n", param->album, param->artist);
}
- else if (deadbeef->conf_get_int ("artwork.enable_albumartorg", 0) && fetch_from_albumart_org (param->artist, param->album, path)) {
+ else if (deadbeef->conf_get_int ("artwork.enable_albumartorg", 0) && !fetch_from_albumart_org (param->artist, param->album, path)) {
trace ("art found on albumart.org for %s %s\n", param->album, param->artist);
}
else {
@@ -503,7 +445,7 @@ fetcher_thread (void *none)
char*
get_album_art (const char *fname, const char *artist, const char *album, artwork_callback callback, void *user_data)
{
- trace ("get_album_art: %s (%s - %s)\n", fname, artist, album);
+// trace ("get_album_art: %s (%s - %s)\n", fname, artist, album);
char path [1024];
if (!album) {
diff --git a/plugins/artwork/artwork.h b/plugins/artwork/artwork.h
index 35b29fb1..c6fd9177 100644
--- a/plugins/artwork/artwork.h
+++ b/plugins/artwork/artwork.h
@@ -5,15 +5,6 @@
typedef void (*artwork_callback) (const char *fname, const char *artist, const char *album, void *user_data);
-char*
-fetch (const char *url);
-
-int
-fetch_to_file (const char *url, const char *filename);
-
-int
-fetch_to_stream (const char *url, FILE *stream);
-
typedef struct {
DB_misc_t plugin;
// returns filename of cached image, or NULL
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;
}
diff --git a/plugins/gtkui/coverart.c b/plugins/gtkui/coverart.c
index 2cf880ff..c86f447c 100644
--- a/plugins/gtkui/coverart.c
+++ b/plugins/gtkui/coverart.c
@@ -25,10 +25,10 @@
#include "../artwork/artwork.h"
#include "gtkui.h"
-#pragma GCC optimize("O0")
+#define DEFAULT_COVER_PATH (PREFIX "/share/deadbeef/pixmaps/noartwork.jpg")
-#define trace(...) { fprintf(stderr, __VA_ARGS__); }
-//#define trace(...)
+//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
+#define trace(...)
extern DB_artwork_plugin_t *coverart_plugin;
@@ -145,6 +145,9 @@ loading_thread (void *none) {
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (queue->fname, NULL);
if (!pixbuf) {
trace ("GDK failed to load pixbuf from file %s\n", queue->fname);
+ pixbuf = gdk_pixbuf_new_from_file (DEFAULT_COVER_PATH, NULL);
+ }
+ if (!pixbuf) {
// make default empty image
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 2, 2);
}