diff options
author | Max Kellermann <max@duempel.org> | 2008-09-16 19:11:40 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-09-16 19:11:40 +0200 |
commit | f41b9942af7278ab67dc799ad6c17ad74dc0aa1b (patch) | |
tree | 69c7c715ff3abd78bb26a7a4d80dd1da5fd5c08b /plugins/leoslyrics | |
parent | 4d01c183b4c1e4c51dff3d9aeec1c01ce13a4323 (diff) | |
download | mpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.tar.gz mpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.tar.xz mpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.zip |
lyrics: converted in-process plugins to external programs
In-process plugins are very problematic. It is much easier and
flexible to move the lyrics plugins to external programs, with a
trivial protocol. This is work in progress, among the things missing:
- protocol specification, including exit codes
- plugin installation
- plugin search directory
- run-time configuration (currently hard coded)
- automatic polling (using glib's main loop?)
- better and more robust error handling
Diffstat (limited to 'plugins/leoslyrics')
-rw-r--r-- | plugins/leoslyrics/Makefile.am | 43 | ||||
-rw-r--r-- | plugins/leoslyrics/easy_download.c | 89 | ||||
-rw-r--r-- | plugins/leoslyrics/easy_download.h | 31 | ||||
-rw-r--r-- | plugins/leoslyrics/lyrics_leoslyrics.c | 162 |
4 files changed, 0 insertions, 325 deletions
diff --git a/plugins/leoslyrics/Makefile.am b/plugins/leoslyrics/Makefile.am deleted file mode 100644 index 4eb76f3ee..000000000 --- a/plugins/leoslyrics/Makefile.am +++ /dev/null @@ -1,43 +0,0 @@ -AM_CPPFLAGS =\ - $(GLIB_CFLAGS)\ - $(GTHREAD_CFLAGS)\ - $(libcurl_CFLAGS)\ - -I../../src - -###---- - -plugin_headers =\ - ../src_lyrics.h\ - ../screen_lyrics.h\ - ../easy_download.h\ - ../options.h - -###----- - -ncmpc_modulesdir = @plugindir@ - -ncmpc_modules_LTLIBRARIES = libleoslyrics.la - -### libleoslyrics - -libleoslyrics_la_LIBADD =\ - $(GLIB_LIBS)\ - $(libcurl_LIBS) - -libleoslyrics_la_headers =\ - easy_download.h\ - $(plugins_headers) - -libleoslyrics_la_SOURCES =\ - easy_download.c\ - lyrics_leoslyrics.c\ - $(libleoslyrics_la_headers) - -install: - @$(MAKE) - mkdir -p ${DESTDIR}${libdir} - if test -f .libs/libleoslyrics.so; then cp .libs/libleoslyrics.so ${ncmpc_modulesdir}/lyrics_leoslyrics.so; fi - -uninstall: - @$(MAKE) - rm -f ${ncmpc_modulesdir}/lyrics_leoslyrics.so diff --git a/plugins/leoslyrics/easy_download.c b/plugins/leoslyrics/easy_download.c deleted file mode 100644 index 83616cf87..000000000 --- a/plugins/leoslyrics/easy_download.c +++ /dev/null @@ -1,89 +0,0 @@ -/* by Qball - * - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -# - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <glib.h> -#include "ncmpc.h" - -#ifdef ENABLE_LYRICS_SCREEN - -#include <curl/curl.h> -#include "easy_download.h" - -static size_t write_data(void *buffer, size_t size, - size_t nmemb, easy_download_struct *dld) -{ - if(dld->data == NULL) - { - dld->size = 0; - } - dld->data = g_realloc(dld->data,(gulong)(size*nmemb+dld->size)); - - memset(&(dld->data)[dld->size], '\0', (size*nmemb)); - memcpy(&(dld->data)[dld->size], buffer, size*nmemb); - - dld->size += size*nmemb; - if(dld->size >= dld->max_size && dld->max_size > 0) - { - return 0; - } - return size*nmemb; -} - -int easy_download(char *url,easy_download_struct *dld, - curl_progress_callback cb) -{ - CURL *curl = NULL; - int res; - if(!dld) return 0; - easy_download_clean(dld); - /* initialize curl */ - curl = curl_easy_init(); - if(!curl) return 0; - /* set uri */ - curl_easy_setopt(curl, CURLOPT_URL, url); - /* set callback data */ - curl_easy_setopt(curl, CURLOPT_WRITEDATA, dld); - /* set callback function */ - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb); - /* run */ - res = curl_easy_perform(curl); - /* cleanup */ - curl_easy_cleanup(curl); - if(!res) - { - return 1; - } - if(dld->data) g_free(dld->data); - return 0; -} - -void easy_download_clean(easy_download_struct *dld) -{ - if(dld->data)g_free(dld->data); - dld->data = NULL; - dld->size = 0; - dld->max_size = -1; -} - -#endif diff --git a/plugins/leoslyrics/easy_download.h b/plugins/leoslyrics/easy_download.h deleted file mode 100644 index 92dc22e9e..000000000 --- a/plugins/leoslyrics/easy_download.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * by Qball - * - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include <curl/curl.h> - -typedef struct _easy_download_struct{ - char *data; - int size; - int max_size; - -}easy_download_struct; - - -int easy_download(char *url,easy_download_struct *dld, - curl_progress_callback cb); -void easy_download_clean(easy_download_struct *dld); diff --git a/plugins/leoslyrics/lyrics_leoslyrics.c b/plugins/leoslyrics/lyrics_leoslyrics.c deleted file mode 100644 index 5bcb32242..000000000 --- a/plugins/leoslyrics/lyrics_leoslyrics.c +++ /dev/null @@ -1,162 +0,0 @@ -#include "src_lyrics.h" -#include "easy_download.h" -#include <expat.h> -#include <string.h> -#include "options.h" - -#define LEOSLYRICS_SEARCH_URL "http://api.leoslyrics.com/api_search.php?auth=ncmpc&artist=%s&songtitle=%s" -#define LEOSLYRICS_CONTENT_URL "http://api.leoslyrics.com/api_lyrics.php?auth=ncmpc&hid=%s" -#define CREDITS "Lyrics provided by www.LeosLyrics.com" - -char *hid; -XML_Parser parser, contentp; - -int check_dl_progress(void *clientp, double dltotal, double dlnow, - double ultotal, double ulnow) -{ - if(g_timer_elapsed(dltime, NULL) >= options.lyrics_timeout || lock == 4) - { - formed_text_init(&lyr_text); - return -1; - } - - return 0; -} - - - -static void check_content(void *data, const char *name, const char **atts) -{ - if(strstr(name, "text") != NULL) - { - - result |= 16; - } -} - - -static void check_search_response(void *data, const char *name, - const char **atts) -{ - if(strstr(name, "response") != NULL) - { - result |=2; - return; - } - - if(result & 4) - { - if(strstr(name, "result") != NULL) - { - if(strstr(atts[2], "hid") != NULL) - { - hid = strdup (atts[3]); - } - - if(strstr(atts[2], "exactMatch") != NULL) - { - result |= 8; - } - } - } - -} - -static void end_tag(void *data, const char *name) -{ - //hmmmmmm -} - - static void check_search_success(void *userData, const XML_Char *s, int len) - { - if(result & 2) //lets first check whether we're right - { //we don't really want to search in the wrong string - if(strstr((char*) s, "SUCCESS")) - { - result |=4; - } - } - } - -static void fetch_text(void *userData, const XML_Char *s, int len) -{ - if(result & 16) - { - if (s[0] == 13 ) return; //ignore any single carriage returns - add_text_line(&lyr_text, s, len); - } -} - -/*int deregister_lyr_leoslyrics () -{ - -}*/ - -int check_lyr_leoslyrics(char *artist, char *title, char *url) -{ - char url_avail[256]; - - //this replacess the whitespaces with '+' - g_strdelimit(artist, " ", '+'); - g_strdelimit(title, " ", '+'); - - //we insert the artist and the title into the url - snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title); - - //download that xml! - easy_download_struct lyr_avail = {NULL, 0,-1}; - - g_timer_start(dltime); - if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return -1; - g_timer_stop(dltime); - - //we gotta parse that stuff with expat - parser = XML_ParserCreate(NULL); - XML_SetUserData(parser, NULL); - - XML_SetElementHandler(parser, check_search_response, end_tag); - XML_SetCharacterDataHandler(parser, check_search_success); - XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), 0); - XML_ParserFree(parser); - - if(!(result & 4)) return -1; //check whether lyrics found - - CURL *curl = curl_easy_init (); - char *esc_hid = curl_easy_escape (curl, hid, 0); - free (hid); - - snprintf(url, 512, LEOSLYRICS_CONTENT_URL, hid); - - return 0; -} - -int get_lyr_leoslyrics(char *artist, char *title) -{ - char url_hid[256]; - if(dltime == NULL) dltime = g_timer_new(); - - if(check_lyr_leoslyrics(artist, title, url_hid) != 0) return -1; - - easy_download_struct lyr_content = {NULL, 0,-1}; - g_timer_continue(dltime); - if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return -1; - g_timer_stop(dltime); - - contentp = XML_ParserCreate(NULL); - XML_SetUserData(contentp, NULL); - XML_SetElementHandler(contentp, check_content, end_tag); - XML_SetCharacterDataHandler(contentp, fetch_text); - XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), 0); - XML_ParserFree(contentp); - - return 0; - -} -int register_me (src_lyr *source_descriptor) -{ - source_descriptor->check_lyr = check_lyr_leoslyrics; - source_descriptor->get_lyr = get_lyr_leoslyrics; - - source_descriptor->name = "Leoslyrics"; - source_descriptor->description = "powered by http://www.leoslyrics.com"; -} |