From 6d9b28ec69212a2542ab2958ed0b012c124dbc07 Mon Sep 17 00:00:00 2001
From: Andreas Obergrusberger <tradiaz@yahoo.de>
Date: Wed, 3 Jan 2007 20:12:16 +0000
Subject: added a plugin system for lyrics sources

git-svn-id: https://svn.musicpd.org/ncmpc/branches/tradiaz@5217 09075e82-0dd4-0310-85a5-a0d7c8717e4f
---
 plugins/Makefile.am                    |   6 ++
 plugins/hd/Makefile.am                 |  40 +++++++++
 plugins/hd/lyrics_hd.c                 |  52 +++++++++++
 plugins/leoslyrics/Makefile.am         |  43 +++++++++
 plugins/leoslyrics/easy_download.c     |  89 +++++++++++++++++++
 plugins/leoslyrics/easy_download.h     |  31 +++++++
 plugins/leoslyrics/lyrics_leoslyrics.c | 158 +++++++++++++++++++++++++++++++++
 7 files changed, 419 insertions(+)
 create mode 100644 plugins/Makefile.am
 create mode 100644 plugins/hd/Makefile.am
 create mode 100644 plugins/hd/lyrics_hd.c
 create mode 100644 plugins/leoslyrics/Makefile.am
 create mode 100644 plugins/leoslyrics/easy_download.c
 create mode 100644 plugins/leoslyrics/easy_download.h
 create mode 100644 plugins/leoslyrics/lyrics_leoslyrics.c

(limited to 'plugins')

diff --git a/plugins/Makefile.am b/plugins/Makefile.am
new file mode 100644
index 000000000..9bb784fa3
--- /dev/null
+++ b/plugins/Makefile.am
@@ -0,0 +1,6 @@
+SUBDIRS = \
+	  ${src_lyr_plugins}
+
+AUTOMAKE_OPTIONS =   foreign 1.6
+
+
diff --git a/plugins/hd/Makefile.am b/plugins/hd/Makefile.am
new file mode 100644
index 000000000..f4d4608d8
--- /dev/null
+++ b/plugins/hd/Makefile.am
@@ -0,0 +1,40 @@
+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 = libhd.la 
+
+### libhd
+
+libhd_la_LIBADD =\
+  $(GLIB_LIBS)\
+  $(libcurl_LIBS)
+
+libhd_la_headers =\
+			  $(plugins_headers)
+
+libhd_la_SOURCES =\
+			  lyrics_hd.c\
+			  $(libhd_la_headers)
+install:
+	@$(MAKE)
+	mkdir -p ${ncmpc_modulesdir}
+	if test -f .libs/libhd.so; then cp .libs/libhd.so ${ncmpc_modulesdir}/lyrics_hd.so; fi
+
+uninstall:
+	@$(MAKE)
+	rm -f ${ncmpc_modulesdir}/lyrics_hd.so
diff --git a/plugins/hd/lyrics_hd.c b/plugins/hd/lyrics_hd.c
new file mode 100644
index 000000000..a90ee9d47
--- /dev/null
+++ b/plugins/hd/lyrics_hd.c
@@ -0,0 +1,52 @@
+#include <glib.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "src_lyrics.h"
+
+char *check_lyr_hd(char *artist, char *title, int how)
+{ //checking whether for lyrics file existence and proper access
+        static char path[1024];
+        snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", 
+                        getenv("HOME"), artist, title);
+    
+    if(g_access(path, how) != 0) return NULL;
+                 
+        return path;
+}		
+
+
+int get_lyr_hd(char *artist, char *title)
+{
+        char *path = check_lyr_hd(artist, title, R_OK);
+        if(path == NULL) return -1;
+        
+        FILE *lyr_file;	
+        lyr_file = fopen(path, "r");
+        if(lyr_file == NULL) return -1;
+        
+        char *buf = NULL;
+        char **line = &buf;
+        size_t n = 0;
+        
+        while(1)
+        {
+         n = getline(line, &n, lyr_file); 
+         if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
+         add_text_line(&lyr_text, *line, n);
+         free(*line);
+         *line = NULL; n = 0;
+         }
+        
+        return 0;
+}	
+
+int register_me (src_lyr *source_descriptor)
+{ 
+  source_descriptor->check_lyr = check_lyr_hd;
+  source_descriptor->get_lyr = get_lyr_hd;
+  
+  source_descriptor->name = "Harddisk";
+  source_descriptor->description = "";
+}
diff --git a/plugins/leoslyrics/Makefile.am b/plugins/leoslyrics/Makefile.am
new file mode 100644
index 000000000..4eb76f3ee
--- /dev/null
+++ b/plugins/leoslyrics/Makefile.am
@@ -0,0 +1,43 @@
+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
new file mode 100644
index 000000000..83616cf87
--- /dev/null
+++ b/plugins/leoslyrics/easy_download.c
@@ -0,0 +1,89 @@
+/*	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
new file mode 100644
index 000000000..92dc22e9e
--- /dev/null
+++ b/plugins/leoslyrics/easy_download.h
@@ -0,0 +1,31 @@
+/*
+ *  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
new file mode 100644
index 000000000..47aabe10b
--- /dev/null
+++ b/plugins/leoslyrics/lyrics_leoslyrics.c
@@ -0,0 +1,158 @@
+#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 = 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)
+        {
+                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
+        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;
+        
+}
+#if SRC_LYR_LEOSLYRICS == plugin
+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";
+}
+#endif
-- 
cgit v1.2.3