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 <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.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"
|
|
|
|
|
2020-05-10 12:49:11 +02:00
|
|
|
MusixmatchCoverProvider::MusixmatchCoverProvider(Application *app, QObject *parent): JsonCoverProvider("Musixmatch", true, false, 1.0, true, false, app, parent), network_(new NetworkAccessManager(this)) {}
|
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);
|
|
|
|
|
|
|
|
QString artist_stripped = artist;
|
|
|
|
QString album_stripped = album;
|
|
|
|
|
|
|
|
artist_stripped = artist_stripped.replace('/', '-');
|
2020-12-20 06:40:45 +01:00
|
|
|
artist_stripped = artist_stripped.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption));
|
2020-05-08 20:14:16 +02:00
|
|
|
artist_stripped = artist_stripped.simplified();
|
|
|
|
artist_stripped = artist_stripped.replace(' ', '-');
|
2020-07-18 04:05:07 +02:00
|
|
|
artist_stripped = artist_stripped.replace(QRegularExpression("(-)\\1+"), "-");
|
2020-05-08 20:14:16 +02:00
|
|
|
artist_stripped = artist_stripped.toLower();
|
|
|
|
|
|
|
|
album_stripped = album_stripped.replace('/', '-');
|
2020-12-20 06:40:45 +01:00
|
|
|
album_stripped = album_stripped.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption));
|
2020-05-08 20:14:16 +02:00
|
|
|
album_stripped = album_stripped.simplified();
|
|
|
|
album_stripped = album_stripped.replace(' ', '-').toLower();
|
2020-07-18 04:05:07 +02:00
|
|
|
album_stripped = album_stripped.replace(QRegularExpression("(-)\\1+"), "-");
|
2020-05-08 20:14:16 +02:00
|
|
|
album_stripped = album_stripped.toLower();
|
|
|
|
|
|
|
|
if (artist_stripped.isEmpty() || album_stripped.isEmpty()) return false;
|
|
|
|
|
2021-03-21 04:47:11 +01:00
|
|
|
QUrl url(QString("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
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
|
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#else
|
2020-05-08 20:14:16 +02:00
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
2020-08-14 20:20:41 +02:00
|
|
|
#endif
|
2020-12-20 07:42:14 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
2020-12-20 06:51:36 +01:00
|
|
|
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);
|
2020-12-20 07:24:44 +01:00
|
|
|
#endif
|
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) {
|
|
|
|
Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|
|
|
Error(QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
Error("Empty reply received from server.");
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
2020-11-04 18:16:23 +01:00
|
|
|
QString content = data;
|
2020-05-08 20:14:16 +02:00
|
|
|
QString data_begin = "var __mxmState = ";
|
|
|
|
QString data_end = ";</script>";
|
|
|
|
int begin_idx = content.indexOf(data_begin);
|
|
|
|
QString content_json;
|
|
|
|
if (begin_idx > 0) {
|
|
|
|
begin_idx += data_begin.length();
|
|
|
|
int end_idx = content.indexOf(data_end, begin_idx);
|
|
|
|
if (end_idx > begin_idx) {
|
|
|
|
content_json = content.mid(begin_idx, end_idx - begin_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content_json.isEmpty()) {
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-18 04:05:07 +02:00
|
|
|
if (content_json.contains(QRegularExpression("<[^>]*>"))) { // 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) {
|
|
|
|
Error(QString("Failed to parse json data: %1").arg(error.errorString()));
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_doc.isEmpty()) {
|
|
|
|
Error("Received empty Json document.", data);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
|
|
|
Error("Json document is not an object.", json_doc);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
|
|
|
Error("Received empty Json object.", json_doc);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_obj.contains("page") || !json_obj["page"].isObject()) {
|
|
|
|
Error("Json reply is missing page object.", json_obj);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
json_obj = json_obj["page"].toObject();
|
|
|
|
|
|
|
|
if (!json_obj.contains("album") || !json_obj["album"].isObject()) {
|
|
|
|
Error("Json page object is missing album object.", json_obj);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonObject obj_album = json_obj["album"].toObject();
|
|
|
|
|
|
|
|
if (!obj_album.contains("artistName") || !obj_album.contains("name")) {
|
|
|
|
Error("Json album object is missing artistName or name.", obj_album);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResult result;
|
2020-08-09 20:10:53 +02:00
|
|
|
result.artist = obj_album["artistName"].toString();
|
|
|
|
result.album = obj_album["name"].toString();
|
2020-05-08 20:14:16 +02:00
|
|
|
|
2020-08-09 20:10:53 +02:00
|
|
|
if (artist.toLower() != result.artist.toLower() && album.toLower() != result.album.toLower()) {
|
2020-05-08 20:14:16 +02:00
|
|
|
emit SearchFinished(id, results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:10:53 +02:00
|
|
|
QList<QPair<QString, QSize>> cover_sizes = QList<QPair<QString, QSize>>() << qMakePair(QString("coverart800x800"), QSize(800, 800))
|
|
|
|
<< qMakePair(QString("coverart500x500"), QSize(500, 500))
|
2020-08-09 20:15:24 +02:00
|
|
|
<< qMakePair(QString("coverart350x350"), 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) {
|
|
|
|
if (!obj_album.contains(cover_size.first)) continue;
|
|
|
|
QUrl cover_url(obj_album[cover_size.first].toString());
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|