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 ***************************************************************************/
21 #include "lastfm_service.h"
23 #include <boost/algorithm/string/trim.hpp>
24 #include <boost/locale/conversion.hpp>
27 #include "curl_handle.h"
29 #include "utility/html.h"
30 #include "utility/string.h"
34 const char *apiUrl
= "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method=";
35 const char *msgInvalidResponse
= "Invalid response";
41 Service::Result
Service::fetch()
46 std::string url
= apiUrl
;
48 for (auto &arg
: m_arguments
)
53 url
+= Curl::escape(arg
.second
);
57 CURLcode code
= Curl::perform(data
, url
);
60 result
.second
= curl_easy_strerror(code
);
61 else if (actionFailed(data
))
62 result
.second
= msgInvalidResponse
;
65 result
= processData(data
);
67 // if relevant part of data was not found and one of arguments
68 // was language, try to fetch it again without that parameter.
69 // otherwise just report failure.
70 if (!result
.first
&& !m_arguments
["lang"].empty())
72 m_arguments
.erase("lang");
80 bool Service::actionFailed(const std::string
&data
)
82 return data
.find("status=\"failed\"") != std::string::npos
;
85 /***********************************************************************/
87 bool ArtistInfo::argumentsOk()
89 return !m_arguments
["artist"].empty();
92 void ArtistInfo::beautifyOutput(NC::Scrollpad
&w
)
94 w
.setProperties(NC::Format::Bold
, "\n\nSimilar artists:\n", NC::Format::NoBold
, 0);
95 w
.setProperties(NC::Format::Bold
, "\n\nSimilar tags:\n", NC::Format::NoBold
, 0);
97 //w.setProperties(Config.color2, "\n * ", NC::Color::End, 0, boost::regex::literal);
100 Service::Result
ArtistInfo::processData(const std::string
&data
)
103 Service::Result result
;
104 result
.first
= false;
106 boost::regex
rx("<content>(.*?)</content>");
108 if (boost::regex_search(data
, what
, rx
))
110 std::string desc
= what
[1];
111 // if there is a description...
112 if (desc
.length() > 0)
114 // ...locate the link to wiki on last.fm...
115 rx
.assign("<link rel=\"original\" href=\"(.*?)\"");
116 if (boost::regex_search(data
, what
, rx
))
118 std::string url
= what
[1], wiki
;
120 unescapeHtmlEntities(url
);
121 // ...try to get the content of it...
122 CURLcode code
= Curl::perform(wiki
, url
, "", true);
124 if (code
!= CURLE_OK
)
126 result
.second
= curl_easy_strerror(code
);
131 // ...and filter it to get the whole description.
132 rx
.assign("<div class=\"wiki\">(.*?)</div>");
133 if (boost::regex_search(wiki
, what
, rx
))
134 desc
= unescapeHtmlUtf8(what
[1]);
139 // otherwise, get rid of CDATA wrapper.
140 rx
.assign("<!\\[CDATA\\[(.*)\\]\\]>");
141 desc
= boost::regex_replace(desc
, rx
, "\\1");
145 result
.second
+= desc
;
148 result
.second
+= "No description available for this artist.";
152 result
.second
= msgInvalidResponse
;
156 auto add_similars
= [&result
](boost::sregex_iterator
&it
, const boost::sregex_iterator
&last
) {
157 for (; it
!= last
; ++it
)
159 std::string value
= it
->str(1);
160 std::string url
= it
->str(2);
161 stripHtmlTags(value
);
163 result
.second
+= "\n * ";
164 result
.second
+= value
;
165 result
.second
+= " (";
166 result
.second
+= url
;
167 result
.second
+= ")";
171 a
= data
.find("<similar>");
172 b
= data
.find("</similar>");
173 if (a
!= std::string::npos
&& b
!= std::string::npos
)
175 rx
.assign("<artist>.*?<name>(.*?)</name>.*?<url>(.*?)</url>.*?</artist>");
176 auto it
= boost::sregex_iterator(data
.begin()+a
, data
.begin()+b
, rx
);
177 auto last
= boost::sregex_iterator();
179 result
.second
+= "\n\nSimilar artists:\n";
180 add_similars(it
, last
);
183 a
= data
.find("<tags>");
184 b
= data
.find("</tags>");
185 if (a
!= std::string::npos
&& b
!= std::string::npos
)
187 rx
.assign("<tag>.*?<name>(.*?)</name>.*?<url>(.*?)</url>.*?</tag>");
188 auto it
= boost::sregex_iterator(data
.begin()+a
, data
.begin()+b
, rx
);
189 auto last
= boost::sregex_iterator();
191 result
.second
+= "\n\nSimilar tags:\n";
192 add_similars(it
, last
);
195 // get artist we look for, it's the one before similar artists
196 rx
.assign("<name>.*?</name>.*?<url>(.*?)</url>.*?<similar>");
198 if (boost::regex_search(data
, what
, rx
))
200 std::string url
= what
[1];
202 result
.second
+= "\n\n";
204 result
.second
+= url
;