Make 'update_environment' action also update local mpd status
[ncmpcpp.git] / src / lastfm_service.cpp
blob59d4941c6b14cc508e4f380b29a9eb4c2c8c24a3
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 "lastfm_service.h"
23 #include <boost/algorithm/string/trim.hpp>
24 #include <boost/locale/conversion.hpp>
25 #include <fstream>
26 #include "charset.h"
27 #include "curl_handle.h"
28 #include "settings.h"
29 #include "utility/html.h"
30 #include "utility/string.h"
32 namespace {
34 const char *apiUrl = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method=";
35 const char *msgInvalidResponse = "Invalid response";
39 namespace LastFm {
41 Service::Result Service::fetch()
43 Result result;
44 result.first = false;
46 std::string url = apiUrl;
47 url += methodName();
48 for (auto &arg : m_arguments)
50 url += "&";
51 url += arg.first;
52 url += "=";
53 url += Curl::escape(arg.second);
56 std::string data;
57 CURLcode code = Curl::perform(data, url);
59 if (code != CURLE_OK)
60 result.second = curl_easy_strerror(code);
61 else if (actionFailed(data))
62 result.second = msgInvalidResponse;
63 else
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");
73 result = fetch();
77 return result;
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);
96 // FIXME
97 //w.setProperties(Config.color2, "\n * ", NC::Color::End, 0, boost::regex::literal);
100 Service::Result ArtistInfo::processData(const std::string &data)
102 size_t a, b;
103 Service::Result result;
104 result.first = false;
106 boost::regex rx("<content>(.*?)</content>");
107 boost::smatch what;
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;
119 // unescape &amp;s
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);
127 return result;
129 else
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]);
137 else
139 // otherwise, get rid of CDATA wrapper.
140 rx.assign("<!\\[CDATA\\[(.*)\\]\\]>");
141 desc = boost::regex_replace(desc, rx, "\\1");
143 stripHtmlTags(desc);
144 boost::trim(desc);
145 result.second += desc;
147 else
148 result.second += "No description available for this artist.";
150 else
152 result.second = msgInvalidResponse;
153 return result;
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);
162 stripHtmlTags(url);
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();
178 if (it != last)
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();
190 if (it != last)
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];
201 stripHtmlTags(url);
202 result.second += "\n\n";
203 // add only url
204 result.second += url;
207 result.first = true;
208 return result;