Improve Musicbrainz cover provider
This commit is contained in:
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
@@ -36,7 +38,6 @@
|
|||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
|
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
#include "core/closure.h"
|
|
||||||
#include "core/network.h"
|
#include "core/network.h"
|
||||||
#include "core/logging.h"
|
#include "core/logging.h"
|
||||||
#include "albumcoverfetcher.h"
|
#include "albumcoverfetcher.h"
|
||||||
@@ -65,14 +66,12 @@ bool MusicbrainzCoverProvider::StartSearch(const QString &artist, const QString
|
|||||||
QNetworkRequest req(url);
|
QNetworkRequest req(url);
|
||||||
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
||||||
QNetworkReply *reply = network_->get(req);
|
QNetworkReply *reply = network_->get(req);
|
||||||
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply *, int)), reply, id);
|
connect(reply, &QNetworkReply::finished, [=] { HandleSearchReply(reply, id); });
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MusicbrainzCoverProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
|
||||||
|
|
||||||
void MusicbrainzCoverProvider::HandleSearchReply(QNetworkReply *reply, const int search_id) {
|
void MusicbrainzCoverProvider::HandleSearchReply(QNetworkReply *reply, const int search_id) {
|
||||||
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
@@ -102,69 +101,70 @@ void MusicbrainzCoverProvider::HandleSearchReply(QNetworkReply *reply, const int
|
|||||||
emit SearchFinished(search_id, results);
|
emit SearchFinished(search_id, results);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QJsonValue json_value = json_obj["releases"];
|
QJsonValue value_releases = json_obj["releases"];
|
||||||
|
|
||||||
if (!json_value.isArray()) {
|
if (!value_releases.isArray()) {
|
||||||
Error("Json releases is not an array.", json_value);
|
Error("Json releases is not an array.", value_releases);
|
||||||
emit SearchFinished(search_id, results);
|
emit SearchFinished(search_id, results);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QJsonArray json_releases = json_value.toArray();
|
QJsonArray array_releases = value_releases.toArray();
|
||||||
|
|
||||||
if (json_releases.isEmpty()) {
|
if (array_releases.isEmpty()) {
|
||||||
emit SearchFinished(search_id, results);
|
emit SearchFinished(search_id, results);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const QJsonValue &value : json_releases) {
|
for (const QJsonValue &value_release : array_releases) {
|
||||||
|
|
||||||
if (!value.isObject()) {
|
if (!value_release.isObject()) {
|
||||||
Error("Invalid Json reply, album value is not an object.", value);
|
Error("Invalid Json reply, releases array value is not an object.", value_release);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonObject json_obj = value.toObject();
|
QJsonObject obj_release = value_release.toObject();
|
||||||
if (!json_obj.contains("id") || !json_obj.contains("artist-credit") || !json_obj.contains("title")) {
|
if (!obj_release.contains("id") || !obj_release.contains("artist-credit") || !obj_release.contains("title")) {
|
||||||
Error("Invalid Json reply, album is missing id, artist-credit or title.", json_obj);
|
Error("Invalid Json reply, releases array object is missing id, artist-credit or title.", obj_release);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonValue json_artists = json_obj["artist-credit"];
|
QJsonValue json_artists = obj_release["artist-credit"];
|
||||||
if (!json_artists.isArray()) {
|
if (!json_artists.isArray()) {
|
||||||
Error("Json artist-credit is not an array.", json_artists);
|
Error("Invalid Json reply, artist-credit is not a array.", json_artists);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonArray json_array_artists = json_artists.toArray();
|
QJsonArray array_artists = json_artists.toArray();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
QString artist;
|
QString artist;
|
||||||
for (const QJsonValue &json_value_artist : json_array_artists) {
|
for (const QJsonValue &value_artist : array_artists) {
|
||||||
if (!json_value_artist.isObject()) {
|
if (!value_artist.isObject()) {
|
||||||
Error("Invalid Json reply, artist is not an object.", json_value_artist);
|
Error("Invalid Json reply, artist is not a object.", value_artist);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonObject json_obj_artist = json_value_artist.toObject();
|
QJsonObject obj_artist = value_artist.toObject();
|
||||||
|
|
||||||
if (!json_obj_artist.contains("artist") ) {
|
if (!obj_artist.contains("artist") ) {
|
||||||
Error("Invalid Json reply, artist is missing.", json_obj_artist);
|
Error("Invalid Json reply, artist is missing.", obj_artist);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonValue json_value_artist2 = json_obj_artist["artist"];
|
QJsonValue value_artist2 = obj_artist["artist"];
|
||||||
if (!json_value_artist2.isObject()) {
|
if (!value_artist2.isObject()) {
|
||||||
Error("Invalid Json reply, artist is not an object.", json_value_artist2);
|
Error("Invalid Json reply, artist is not an object.", value_artist2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonObject json_obj_artist2 = json_value_artist2.toObject();
|
QJsonObject obj_artist2 = value_artist2.toObject();
|
||||||
|
|
||||||
if (!json_obj_artist2.contains("name") ) {
|
if (!obj_artist2.contains("name") ) {
|
||||||
Error("Invalid Json reply, artist is missing name.", json_value_artist2);
|
Error("Invalid Json reply, artist is missing name.", value_artist2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
artist = json_obj_artist2["name"].toString();
|
artist = obj_artist2["name"].toString();
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
if (i > 1) artist = "Various artists";
|
if (i > 1) artist = "Various artists";
|
||||||
|
|
||||||
QString id = json_obj["id"].toString();
|
QString id = obj_release["id"].toString();
|
||||||
QString album = json_obj["title"].toString();
|
QString album = obj_release["title"].toString();
|
||||||
|
|
||||||
CoverSearchResult cover_result;
|
CoverSearchResult cover_result;
|
||||||
QUrl url(QString(kAlbumCoverUrl).arg(id));
|
QUrl url(QString(kAlbumCoverUrl).arg(id));
|
||||||
cover_result.artist = artist;
|
cover_result.artist = artist;
|
||||||
@@ -227,7 +227,7 @@ QJsonObject MusicbrainzCoverProvider::ExtractJsonObj(const QByteArray &data) {
|
|||||||
Error("Reply from server is missing Json data.", data);
|
Error("Reply from server is missing Json data.", data);
|
||||||
return QJsonObject();
|
return QJsonObject();
|
||||||
}
|
}
|
||||||
if (json_doc.isNull() || json_doc.isEmpty()) {
|
if (json_doc.isEmpty()) {
|
||||||
Error("Received empty Json document.", json_doc);
|
Error("Received empty Json document.", json_doc);
|
||||||
return QJsonObject();
|
return QJsonObject();
|
||||||
}
|
}
|
||||||
@@ -245,7 +245,9 @@ QJsonObject MusicbrainzCoverProvider::ExtractJsonObj(const QByteArray &data) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MusicbrainzCoverProvider::Error(QString error, QVariant debug) {
|
void MusicbrainzCoverProvider::Error(const QString &error, const QVariant &debug) {
|
||||||
|
|
||||||
qLog(Error) << "Musicbrainz:" << error;
|
qLog(Error) << "Musicbrainz:" << error;
|
||||||
if (debug.isValid()) qLog(Debug) << debug;
|
if (debug.isValid()) qLog(Debug) << debug;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ class MusicbrainzCoverProvider : public CoverProvider {
|
|||||||
explicit MusicbrainzCoverProvider(Application *app, QObject *parent = nullptr);
|
explicit MusicbrainzCoverProvider(Application *app, QObject *parent = nullptr);
|
||||||
|
|
||||||
bool StartSearch(const QString &artist, const QString &album, const QString &title, const int id);
|
bool StartSearch(const QString &artist, const QString &album, const QString &title, const int id);
|
||||||
void CancelSearch(const int id);
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void HandleSearchReply(QNetworkReply *reply, const int search_id);
|
void HandleSearchReply(QNetworkReply *reply, const int search_id);
|
||||||
@@ -48,12 +47,13 @@ class MusicbrainzCoverProvider : public CoverProvider {
|
|||||||
private:
|
private:
|
||||||
QByteArray GetReplyData(QNetworkReply *reply);
|
QByteArray GetReplyData(QNetworkReply *reply);
|
||||||
QJsonObject ExtractJsonObj(const QByteArray &data);
|
QJsonObject ExtractJsonObj(const QByteArray &data);
|
||||||
void Error(QString error, QVariant debug = QVariant());
|
void Error(const QString &error, const QVariant &debug = QVariant());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const char *kReleaseSearchUrl;
|
static const char *kReleaseSearchUrl;
|
||||||
static const char *kAlbumCoverUrl;
|
static const char *kAlbumCoverUrl;
|
||||||
static const int kLimit;
|
static const int kLimit;
|
||||||
|
|
||||||
QNetworkAccessManager *network_;
|
QNetworkAccessManager *network_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user