make fetching lyrics and artist's info work in background

This commit is contained in:
unK
2008-09-27 20:33:30 +02:00
parent de70c6ceff
commit d61cb54653
4 changed files with 153 additions and 77 deletions

View File

@@ -21,11 +21,17 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "lyrics.h" #include "lyrics.h"
#include "settings.h" #include "settings.h"
#include "song.h"
extern ncmpcpp_config Config; extern ncmpcpp_config Config;
const string lyrics_folder = home_folder + "/" + ".lyrics"; const string lyrics_folder = home_folder + "/" + ".lyrics";
#ifdef HAVE_CURL_CURL_H
pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER;
bool data_ready = 0;
#endif
size_t write_data(char *buffer, size_t size, size_t nmemb, string data) size_t write_data(char *buffer, size_t size, size_t nmemb, string data)
{ {
int result = 0; int result = 0;
@@ -53,21 +59,27 @@ void EscapeHtml(string &str)
} }
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
string GetArtistInfo(string artist) void * GetArtistInfo(void *ptr)
{ {
string *strptr = static_cast<string *>(ptr);
string artist = *strptr;
delete strptr;
const string filename = artist + ".txt"; const string filename = artist + ".txt";
const string fullpath = lyrics_folder + "/" + filename; const string fullpath = lyrics_folder + "/" + filename;
mkdir(lyrics_folder.c_str(), 0755); mkdir(lyrics_folder.c_str(), 0755);
string result; string *result = new string();
std::ifstream input(fullpath.c_str()); std::ifstream input(fullpath.c_str());
if (input.is_open()) if (input.is_open())
{ {
string line; string line;
while (getline(input, line)) while (getline(input, line))
result += line + "\n"; *result += line + "\n";
return result.substr(0, result.length()-1); *result = result->substr(0, result->length()-1);
data_ready = 1;
pthread_exit(result);
} }
for (string::iterator it = artist.begin(); it != artist.end(); it++) for (string::iterator it = artist.begin(); it != artist.end(); it++)
@@ -78,126 +90,140 @@ string GetArtistInfo(string artist)
string url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" + artist + "&api_key=d94e5b6e26469a2d1ffae8ef20131b79"; string url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" + artist + "&api_key=d94e5b6e26469a2d1ffae8ef20131b79";
pthread_mutex_lock(&curl);
CURL *info = curl_easy_init(); CURL *info = curl_easy_init();
curl_easy_setopt(info, CURLOPT_URL, url.c_str()); curl_easy_setopt(info, CURLOPT_URL, url.c_str());
curl_easy_setopt(info, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(info, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(info, CURLOPT_WRITEDATA, &result); curl_easy_setopt(info, CURLOPT_WRITEDATA, result);
curl_easy_setopt(info, CURLOPT_CONNECTTIMEOUT, 10); curl_easy_setopt(info, CURLOPT_CONNECTTIMEOUT, 10);
code = curl_easy_perform(info); code = curl_easy_perform(info);
curl_easy_cleanup(info); curl_easy_cleanup(info);
pthread_mutex_unlock(&curl);
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
result = "Error while fetching artist's info: " + string(curl_easy_strerror(code)); *result = "Error while fetching artist's info: " + string(curl_easy_strerror(code));
return result; data_ready = 1;
pthread_exit(result);
} }
int a, b; int a, b;
bool erase = 0; bool erase = 0;
bool save = 1; bool save = 1;
a = result.find("status=\"failed\""); a = result->find("status=\"failed\"");
if (a != string::npos) if (a != string::npos)
{ {
EscapeHtml(result); EscapeHtml(*result);
return "Last.fm returned an error message: " + result; *result = "Last.fm returned an error message: " + *result;
data_ready = 1;
pthread_exit(result);
} }
vector<string> similar; vector<string> similar;
for (int i = result.find("<name>"); i != string::npos; i = result.find("<name>")) for (int i = result->find("<name>"); i != string::npos; i = result->find("<name>"))
{ {
result[i] = '.'; (*result)[i] = '.';
int j = result.find("</name>"); int j = result->find("</name>");
result[j] = '.'; (*result)[j] = '.';
i += 6; i += 6;
similar.push_back(result.substr(i, j-i)); similar.push_back(result->substr(i, j-i));
EscapeHtml(similar.back()); EscapeHtml(similar.back());
} }
vector<string> urls; vector<string> urls;
for (int i = result.find("<url>"); i != string::npos; i = result.find("<url>")) for (int i = result->find("<url>"); i != string::npos; i = result->find("<url>"))
{ {
result[i] = '.'; (*result)[i] = '.';
int j = result.find("</url>"); int j = result->find("</url>");
result[j] = '.'; (*result)[j] = '.';
i += 5; i += 5;
urls.push_back(result.substr(i, j-i)); urls.push_back(result->substr(i, j-i));
} }
a = result.find("<content>")+9; a = result->find("<content>")+9;
b = result.find("</content>"); b = result->find("</content>");
if (a == b) if (a == b)
{ {
result = "No description available for this artist."; *result = "No description available for this artist.";
save = 0; save = 0;
} }
else else
{ {
a += 9; // for <![CDATA[ a += 9; // for <![CDATA[
b -= 3; // for ]]> b -= 3; // for ]]>
result = result.substr(a, b-a); *result = result->substr(a, b-a);
} }
EscapeHtml(result); EscapeHtml(*result);
for (int i = 0; i < result.length(); i++) for (int i = 0; i < result->length(); i++)
{ {
if (erase) if (erase)
{ {
result.erase(result.begin()+i); result->erase(result->begin()+i);
erase = 0; erase = 0;
} }
if (result[i] == 13) if ((*result)[i] == 13)
{ {
result[i] = '\n'; (*result)[i] = '\n';
erase = 1; erase = 1;
} }
else if (result[i] == '\t') else if ((*result)[i] == '\t')
result[i] = ' '; (*result)[i] = ' ';
} }
int i = result.length(); int i = result->length();
if (!isgraph(result[i-1])) if (!isgraph((*result)[i-1]))
{ {
while (!isgraph(result[--i])); while (!isgraph((*result)[--i]));
result = result.substr(0, i+1); *result = result->substr(0, i+1);
} }
result += "\n\n[.b]Similar artists:[/b]\n"; *result += "\n\n[.b]Similar artists:[/b]\n";
for (int i = 1; i < similar.size(); i++) for (int i = 1; i < similar.size(); i++)
result += "\n [." + Config.color2 + "]*[/" + Config.color2 + "] " + similar[i] + " (" + urls[i] + ")"; *result += "\n [." + Config.color2 + "]*[/" + Config.color2 + "] " + similar[i] + " (" + urls[i] + ")";
result += "\n\n" + urls.front(); *result += "\n\n" + urls.front();
if (save) if (save)
{ {
std::ofstream output(fullpath.c_str()); std::ofstream output(fullpath.c_str());
if (output.is_open()) if (output.is_open())
{ {
output << result; output << *result;
output.close(); output.close();
} }
} }
return result; data_ready = 1;
pthread_exit(result);
} }
#endif // HAVE_CURL_CURL_H #endif // HAVE_CURL_CURL_H
string GetLyrics(string artist, string song) void * GetLyrics(void *song)
{ {
const string filename = artist + " - " + song + ".txt"; string artist = static_cast<Song *>(song)->GetArtist();
string title = static_cast<Song *>(song)->GetTitle();
const string filename = artist + " - " + title + ".txt";
const string fullpath = lyrics_folder + "/" + filename; const string fullpath = lyrics_folder + "/" + filename;
mkdir(lyrics_folder.c_str(), 0755); mkdir(lyrics_folder.c_str(), 0755);
string result; string *result = new string();
std::ifstream input(fullpath.c_str()); std::ifstream input(fullpath.c_str());
if (input.is_open()) if (input.is_open())
{ {
string line; string line;
while (getline(input, line)) while (getline(input, line))
result += line + "\n"; *result += line + "\n";
return result.substr(0, result.length()-1); *result = result->substr(0, result->length()-1);
# ifdef HAVE_CURL_CURL_H
data_ready = 1;
pthread_exit(result);
# endif
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
@@ -205,53 +231,63 @@ string GetLyrics(string artist, string song)
if (*it == ' ') if (*it == ' ')
*it = '+'; *it = '+';
for (string::iterator it = song.begin(); it != song.end(); it++) for (string::iterator it = title.begin(); it != title.end(); it++)
if (*it == ' ') if (*it == ' ')
*it = '+'; *it = '+';
CURLcode code; CURLcode code;
string url = "http://lyricwiki.org/api.php?artist=" + artist + "&song=" + song + "&fmt=xml"; string url = "http://lyricwiki.org/api.php?artist=" + artist + "&song=" + title + "&fmt=xml";
pthread_mutex_lock(&curl);
CURL *lyrics = curl_easy_init(); CURL *lyrics = curl_easy_init();
curl_easy_setopt(lyrics, CURLOPT_URL, url.c_str()); curl_easy_setopt(lyrics, CURLOPT_URL, url.c_str());
curl_easy_setopt(lyrics, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(lyrics, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(lyrics, CURLOPT_WRITEDATA, &result); curl_easy_setopt(lyrics, CURLOPT_WRITEDATA, result);
curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 10); curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 10);
code = curl_easy_perform(lyrics); code = curl_easy_perform(lyrics);
curl_easy_cleanup(lyrics); curl_easy_cleanup(lyrics);
pthread_mutex_unlock(&curl);
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
result = "Error while fetching lyrics: " + string(curl_easy_strerror(code)); *result = "Error while fetching lyrics: " + string(curl_easy_strerror(code));
return result; data_ready = 1;
pthread_exit(result);
} }
int a, b; int a, b;
a = result.find("<lyrics>")+8; a = result->find("<lyrics>")+8;
b = result.find("</lyrics>"); b = result->find("</lyrics>");
result = result.substr(a, b-a); *result = result->substr(a, b-a);
if (result == "Not found") if (*result == "Not found")
return result; {
data_ready = 1;
pthread_exit(result);
}
for (int i = result.find("&lt;"); i != string::npos; i = result.find("&lt;")) for (int i = result->find("&lt;"); i != string::npos; i = result->find("&lt;"))
result.replace(i, 4, "<"); result->replace(i, 4, "<");
for (int i = result.find("&gt;"); i != string::npos; i = result.find("&gt;")) for (int i = result->find("&gt;"); i != string::npos; i = result->find("&gt;"))
result.replace(i, 4, ">"); result->replace(i, 4, ">");
EscapeHtml(result); EscapeHtml(*result);
std::ofstream output(fullpath.c_str()); std::ofstream output(fullpath.c_str());
if (output.is_open()) if (output.is_open())
{ {
output << result; output << *result;
output.close(); output.close();
} }
data_ready = 1;
pthread_exit(result);
# else # else
result = "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support."; else
# endif *result = "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support.";
return result; return result;
# endif
} }

