parent
d3cb0ea6f6
commit
daad444f3c
11 changed files with 209 additions and 195 deletions
@ -0,0 +1,93 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2010 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* 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., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include "curl_handle.h" |
||||
|
||||
#ifdef HAVE_CURL_CURL_H |
||||
|
||||
#include "conv.h" |
||||
#include "lyrics_fetcher.h" |
||||
|
||||
LyricsFetcher *lyricsPlugins[] = |
||||
{ |
||||
new LyrcComArFetcher(), |
||||
new LyricsflyFetcher(), |
||||
0 |
||||
}; |
||||
|
||||
LyricsFetcher::Result LyricsFetcher::fetch(const std::string &artist, const std::string &title) |
||||
{ |
||||
Result result; |
||||
result.first = false; |
||||
|
||||
std::string url = getURL(); |
||||
Replace(url, "%artist%", artist.c_str()); |
||||
Replace(url, "%title%", title.c_str()); |
||||
|
||||
std::string data; |
||||
CURLcode code = Curl::perform(url, data); |
||||
|
||||
if (code != CURLE_OK) |
||||
{ |
||||
result.second = "Error while fetching lyrics: "; |
||||
result.second += curl_easy_strerror(code); |
||||
return result; |
||||
} |
||||
|
||||
size_t a, b; |
||||
bool parse_failed = false; |
||||
if ((a = data.find(getOpenTag())) != std::string::npos) |
||||
{ |
||||
a += strlen(getCloseTag())-1; |
||||
if ((b = data.find(getCloseTag(), a)) != std::string::npos) |
||||
data = data.substr(a, b-a); |
||||
else |
||||
parse_failed = true; |
||||
} |
||||
else |
||||
parse_failed = true; |
||||
|
||||
if (parse_failed || notLyrics(data)) |
||||
{ |
||||
result.second = "Not found"; |
||||
return result; |
||||
} |
||||
|
||||
postProcess(data); |
||||
|
||||
result.second = data; |
||||
result.first = true; |
||||
return result; |
||||
} |
||||
|
||||
void LyricsFetcher::postProcess(std::string &data) |
||||
{ |
||||
EscapeHtml(data); |
||||
Trim(data); |
||||
} |
||||
|
||||
void LyricsflyFetcher::postProcess(std::string &data) |
||||
{ |
||||
Replace(data, "[br]", ""); |
||||
Trim(data); |
||||
} |
||||
|
||||
#endif // HAVE_CURL_CURL_H
|
||||
|
||||
@ -0,0 +1,74 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2010 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* 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., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#ifndef _LYRICS_FETCHER_H |
||||
#define _LYRICS_FETCHER_H |
||||
|
||||
#ifdef HAVE_CONFIG_H |
||||
# include "config.h" |
||||
#endif |
||||
|
||||
#ifdef HAVE_CURL_CURL_H |
||||
|
||||
#include <string> |
||||
|
||||
struct LyricsFetcher |
||||
{ |
||||
typedef std::pair<bool, std::string> Result; |
||||
|
||||
virtual const char *name() = 0; |
||||
virtual Result fetch(const std::string &artist, const std::string &title); |
||||
|
||||
protected: |
||||
virtual const char *getURL() = 0; |
||||
virtual const char *getOpenTag() = 0; |
||||
virtual const char *getCloseTag() = 0; |
||||
|
||||
virtual bool notLyrics(GNUC_UNUSED const std::string &data) { return false; } |
||||
virtual void postProcess(std::string &data); |
||||
}; |
||||
|
||||
struct LyrcComArFetcher : public LyricsFetcher |
||||
{ |
||||
virtual const char *name() { return "lyrc.com.ar"; } |
||||
|
||||
protected: |
||||
virtual const char *getURL() { return "http://lyrc.com.ar/tema1es.php?artist=%artist%&songname=%title%"; } |
||||
virtual const char *getOpenTag() { return "</table>"; } |
||||
virtual const char *getCloseTag() { return "<p>"; } |
||||
}; |
||||
|
||||
struct LyricsflyFetcher : public LyricsFetcher |
||||
{ |
||||
virtual const char *name() { return "lyricsfly.com"; } |
||||
|
||||
protected: |
||||
virtual const char *getURL() { return "http://api.lyricsfly.com/api/api.php?i=1b76e55254f5f22ae-temporary.API.access&a=%artist%&t=%title%"; } |
||||
virtual const char *getOpenTag() { return "<tx>"; } |
||||
virtual const char *getCloseTag() { return "</tx>"; } |
||||
|
||||
virtual void postProcess(std::string &data); |
||||
}; |
||||
|
||||
extern LyricsFetcher *lyricsPlugins[]; |
||||
|
||||
#endif // HAVE_CURL_CURL_H
|
||||
|
||||
#endif |
||||
Loading…
Reference in new issue