From c1a0e75ebe1ece92149affa5401a723738bcb75c Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Wed, 24 Nov 2010 21:56:49 +0100 Subject: [PATCH] Fix fetching cover art and fetch art for different releases. * Change to g_log with domain Gmpc.Provider.DiscoGS --- po/Makefile.in.in | 6 ++-- src/plugin.c | 94 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/po/Makefile.in.in b/po/Makefile.in.in index 57ef267..cc8a222 100644 --- a/po/Makefile.in.in +++ b/po/Makefile.in.in @@ -21,7 +21,7 @@ GETTEXT_PACKAGE = @GETTEXT_PACKAGE@ PACKAGE = @PACKAGE@ VERSION = @VERSION@ -SHELL = /bin/sh +SHELL = @SHELL@ srcdir = @srcdir@ top_srcdir = @top_srcdir@ @@ -56,7 +56,7 @@ ALL_LINGUAS = @ALL_LINGUAS@ PO_LINGUAS=$(shell if test -r $(srcdir)/LINGUAS; then grep -v "^\#" $(srcdir)/LINGUAS; else echo "$(ALL_LINGUAS)"; fi) -USER_LINGUAS=$(shell if test -n "$(LINGUAS)"; then LLINGUAS="$(LINGUAS)"; ALINGUAS="$(ALL_LINGUAS)"; for lang in $$LLINGUAS; do if test -n "`grep ^$$lang$$ $(srcdir)/LINGUAS 2>/dev/null`" -o -n "`echo $$ALINGUAS|tr ' ' '\n'|grep ^$$lang$$`"; then printf "$$lang "; fi; done; fi) +USER_LINGUAS=$(shell if test -n "$(LINGUAS)"; then LLINGUAS="$(LINGUAS)"; ALINGUAS="$(ALL_LINGUAS)"; for lang in $$LLINGUAS; do if test -n "`grep \^$$lang$$ $(srcdir)/LINGUAS 2>/dev/null`" -o -n "`echo $$ALINGUAS|tr ' ' '\n'|grep \^$$lang$$`"; then printf "$$lang "; fi; done; fi) USE_LINGUAS=$(shell if test -n "$(USER_LINGUAS)" -o -n "$(LINGUAS)"; then LLINGUAS="$(USER_LINGUAS)"; else if test -n "$(PO_LINGUAS)"; then LLINGUAS="$(PO_LINGUAS)"; else LLINGUAS="$(ALL_LINGUAS)"; fi; fi; for lang in $$LLINGUAS; do printf "$$lang "; done) @@ -129,7 +129,7 @@ install-data-yes: all done # Empty stubs to satisfy archaic automake needs -dvi info tags TAGS ID: +dvi info ctags tags CTAGS TAGS ID: # Define this as empty until I found a useful application. install-exec installcheck: diff --git a/src/plugin.c b/src/plugin.c index fa34a18..e01033a 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -39,7 +38,7 @@ #define DISCOGS_API_KEY "332020810c" #define DISCOGS_API_ROOT "http://www.discogs.com/" -#define LOG_DOMAIN "DiscogsPlugin" +#define LOG_DOMAIN "Gmpc.Provider.DiscoGS" gmpcPlugin plugin; typedef struct ii { @@ -47,6 +46,7 @@ typedef struct ii { mpd_Song *song; void (*callback)(GList *list, gpointer data); gpointer data; + GList *releases; GList *uris; }ii; @@ -78,9 +78,21 @@ static xmlNodePtr get_first_node_by_name(xmlNodePtr xml, gchar *name) { return NULL; } -static gchar * __query_album_get_uri(mpd_Song *song, const gchar*data, gsize size) +static xmlNodePtr get_next_node_by_name(xmlNodePtr xml, gchar *name) { + if(name == NULL) return NULL; + if(xml) { + xmlNodePtr c = xml->next; + for(;c;c=c->next) { + if(c->name && xmlStrEqual(c->name, (xmlChar *) name)) + return c; + } + } + return NULL; +} + +static GList * __query_album_get_uri(mpd_Song *song, const gchar*data, gsize size) { - char *retv = NULL; + GList *retv = NULL; char *temp_b = g_utf8_casefold(song->album,-1); xmlDocPtr doc; /** @@ -103,36 +115,43 @@ static gchar * __query_album_get_uri(mpd_Song *song, const gchar*data, gsize siz if(cur) { xmlNodePtr cur2 = get_first_node_by_name(cur,"result"); - if(cur2) { - xmlNodePtr cur4 = get_first_node_by_name(cur2,"title"); - if(cur4){ - xmlChar *title = xmlNodeGetContent(cur4); - - if(title) - { - char *temp_a = g_utf8_casefold((gchar *)title,-1); - /** Todo make this check fuzzy */ - if(strstr((char *)temp_a, temp_b)) - { - xmlNodePtr cur3 = get_first_node_by_name(cur2,"uri"); - if(cur3){ - xmlChar *xurl = xmlNodeGetContent(cur3); - retv = g_strdup((char *)xurl); - xmlFree(xurl); + while(cur2) + { + xmlChar *temp = xmlGetProp(cur2, (xmlChar *)"type"); + if(temp != NULL && xmlStrEqual(temp, (xmlChar *)"release")) + { + xmlNodePtr cur4 = get_first_node_by_name(cur2,"title"); + if(cur4){ + xmlChar *title = xmlNodeGetContent(cur4); + + if(title) + { + char *temp_a = g_utf8_casefold((gchar *)title,-1); + /** Todo make this check fuzzy */ + if(strstr((char *)temp_a, temp_b)) + { + xmlNodePtr cur3 = get_first_node_by_name(cur2,"uri"); + if(cur3){ + xmlChar *xurl = xmlNodeGetContent(cur3); + retv = g_list_prepend(retv, g_strdup((char *)xurl)); + xmlFree(xurl); + } } + g_free(temp_a); } - g_free(temp_a); + if(title)xmlFree(title); } - if(title)xmlFree(title); } - } + if(temp) xmlFree(temp); + cur2 = get_next_node_by_name(cur2, "result"); + }// while (Cur2) } } xmlFreeDoc(doc); } } g_free(temp_b); - return retv; + return g_list_reverse(retv); } static GList *__query_album_get_uri_list(mpd_Song *song, const gchar*data, gsize size) { @@ -210,7 +229,22 @@ static void __query_get_album_art_uris(const GEADAsyncHandler *handle, GEADStatu goffset size; const gchar *data = gmpc_easy_handler_get_data(handle, &size); GList *list = __query_album_get_uri_list(i->song, data, (gsize)size); - i->callback(list, i->data); + i->uris = g_list_concat(i->uris, list); + + if(i->releases) { + int j=0; + char furl[1024]; + char *artist_uri = i->releases->data; + /* Hack to fix bug in discogs api */ + for(j=strlen(artist_uri); artist_uri[j] != '/' && j > 0; j--); + snprintf(furl,1024,DISCOGS_API_ROOT"release%s?f=xml&api_key=%s", &artist_uri[j],DISCOGS_API_KEY); + i->releases = g_list_delete_link(i->releases, i->releases); + g_free(artist_uri); + gmpc_easy_async_downloader(furl, __query_get_album_art_uris, i); + return; + } + + i->callback(i->uris, i->data); g_free(i); return; } @@ -222,17 +256,19 @@ static void __query_get_album_art(const GEADAsyncHandler *handle, GEADStatus sta ii *i = (ii *) data; if(status == GEAD_PROGRESS) return; if(status == GEAD_DONE) { - gchar *artist_uri = NULL; goffset size; char furl[1024]; const gchar *data = gmpc_easy_handler_get_data(handle, &size); - artist_uri = __query_album_get_uri(i->song, data, (gsize)(size)); - if(artist_uri) + i->releases =g_list_first( __query_album_get_uri(i->song, data, (gsize)(size))); + if(i->releases) { int j=0; + char *artist_uri = i->releases->data; /* Hack to fix bug in discogs api */ for(j=strlen(artist_uri); artist_uri[j] != '/' && j > 0; j--); snprintf(furl,1024,DISCOGS_API_ROOT"release%s?f=xml&api_key=%s", &artist_uri[j],DISCOGS_API_KEY); + i->releases = g_list_delete_link(i->releases, i->releases); + g_free(artist_uri); gmpc_easy_async_downloader(furl, __query_get_album_art_uris, i); return; } @@ -246,7 +282,7 @@ static void discogs_fetch_cover_album_art(ii *i) { char *artist = gmpc_easy_download_uri_escape(i->song->artist); char *album = gmpc_easy_download_uri_escape(i->song->album); - debug_printf(DEBUG_INFO, "Trying to fetch: %s:%s\n", artist, album); + g_log(LOG_DOMAIN, G_LOG_LEVEL_INFO, "Trying to fetch: %s:%s\n", artist, album); char furl[1024]; snprintf(furl,1024,DISCOGS_API_ROOT"search?type=all&f=xml&q=%s%%20%s&api_key=%s", artist,album,DISCOGS_API_KEY); gmpc_easy_async_downloader(furl, __query_get_album_art, i); -- 2.11.4.GIT