mirror of
https://github.com/strawberrymusicplayer/strawberry
synced 2025-02-08 07:48:48 +01:00
Minor code improvements to Deezer cover provider
This commit is contained in:
parent
45ae1ed265
commit
c379d7f846
@ -39,7 +39,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 "core/song.h"
|
#include "core/song.h"
|
||||||
@ -56,35 +55,33 @@ bool DeezerCoverProvider::StartSearch(const QString &artist, const QString &albu
|
|||||||
|
|
||||||
typedef QPair<QString, QString> Param;
|
typedef QPair<QString, QString> Param;
|
||||||
typedef QList<Param> Params;
|
typedef QList<Param> Params;
|
||||||
typedef QPair<QByteArray, QByteArray> EncodedParam;
|
|
||||||
|
|
||||||
QUrl url(kApiUrl);
|
QString resource;
|
||||||
QString search;
|
QString query;
|
||||||
if (album.isEmpty()) {
|
if (album.isEmpty()) {
|
||||||
url.setPath("/search/track");
|
resource = "search/track";
|
||||||
search = artist + " " + title;
|
query = artist + " " + title;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
url.setPath("/search/album");
|
resource = "search/album";
|
||||||
search = artist + " " + album;
|
query = artist + " " + album;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Params params = Params() << Param("output", "json")
|
const Params params = Params() << Param("output", "json")
|
||||||
<< Param("q", search)
|
<< Param("q", query)
|
||||||
<< Param("limit", QString::number(kLimit));
|
<< Param("limit", QString::number(kLimit));
|
||||||
|
|
||||||
QUrlQuery url_query;
|
QUrlQuery url_query;
|
||||||
for (const Param ¶m : params) {
|
for (const Param ¶m : params) {
|
||||||
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
||||||
url_query.addQueryItem(encoded_param.first, encoded_param.second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QUrl url(kApiUrl + QString("/") + resource);
|
||||||
url.setQuery(url_query);
|
url.setQuery(url_query);
|
||||||
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);
|
||||||
|
connect(reply, &QNetworkReply::finished, [=] { this->HandleSearchReply(reply, id); });
|
||||||
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, int)), reply, id);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -111,15 +108,15 @@ QByteArray DeezerCoverProvider::GetReplyData(QNetworkReply *reply) {
|
|||||||
QJsonParseError json_error;
|
QJsonParseError json_error;
|
||||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||||
QString error;
|
QString error;
|
||||||
if (json_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
|
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
||||||
QJsonObject json_obj = json_doc.object();
|
QJsonObject json_obj = json_doc.object();
|
||||||
if (json_obj.contains("error")) {
|
if (json_obj.contains("error")) {
|
||||||
QJsonValue json_value_error = json_obj["error"];
|
QJsonValue value_error = json_obj["error"];
|
||||||
if (json_value_error.isObject()) {
|
if (value_error.isObject()) {
|
||||||
QJsonObject json_error = json_value_error.toObject();
|
QJsonObject obj_error = value_error.toObject();
|
||||||
int code = json_error["code"].toInt();
|
int code = obj_error["code"].toInt();
|
||||||
QString message = json_error["message"].toString();
|
QString message = obj_error["message"].toString();
|
||||||
QString type = json_error["type"].toString();
|
QString type = obj_error["type"].toString();
|
||||||
error = QString("%1 (%2)").arg(message).arg(code);
|
error = QString("%1 (%2)").arg(message).arg(code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,7 +148,7 @@ QJsonObject DeezerCoverProvider::ExtractJsonObj(const QByteArray &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();
|
||||||
}
|
}
|
||||||
@ -177,29 +174,29 @@ QJsonValue DeezerCoverProvider::ExtractData(const QByteArray &data) {
|
|||||||
if (json_obj.isEmpty()) return QJsonObject();
|
if (json_obj.isEmpty()) return QJsonObject();
|
||||||
|
|
||||||
if (json_obj.contains("error")) {
|
if (json_obj.contains("error")) {
|
||||||
QJsonValue json_value_error = json_obj["error"];
|
QJsonValue value_error = json_obj["error"];
|
||||||
if (!json_value_error.isObject()) {
|
if (!value_error.isObject()) {
|
||||||
Error("Error missing object", json_obj);
|
Error("Error missing object", json_obj);
|
||||||
return QJsonValue();
|
return QJsonValue();
|
||||||
}
|
}
|
||||||
QJsonObject json_error = json_value_error.toObject();
|
QJsonObject obj_error = value_error.toObject();
|
||||||
int code = json_error["code"].toInt();
|
const int code = obj_error["code"].toInt();
|
||||||
QString message = json_error["message"].toString();
|
QString message = obj_error["message"].toString();
|
||||||
QString type = json_error["type"].toString();
|
QString type = obj_error["type"].toString();
|
||||||
Error(QString("%1 (%2)").arg(message).arg(code));
|
Error(QString("%1 (%2)").arg(message).arg(code));
|
||||||
return QJsonValue();
|
return QJsonValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!json_obj.contains("data") && !json_obj.contains("DATA")) {
|
if (!json_obj.contains("data") && !json_obj.contains("DATA")) {
|
||||||
Error("Json reply is missing data.", json_obj);
|
Error("Json reply object is missing data.", json_obj);
|
||||||
return QJsonValue();
|
return QJsonValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonValue json_data;
|
QJsonValue value_data;
|
||||||
if (json_obj.contains("data")) json_data = json_obj["data"];
|
if (json_obj.contains("data")) value_data = json_obj["data"];
|
||||||
else json_data = json_obj["DATA"];
|
else value_data = json_obj["DATA"];
|
||||||
|
|
||||||
return json_data;
|
return value_data;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,83 +212,86 @@ void DeezerCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonValue json_value = ExtractData(data);
|
QJsonValue value_data = ExtractData(data);
|
||||||
if (!json_value.isArray()) {
|
if (!value_data.isArray()) {
|
||||||
emit SearchFinished(id, results);
|
emit SearchFinished(id, results);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray json_data = json_value.toArray();
|
QJsonArray array_data = value_data.toArray();
|
||||||
if (json_data.isEmpty()) {
|
if (array_data.isEmpty()) {
|
||||||
emit SearchFinished(id, results);
|
emit SearchFinished(id, results);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const QJsonValue &value : json_data) {
|
for (const QJsonValue &json_value : array_data) {
|
||||||
|
|
||||||
if (!value.isObject()) {
|
if (!json_value.isObject()) {
|
||||||
Error("Invalid Json reply, data in array is not a object.", value);
|
Error("Invalid Json reply, data array value is not a object.", json_value);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonObject json_obj = value.toObject();
|
QJsonObject json_obj = json_value.toObject();
|
||||||
QJsonObject json_album = json_obj;
|
QJsonObject obj_album;
|
||||||
if (json_obj.contains("album") && json_obj["album"].isObject()) { // Song search, so extract the album.
|
if (json_obj.contains("album") && json_obj["album"].isObject()) { // Song search, so extract the album.
|
||||||
json_album = json_obj["album"].toObject();
|
obj_album = json_obj["album"].toObject();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
obj_album = json_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!json_obj.contains("id") || !json_album.contains("id")) {
|
if (!json_obj.contains("id") || !obj_album.contains("id")) {
|
||||||
Error("Invalid Json reply, object is missing ID.", json_obj);
|
Error("Invalid Json reply, data array value object is missing ID.", json_obj);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!json_album.contains("type")) {
|
if (!obj_album.contains("type")) {
|
||||||
Error("Invalid Json reply, album object is missing type.", json_album);
|
Error("Invalid Json reply, data array value album object is missing type.", obj_album);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QString type = json_album["type"].toString();
|
QString type = obj_album["type"].toString();
|
||||||
if (type != "album") {
|
if (type != "album") {
|
||||||
Error("Invalid Json reply, incorrect type returned", json_album);
|
Error("Invalid Json reply, data array value album object has incorrect type returned", obj_album);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!json_obj.contains("artist")) {
|
if (!json_obj.contains("artist")) {
|
||||||
Error("Invalid Json reply, item missing artist.", json_obj);
|
Error("Invalid Json reply, data array value object is missing artist.", json_obj);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonValue json_value_artist = json_obj["artist"];
|
QJsonValue value_artist = json_obj["artist"];
|
||||||
if (!json_value_artist.isObject()) {
|
if (!value_artist.isObject()) {
|
||||||
Error("Invalid Json reply, artist is not a object.", json_value_artist);
|
Error("Invalid Json reply, data array value artist is not a object.", value_artist);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonObject json_artist = json_value_artist.toObject();
|
QJsonObject obj_artist = value_artist.toObject();
|
||||||
|
|
||||||
if (!json_artist.contains("name")) {
|
if (!obj_artist.contains("name")) {
|
||||||
Error("Invalid Json reply, artist data missing name.", json_artist);
|
Error("Invalid Json reply, data array value artist object is missing name.", obj_artist);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QString artist = json_artist["name"].toString();
|
QString artist = obj_artist["name"].toString();
|
||||||
|
|
||||||
if (!json_album.contains("title")) {
|
if (!obj_album.contains("title")) {
|
||||||
Error("Invalid Json reply, data missing title.", json_album);
|
Error("Invalid Json reply, data array value album object is missing title.", obj_album);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QString album = json_album["title"].toString();
|
QString album = obj_album["title"].toString();
|
||||||
|
|
||||||
QString cover;
|
QString cover;
|
||||||
if (json_album.contains("cover_xl")) {
|
if (obj_album.contains("cover_xl")) {
|
||||||
cover = json_album["cover_xl"].toString();
|
cover = obj_album["cover_xl"].toString();
|
||||||
}
|
}
|
||||||
else if (json_album.contains("cover_big")) {
|
else if (obj_album.contains("cover_big")) {
|
||||||
cover = json_album["cover_big"].toString();
|
cover = obj_album["cover_big"].toString();
|
||||||
}
|
}
|
||||||
else if (json_album.contains("cover_medium")) {
|
else if (obj_album.contains("cover_medium")) {
|
||||||
cover = json_album["cover_medium"].toString();
|
cover = obj_album["cover_medium"].toString();
|
||||||
}
|
}
|
||||||
else if (json_album.contains("cover_small")) {
|
else if (obj_album.contains("cover_small")) {
|
||||||
cover = json_album["cover_small"].toString();
|
cover = obj_album["cover_small"].toString();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Error("Invalid Json reply, album missing cover.", json_album);
|
Error("Invalid Json reply, data array value album object is missing cover.", obj_album);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QUrl url(cover);
|
QUrl url(cover);
|
||||||
|
@ -47,14 +47,15 @@ class DeezerCoverProvider : public CoverProvider {
|
|||||||
void HandleSearchReply(QNetworkReply *reply, const int id);
|
void HandleSearchReply(QNetworkReply *reply, const int id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const char *kApiUrl;
|
|
||||||
static const int kLimit;
|
|
||||||
|
|
||||||
QByteArray GetReplyData(QNetworkReply *reply);
|
QByteArray GetReplyData(QNetworkReply *reply);
|
||||||
QJsonObject ExtractJsonObj(const QByteArray &data);
|
QJsonObject ExtractJsonObj(const QByteArray &data);
|
||||||
QJsonValue ExtractData(const QByteArray &data);
|
QJsonValue ExtractData(const QByteArray &data);
|
||||||
void Error(const QString &error, const QVariant &debug = QVariant());
|
void Error(const QString &error, const QVariant &debug = QVariant());
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const char *kApiUrl;
|
||||||
|
static const int kLimit;
|
||||||
|
|
||||||
QNetworkAccessManager *network_;
|
QNetworkAccessManager *network_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user