File: lastfmengine.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (192 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download
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
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "lastfmengine.h"
#include "config.h"
#include "gui/apikeys.h"
#include "gui/covers.h"
#include "network/networkaccessmanager.h"
#include <QRegularExpression>
#include <QUrlQuery>
#include <QXmlStreamReader>

#include <QDebug>
static bool debugEnabled = false;
#define DBUG \
	if (debugEnabled) qWarning() << metaObject()->className() << __FUNCTION__
void LastFmEngine::enableDebug()
{
	debugEnabled = true;
}

const QLatin1String LastFmEngine::constLang("lastfm");
const QLatin1String LastFmEngine::constLinkPlaceholder("XXX_CONTEXT_READ_MORE_ON_LASTFM_XXX");

static const char* constModeProperty = "mode";
static const char* constQuery = "query";

LastFmEngine::LastFmEngine(QObject* p)
	: ContextEngine(p)
{
}

QStringList LastFmEngine::getLangs() const
{
	QStringList langs;
	langs.append(constLang);
	return langs;
}

QString LastFmEngine::translateLinks(QString text) const
{
	text = text.replace(constLinkPlaceholder, tr("Read more on last.fm"));
	return text;
}

void LastFmEngine::search(const QStringList& query, Mode mode)
{
	QStringList fixedQuery = fixQuery(query);
	QUrl url("https://ws.audioscrobbler.com/2.0/");
	QUrlQuery urlQuery;

	switch (mode) {
	case Artist:
		urlQuery.addQueryItem("method", "artist.getInfo");
		break;
	case Album:
		urlQuery.addQueryItem("method", "album.getInfo");
		urlQuery.addQueryItem("album", fixedQuery.at(1));
		break;
	case Track:
		urlQuery.addQueryItem("method", "track.getInfo");
		urlQuery.addQueryItem("track", fixedQuery.at(1));
		break;
	}

	ApiKeys::self()->addKey(urlQuery, ApiKeys::LastFm);
	urlQuery.addQueryItem("autocorrect", "1");
	urlQuery.addQueryItem("artist", Covers::fixArtist(fixedQuery.at(0)));

	url.setQuery(urlQuery);

	job = NetworkAccessManager::self()->get(url);
	job->setProperty(constModeProperty, (int)mode);

	QStringList queryString;
	for (QString q : fixedQuery) {
		q = q.replace("/", "%2F");
		q = q.replace(" ", "+");
		queryString.append(q);
	}
	job->setProperty(constQuery, queryString.join("/"));
	DBUG << url.toString();
	connect(job, SIGNAL(finished()), this, SLOT(parseResponse()));
}

void LastFmEngine::parseResponse()
{
	DBUG << __FUNCTION__;
	NetworkJob* reply = getReply(sender());
	if (!reply) {
		return;
	}

	QByteArray data = reply->readAll();
	if (!reply->ok() || data.isEmpty()) {
		DBUG << "Empty/error";
		emit searchResult(QString(), QString());
		return;
	}

	Mode mode = (Mode)reply->property(constModeProperty).toInt();
	QString text;

	switch (mode) {
	case Artist:
		text = parseResponse(data, QLatin1String("artist"), QLatin1String("bio"));
		break;
	case Album:
		text = parseResponse(data, QLatin1String("album"), QLatin1String("wiki"));
		break;
	case Track:
		text = parseResponse(data, QLatin1String("track"), QLatin1String("wiki"));
		break;
	}

	if (!text.isEmpty()) {
		static const QRegularExpression constLicense("User-contributed text is available.*");
		text.remove(constLicense);
		text.replace("\n", "<br>");
		text = text.simplified();
		text.replace(" <br>", "<br>");

		// Remove last.fm read more link (as we add our own!!)
		int end = text.lastIndexOf(QLatin1String("on Last.fm</a>"));
		if (-1 != end) {
			int start = text.lastIndexOf(QLatin1String("<a href=\"https://www.last.fm/music/"), end);
			if (-1 == start) {
				start = text.lastIndexOf(QLatin1String("<a href=\"http://www.last.fm/music/"), end);
			}
			if (-1 != start) {
				if (text.indexOf(QLatin1String("Read more about"), start) < end) {
					text = text.left(start);
				}
			}
		}
		text += QLatin1String("<br><br><a href='http://www.last.fm/music/") + reply->property(constQuery).toString() + QLatin1String("/+wiki'>") + constLinkPlaceholder + QLatin1String("</a>");
		text.replace("<br><br><br><br><br>", "<br><br>");
		text.replace("<br><br><br><br>", "<br><br>");
		text.replace("<br><br><br>", "<br><br>");
	}
	emit searchResult(text, text.isEmpty() ? QString() : constLang);
}

QString LastFmEngine::parseResponse(const QByteArray& data, const QString& firstTag, const QString& secondTag)
{
	DBUG << __FUNCTION__ << data;
	QXmlStreamReader xml(data);
	xml.setNamespaceProcessing(false);
	while (xml.readNextStartElement()) {
		if (firstTag == xml.name()) {
			while (xml.readNextStartElement()) {
				if (secondTag == xml.name()) {
					while (xml.readNextStartElement()) {
						if (QLatin1String("content") == xml.name()) {
							return xml.readElementText().trimmed();
						}
						else {
							xml.skipCurrentElement();
						}
					}
				}
				else {
					xml.skipCurrentElement();
				}
			}
		}
	}

	return QString();
}

#include "moc_lastfmengine.cpp"