1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
22 #include "curl_handle.h"
24 #ifdef HAVE_CURL_CURL_H
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>
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
)
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
)
69 std::string url
= urlTemplate();
70 boost::replace_all(url
, "%artist%", artist
);
71 boost::replace_all(url
, "%title%", title
);
74 CURLcode code
= Curl::perform(data
, url
);
78 result
.second
= curl_easy_strerror(code
);
82 auto lyrics
= getContent(regex(), data
);
84 if (lyrics
.empty() || notLyrics(data
))
86 result
.second
= msgNotFound
;
91 for (auto it
= lyrics
.begin(); it
!= lyrics
.end(); ++it
)
97 if (it
!= lyrics
.end()-1)
98 data
+= "\n\n----------\n\n";
102 result
.second
= data
;
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));
118 void LyricsFetcher::postProcess(std::string
&data
) const
120 data
= unescapeHtmlUtf8(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
)
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");
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;
144 CURLcode code
= Curl::perform(data
, result
.second
, "", true);
146 if (code
!= CURLE_OK
)
148 result
.second
= curl_easy_strerror(code
);
152 auto lyrics
= getContent("<div class='lyricbox'>(.*?)</div>", data
);
156 result
.second
= msgNotFound
;
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";
170 for (auto it
= lyrics
.begin(); it
!= lyrics
.end(); ++it
)
177 if (it
!= lyrics
.end()-1)
178 data
+= "\n\n----------\n\n";
182 result
.second
= data
;
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
)
198 result
.first
= false;
200 std::string search_str
= artist
;
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";
211 CURLcode code
= Curl::perform(data
, google_url
, google_url
);
213 if (code
!= CURLE_OK
)
215 result
.second
= curl_easy_strerror(code
);
219 auto urls
= getContent("<A HREF=\"(.*?)\">here</A>", data
);
221 if (urls
.empty() || !isURLOk(urls
[0]))
223 result
.second
= msgNotFound
;
227 data
= unescapeHtmlUtf8(urls
[0]);
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
;
258 bool InternetLyricsFetcher::isURLOk(const std::string
&url
)
264 #endif // HAVE_CURL_CURL_H