Make the list of lyrics fetchers customizable
[ncmpcpp.git] / src / lyrics_fetcher.cpp
blobd9676510fe5b164ed023427029d61d66985cf5ea
1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "config.h"
22 #include "curl_handle.h"
24 #ifdef HAVE_CURL_CURL_H
26 #include <cstdlib>
27 #include <cstring>
28 #include <boost/algorithm/string/join.hpp>
29 #include <boost/algorithm/string/replace.hpp>
30 #include <boost/algorithm/string/split.hpp>
31 #include <boost/algorithm/string/trim.hpp>
32 #include <boost/regex.hpp>
34 #include "charset.h"
35 #include "lyrics_fetcher.h"
36 #include "utility/html.h"
37 #include "utility/string.h"
39 std::unique_ptr<LyricsFetcher> toLyricsFetcher(const std::string &s)
41 if (s == "lyricwiki")
42 return std::make_unique<LyricwikiFetcher>();
43 else if (s == "azlyrics")
44 return std::make_unique<AzLyricsFetcher>();
45 else if (s == "genius")
46 return std::make_unique<GeniusFetcher>();
47 else if (s == "sing365")
48 return std::make_unique<Sing365Fetcher>();
49 else if (s == "lyricsmania")
50 return std::make_unique<LyricsmaniaFetcher>();
51 else if (s == "metrolyrics")
52 return std::make_unique<MetrolyricsFetcher>();
53 else if (s == "justsomelyrics")
54 return std::make_unique<JustSomeLyricsFetcher>();
55 else if (s == "tekstowo")
56 return std::make_unique<TekstowoFetcher>();
57 else if (s == "internet")
58 return std::make_unique<InternetLyricsFetcher>();
59 throw std::runtime_error("no lyrics fetcher named '" + s + "'");
62 const char LyricsFetcher::msgNotFound[] = "Not found";
64 LyricsFetcher::Result LyricsFetcher::fetch(const std::string &artist, const std::string &title)
66 Result result;
67 result.first = false;
69 std::string url = urlTemplate();
70 boost::replace_all(url, "%artist%", artist);
71 boost::replace_all(url, "%title%", title);
73 std::string data;
74 CURLcode code = Curl::perform(data, url);
76 if (code != CURLE_OK)
78 result.second = curl_easy_strerror(code);
79 return result;
82 auto lyrics = getContent(regex(), data);
84 if (lyrics.empty() || notLyrics(data))
86 result.second = msgNotFound;
87 return result;
90 data.clear();
91 for (auto it = lyrics.begin(); it != lyrics.end(); ++it)
93 postProcess(*it);
94 if (!it->empty())
96 data += *it;
97 if (it != lyrics.end()-1)
98 data += "\n\n----------\n\n";
102 result.second = data;
103 result.first = true;
104 return result;
107 std::vector<std::string> LyricsFetcher::getContent(const char *regex_, const std::string &data)
109 std::vector<std::string> result;
110 boost::regex rx(regex_);
111 auto first = boost::sregex_iterator(data.begin(), data.end(), rx);
112 auto last = boost::sregex_iterator();
113 for (; first != last; ++first)
114 result.push_back(first->str(1));
115 return result;
118 void LyricsFetcher::postProcess(std::string &data) const
120 data = unescapeHtmlUtf8(data);
121 stripHtmlTags(data);
122 // Remove indentation from each line and collapse multiple newlines into one.
123 std::vector<std::string> lines;
124 boost::split(lines, data, boost::is_any_of("\n"));
125 for (auto &line : lines)
126 boost::trim(line);
127 std::unique(lines.begin(), lines.end(), [](std::string &a, std::string &b) {
128 return a.empty() && b.empty();
130 data = boost::algorithm::join(lines, "\n");
131 boost::trim(data);
134 /***********************************************************************/
136 LyricsFetcher::Result LyricwikiFetcher::fetch(const std::string &artist, const std::string &title)
138 LyricsFetcher::Result result = LyricsFetcher::fetch(artist, title);
139 if (result.first == true)
141 result.first = false;
143 std::string data;
144 CURLcode code = Curl::perform(data, result.second, "", true);
146 if (code != CURLE_OK)
148 result.second = curl_easy_strerror(code);
149 return result;
152 auto lyrics = getContent("<div class='lyricbox'>(.*?)</div>", data);
154 if (lyrics.empty())
156 result.second = msgNotFound;
157 return result;
159 std::transform(lyrics.begin(), lyrics.end(), lyrics.begin(), unescapeHtmlUtf8);
160 bool license_restriction = std::any_of(lyrics.begin(), lyrics.end(), [](const std::string &s) {
161 return s.find("Unfortunately, we are not licensed to display the full lyrics for this song at the moment.") != std::string::npos;
163 if (license_restriction)
165 result.second = "Licence restriction";
166 return result;
169 data.clear();
170 for (auto it = lyrics.begin(); it != lyrics.end(); ++it)
172 stripHtmlTags(*it);
173 boost::trim(*it);
174 if (!it->empty())
176 data += *it;
177 if (it != lyrics.end()-1)
178 data += "\n\n----------\n\n";
182 result.second = data;
183 result.first = true;
185 return result;
188 bool LyricwikiFetcher::notLyrics(const std::string &data) const
190 return data.find("action=edit") != std::string::npos;
193 /**********************************************************************/
195 LyricsFetcher::Result GoogleLyricsFetcher::fetch(const std::string &artist, const std::string &title)
197 Result result;
198 result.first = false;
200 std::string search_str = artist;
201 search_str += "+";
202 search_str += title;
203 search_str += "+%2B";
204 search_str += siteKeyword();
206 std::string google_url = "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=";
207 google_url += search_str;
208 google_url += "&btnI=I%27m+Feeling+Lucky";
210 std::string data;
211 CURLcode code = Curl::perform(data, google_url, google_url);
213 if (code != CURLE_OK)
215 result.second = curl_easy_strerror(code);
216 return result;
219 auto urls = getContent("<A HREF=\"(.*?)\">here</A>", data);
221 if (urls.empty() || !isURLOk(urls[0]))
223 result.second = msgNotFound;
224 return result;
227 data = unescapeHtmlUtf8(urls[0]);
229 URL = data.c_str();
230 return LyricsFetcher::fetch("", "");
233 bool GoogleLyricsFetcher::isURLOk(const std::string &url)
235 return url.find(siteKeyword()) != std::string::npos;
238 /**********************************************************************/
240 bool MetrolyricsFetcher::isURLOk(const std::string &url)
242 // it sometimes return link to sitemap.xml, which is huge so we need to discard it
243 return GoogleLyricsFetcher::isURLOk(url) && url.find("sitemap") == std::string::npos;
246 /**********************************************************************/
248 LyricsFetcher::Result InternetLyricsFetcher::fetch(const std::string &artist, const std::string &title)
250 GoogleLyricsFetcher::fetch(artist, title);
251 LyricsFetcher::Result result;
252 result.first = false;
253 result.second = "The following site may contain lyrics for this song: ";
254 result.second += URL;
255 return result;
258 bool InternetLyricsFetcher::isURLOk(const std::string &url)
260 URL = url;
261 return false;
264 #endif // HAVE_CURL_CURL_H