strawberry-audio-player-win.../src/covermanager/musicbrainzcoverprovider.cpp

254 lines
7.9 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* Copyright 2018, 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:04:37 +02:00
#include <algorithm>
2020-02-09 02:29:35 +01:00
#include <QtGlobal>
#include <QObject>
#include <QVariant>
#include <QByteArray>
#include <QString>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonObject>
#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"
2018-02-27 18:06:05 +01:00
#include "core/network.h"
2018-09-01 22:21:45 +02:00
#include "core/logging.h"
#include "albumcoverfetcher.h"
#include "coverprovider.h"
2018-02-27 18:06:05 +01:00
#include "musicbrainzcoverprovider.h"
2018-09-01 22:21:45 +02:00
const char *MusicbrainzCoverProvider::kReleaseSearchUrl = "https://musicbrainz.org/ws/2/release/";
const char *MusicbrainzCoverProvider::kAlbumCoverUrl = "https://coverartarchive.org/release/%1/front";
const int MusicbrainzCoverProvider::kLimit = 8;
2018-02-27 18:06:05 +01:00
2020-05-09 01:48:08 +02:00
MusicbrainzCoverProvider::MusicbrainzCoverProvider(Application *app, QObject *parent): CoverProvider("MusicBrainz", true, false, 1.5, true, false, app, parent), network_(new NetworkAccessManager(this)) {}
2018-02-27 18:06:05 +01:00
bool MusicbrainzCoverProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const int id) {
Q_UNUSED(title);
2018-02-27 18:06:05 +01:00
QString query = QString("release:\"%1\" AND artist:\"%2\"").arg(album.trimmed().replace('"', "\\\"")).arg(artist.trimmed().replace('"', "\\\""));
2018-02-27 18:06:05 +01:00
QUrlQuery url_query;
url_query.addQueryItem("query", query);
url_query.addQueryItem("limit", QString::number(kLimit));
url_query.addQueryItem("fmt", "json");
2018-09-01 22:21:45 +02:00
QUrl url(kReleaseSearchUrl);
2018-02-27 18:06:05 +01:00
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
QNetworkReply *reply = network_->get(req);
2020-04-23 21:04:37 +02:00
connect(reply, &QNetworkReply::finished, [=] { HandleSearchReply(reply, id); });
2018-02-27 18:06:05 +01:00
return true;
2018-02-27 18:06:05 +01:00
}
2019-09-15 20:27:32 +02:00
void MusicbrainzCoverProvider::HandleSearchReply(QNetworkReply *reply, const int search_id) {
2018-02-27 18:06:05 +01:00
reply->deleteLater();
CoverSearchResults results;
QByteArray data = GetReplyData(reply);
if (data.isEmpty()) {
emit SearchFinished(search_id, results);
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
emit SearchFinished(search_id, results);
return;
}
if (!json_obj.contains("releases")) {
if (json_obj.contains("error")) {
QString error = json_obj["error"].toString();
Error(error);
2018-02-27 18:06:05 +01:00
}
else {
Error(QString("Json reply is missing releases."), json_obj);
}
emit SearchFinished(search_id, results);
return;
}
2020-04-23 21:04:37 +02:00
QJsonValue value_releases = json_obj["releases"];
2020-04-23 21:04:37 +02:00
if (!value_releases.isArray()) {
Error("Json releases is not an array.", value_releases);
emit SearchFinished(search_id, results);
return;
2018-02-27 18:06:05 +01:00
}
2020-04-23 21:04:37 +02:00
QJsonArray array_releases = value_releases.toArray();
2018-02-27 18:06:05 +01:00
2020-04-23 21:04:37 +02:00
if (array_releases.isEmpty()) {
emit SearchFinished(search_id, results);
return;
2018-02-27 18:06:05 +01:00
}
2020-04-23 21:04:37 +02:00
for (const QJsonValue &value_release : array_releases) {
2019-04-17 22:18:03 +02:00
2020-04-23 21:04:37 +02:00
if (!value_release.isObject()) {
Error("Invalid Json reply, releases array value is not an object.", value_release);
continue;
}
2020-04-23 21:04:37 +02:00
QJsonObject obj_release = value_release.toObject();
if (!obj_release.contains("id") || !obj_release.contains("artist-credit") || !obj_release.contains("title")) {
Error("Invalid Json reply, releases array object is missing id, artist-credit or title.", obj_release);
2019-04-17 22:18:03 +02:00
continue;
}
2020-04-23 21:04:37 +02:00
QJsonValue json_artists = obj_release["artist-credit"];
2019-04-17 22:18:03 +02:00
if (!json_artists.isArray()) {
2020-04-23 21:04:37 +02:00
Error("Invalid Json reply, artist-credit is not a array.", json_artists);
continue;
}
2020-04-23 21:04:37 +02:00
QJsonArray array_artists = json_artists.toArray();
2019-04-17 22:18:03 +02:00
int i = 0;
QString artist;
2020-04-23 21:04:37 +02:00
for (const QJsonValue &value_artist : array_artists) {
if (!value_artist.isObject()) {
Error("Invalid Json reply, artist is not a object.", value_artist);
2019-04-17 22:18:03 +02:00
continue;
}
2020-04-23 21:04:37 +02:00
QJsonObject obj_artist = value_artist.toObject();
2019-04-17 22:18:03 +02:00
2020-04-23 21:04:37 +02:00
if (!obj_artist.contains("artist") ) {
Error("Invalid Json reply, artist is missing.", obj_artist);
2019-04-17 22:18:03 +02:00
continue;
}
2020-04-23 21:04:37 +02:00
QJsonValue value_artist2 = obj_artist["artist"];
if (!value_artist2.isObject()) {
Error("Invalid Json reply, artist is not an object.", value_artist2);
2019-04-17 22:18:03 +02:00
continue;
}
2020-04-23 21:04:37 +02:00
QJsonObject obj_artist2 = value_artist2.toObject();
2019-04-17 22:18:03 +02:00
2020-04-23 21:04:37 +02:00
if (!obj_artist2.contains("name") ) {
Error("Invalid Json reply, artist is missing name.", value_artist2);
2019-04-17 22:18:03 +02:00
continue;
}
2020-04-23 21:04:37 +02:00
artist = obj_artist2["name"].toString();
2019-04-17 22:18:03 +02:00
++i;
}
if (i > 1) artist = "Various artists";
2020-04-23 21:04:37 +02:00
QString id = obj_release["id"].toString();
QString album = obj_release["title"].toString();
2019-04-17 22:18:03 +02:00
CoverSearchResult cover_result;
QUrl url(QString(kAlbumCoverUrl).arg(id));
2019-04-17 22:18:03 +02:00
cover_result.artist = artist;
cover_result.album = album;
cover_result.image_url = url;
results.append(cover_result);
}
emit SearchFinished(search_id, results);
2018-02-27 18:06:05 +01:00
}
QByteArray MusicbrainzCoverProvider::GetReplyData(QNetworkReply *reply) {
QByteArray data;
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
data = reply->readAll();
}
else {
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
// This is a network error, there is nothing more to do.
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(failure_reason);
}
else {
// See if there is Json data containing "error" - then use that instead.
data = reply->readAll();
QString error;
QJsonParseError json_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
QJsonObject json_obj = json_doc.object();
if (json_obj.contains("error")) {
error = json_obj["error"].toString();
}
}
if (error.isEmpty()) {
if (reply->error() != QNetworkReply::NoError) {
error = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
}
else {
error = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
}
}
Error(error);
2018-02-27 18:06:05 +01:00
}
return QByteArray();
2018-02-27 18:06:05 +01:00
}
return data;
2018-02-27 18:06:05 +01:00
}
QJsonObject MusicbrainzCoverProvider::ExtractJsonObj(const QByteArray &data) {
2018-02-27 18:06:05 +01:00
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error("Reply from server is missing Json data.", data);
return QJsonObject();
}
2020-04-23 21:04:37 +02:00
if (json_doc.isEmpty()) {
Error("Received empty Json document.", json_doc);
return QJsonObject();
}
if (!json_doc.isObject()) {
Error("Json document is not an object.", json_doc);
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error("Received empty Json object.", json_doc);
return QJsonObject();
2018-02-27 18:06:05 +01:00
}
return json_obj;
}
2020-04-23 21:04:37 +02:00
void MusicbrainzCoverProvider::Error(const QString &error, const QVariant &debug) {
qLog(Error) << "Musicbrainz:" << error;
if (debug.isValid()) qLog(Debug) << debug;
2020-04-23 21:04:37 +02:00
2018-02-27 18:06:05 +01:00
}