add support for lyricsplugin database

This commit is contained in:
Andrzej Rybczak
2009-02-02 12:00:19 +01:00
parent e59b14c56d
commit 6217bf04c2
5 changed files with 86 additions and 13 deletions

View File

@@ -146,6 +146,16 @@
# #
#enable_window_title = "yes" #enable_window_title = "yes"
# #
##### lyrics support #####
##
## supported lyrics databases:
##
## - 1 - lyricwiki.org
## - 2 - lyricsplugin.com
##
#
#lyrics_database = "1"
#
##### colors definitions ##### ##### colors definitions #####
# #
#colors_enabled = "yes" #colors_enabled = "yes"

View File

@@ -31,8 +31,8 @@ extern Window *wCurrent;
extern Scrollpad *sLyrics; extern Scrollpad *sLyrics;
extern Scrollpad *sInfo; extern Scrollpad *sInfo;
const string artists_folder = home_folder + "/" + ".ncmpcpp/artists"; const string artists_folder = home_folder + "/.ncmpcpp/artists";
const string lyrics_folder = home_folder + "/" + ".lyrics"; const string lyrics_folder = home_folder + "/.lyrics";
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
extern pthread_t lyrics_downloader; extern pthread_t lyrics_downloader;
@@ -40,7 +40,6 @@ extern pthread_t artist_info_downloader;
extern bool lyrics_ready; extern bool lyrics_ready;
extern bool artist_info_ready; extern bool artist_info_ready;
pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER;
#endif
namespace namespace
{ {
@@ -101,7 +100,6 @@ namespace
} }
} }
#ifdef HAVE_CURL_CURL_H
void * GetArtistInfo(void *ptr) void * GetArtistInfo(void *ptr)
{ {
string *strptr = static_cast<string *>(ptr); string *strptr = static_cast<string *>(ptr);
@@ -253,6 +251,54 @@ void * GetArtistInfo(void *ptr)
artist_info_ready = 1; artist_info_ready = 1;
pthread_exit(NULL); 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",
"<lyrics>",
"</lyrics>",
lyricwiki_not_found
};
const LyricsPlugin lyricsplugin =
{
"http://www.lyricsplugin.com/winamp03/plugin/?artist=%artist%&title=%title%",
"<div id=\"lyrics\">",
"</div>",
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 #endif // HAVE_CURL_CURL_H
void *GetLyrics(void *song) void *GetLyrics(void *song)
@@ -289,16 +335,16 @@ void *GetLyrics(void *song)
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
CURLcode code; CURLcode code;
const LyricsPlugin *my_lyrics = ChooseLyricsPlugin(Config.lyrics_db);
string result; string result;
char *c_artist = curl_easy_escape(0, artist.c_str(), artist.length()); char *c_artist = curl_easy_escape(0, artist.c_str(), artist.length());
char *c_title = curl_easy_escape(0, title.c_str(), title.length()); char *c_title = curl_easy_escape(0, title.c_str(), title.length());
string url = "http://lyricwiki.org/api.php?artist="; string url = my_lyrics->url;
url += c_artist; url.replace(url.find("%artist%"), 8, c_artist);
url += "&song="; url.replace(url.find("%title%"), 7, c_title);
url += c_title;
url += "&fmt=xml";
pthread_mutex_lock(&curl); pthread_mutex_lock(&curl);
CURL *lyrics = curl_easy_init(); CURL *lyrics = curl_easy_init();
@@ -322,13 +368,13 @@ void *GetLyrics(void *song)
} }
size_t a, b; size_t a, b;
a = result.find("<lyrics>")+8; a = result.find(my_lyrics->tag_open)+strlen(my_lyrics->tag_open);
b = result.find("</lyrics>"); b = result.find(my_lyrics->tag_close, a);
result = result.substr(a, b-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; lyrics_ready = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -339,6 +385,7 @@ void *GetLyrics(void *song)
result.replace(i, 4, ">"); result.replace(i, 4, ">");
EscapeHtml(result); EscapeHtml(result);
Trim(result);
*sLyrics << utf_to_locale_cpy(result); *sLyrics << utf_to_locale_cpy(result);

View File

@@ -27,6 +27,15 @@
# include <pthread.h> # include <pthread.h>
# include "curl/curl.h" # include "curl/curl.h"
void * GetArtistInfo(void *); void * GetArtistInfo(void *);
struct LyricsPlugin
{
const char *url;
const char *tag_open;
const char *tag_close;
bool (*not_found)(const string &);
};
#endif #endif
void * GetLyrics(void *); void * GetLyrics(void *);

View File

@@ -284,6 +284,7 @@ void DefaultConfiguration(ncmpcpp_config &conf)
conf.seek_time = 1; conf.seek_time = 1;
conf.playlist_disable_highlight_delay = 5; conf.playlist_disable_highlight_delay = 5;
conf.message_delay_time = 4; conf.message_delay_time = 4;
conf.lyrics_db = 1;
} }
string GetLineValue(string &line, char a, char b, bool once) 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"; 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) else if (cl.find("song_window_title_format") != string::npos)
{ {
if (!v.empty()) if (!v.empty())

View File

@@ -157,6 +157,7 @@ struct ncmpcpp_config
int seek_time; int seek_time;
int playlist_disable_highlight_delay; int playlist_disable_highlight_delay;
int message_delay_time; int message_delay_time;
int lyrics_db;
}; };
extern ncmpcpp_config Config; extern ncmpcpp_config Config;