2016-06-22 15:53:35 +02:00
|
|
|
#include "spotifyimages.h"
|
|
|
|
|
2016-10-07 13:29:50 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QPair>
|
2016-10-07 13:29:50 +02:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <algorithm>
|
2016-06-22 15:53:35 +02:00
|
|
|
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
|
|
|
|
namespace {
|
2017-03-06 19:15:59 +01:00
|
|
|
static const char* kSpotifyImagesUrl =
|
|
|
|
"https://data.clementine-player.org/fetchimages";
|
2016-06-22 15:53:35 +02:00
|
|
|
} // namespace
|
|
|
|
|
2016-07-07 18:55:28 +02:00
|
|
|
SpotifyImages::SpotifyImages() : network_(new NetworkAccessManager) {}
|
2016-06-22 15:53:35 +02:00
|
|
|
|
|
|
|
SpotifyImages::~SpotifyImages() {}
|
|
|
|
|
|
|
|
void SpotifyImages::FetchInfo(int id, const Song& metadata) {
|
|
|
|
if (metadata.artist().isEmpty()) {
|
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch artist id.
|
2017-03-06 19:15:59 +01:00
|
|
|
QUrl url(kSpotifyImagesUrl);
|
2017-06-05 21:28:05 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("artist", metadata.artist());
|
|
|
|
url.setQuery(url_query);
|
2016-06-22 15:53:35 +02:00
|
|
|
|
2017-06-05 21:28:05 +02:00
|
|
|
qLog(Debug) << "Fetching artist:" << url;
|
2016-06-22 15:53:35 +02:00
|
|
|
|
2017-03-06 19:15:59 +01:00
|
|
|
QNetworkRequest request(url);
|
2016-06-22 15:53:35 +02:00
|
|
|
QNetworkReply* reply = network_->get(request);
|
|
|
|
NewClosure(reply, SIGNAL(finished()), [this, id, reply]() {
|
|
|
|
reply->deleteLater();
|
2017-06-05 21:28:05 +02:00
|
|
|
|
|
|
|
QJsonParseError error;
|
2020-09-18 16:15:19 +02:00
|
|
|
QJsonDocument json_document =
|
|
|
|
QJsonDocument::fromJson(reply->readAll(), &error);
|
2017-06-05 21:28:05 +02:00
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2016-06-22 15:53:35 +02:00
|
|
|
emit Finished(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:28:05 +02:00
|
|
|
QJsonArray results = json_document.array();
|
2016-06-22 15:53:35 +02:00
|
|
|
QList<QPair<QUrl, QSize>> image_candidates;
|
2020-09-18 16:15:19 +02:00
|
|
|
for (const QJsonValue& v : results) {
|
2017-06-05 21:28:05 +02:00
|
|
|
QJsonObject image = v.toObject();
|
|
|
|
QUrl url = image["url"].toVariant().toUrl();
|
2016-06-22 15:53:35 +02:00
|
|
|
int height = image["height"].toInt();
|
|
|
|
int width = image["width"].toInt();
|
|
|
|
image_candidates.append(qMakePair(url, QSize(width, height)));
|
|
|
|
}
|
2016-07-07 18:55:28 +02:00
|
|
|
if (!image_candidates.isEmpty()) {
|
2017-03-06 19:15:59 +01:00
|
|
|
QPair<QUrl, QSize> best = *std::max_element(
|
|
|
|
image_candidates.begin(), image_candidates.end(),
|
|
|
|
[](const QPair<QUrl, QSize>& a, const QPair<QUrl, QSize>& b) {
|
|
|
|
return (a.second.height() * a.second.width()) <
|
|
|
|
(b.second.height() * b.second.width());
|
|
|
|
});
|
|
|
|
emit ImageReady(id, best.first);
|
2016-07-07 18:55:28 +02:00
|
|
|
}
|
2016-06-22 15:53:35 +02:00
|
|
|
emit Finished(id);
|
|
|
|
});
|
|
|
|
}
|