From 0086278e233e0dbac6f52ee5a2a20621963c5bc7 Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Fri, 27 Mar 2009 16:10:31 +0100 Subject: [PATCH] Adjust to new api and make it check more precise --- src/plugin.c | 178 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 98 insertions(+), 80 deletions(-) diff --git a/src/plugin.c b/src/plugin.c index c4807b4..470adf1 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -47,108 +47,126 @@ static void lyrdb_fetch_cover_priority_set(int priority){ cfg_set_single_value_as_int(config, "lyrdb-plugin", "priority", priority); } -static int lyrdb_fetch_get_lyric(mpd_Song *song,MetaDataType type, char **path) +typedef struct Query { + mpd_Song *song; + void (*callback)(GList *, gpointer); + gpointer user_data; + GList *downloads; + GList *list; +}Query; + +static void lyrdb_download_actual_lyric_callback(const GEADAsyncHandler *handle, GEADStatus status, gpointer data) { - int result = 0; - /* Check if enabled */ - if(lyrdb_get_enabled() == FALSE) - { - return META_DATA_UNAVAILABLE; - } - /* Can I fetch this type? */ - if(type != META_SONG_TXT) + Query *q = (Query *)data; + if(status == GEAD_PROGRESS) return; + if(status == GEAD_DONE) { - return META_DATA_UNAVAILABLE; + goffset size = 0; + const char *data = gmpc_easy_handler_get_data(handle, &size); + if(g_utf8_validate(data, (int)size,NULL)){ + q->list = g_list_append(q->list, g_strdup(data)); + } } - /* Check available fields */ - if(song->artist == NULL || song->title == NULL) + q->downloads = g_list_remove(q->downloads, handle); + if(q->downloads == NULL) { - return META_DATA_UNAVAILABLE; + q->callback(q->list, q->user_data); + g_free(q); } - gchar *artist, *title; - int retv = META_DATA_UNAVAILABLE; - /* escape, for html query */ - artist = g_uri_escape_string(song->artist, NULL, TRUE); - title = g_uri_escape_string(song->title, NULL, TRUE); +} - if(artist && title) +static void lyrdb_download_callback(const GEADAsyncHandler *handle, GEADStatus status, gpointer data) +{ + Query *q = (Query *)data; + int found = FALSE; + if(status == GEAD_PROGRESS) return; + if(status == GEAD_DONE) { - gmpc_easy_download_struct data= {NULL, 0,-1,NULL, NULL}; - /* create the uri */ - gchar *uri_path = g_strdup_printf("http://webservices.lyrdb.com/lookup.php?q=%s%%20%s&for=fullt&agent=gmpc-lyrdb", - artist, title); - debug_printf(DEBUG_INFO,"%s\n", uri_path); - if(gmpc_easy_download(uri_path, &data)) + goffset size= 0; + const char *data = gmpc_easy_handler_get_data(handle, &size); + /* Check error */ + if(size > 6 && strncmp(data, "error:", 6) == 0 ) { - /* Check error */ - if(data.size > 6 && strncmp(data.data, "error:", 6) == 0 ) - { - debug_printf(DEBUG_INFO,"Error occured: %s\n", data.data); - } - else if( data.size > 6) + debug_printf(DEBUG_INFO,"Error occured: %s\n", data); + } + else if(size > 6) + { + /* Parse the file */ + gchar **entries = g_strsplit(data, "\n", -1); + if(entries) { - gboolean found = FALSE; - /* Parse the file */ - gchar **entries = g_strsplit(data.data, "\n", -1); - if(entries) + int i=0; + for(i=0;entries[i];i++) { - int i=0; - for(i=0;entries[i] && !found;i++) + + char **fields = g_strsplit(entries[i],"\\",-1); + if(fields) { - - char **fields = g_strsplit(entries[i],"\\",-1); - if(fields) + /* we need 3 fields, last is always NULL, so this should be safe */ + if( fields[0] && fields[1] && fields[2]) { - /* we need 3 fields, last is always NULL, so this should be safe */ - if( fields[0] && fields[1] && fields[2]) + /** + * TODO: Make this check utf8 + */ + printf("comparing: '%s::%s' and '%s::%s'\n", fields[2], q->song->artist, fields[1], q->song->title); + if(strcasecmp(fields[2], q->song->artist) == 0 && strcasecmp(fields[1], q->song->title) == 0) { - if(strcmp(fields[2], song->artist) == 0) - { - debug_printf(DEBUG_INFO," Goint to download lyric: %s\n", fields[0]); - gmpc_easy_download_clean(&data); - g_free(uri_path); - uri_path = g_strdup_printf("http://www.lyrdb.com/getlyr.php?q=%s", fields[0]); - if(gmpc_easy_download(uri_path, &data)) - { - if(g_utf8_validate(data.data, data.size,NULL)) - { - /* now we want to store it somewhere */ - gchar *full_path = gmpc_get_metadata_filename(META_SONG_TXT, song, NULL); - /* don't need fancy writers/loaders, just dump the file. */ - if(g_file_set_contents(full_path, data.data, data.size, NULL)) - { - *path = full_path; - retv = META_DATA_AVAILABLE; - found = TRUE; - } - else - g_free(full_path); - }else{ - debug_printf(DEBUG_ERROR, "LYRDB returned a non-utf8 lyric, skipping"); - found = FALSE; - } - } - + gchar *uri_path = NULL; + GEADAsyncHandler *handle2; + debug_printf(DEBUG_INFO," Goint to download lyric: %s\n", fields[0]); + uri_path = g_strdup_printf("http://webservices.lyrdb.com/getlyr.php?q=%s", fields[0]); + handle2 = gmpc_easy_async_downloader(uri_path, lyrdb_download_actual_lyric_callback, q); + if(handle2) { + found = TRUE; + q->downloads = g_list_append(q->downloads, handle2); } + g_free(uri_path); } - g_strfreev(fields); } + g_strfreev(fields); } - g_strfreev(entries); } + g_strfreev(entries); } - gmpc_easy_download_clean(&data); - } + } + if(!found) + { + q->callback(q->list, q->user_data); + g_free(q); + } +} + +static void lyrdb_get_uri(mpd_Song *song, MetaDataType type, void (*callback)(GList *list, gpointer data), gpointer user_data) +{ + printf("Lyricwiki plugin api V2\n"); + if(lyrdb_get_enabled() && type == META_SONG_TXT && song && song->artist &&song->title) + { + Query *q = g_malloc0(sizeof(*q)); + gchar *artist = gmpc_easy_download_uri_escape(song->artist); + gchar *title = gmpc_easy_download_uri_escape(song->title); + gchar *uri_path = g_strdup_printf("http://webservices.lyrdb.com/lookup.php?q=%s%%20%s&for=fullt&agent=gmpc-lyrdb", + artist, title); + q->callback = callback; + q->song = song; + q->user_data = user_data; + q->list = NULL; + q->downloads = NULL; + g_free(artist); g_free(title); + printf("Trying: '%s'\n", uri_path); + if(gmpc_easy_async_downloader(uri_path, lyrdb_download_callback, q)!= NULL) + { + g_free(uri_path); + return; + } + g_free(q); g_free(uri_path); } - - if(artist) g_free(artist); - if(title) g_free(title); - return retv; -} + /* Return nothing found */ + callback(NULL, user_data); +} /** * Preferences @@ -187,7 +205,7 @@ static void preferences_destroy(GtkWidget *container) gmpcMetaDataPlugin lw_cover = { .get_priority = lyrdb_fetch_cover_priority, .set_priority = lyrdb_fetch_cover_priority_set, - .get_image = lyrdb_fetch_get_lyric + .get_uris = lyrdb_get_uri }; gmpcPrefPlugin lw_pref = { @@ -204,5 +222,5 @@ gmpcPlugin plugin = { .metadata = &lw_cover, .get_enabled = lyrdb_get_enabled, .set_enabled = lyrdb_set_enabled, - .pref = &lw_pref + .pref = &lw_pref }; -- 2.11.4.GIT