2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2012, Martin Björklund <mbj4668@gmail.com>
|
|
|
|
* Copyright 2016, Jonas Kvinge <jonas@jkvinge.net>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
2018-03-17 14:28:45 +01:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QList>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QPair>
|
|
|
|
#include <QVariant>
|
2018-03-17 14:28:45 +01:00
|
|
|
#include <QString>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QStringBuilder>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QStringList>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QUrl>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QUrlQuery>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
2018-03-02 22:51:42 +01:00
|
|
|
#include <QJsonDocument>
|
2018-03-17 14:28:45 +01:00
|
|
|
#include <QJsonObject>
|
2018-12-02 23:29:22 +01:00
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonArray>
|
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/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/utilities.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "coverprovider.h"
|
|
|
|
#include "albumcoverfetcher.h"
|
|
|
|
#include "discogscoverprovider.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
const char *DiscogsCoverProvider::kUrlSearch = "https://api.discogs.com/database/search";
|
|
|
|
const char *DiscogsCoverProvider::kUrlReleases = "https://api.discogs.com/releases";
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
const char *DiscogsCoverProvider::kAccessKeyB64 = "dGh6ZnljUGJlZ1NEeXBuSFFxSVk=";
|
2018-03-17 14:28:45 +01:00
|
|
|
const char *DiscogsCoverProvider::kSecretKeyB64 = "ZkFIcmlaSER4aHhRSlF2U3d0bm5ZVmdxeXFLWUl0UXI=";
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-17 22:18:03 +02:00
|
|
|
DiscogsCoverProvider::DiscogsCoverProvider(Application *app, QObject *parent) : CoverProvider("Discogs", 0.0, false, app, parent), network_(new NetworkAccessManager(this)) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
bool DiscogsCoverProvider::StartSearch(const QString &artist, const QString &album, int s_id) {
|
|
|
|
|
|
|
|
DiscogsCoverSearchContext *s_ctx = new DiscogsCoverSearchContext;
|
2018-03-17 14:28:45 +01:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
s_ctx->id = s_id;
|
|
|
|
s_ctx->artist = artist;
|
|
|
|
s_ctx->album = album;
|
|
|
|
s_ctx->r_count = 0;
|
|
|
|
requests_search_.insert(s_id, s_ctx);
|
|
|
|
SendSearchRequest(s_ctx);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
return true;
|
2018-03-02 22:51:42 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2018-03-02 22:51:42 +01:00
|
|
|
|
2018-03-17 14:28:45 +01:00
|
|
|
void DiscogsCoverProvider::CancelSearch(int id) {
|
|
|
|
delete requests_search_.take(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-17 22:22:35 +02:00
|
|
|
bool DiscogsCoverProvider::StartRelease(DiscogsCoverSearchContext *s_ctx, int r_id, QString &resource_url) {
|
2018-03-02 22:51:42 +01:00
|
|
|
|
|
|
|
DiscogsCoverReleaseContext *r_ctx = new DiscogsCoverReleaseContext;
|
|
|
|
|
|
|
|
s_ctx->r_count++;
|
|
|
|
|
|
|
|
r_ctx->id = r_id;
|
|
|
|
r_ctx->resource_url = resource_url;
|
|
|
|
|
|
|
|
r_ctx->s_id = s_ctx->id;
|
|
|
|
|
|
|
|
requests_release_.insert(r_id, r_ctx);
|
|
|
|
SendReleaseRequest(s_ctx, r_ctx);
|
|
|
|
|
|
|
|
return true;
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext *s_ctx) {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
typedef QPair<QString, QString> Arg;
|
|
|
|
typedef QList<Arg> ArgList;
|
|
|
|
typedef QPair<QByteArray, QByteArray> EncodedArg;
|
|
|
|
|
|
|
|
ArgList args = ArgList()
|
|
|
|
<< Arg("key", QByteArray::fromBase64(kAccessKeyB64))
|
2018-03-17 14:28:45 +01:00
|
|
|
<< Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
|
2018-03-02 22:51:42 +01:00
|
|
|
|
|
|
|
args.append(Arg("type", "release"));
|
|
|
|
if (!s_ctx->artist.isEmpty()) {
|
|
|
|
args.append(Arg("artist", s_ctx->artist.toLower()));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2018-03-02 22:51:42 +01:00
|
|
|
if (!s_ctx->album.isEmpty()) {
|
|
|
|
args.append(Arg("release_title", s_ctx->album.toLower()));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
QStringList query_items;
|
|
|
|
|
|
|
|
// Encode the arguments
|
|
|
|
for (const Arg &arg : args) {
|
|
|
|
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second));
|
|
|
|
query_items << QString(encoded_arg.first + "=" + encoded_arg.second);
|
|
|
|
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QUrl url(kUrlSearch);
|
|
|
|
url.setQuery(url_query);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Sign the request
|
2018-04-05 21:40:05 +02:00
|
|
|
const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toUtf8();
|
2018-03-17 14:28:45 +01:00
|
|
|
const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Add the signature to the request
|
|
|
|
url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64()));
|
|
|
|
|
2019-08-22 19:28:54 +02:00
|
|
|
QNetworkRequest req(url);
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
|
|
|
#endif
|
|
|
|
QNetworkReply *reply = network_->get(req);
|
2018-03-02 22:51:42 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, int)), reply, s_ctx->id);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
typedef QPair<QString, QString> Arg;
|
|
|
|
typedef QList<Arg> ArgList;
|
|
|
|
typedef QPair<QByteArray, QByteArray> EncodedArg;
|
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
QStringList query_items;
|
|
|
|
|
|
|
|
ArgList args = ArgList()
|
|
|
|
<< Arg("key", QByteArray::fromBase64(kAccessKeyB64))
|
2018-03-17 14:28:45 +01:00
|
|
|
<< Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
|
2018-03-02 22:51:42 +01:00
|
|
|
// Encode the arguments
|
|
|
|
for (const Arg &arg : args) {
|
|
|
|
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second));
|
|
|
|
query_items << QString(encoded_arg.first + "=" + encoded_arg.second);
|
|
|
|
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url(r_ctx->resource_url);
|
|
|
|
|
|
|
|
// Sign the request
|
2018-03-17 14:28:45 +01:00
|
|
|
const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toUtf8();
|
|
|
|
const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
|
2018-03-02 22:51:42 +01:00
|
|
|
|
|
|
|
// Add the signature to the request
|
|
|
|
url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64()));
|
|
|
|
|
|
|
|
url.setQuery(url_query);
|
|
|
|
|
2019-08-22 19:28:54 +02:00
|
|
|
QNetworkRequest req(url);
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
|
|
|
#endif
|
|
|
|
QNetworkReply *reply = network_->get(req);
|
2018-03-02 22:51:42 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleReleaseReply(QNetworkReply*, int, int)), reply, s_ctx->id, r_ctx->id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QByteArray DiscogsCoverProvider::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-02 23:29:22 +01:00
|
|
|
// This is a network error, there is nothing more to do.
|
2019-07-07 21:14:24 +02:00
|
|
|
QString error = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
|
|
|
Error(error);
|
2018-12-02 23:29:22 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// See if there is Json data containing "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);
|
|
|
|
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
2018-12-02 23:29:22 +01:00
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.contains("message")) {
|
2019-07-07 21:14:24 +02:00
|
|
|
error = json_obj["message"].toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error.isEmpty()) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
error = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
2018-12-02 23:29:22 +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-02 23:29:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 21:14:24 +02:00
|
|
|
Error(error);
|
2018-12-02 23:29:22 +01:00
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject DiscogsCoverProvider::ExtractJsonObj(const QByteArray &data) {
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
Error("Reply from server missing Json data.", data);
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
2019-04-17 22:18:03 +02:00
|
|
|
if (json_doc.isEmpty()) {
|
2018-12-02 23:29:22 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_obj;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue DiscogsCoverProvider::ExtractData(const QByteArray &data, const QString name, const bool silent) {
|
|
|
|
|
|
|
|
QJsonObject json_obj = ExtractJsonObj(data);
|
|
|
|
if (json_obj.isEmpty()) return QJsonObject();
|
|
|
|
|
|
|
|
if (json_obj.contains(name)) {
|
|
|
|
QJsonValue json_results = json_obj[name];
|
|
|
|
return json_results;
|
|
|
|
}
|
|
|
|
else if (json_obj.contains("message")) {
|
|
|
|
QString message = json_obj["message"].toString();
|
|
|
|
Error(QString("%1").arg(message));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!silent) Error(QString("Json reply is missing \"%1\".").arg(name), json_obj);
|
|
|
|
}
|
|
|
|
return QJsonValue();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) {
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
if (!requests_search_.contains(s_id)) {
|
2018-12-02 23:29:22 +01:00
|
|
|
Error(QString("Got reply for cancelled request: %1").arg(s_id));
|
2018-03-02 22:51:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-17 14:28:45 +01:00
|
|
|
DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QByteArray data = GetReplyData(reply);
|
2018-12-15 00:43:00 +01:00
|
|
|
if (data.isEmpty()) {
|
|
|
|
EndSearch(s_ctx);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QJsonValue json_value = ExtractData(data, "results");
|
|
|
|
if (!json_value.isArray()) {
|
2018-03-02 22:51:42 +01:00
|
|
|
EndSearch(s_ctx);
|
2018-02-27 18:06:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QJsonArray json_results = json_value.toArray();
|
|
|
|
if (json_results.isEmpty()) {
|
|
|
|
Error("Json array is empty.");
|
2018-03-02 22:51:42 +01:00
|
|
|
EndSearch(s_ctx);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-03-02 22:51:42 +01:00
|
|
|
int i = 0;
|
2018-12-02 23:29:22 +01:00
|
|
|
for (const QJsonValue &value : json_results) {
|
|
|
|
if (!value.isObject()) {
|
|
|
|
Error("Invalid Json reply, data is not an object.", value);
|
|
|
|
continue;
|
2018-03-02 22:51:42 +01:00
|
|
|
}
|
2018-12-02 23:29:22 +01:00
|
|
|
QJsonObject json_obj = value.toObject();
|
|
|
|
if (!json_obj.contains("id") || !json_obj.contains("title") || !json_obj.contains("resource_url")) {
|
|
|
|
Error("Invalid Json reply, value is missing ID, title or resource_url.", json_obj);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int r_id = json_obj["id"].toInt();
|
|
|
|
QString resource_url = json_obj["resource_url"].toString();
|
|
|
|
if (resource_url.isEmpty()) continue;
|
2019-04-17 22:22:35 +02:00
|
|
|
StartRelease(s_ctx, r_id, resource_url);
|
2018-12-02 23:29:22 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i <= 0) {
|
|
|
|
Error("Ending search with no results.");
|
|
|
|
EndSearch(s_ctx);
|
2018-03-02 22:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply *reply, int s_id, int r_id) {
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (!requests_release_.contains(r_id)) {
|
2018-12-02 23:29:22 +01:00
|
|
|
Error(QString("Got reply for cancelled request: %1 %2").arg(s_id).arg(r_id));
|
2018-03-02 22:51:42 +01:00
|
|
|
return;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2018-03-17 14:28:45 +01:00
|
|
|
DiscogsCoverReleaseContext *r_ctx = requests_release_.value(r_id);
|
2018-03-02 22:51:42 +01:00
|
|
|
|
|
|
|
if (!requests_search_.contains(s_id)) {
|
2018-12-02 23:29:22 +01:00
|
|
|
Error(QString("Got reply for cancelled request: %1 %2").arg(s_id).arg(r_id));
|
2018-03-17 14:28:45 +01:00
|
|
|
EndSearch(r_ctx);
|
2018-03-02 22:51:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-17 14:28:45 +01:00
|
|
|
DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
QByteArray data = GetReplyData(reply);
|
2018-12-15 00:43:00 +01:00
|
|
|
if (data.isEmpty()) {
|
2019-04-17 22:18:03 +02:00
|
|
|
EndSearch(s_ctx, r_ctx);
|
2018-12-15 00:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-17 22:18:03 +02:00
|
|
|
QJsonObject json_obj = ExtractJsonObj(data);
|
|
|
|
if (json_obj.isEmpty()) {
|
2018-03-02 22:51:42 +01:00
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 22:18:03 +02:00
|
|
|
if (!json_obj.contains("artists") || !json_obj.contains("title") || !json_obj.contains("images")) {
|
|
|
|
Error("Json reply is missing artists, title or images.");
|
2018-03-02 22:51:42 +01:00
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-17 22:18:03 +02:00
|
|
|
QJsonValue json_artists = json_obj["artists"];
|
|
|
|
if (!json_artists.isArray()) {
|
|
|
|
Error("Json artists is not a array.", json_artists);
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray json_array_artists = json_artists.toArray();
|
2018-12-02 23:29:22 +01:00
|
|
|
int i = 0;
|
2019-04-17 22:18:03 +02:00
|
|
|
QString artist;
|
|
|
|
for (const QJsonValue &value : json_array_artists) {
|
|
|
|
if (!value.isObject()) {
|
|
|
|
Error("Invalid Json reply, value is not a object.", value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QJsonObject json_obj_artist = value.toObject();
|
|
|
|
if (!json_obj_artist.contains("name") ) {
|
|
|
|
Error("Invalid Json reply, artists is missing name.", json_obj_artist);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
artist = json_obj_artist["name"].toString();
|
|
|
|
++i;
|
|
|
|
if (artist == s_ctx->artist) break;
|
|
|
|
}
|
|
|
|
if (artist.isEmpty()) {
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i > 1 && artist != s_ctx->artist) artist = "Various artists";
|
|
|
|
|
|
|
|
QString album = json_obj["title"].toString();
|
|
|
|
if (artist != s_ctx->artist && album != s_ctx->album) {
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue json_images = json_obj["images"];
|
|
|
|
if (!json_images.isArray()) {
|
|
|
|
Error("Json images is not an array.");
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray json_array_images = json_images.toArray();
|
|
|
|
|
|
|
|
if (json_array_images.isEmpty()) {
|
|
|
|
Error("Json array is empty.");
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (const QJsonValue &value : json_array_images) {
|
|
|
|
|
2018-12-02 23:29:22 +01:00
|
|
|
if (!value.isObject()) {
|
|
|
|
Error("Invalid Json reply, value is not an object.", value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QJsonObject json_obj = value.toObject();
|
2019-04-17 22:18:03 +02:00
|
|
|
if (!json_obj.contains("type") || !json_obj.contains("resource_url") || !json_obj.contains("width") || !json_obj.contains("height") ) {
|
|
|
|
Error("Invalid Json reply, images value is missing type, resource_url, width or height.", json_obj);
|
2018-12-02 23:29:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QString type = json_obj["type"].toString();
|
|
|
|
i++;
|
|
|
|
if (type != "primary") {
|
|
|
|
continue;
|
2018-03-02 22:51:42 +01:00
|
|
|
}
|
2019-04-17 22:18:03 +02:00
|
|
|
int width = json_obj["width"].toInt();
|
|
|
|
int height = json_obj["height"].toInt();
|
|
|
|
if (width < 300 || height < 300) continue;
|
|
|
|
const float aspect_score = 1.0 - float(std::max(width, height) - std::min(width, height)) / std::max(height, width);
|
|
|
|
if (aspect_score < 0.85) continue;
|
|
|
|
CoverSearchResult cover_result;
|
|
|
|
cover_result.artist = artist;
|
|
|
|
cover_result.album = album;
|
2018-12-02 23:29:22 +01:00
|
|
|
cover_result.image_url = QUrl(json_obj["resource_url"].toString());
|
2018-03-17 14:28:45 +01:00
|
|
|
if (cover_result.image_url.isEmpty()) continue;
|
2018-03-02 22:51:42 +01:00
|
|
|
s_ctx->results.append(cover_result);
|
|
|
|
}
|
2018-12-02 23:29:22 +01:00
|
|
|
if (i <= 0) {
|
|
|
|
Error("Ending search with no results.");
|
2018-03-02 22:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EndSearch(s_ctx, r_ctx);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) {
|
|
|
|
|
2018-03-17 14:28:45 +01:00
|
|
|
delete requests_release_.take(r_ctx->id);
|
2018-03-02 22:51:42 +01:00
|
|
|
s_ctx->r_count--;
|
|
|
|
if (s_ctx->r_count <= 0) EndSearch(s_ctx);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext *s_ctx) {
|
|
|
|
|
2018-03-17 14:28:45 +01:00
|
|
|
requests_search_.remove(s_ctx->id);
|
2018-03-02 22:51:42 +01:00
|
|
|
emit SearchFinished(s_ctx->id, s_ctx->results);
|
|
|
|
delete s_ctx;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
2018-03-17 14:28:45 +01:00
|
|
|
|
2019-04-17 22:18:03 +02:00
|
|
|
void DiscogsCoverProvider::EndSearch(DiscogsCoverReleaseContext *r_ctx) {
|
2018-03-17 14:28:45 +01:00
|
|
|
delete requests_release_.take(r_ctx->id);
|
|
|
|
}
|
2018-12-02 23:29:22 +01:00
|
|
|
|
|
|
|
void DiscogsCoverProvider::Error(QString error, QVariant debug) {
|
|
|
|
qLog(Error) << "Discogs:" << error;
|
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
|
|
|
}
|