2011-03-12 22:19:41 +01:00
|
|
|
/* 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"
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonParseError>
|
2011-03-12 22:19:41 +01:00
|
|
|
#include <QNetworkReply>
|
2014-08-09 01:48:35 +02:00
|
|
|
#include <QStringList>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QUrlQuery>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <algorithm>
|
2012-01-06 17:31:29 +01:00
|
|
|
|
2012-10-12 14:25:04 +02:00
|
|
|
#include "core/closure.h"
|
2012-01-06 17:31:29 +01:00
|
|
|
#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";
|
2014-02-07 16:34:20 +01:00
|
|
|
const int AcoustidClient::kDefaultTimeout = 5000; // msec
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
AcoustidClient::AcoustidClient(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
network_(new NetworkAccessManager(this)),
|
|
|
|
timeouts_(new NetworkTimeouts(kDefaultTimeout, this)) {}
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::SetTimeout(int msec) { timeouts_->SetTimeout(msec); }
|
2011-03-13 14:17:35 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::Start(int id, const QString& fingerprint,
|
|
|
|
int duration_msec) {
|
2011-03-12 22:19:41 +01:00
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
|
|
|
|
QList<Param> parameters;
|
2014-02-07 16:34:20 +01:00
|
|
|
parameters << Param("format", "json") << Param("client", kClientId)
|
2012-01-06 17:31:29 +01:00
|
|
|
<< Param("duration", QString::number(duration_msec / kMsecPerSec))
|
2014-08-09 01:48:35 +02:00
|
|
|
<< Param("meta", "recordingids+sources")
|
2012-01-06 17:31:29 +01:00
|
|
|
<< Param("fingerprint", fingerprint);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
QUrl url(kUrl);
|
2015-04-11 22:52:31 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.setQueryItems(parameters);
|
|
|
|
url.setQuery(url_query);
|
2011-03-12 22:19:41 +01:00
|
|
|
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;
|
2011-03-13 14:17:35 +01:00
|
|
|
|
|
|
|
timeouts_->AddReply(reply);
|
2011-03-12 22:19:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::Cancel(int id) { delete requests_.take(id); }
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
void AcoustidClient::CancelAll() {
|
2012-10-12 14:25:04 +02:00
|
|
|
qDeleteAll(requests_.values());
|
2011-03-12 22:19:41 +01:00
|
|
|
requests_.clear();
|
|
|
|
}
|
|
|
|
|
2014-08-09 01:48:35 +02:00
|
|
|
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) {}
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
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
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
void AcoustidClient::RequestFinished(QNetworkReply* reply, int request_id) {
|
2011-03-12 22:19:41 +01:00
|
|
|
reply->deleteLater();
|
2014-08-09 01:48:35 +02:00
|
|
|
requests_.remove(request_id);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() !=
|
|
|
|
200) {
|
2014-08-09 01:48:35 +02:00
|
|
|
emit Finished(request_id, QStringList());
|
2011-03-12 22:19:41 +01:00
|
|
|
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) {
|
2014-08-09 01:48:35 +02:00
|
|
|
emit Finished(request_id, QStringList());
|
2012-01-06 17:31:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonObject json_object = json_document.object();
|
|
|
|
|
|
|
|
QString status = json_object["status"].toString();
|
2012-01-06 17:31:29 +01:00
|
|
|
if (status != "ok") {
|
2014-08-09 01:48:35 +02:00
|
|
|
emit Finished(request_id, QStringList());
|
2012-01-06 17:31:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
// 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();
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
// 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();
|
2020-02-13 04:52:03 +01:00
|
|
|
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();
|
2020-02-13 04:52:03 +01:00
|
|
|
if (o.contains("id")) {
|
2014-08-09 01:48:35 +02:00
|
|
|
id_source_list << IdSource(o["id"].toString(), o["sources"].toInt());
|
2012-01-06 17:31:29 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-12 22:19:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:19:05 +02:00
|
|
|
std::stable_sort(id_source_list.begin(), id_source_list.end());
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
QList<QString> id_list;
|
|
|
|
for (const IdSource& is : id_source_list) {
|
|
|
|
id_list << is.id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit Finished(request_id, id_list);
|
2011-03-12 22:19:41 +01:00
|
|
|
}
|