2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtGlobal>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QLocale>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QList>
|
2018-12-15 00:52:58 +01:00
|
|
|
#include <QPair>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QSet>
|
2018-12-15 00:43:50 +01:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
2018-12-15 00:43:50 +01:00
|
|
|
#include <QUrlQuery>
|
2018-12-15 00:52:58 +01:00
|
|
|
#include <QNetworkRequest>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QNetworkReply>
|
2018-12-15 00:43:50 +01:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonValue>
|
2018-12-15 00:52:58 +01:00
|
|
|
#include <QJsonObject>
|
2018-12-15 00:43:50 +01:00
|
|
|
#include <QJsonArray>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtDebug>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-14 16:40:05 +02:00
|
|
|
#include "core/application.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2018-12-15 00:43:50 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
2020-05-10 12:49:11 +02:00
|
|
|
#include "jsoncoverprovider.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "albumcoverfetcher.h"
|
|
|
|
#include "lastfmcoverprovider.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
const char *LastFmCoverProvider::kUrl = "https://ws.audioscrobbler.com/2.0/";
|
2018-02-27 18:06:05 +01:00
|
|
|
const char *LastFmCoverProvider::kApiKey = "211990b4c96782c05d1536e7219eb56e";
|
|
|
|
const char *LastFmCoverProvider::kSecret = "80fd738f49596e9709b1bf9319c444a8";
|
|
|
|
|
2021-08-12 23:00:10 +02:00
|
|
|
LastFmCoverProvider::LastFmCoverProvider(Application *app, NetworkAccessManager *network, QObject *parent)
|
|
|
|
: JsonCoverProvider("Last.fm", true, false, 1.0, true, false, app, network, parent) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
LastFmCoverProvider::~LastFmCoverProvider() {
|
|
|
|
|
|
|
|
while (!replies_.isEmpty()) {
|
|
|
|
QNetworkReply *reply = replies_.takeFirst();
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
2020-05-12 21:28:42 +02:00
|
|
|
reply->abort();
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
bool LastFmCoverProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const int id) {
|
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
typedef QList<Param> ParamList;
|
|
|
|
|
2020-08-07 00:18:31 +02:00
|
|
|
if (artist.isEmpty() && album.isEmpty() && title.isEmpty()) return false;
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QString method;
|
|
|
|
QString type;
|
2020-08-07 00:18:31 +02:00
|
|
|
QString query = artist;
|
|
|
|
if (album.isEmpty() && !title.isEmpty()) {
|
2020-04-23 21:03:36 +02:00
|
|
|
method = "track.search";
|
|
|
|
type = "track";
|
2020-08-07 00:18:31 +02:00
|
|
|
if (!query.isEmpty()) query.append(" ");
|
|
|
|
query.append(title);
|
2020-04-23 21:03:36 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
method = "album.search";
|
|
|
|
type = "album";
|
2020-08-07 00:18:31 +02:00
|
|
|
if (!album.isEmpty()) {
|
|
|
|
if (!query.isEmpty()) query.append(" ");
|
|
|
|
query.append(album);
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParamList params = ParamList() << Param("api_key", kApiKey)
|
|
|
|
<< Param("lang", QLocale().name().left(2).toLower())
|
|
|
|
<< Param("method", method)
|
|
|
|
<< Param(type, query);
|
|
|
|
|
|
|
|
std::sort(params.begin(), params.end());
|
2018-12-15 00:43:50 +01:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
QString data_to_sign;
|
|
|
|
for (const Param ¶m : params) {
|
2020-04-23 21:03:36 +02:00
|
|
|
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
2018-12-15 00:43:50 +01:00
|
|
|
data_to_sign += param.first + param.second;
|
|
|
|
}
|
|
|
|
data_to_sign += kSecret;
|
|
|
|
|
|
|
|
QByteArray const digest = QCryptographicHash::hash(data_to_sign.toUtf8(), QCryptographicHash::Md5);
|
|
|
|
QString signature = QString::fromLatin1(digest.toHex()).rightJustified(32, '0').toLower();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
url_query.addQueryItem(QUrl::toPercentEncoding("api_sig"), QUrl::toPercentEncoding(signature));
|
|
|
|
url_query.addQueryItem(QUrl::toPercentEncoding("format"), QUrl::toPercentEncoding("json"));
|
|
|
|
|
|
|
|
QUrl url(kUrl);
|
|
|
|
QNetworkRequest req(url);
|
2020-08-14 20:20:41 +02:00
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
2018-12-15 00:43:50 +01:00
|
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
QNetworkReply *reply = network_->post(req, url_query.toString(QUrl::FullyEncoded).toUtf8());
|
2020-05-12 21:28:42 +02:00
|
|
|
replies_ << reply;
|
2021-03-21 00:37:17 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, type]() { QueryFinished(reply, id, type); });
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
return true;
|
2018-12-15 00:43:50 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
void LastFmCoverProvider::QueryFinished(QNetworkReply *reply, const int id, const QString &type) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
if (!replies_.contains(reply)) return;
|
|
|
|
replies_.removeAll(reply);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResults results;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
QByteArray data = GetReplyData(reply);
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonObject json_obj = ExtractJsonObj(data);
|
|
|
|
if (json_obj.isEmpty()) {
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonValue value_results;
|
|
|
|
if (json_obj.contains("results")) {
|
|
|
|
value_results = json_obj["results"];
|
|
|
|
}
|
|
|
|
else if (json_obj.contains("error") && json_obj.contains("message")) {
|
|
|
|
int error = json_obj["error"].toInt();
|
|
|
|
QString message = json_obj["message"].toString();
|
2021-03-21 04:47:11 +01:00
|
|
|
Error(QString("Error: %1: %2").arg(QString::number(error), message));
|
2020-04-23 21:03:36 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Error(QString("Json reply is missing results."), json_obj);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
if (!value_results.isObject()) {
|
|
|
|
Error("Json results is not a object.", value_results);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonObject obj_results = value_results.toObject();
|
|
|
|
if (obj_results.isEmpty()) {
|
|
|
|
Error("Json results object is empty.", value_results);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonValue value_matches;
|
|
|
|
|
|
|
|
if (type == "album") {
|
|
|
|
if (obj_results.contains("albummatches")) {
|
|
|
|
value_matches = obj_results["albummatches"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Error("Json results object is missing albummatches.", obj_results);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == "track") {
|
|
|
|
if (obj_results.contains("trackmatches")) {
|
|
|
|
value_matches = obj_results["trackmatches"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Error("Json results object is missing trackmatches.", obj_results);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value_matches.isObject()) {
|
|
|
|
Error("Json albummatches or trackmatches is not an object.", value_matches);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
|
|
|
|
QJsonObject obj_matches = value_matches.toObject();
|
|
|
|
if (obj_matches.isEmpty()) {
|
|
|
|
Error("Json albummatches or trackmatches object is empty.", value_matches);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonValue value_type;
|
|
|
|
if (!obj_matches.contains(type)) {
|
|
|
|
Error(QString("Json object is missing %1.").arg(type), obj_matches);
|
2018-12-15 00:43:50 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
value_type = obj_matches[type];
|
|
|
|
|
|
|
|
if (!value_type.isArray()) {
|
|
|
|
Error("Json album value in albummatches object is not an array.", value_type);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray array_type = value_type.toArray();
|
|
|
|
|
2021-03-26 22:10:43 +01:00
|
|
|
for (const QJsonValueRef value : array_type) {
|
2018-12-15 00:43:50 +01:00
|
|
|
|
|
|
|
if (!value.isObject()) {
|
2021-03-26 22:10:43 +01:00
|
|
|
Error("Invalid Json reply, value in albummatches/trackmatches array is not a object.");
|
2018-12-15 00:43:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonObject obj = value.toObject();
|
|
|
|
if (!obj.contains("artist") || !obj.contains("image") || !obj.contains("name")) {
|
|
|
|
Error("Invalid Json reply, album is missing artist, image or name.", obj);
|
2018-12-15 00:43:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
QString artist = obj["artist"].toString();
|
|
|
|
QString album;
|
|
|
|
if (type == "album") {
|
|
|
|
album = obj["name"].toString();
|
|
|
|
}
|
2019-04-17 22:18:03 +02:00
|
|
|
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonValue json_image = obj["image"];
|
2018-12-15 00:43:50 +01:00
|
|
|
if (!json_image.isArray()) {
|
2020-04-23 21:03:36 +02:00
|
|
|
Error("Invalid Json reply, album image is not a array.", json_image);
|
2018-12-15 00:43:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonArray array_image = json_image.toArray();
|
2021-06-25 22:41:17 +02:00
|
|
|
QString image_url_use;
|
|
|
|
LastFmImageSize image_size_use(LastFmImageSize::Unknown);
|
2021-03-26 22:10:43 +01:00
|
|
|
for (const QJsonValueRef value_image : array_image) {
|
2020-04-23 21:03:36 +02:00
|
|
|
if (!value_image.isObject()) {
|
2021-03-26 22:10:43 +01:00
|
|
|
Error("Invalid Json reply, album image value is not an object.");
|
2018-12-15 00:43:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
QJsonObject obj_image = value_image.toObject();
|
|
|
|
if (!obj_image.contains("#text") || !obj_image.contains("size")) {
|
|
|
|
Error("Invalid Json reply, album image value is missing #text or size.", obj_image);
|
2018-12-15 00:43:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-23 21:03:36 +02:00
|
|
|
QString image_url = obj_image["#text"].toString();
|
2021-06-25 22:41:17 +02:00
|
|
|
if (image_url.isEmpty()) continue;
|
2020-04-23 21:03:36 +02:00
|
|
|
LastFmImageSize image_size = ImageSizeFromString(obj_image["size"].toString().toLower());
|
2021-06-25 22:41:17 +02:00
|
|
|
if (image_url_use.isEmpty() || image_size > image_size_use) {
|
|
|
|
image_url_use = image_url;
|
|
|
|
image_size_use = image_size;
|
2018-12-15 00:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 22:41:17 +02:00
|
|
|
if (image_url_use.isEmpty()) continue;
|
|
|
|
|
|
|
|
// Workaround for API limiting to 300x300 images.
|
|
|
|
if (image_url_use.contains("/300x300/")) {
|
|
|
|
image_url_use = image_url_use.replace("/300x300/", "/740x0/");
|
|
|
|
}
|
|
|
|
QUrl url(image_url_use);
|
2020-04-23 21:03:36 +02:00
|
|
|
if (!url.isValid()) continue;
|
2018-12-15 00:43:50 +01:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResult cover_result;
|
2019-04-17 22:18:03 +02:00
|
|
|
cover_result.artist = artist;
|
|
|
|
cover_result.album = album;
|
2018-12-15 00:43:50 +01:00
|
|
|
cover_result.image_url = url;
|
2021-06-25 22:41:17 +02:00
|
|
|
cover_result.image_size = QSize(300, 300);
|
2018-12-15 00:43:50 +01:00
|
|
|
results << cover_result;
|
|
|
|
}
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray LastFmCoverProvider::GetReplyData(QNetworkReply *reply) {
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
|
|
|
|
data = reply->readAll();
|
|
|
|
}
|
|
|
|
else {
|
2019-07-07 21:14:24 +02:00
|
|
|
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
2018-12-15 00:43:50 +01:00
|
|
|
// This is a network error, there is nothing more to do.
|
2019-07-07 21:14:24 +02:00
|
|
|
Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2018-12-15 00:43:50 +01:00
|
|
|
else {
|
|
|
|
// See if there is Json data containing "error" and "message" - then use that instead.
|
|
|
|
data = reply->readAll();
|
2019-07-07 21:14:24 +02:00
|
|
|
QString error;
|
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
2020-10-02 19:27:47 +02:00
|
|
|
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
2018-12-15 00:43:50 +01:00
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.contains("error") && json_obj.contains("message")) {
|
2019-07-07 21:14:24 +02:00
|
|
|
int code = json_obj["error"].toInt();
|
2018-12-15 00:43:50 +01:00
|
|
|
QString message = json_obj["message"].toString();
|
2019-07-07 21:14:24 +02:00
|
|
|
error = "Error: " + QString::number(code) + ": " + message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error.isEmpty()) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
error = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
2018-12-15 00:43:50 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-07-07 21:14:24 +02:00
|
|
|
error = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
2018-12-15 00:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 21:14:24 +02:00
|
|
|
Error(error);
|
2018-12-15 00:43:50 +01:00
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-15 20:27:32 +02:00
|
|
|
void LastFmCoverProvider::Error(const QString &error, const QVariant &debug) {
|
2020-04-23 21:03:36 +02:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
qLog(Error) << "Last.fm:" << error;
|
2018-12-15 00:43:50 +01:00
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
2020-04-23 21:03:36 +02:00
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 20:27:32 +02:00
|
|
|
LastFmCoverProvider::LastFmImageSize LastFmCoverProvider::ImageSizeFromString(const QString &size) {
|
2020-04-23 21:03:36 +02:00
|
|
|
|
2018-12-15 00:43:50 +01:00
|
|
|
if (size == "small") return LastFmImageSize::Small;
|
|
|
|
else if (size == "medium") return LastFmImageSize::Medium;
|
|
|
|
else if (size == "large") return LastFmImageSize::Large;
|
|
|
|
else if (size == "extralarge") return LastFmImageSize::ExtraLarge;
|
|
|
|
else return LastFmImageSize::Unknown;
|
2020-04-23 21:03:36 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|