summaryrefslogtreecommitdiff
path: root/src/lastfm_service.cpp
blob: 9eec5e508fecc1d4d25e3276f1729d25d704526c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***************************************************************************
 *   Copyright (C) 2008-2014 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 "lastfm_service.h"

#ifdef HAVE_CURL_CURL_H

#include <boost/algorithm/string/trim.hpp>
#include <boost/locale/conversion.hpp>
#include <fstream>
#include "charset.h"
#include "curl_handle.h"
#include "settings.h"
#include "utility/html.h"
#include "utility/string.h"

namespace {

const char *apiUrl = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method=";
const char *msgInvalidResponse = "Invalid response";

}

namespace LastFm {

Service::Result Service::fetch()
{
	Result result;
	result.first = false;
	
	std::string url = apiUrl;
	url += methodName();
	for (auto &arg : m_arguments)
	{
		url += "&";
		url += arg.first;
		url += "=";
		url += Curl::escape(arg.second);
	}
	
	std::string data;
	CURLcode code = Curl::perform(data, url);
	
	if (code != CURLE_OK)
		result.second = curl_easy_strerror(code);
	else if (actionFailed(data))
		result.second = msgInvalidResponse;
	else
	{
		result = processData(data);
		
		// if relevant part of data was not found and one of arguments
		// was language, try to fetch it again without that parameter.
		// otherwise just report failure.
		if (!result.first && !m_arguments["lang"].empty())
		{
			m_arguments.erase("lang");
			result = fetch();
		}
	}
	
	return result;
}

bool Service::actionFailed(const std::string &data)
{
	return data.find("status=\"failed\"") != std::string::npos;
}

/***********************************************************************/

bool ArtistInfo::argumentsOk()
{
	return !m_arguments["artist"].empty();
}

void ArtistInfo::beautifyOutput(NC::Scrollpad &w)
{
	w.setProperties(NC::Format::Bold, "\n\nSimilar artists:\n", NC::Format::NoBold, 0);
	w.setProperties(NC::Format::Bold, "\n\nSimilar tags:\n", NC::Format::NoBold, 0);
	w.setProperties(Config.color2, "\n * ", NC::Color::End, 0, boost::regex::literal);
}

Service::Result ArtistInfo::processData(const std::string &data)
{
	size_t a, b;
	Service::Result result;
	result.first = false;
	
	boost::regex rx("<content>(.*?)</content>");
	boost::smatch what;
	if (boost::regex_search(data, what, rx))
	{
		std::string desc = what[1];
		// if there is a description...
		if (desc.length() > 0)
		{
			// ...locate the link to wiki on last.fm...
			rx.assign("<link rel=\"original\" href=\"(.*?)\"");
			if (boost::regex_search(data, what, rx))
			{
				// ...try to get the content of it...
				std::string wiki;
				CURLcode code = Curl::perform(wiki, what[1]);
				
				if (code != CURLE_OK)
				{
					result.second = curl_easy_strerror(code);
					return result;
				}
				else
				{
					// ...and filter it to get the whole description.
					rx.assign("<div id=\"wiki\">(.*?)</div>");
					if (boost::regex_search(wiki, what, rx))
						desc = unescapeHtmlUtf8(what[1]);
				}
			}
			else
			{
				// otherwise, get rid of CDATA wrapper.
				rx.assign("<!\\[CDATA\\[(.*)\\]\\]>");
				desc = boost::regex_replace(desc, rx, "\\1");
			}
			stripHtmlTags(desc);
			boost::trim(desc);
			result.second += desc;
		}
		else
			result.second += "No description available for this artist.";
	}
	else
	{
		result.second = msgInvalidResponse;
		return result;
	}
	
	auto add_similars = [&result](boost::sregex_iterator &it, const boost::sregex_iterator &last) {
		for (; it != last; ++it)
		{
			std::string name = it->str(1);
			std::string url = it->str(2);
			stripHtmlTags(name);
			stripHtmlTags(url);
			result.second += "\n * ";
			result.second += name;
			result.second += " (";
			result.second += url;
			result.second += ")";
		}
	};
	
	a = data.find("<similar>");
	b = data.find("</similar>");
	if (a != std::string::npos && b != std::string::npos)
	{
		rx.assign("<artist>.*?<name>(.*?)</name>.*?<url>(.*?)</url>.*?</artist>");
		auto it = boost::sregex_iterator(data.begin()+a, data.begin()+b, rx);
		auto last = boost::sregex_iterator();
		if (it != last)
			result.second += "\n\nSimilar artists:\n";
		add_similars(it, last);
	}
	
	a = data.find("<tags>");
	b = data.find("</tags>");
	if (a != std::string::npos && b != std::string::npos)
	{
		rx.assign("<tag>.*?<name>(.*?)</name>.*?<url>(.*?)</url>.*?</tag>");
		auto it = boost::sregex_iterator(data.begin()+a, data.begin()+b, rx);
		auto last = boost::sregex_iterator();
		if (it != last)
			result.second += "\n\nSimilar tags:\n";
		add_similars(it, last);
	}
	
	// get artist we look for, it's the one before similar artists
	rx.assign("<name>.*?</name>.*?<url>(.*?)</url>.*?<similar>");
	
	if (boost::regex_search(data, what, rx))
	{
		std::string url = what[1];
		stripHtmlTags(url);
		result.second += "\n\n";
		// add only url
		result.second += url;
	}
	
	result.first = true;
	return result;
}

}

#endif