View File

@@ -25,12 +25,13 @@
#include <fstream> #include <fstream>
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
# include <pthread.h>
# include "curl/curl.h" # include "curl/curl.h"
string GetArtistInfo(string); void * GetArtistInfo(void *);
#endif #endif
void EscapeHtml(string &); void EscapeHtml(string &);
string GetLyrics(string, string); void * GetLyrics(void *);
#endif #endif

View File

@@ -106,6 +106,12 @@ string editor_highlighted_dir;
NcmpcppScreen current_screen; NcmpcppScreen current_screen;
NcmpcppScreen prev_screen; NcmpcppScreen prev_screen;
#ifdef HAVE_CURL_CURL_H
pthread_t lyrics_downloader;
pthread_t artist_info_downloader;
extern bool data_ready;
#endif
bool dont_change_now_playing = 0; bool dont_change_now_playing = 0;
bool block_progressbar_update = 0; bool block_progressbar_update = 0;
bool block_statusbar_update = 0; bool block_statusbar_update = 0;
@@ -669,6 +675,29 @@ int main(int argc, char *argv[])
else else
reload_lyrics = 0; reload_lyrics = 0;
} }
# ifdef HAVE_CURL_CURL_H
if (data_ready)
{
void *result;
string *str_result = 0;
if (lyrics_downloader)
{
pthread_join(lyrics_downloader, &result);
str_result = static_cast<string *>(result);
sLyrics->Add(*str_result);
lyrics_downloader = 0;
}
if (artist_info_downloader)
{
pthread_join(artist_info_downloader, &result);
str_result = static_cast<string *>(result);
sInfo->Add(*str_result);
artist_info_downloader = 0;
}
delete str_result;
data_ready = 0;
}
# endif
// lyrics end // lyrics end
if (Config.columns_in_playlist && wCurrent == mPlaylist) if (Config.columns_in_playlist && wCurrent == mPlaylist)
@@ -3097,45 +3126,47 @@ int main(int argc, char *argv[])
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty()) || (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())
|| (wCurrent == mEditorTags && !mEditorTags->Empty())) || (wCurrent == mEditorTags && !mEditorTags->Empty()))
{ {
string artist; string *artist = new string();
int id = wCurrent->GetChoice(); int id = wCurrent->GetChoice();
switch (current_screen) switch (current_screen)
{ {
case csPlaylist: case csPlaylist:
artist = mPlaylist->at(id).GetArtist(); *artist = mPlaylist->at(id).GetArtist();
break; break;
case csBrowser: case csBrowser:
artist = mBrowser->at(id).song->GetArtist(); *artist = mBrowser->at(id).song->GetArtist();
break; break;
case csSearcher: case csSearcher:
artist = mSearcher->at(id).second.GetArtist(); *artist = mSearcher->at(id).second.GetArtist();
break; break;
case csLibrary: case csLibrary:
artist = mLibArtists->at(id); *artist = mLibArtists->at(id);
break; break;
case csPlaylistEditor: case csPlaylistEditor:
artist = mPlaylistEditor->at(id).GetArtist(); *artist = mPlaylistEditor->at(id).GetArtist();
break; break;
case csTagEditor: case csTagEditor:
artist = mEditorTags->at(id).GetArtist(); *artist = mEditorTags->at(id).GetArtist();
break; break;
default: default:
break; break;
} }
if (artist != UNKNOWN_ARTIST) if (*artist != UNKNOWN_ARTIST)
{ {
wPrev = wCurrent; wPrev = wCurrent;
wCurrent = sInfo; wCurrent = sInfo;
prev_screen = current_screen; prev_screen = current_screen;
current_screen = csInfo; current_screen = csInfo;
redraw_header = 1; redraw_header = 1;
info_title = "Artist's info - " + artist; info_title = "Artist's info - " + *artist;
sInfo->Clear(); sInfo->Clear();
sInfo->WriteXY(0, 0, "Fetching artist's info..."); sInfo->WriteXY(0, 0, "Fetching artist's info...");
sInfo->Refresh(); sInfo->Refresh();
sInfo->Add(GetArtistInfo(artist)); if (!artist_info_downloader)
sInfo->Hide(); pthread_create(&artist_info_downloader, NULL, GetArtistInfo, artist);
} }
else
delete artist;
} }
} }
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
@@ -3220,7 +3251,14 @@ int main(int argc, char *argv[])
lyrics_title = "Lyrics: " + s->GetArtist() + " - " + s->GetTitle(); lyrics_title = "Lyrics: " + s->GetArtist() + " - " + s->GetTitle();
sLyrics->WriteXY(0, 0, "Fetching lyrics..."); sLyrics->WriteXY(0, 0, "Fetching lyrics...");
sLyrics->Refresh(); sLyrics->Refresh();
sLyrics->Add(GetLyrics(s->GetArtist(), s->GetTitle())); # ifdef HAVE_CURL_CURL_H
if (!lyrics_downloader)
pthread_create(&lyrics_downloader, NULL, GetLyrics, s);
# else
string *lyrics = static_cast<string *>(GetLyrics(s));
sLyrics->Add(*lyrics);
delete lyrics;
# endif
} }
} }
} }

View File

@@ -210,6 +210,7 @@ void Scrollpad::Clear(bool clear_screen)
delwin(itsWindow); delwin(itsWindow);
itsWindow = newpad(itsHeight, itsWidth); itsWindow = newpad(itsHeight, itsWidth);
SetColor(itsColor, itsBgColor); SetColor(itsColor, itsBgColor);
SetTimeout(itsWindowTimeout);
if (clear_screen) if (clear_screen)
Window::Clear(); Window::Clear();
} }