From 6217bf04c2b28ffb586674184e29b25c8318bfa6 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Mon, 2 Feb 2009 12:00:19 +0100 Subject: [PATCH] add support for lyricsplugin database --- doc/config | 10 +++++++ src/lyrics.cpp | 73 +++++++++++++++++++++++++++++++++++++++--------- src/lyrics.h | 9 ++++++ src/settings.cpp | 6 ++++ src/settings.h | 1 + 5 files changed, 86 insertions(+), 13 deletions(-) diff --git a/doc/config b/doc/config index 0c331078..a2114b76 100644 --- a/doc/config +++ b/doc/config @@ -146,6 +146,16 @@ # #enable_window_title = "yes" # +##### lyrics support ##### +## +## supported lyrics databases: +## +## - 1 - lyricwiki.org +## - 2 - lyricsplugin.com +## +# +#lyrics_database = "1" +# ##### colors definitions ##### # #colors_enabled = "yes" diff --git a/src/lyrics.cpp b/src/lyrics.cpp index f03551f3..cd159dda 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -31,8 +31,8 @@ extern Window *wCurrent; extern Scrollpad *sLyrics; extern Scrollpad *sInfo; -const string artists_folder = home_folder + "/" + ".ncmpcpp/artists"; -const string lyrics_folder = home_folder + "/" + ".lyrics"; +const string artists_folder = home_folder + "/.ncmpcpp/artists"; +const string lyrics_folder = home_folder + "/.lyrics"; #ifdef HAVE_CURL_CURL_H extern pthread_t lyrics_downloader; @@ -40,7 +40,6 @@ extern pthread_t artist_info_downloader; extern bool lyrics_ready; extern bool artist_info_ready; pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER; -#endif namespace { @@ -101,7 +100,6 @@ namespace } } -#ifdef HAVE_CURL_CURL_H void * GetArtistInfo(void *ptr) { string *strptr = static_cast(ptr); @@ -253,6 +251,54 @@ void * GetArtistInfo(void *ptr) artist_info_ready = 1; pthread_exit(NULL); } + +namespace +{ + bool lyricwiki_not_found(const string &s) + { + return s == "Not found"; + } + + bool lyricsplugin_not_found(const string &s) + { + if (s.empty()) + return true; + for (string::const_iterator it = s.begin(); it != s.end(); it++) + if (isprint(*it)) + return false; + return true; + } + + const LyricsPlugin lyricwiki = + { + "http://lyricwiki.org/api.php?artist=%artist%&song=%title%&fmt=xml", + "", + "", + lyricwiki_not_found + }; + + const LyricsPlugin lyricsplugin = + { + "http://www.lyricsplugin.com/winamp03/plugin/?artist=%artist%&title=%title%", + "
", + "
", + lyricsplugin_not_found + }; + + const LyricsPlugin *ChooseLyricsPlugin(int i) + { + switch (i) + { + case 1: + return &lyricwiki; + case 2: + return &lyricsplugin; + default: + return &lyricwiki; + } + } +} + #endif // HAVE_CURL_CURL_H void *GetLyrics(void *song) @@ -289,16 +335,16 @@ void *GetLyrics(void *song) # ifdef HAVE_CURL_CURL_H CURLcode code; + const LyricsPlugin *my_lyrics = ChooseLyricsPlugin(Config.lyrics_db); + string result; char *c_artist = curl_easy_escape(0, artist.c_str(), artist.length()); char *c_title = curl_easy_escape(0, title.c_str(), title.length()); - string url = "http://lyricwiki.org/api.php?artist="; - url += c_artist; - url += "&song="; - url += c_title; - url += "&fmt=xml"; + string url = my_lyrics->url; + url.replace(url.find("%artist%"), 8, c_artist); + url.replace(url.find("%title%"), 7, c_title); pthread_mutex_lock(&curl); CURL *lyrics = curl_easy_init(); @@ -322,13 +368,13 @@ void *GetLyrics(void *song) } size_t a, b; - a = result.find("")+8; - b = result.find(""); + a = result.find(my_lyrics->tag_open)+strlen(my_lyrics->tag_open); + b = result.find(my_lyrics->tag_close, a); result = result.substr(a, b-a); - if (result == "Not found") + if (my_lyrics->not_found(result)) { - *sLyrics << result; + *sLyrics << "Not found"; lyrics_ready = 1; pthread_exit(NULL); } @@ -339,6 +385,7 @@ void *GetLyrics(void *song) result.replace(i, 4, ">"); EscapeHtml(result); + Trim(result); *sLyrics << utf_to_locale_cpy(result); diff --git a/src/lyrics.h b/src/lyrics.h index e29a27c2..1834f8a3 100644 --- a/src/lyrics.h +++ b/src/lyrics.h @@ -27,6 +27,15 @@ # include # include "curl/curl.h" void * GetArtistInfo(void *); + +struct LyricsPlugin +{ + const char *url; + const char *tag_open; + const char *tag_close; + bool (*not_found)(const string &); +}; + #endif void * GetLyrics(void *); diff --git a/src/settings.cpp b/src/settings.cpp index 964ce9ed..a0efb58d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -284,6 +284,7 @@ void DefaultConfiguration(ncmpcpp_config &conf) conf.seek_time = 1; conf.playlist_disable_highlight_delay = 5; conf.message_delay_time = 4; + conf.lyrics_db = 1; } string GetLineValue(string &line, char a, char b, bool once) @@ -699,6 +700,11 @@ void ReadConfiguration(ncmpcpp_config &conf) { conf.set_window_title = v == "yes"; } + else if (cl.find("lyrics_database") != string::npos) + { + if (!v.empty()) + conf.lyrics_db = StrToInt(v); + } else if (cl.find("song_window_title_format") != string::npos) { if (!v.empty()) diff --git a/src/settings.h b/src/settings.h index a472c03f..1d6d7e17 100644 --- a/src/settings.h +++ b/src/settings.h @@ -157,6 +157,7 @@ struct ncmpcpp_config int seek_time; int playlist_disable_highlight_delay; int message_delay_time; + int lyrics_db; }; extern ncmpcpp_config Config;