2020-05-08 20:14:16 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2020-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2020-05-08 20:14:16 +02: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
2020-07-18 04:05:07 +02:00
|
|
|
#include <QRegularExpression>
|
2020-05-08 20:14:16 +02:00
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2020-05-08 20:14:16 +02:00
|
|
|
#include "core/logging.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2022-10-18 22:49:15 +02:00
|
|
|
#include "providers/musixmatchprovider.h"
|
2020-05-08 20:14:16 +02:00
|
|
|
#include "albumcoverfetcher.h"
|
2020-05-10 12:49:11 +02:00
|
|
|
#include "jsoncoverprovider.h"
|
2020-05-08 20:14:16 +02:00
|
|
|
#include "musixmatchcoverprovider.h"
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
MusixmatchCoverProvider::MusixmatchCoverProvider(Application *app, SharedPtr<NetworkAccessManager> network, QObject *parent)
|
2024-04-09 23:20:26 +02:00
|
|
|
: JsonCoverProvider(QStringLiteral("Musixmatch"), true, false, 1.0, true, false, app, network, parent) {}
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
MusixmatchCoverProvider::~MusixmatchCoverProvider() {
|
|
|
|
|
|
|
|
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-05-08 20:14:16 +02:00
|
|
|
bool MusixmatchCoverProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const int id) {
|
|
|
|
|
|
|
|
Q_UNUSED(title);
|
|
|
|
|
2022-10-18 22:49:15 +02:00
|
|
|
if (artist.isEmpty() || album.isEmpty()) return false;
|
|
|
|
|
|
|
|
QString artist_stripped = StringFixup(artist);
|
|
|
|
QString album_stripped = StringFixup(album);
|
2020-05-08 20:14:16 +02:00
|
|
|
|
|
|
|
if (artist_stripped.isEmpty() || album_stripped.isEmpty()) return false;
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
QUrl url(QStringLiteral("https://www.musixmatch.com/album/%1/%2").arg(artist_stripped, album_stripped));
|
2020-05-08 20:14:16 +02:00
|
|
|
QNetworkRequest req(url);
|
2020-08-14 20:20:41 +02:00
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
2020-05-08 20:14:16 +02:00
|
|
|
QNetworkReply *reply = network_->get(req);
|
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, artist, album]() { HandleSearchReply(reply, id, artist, album); });
|
2020-05-08 20:14:16 +02:00
|
|
|
|
|
|
|
//qLog(Debug) << "Musixmatch: Sending request for" << artist_stripped << album_stripped << url;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusixmatchCoverProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
|
|
|
|
|
|
|
void MusixmatchCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id, const QString &artist, const QString &album) {
|
|
|
|
|
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);
|
2020-05-08 20:14:16 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResults results;
|
2020-05-08 20:14:16 +02:00
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
const QByteArray data = reply->readAll();
|
2020-05-08 20:14:16 +02:00
|
|
|
if (data.isEmpty()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Empty reply received from server."));
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
const QString content = QString::fromUtf8(data);
|
2024-04-09 23:20:26 +02:00
|
|
|
const QString data_begin = QStringLiteral("<script id=\"__NEXT_DATA__\" type=\"application/json\">");
|
|
|
|
const QString data_end = QStringLiteral("</script>");
|
2024-03-24 06:59:01 +01:00
|
|
|
if (!content.contains(data_begin) || !content.contains(data_end)) {
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-30 02:21:29 +02:00
|
|
|
qint64 begin_idx = content.indexOf(data_begin);
|
2020-05-08 20:14:16 +02:00
|
|
|
QString content_json;
|
|
|
|
if (begin_idx > 0) {
|
|
|
|
begin_idx += data_begin.length();
|
2021-10-30 02:21:29 +02:00
|
|
|
qint64 end_idx = content.indexOf(data_end, begin_idx);
|
2020-05-08 20:14:16 +02:00
|
|
|
if (end_idx > begin_idx) {
|
|
|
|
content_json = content.mid(begin_idx, end_idx - begin_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content_json.isEmpty()) {
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (content_json.contains(QRegularExpression(QStringLiteral("<[^>]*>")))) { // Make sure it's not HTML code.
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(content_json.toUtf8(), &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Failed to parse json data: %1").arg(error.errorString()));
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_doc.isEmpty()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received empty Json document."), data);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Json document is not an object."), json_doc);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-24 06:59:01 +01:00
|
|
|
QJsonObject obj_data = json_doc.object();
|
|
|
|
if (obj_data.isEmpty()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received empty Json object."), json_doc);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!obj_data.contains(QStringLiteral("props")) || !obj_data[QStringLiteral("props")].isObject()) {
|
|
|
|
Error(QStringLiteral("Json reply is missing props."), obj_data);
|
2024-03-24 06:59:01 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
obj_data = obj_data[QStringLiteral("props")].toObject();
|
2024-03-24 06:59:01 +01:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!obj_data.contains(QStringLiteral("pageProps")) || !obj_data[QStringLiteral("pageProps")].isObject()) {
|
|
|
|
Error(QStringLiteral("Json props is missing pageProps."), obj_data);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
obj_data = obj_data[QStringLiteral("pageProps")].toObject();
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!obj_data.contains(QStringLiteral("data")) || !obj_data[QStringLiteral("data")].isObject()) {
|
|
|
|
Error(QStringLiteral("Json pageProps is missing data."), obj_data);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
obj_data = obj_data[QStringLiteral("data")].toObject();
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!obj_data.contains(QStringLiteral("albumGet")) || !obj_data[QStringLiteral("albumGet")].isObject()) {
|
|
|
|
Error(QStringLiteral("Json data is missing albumGet."), obj_data);
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
obj_data = obj_data[QStringLiteral("albumGet")].toObject();
|
2024-03-24 06:59:01 +01:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!obj_data.contains(QStringLiteral("data")) || !obj_data[QStringLiteral("data")].isObject()) {
|
|
|
|
Error(QStringLiteral("Json albumGet reply is missing data."), obj_data);
|
2024-03-24 06:59:01 +01:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
obj_data = obj_data[QStringLiteral("data")].toObject();
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResult result;
|
2024-04-09 23:20:26 +02:00
|
|
|
if (obj_data.contains(QStringLiteral("artistName")) && obj_data[QStringLiteral("artistName")].isString()) {
|
|
|
|
result.artist = obj_data[QStringLiteral("artistName")].toString();
|
2024-03-24 06:59:01 +01:00
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
if (obj_data.contains(QStringLiteral("name")) && obj_data[QStringLiteral("name")].isString()) {
|
|
|
|
result.album = obj_data[QStringLiteral("name")].toString();
|
2024-03-24 06:59:01 +01:00
|
|
|
}
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2021-07-13 23:18:12 +02:00
|
|
|
if (result.artist.compare(artist, Qt::CaseInsensitive) != 0 && result.album.compare(album, Qt::CaseInsensitive) != 0) {
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
QList<QPair<QString, QSize>> cover_sizes = QList<QPair<QString, QSize>>() << qMakePair(QStringLiteral("coverImage800x800"), QSize(800, 800))
|
|
|
|
<< qMakePair(QStringLiteral("coverImage500x500"), QSize(500, 500))
|
|
|
|
<< qMakePair(QStringLiteral("coverImage350x350"), QSize(350, 350));
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2020-08-09 20:10:53 +02:00
|
|
|
for (const QPair<QString, QSize> &cover_size : cover_sizes) {
|
2024-03-24 06:59:01 +01:00
|
|
|
if (!obj_data.contains(cover_size.first)) continue;
|
|
|
|
QUrl cover_url(obj_data[cover_size.first].toString());
|
2020-08-09 20:10:53 +02:00
|
|
|
if (cover_url.isValid()) {
|
|
|
|
result.image_url = cover_url;
|
|
|
|
result.image_size = cover_size.second;
|
|
|
|
results << result;
|
|
|
|
}
|
2020-05-08 20:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusixmatchCoverProvider::Error(const QString &error, const QVariant &debug) {
|
|
|
|
|
|
|
|
qLog(Error) << "Musixmatch:" << error;
|
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
|
|
|
|
|
|
|
}
|