Clementine-audio-player-Mac.../src/musicbrainz/acoustidclient.cpp

151 lines
4.6 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine 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.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
2012-01-06 17:51:27 +01:00
#include "acoustidclient.h"
#include <QCoreApplication>
2020-09-18 16:15:19 +02:00
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QNetworkReply>
#include <QStringList>
2015-04-11 22:52:31 +02:00
#include <QUrlQuery>
2020-09-18 16:15:19 +02:00
#include <algorithm>
2012-10-12 14:25:04 +02:00
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/timeconstants.h"
2012-01-06 17:51:27 +01:00
const char* AcoustidClient::kClientId = "qsZGpeLx";
2020-01-27 22:46:38 +01:00
const char* AcoustidClient::kUrl = "https://api.acoustid.org/v2/lookup";
const int AcoustidClient::kDefaultTimeout = 5000; // msec
2012-01-06 17:51:27 +01:00
AcoustidClient::AcoustidClient(QObject* parent)
: QObject(parent),
network_(new NetworkAccessManager(this)),
timeouts_(new NetworkTimeouts(kDefaultTimeout, this)) {}
void AcoustidClient::SetTimeout(int msec) { timeouts_->SetTimeout(msec); }
void AcoustidClient::Start(int id, const QString& fingerprint,
int duration_msec) {
typedef QPair<QString, QString> Param;
QList<Param> parameters;
parameters << Param("format", "json") << Param("client", kClientId)
<< Param("duration", QString::number(duration_msec / kMsecPerSec))
<< Param("meta", "recordingids+sources")
<< Param("fingerprint", fingerprint);
QUrl url(kUrl);
2015-04-11 22:52:31 +02:00
QUrlQuery url_query;
url_query.setQueryItems(parameters);
url.setQuery(url_query);
QNetworkRequest req(url);
QNetworkReply* reply = network_->get(req);
2012-10-12 14:25:04 +02:00
NewClosure(reply, SIGNAL(finished()), this,
SLOT(RequestFinished(QNetworkReply*, int)), reply, id);
requests_[id] = reply;
timeouts_->AddReply(reply);
}
void AcoustidClient::Cancel(int id) { delete requests_.take(id); }
2012-01-06 17:51:27 +01:00
void AcoustidClient::CancelAll() {
2012-10-12 14:25:04 +02:00
qDeleteAll(requests_.values());
requests_.clear();
}
namespace {
// Struct used when extracting results in RequestFinished
struct IdSource {
2020-09-18 16:15:19 +02:00
IdSource(const QString& id, int source) : id_(id), nb_sources_(source) {}
bool operator<(const IdSource& other) const {
// We want the items with more sources to be at the beginning of the list
return nb_sources_ > other.nb_sources_;
}
QString id_;
int nb_sources_;
};
2020-09-18 16:15:19 +02:00
} // namespace
void AcoustidClient::RequestFinished(QNetworkReply* reply, int request_id) {
reply->deleteLater();
requests_.remove(request_id);
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() !=
200) {
emit Finished(request_id, QStringList());
return;
}
2015-04-11 22:52:31 +02:00
QJsonParseError error;
2020-09-18 16:15:19 +02:00
QJsonDocument json_document =
QJsonDocument::fromJson(reply->readAll(), &error);
2015-04-11 22:52:31 +02:00
if (error.error != QJsonParseError::NoError) {
emit Finished(request_id, QStringList());
return;
}
2015-04-11 22:52:31 +02:00
QJsonObject json_object = json_document.object();
QString status = json_object["status"].toString();
if (status != "ok") {
emit Finished(request_id, QStringList());
return;
}
// Get the results:
// -in a first step, gather ids and their corresponding number of sources
// -then sort results by number of sources (the results are originally
// unsorted but results with more sources are likely to be more accurate)
// -keep only the ids, as sources where useful only to sort the results
2015-04-11 22:52:31 +02:00
QJsonArray json_results = json_object["results"].toArray();
// List of <id, nb of sources> pairs
QList<IdSource> id_source_list;
2015-04-11 22:52:31 +02:00
for (const QJsonValue& v : json_results) {
QJsonObject r = v.toObject();
if (r.contains("recordings")) {
2015-04-11 22:52:31 +02:00
QJsonArray json_recordings = r["recordings"].toArray();
for (const QJsonValue& recording : json_recordings) {
QJsonObject o = recording.toObject();
if (o.contains("id")) {
id_source_list << IdSource(o["id"].toString(), o["sources"].toInt());
}
}
}
}
std::stable_sort(id_source_list.begin(), id_source_list.end());
QList<QString> id_list;
for (const IdSource& is : id_source_list) {
id_list << is.id_;
}
emit Finished(request_id, id_list);
}