summaryrefslogtreecommitdiff
path: root/plugins/artwork
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/artwork')
-rw-r--r--plugins/artwork/Makefile.am8
-rw-r--r--plugins/artwork/albumartorg.c54
-rw-r--r--plugins/artwork/albumartorg.h25
-rw-r--r--plugins/artwork/artwork.c233
-rw-r--r--plugins/artwork/artwork.h24
-rw-r--r--plugins/artwork/lastfm.c40
-rw-r--r--plugins/artwork/lastfm.h25
7 files changed, 409 insertions, 0 deletions
diff --git a/plugins/artwork/Makefile.am b/plugins/artwork/Makefile.am
new file mode 100644
index 00000000..de137844
--- /dev/null
+++ b/plugins/artwork/Makefile.am
@@ -0,0 +1,8 @@
+artworkdir = $(libdir)/$(PACKAGE)
+pkglib_LTLIBRARIES = artwork.la
+artwork_la_SOURCES = artwork.h albumartorg.c artwork.c lastfm.c
+
+artwork_la_LDFLAGS = -module
+
+artwork_la_LIBADD = $(LDADD) $(ARTWORK_DEPS_LIBS) $(CURL_LIBS)
+AM_CFLAGS = -std=c99 $(ARTWORK_DEPS_CFLAGS)
diff --git a/plugins/artwork/albumartorg.c b/plugins/artwork/albumartorg.c
new file mode 100644
index 00000000..06247f74
--- /dev/null
+++ b/plugins/artwork/albumartorg.c
@@ -0,0 +1,54 @@
+/*
+ DeaDBeeF - ultimate music player for GNU/Linux systems with X11
+ Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+#include <stdlib.h>
+#include <curl/curl.h>
+#include <string.h>
+#include "artwork.h"
+
+int
+fetch_from_albumart_org (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), "http://www.albumart.org/index.php?srchkey=%s+%s&itempage=1&newsearch=1&searchindex=Music", artist_url, album_url);
+ 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;
+ }
+ char *end = strstr (img, "._SL160_");
+ if (!end)
+ {
+ free (response);
+ return 0;
+ }
+ strcpy (end, ".jpg");
+
+ int res = fetch_to_file (img, dest);
+ free (response);
+ return res;
+}
+
diff --git a/plugins/artwork/albumartorg.h b/plugins/artwork/albumartorg.h
new file mode 100644
index 00000000..4d1d494a
--- /dev/null
+++ b/plugins/artwork/albumartorg.h
@@ -0,0 +1,25 @@
+/*
+ DeaDBeeF - ultimate music player for GNU/Linux systems with X11
+ Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+#ifndef __ALBUMARTORG_H
+#define __ALBUMARTORG_H
+
+int
+fetch_from_albumart_org (const char *artist, const char *album, const char *dest);
+
+#endif
diff --git a/plugins/artwork/artwork.c b/plugins/artwork/artwork.c
new file mode 100644
index 00000000..a136d29e
--- /dev/null
+++ b/plugins/artwork/artwork.c
@@ -0,0 +1,233 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <curl/curl.h>
+#include <errno.h>
+#include <dirent.h>
+#include "../../deadbeef.h"
+#include "artwork.h"
+#include "lastfm.h"
+#include "albumartorg.h"
+
+//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
+#define trace(...)
+
+static DB_artwork_plugin_t plugin;
+DB_functions_t *deadbeef;
+
+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)
+ {
+ fprintf (stderr, "Could not open %s for writing\n", temp);
+ return 0;
+ }
+ ret = fetch_to_stream (url, stream);
+ if (ret != 0)
+ printf ("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);
+ }
+ 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)
+{
+ char *tmp = strdup (dir);
+ char *slash = tmp;
+ struct stat stat_buf;
+ do
+ {
+ slash = strstr (slash+1, "/");
+ if (slash)
+ *slash = 0;
+// trace ("Checking %s\n", tmp);
+ if (-1 == stat (tmp, &stat_buf))
+ {
+ if (0 != mkdir (tmp, mode))
+ {
+ trace ("Failed to create %s (%d)\n", tmp, errno);
+ free (tmp);
+ return 0;
+ }
+ }
+ if (slash)
+ *slash = '/';
+ }
+ while (slash);
+ free (tmp);
+ return 1;
+}
+
+typedef struct
+{
+ const char *artist;
+ const char *album;
+ artwork_callback callback;
+} fetcher_thread_param_t;
+
+static void
+fetcher_thread (fetcher_thread_param_t *param)
+{
+ char path [1024];
+
+ snprintf (path, sizeof (path), "%s/artcache/%s", deadbeef->get_config_dir (), param->artist);
+ if (!check_dir (path, 0755))
+ goto finalize;
+
+ snprintf (path, sizeof (path), "%s/artcache/%s/%s.jpg", deadbeef->get_config_dir (), param->artist, param->album);
+
+ if (!fetch_from_lastfm (param->artist, param->album, path))
+ if (!fetch_from_albumart_org (param->artist, param->album, path))
+ goto finalize;
+
+ if (param->callback)
+ param->callback (param->artist, param->album);
+
+finalize:
+ free (param);
+ return;
+}
+
+static int
+filter_jpg (const struct dirent *f)
+{
+ const char *ext = strrchr (f->d_name, '.');
+ if (!ext)
+ return 0;
+ if (0 == strcasecmp (ext, ".jpg") ||
+ 0 == strcasecmp (ext, ".jpeg"))
+ return 1;
+ return 0;
+}
+
+char*
+get_album_art (DB_playItem_t *track, artwork_callback callback)
+{
+ char path [1024];
+ struct dirent **files;
+ int files_count;
+
+ const char *album = deadbeef->pl_find_meta (track, "album");
+ const char *artist = deadbeef->pl_find_meta (track, "artist");
+
+ /* Searching in track diectory */
+ strncpy (path, track->fname, sizeof (path));
+ *(strrchr (path, '/')) = 0; //Supposing at least one slash exist
+// trace ("Scanning: %s\n", path);
+ files_count = scandir (path, &files, filter_jpg, alphasort);
+
+/* if (files_count < 0)
+ trace ("Failed to scan dir");
+ if (files_count == 0)
+ trace ("No jpg's found in directoy");*/
+ if (files_count > 0)
+ {
+ strcat (path, "/");
+ strcat (path, files[0]->d_name);
+ char *res = strdup (path);
+ trace ("Found: %s\n", res);
+ int i;
+ for (i = 0; i < files_count; i++)
+ free (files [i]);
+ return res;
+ }
+
+ /* Searching in cache */
+ if (!artist || !*artist || !album || !*album)
+ {
+ //give up
+ return PREFIX "/share/deadbeef/pixmaps/blank_cd.jpg";
+ }
+
+ snprintf (path, sizeof (path), "%s/artcache/%s/%s.jpg", deadbeef->get_config_dir (), artist, album);
+
+ struct stat stat_buf;
+ if (0 == stat (path, &stat_buf))
+ {
+ char *res = strdup (path);
+// printf ("Found in cache: %s\n", res);
+ return res;
+ }
+
+ /* Downloading from internet */
+ trace ("Downloading: %s %s\n", artist, album);
+ fetcher_thread_param_t *param = malloc (sizeof (fetcher_thread_param_t));
+ param->artist = artist;
+ param->album = album;
+ param->callback = callback;
+ deadbeef->thread_start ((void (*)(void *))fetcher_thread, param);
+ return PREFIX "/share/deadbeef/pixmaps/blank_cd.jpg";
+}
+
+DB_plugin_t *
+artwork_load (DB_functions_t *api) {
+ deadbeef = api;
+ return DB_PLUGIN (&plugin);
+}
+
+static int
+artwork_plugin_start (void)
+{
+}
+
+static int
+artwork_plugin_stop (void)
+{
+}
+
+// define plugin interface
+static DB_artwork_plugin_t plugin = {
+ .plugin.plugin.api_vmajor = DB_API_VERSION_MAJOR,
+ .plugin.plugin.api_vminor = DB_API_VERSION_MINOR,
+ .plugin.plugin.type = DB_PLUGIN_MISC,
+ .plugin.plugin.id = "cover_loader",
+ .plugin.plugin.name = "Album Artwork",
+ .plugin.plugin.descr = "Loads album artwork either from local directories or from internet",
+ .plugin.plugin.author = "Viktor Semykin",
+ .plugin.plugin.email = "thesame.ml@gmail.com",
+ .plugin.plugin.website = "http://deadbeef.sf.net",
+ .plugin.plugin.start = artwork_plugin_start,
+ .plugin.plugin.stop = artwork_plugin_stop,
+ .get_album_art = get_album_art,
+};
diff --git a/plugins/artwork/artwork.h b/plugins/artwork/artwork.h
new file mode 100644
index 00000000..816ca349
--- /dev/null
+++ b/plugins/artwork/artwork.h
@@ -0,0 +1,24 @@
+#ifndef __ARTWORK_H
+#define __ARTWORK_H
+
+#include "../../deadbeef.h"
+
+typedef void (*artwork_callback) (const char *artist, const char *album);
+
+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
+ char* (*get_album_art) (DB_playItem_t *track, artwork_callback callback);
+} DB_artwork_plugin_t;
+
+#endif /*__ARTWORK_H*/
+
diff --git a/plugins/artwork/lastfm.c b/plugins/artwork/lastfm.c
new file mode 100644
index 00000000..e64af10b
--- /dev/null
+++ b/plugins/artwork/lastfm.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <string.h>
+#include <curl/curl.h>
+#include <stdlib.h>
+
+#include "artwork.h"
+
+#define BASE_URL "http://ws.audioscrobbler.com/2.0/"
+#define API_KEY "b25b959554ed76058ac220b7b2e0a026"
+
+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 );
+ 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;
+ }
+ img += 25;
+ char *end = strstr (img, "</image>");
+ if (!end || (end == img))
+ {
+ free (response);
+ return 0;
+ }
+ *end = 0;
+ int res = fetch_to_file (img, dest);
+ free (response);
+ return res;
+}
diff --git a/plugins/artwork/lastfm.h b/plugins/artwork/lastfm.h
new file mode 100644
index 00000000..09ec817c
--- /dev/null
+++ b/plugins/artwork/lastfm.h
@@ -0,0 +1,25 @@
+/*
+ DeaDBeeF - ultimate music player for GNU/Linux systems with X11
+ Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+#ifndef __LASTFM_H
+#define __LASTFM_H
+
+int
+fetch_from_lastfm (const char *artist, const char *album, const char *dest);
+
+#endif