2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* 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-10-19 20:18:46 +02:00
|
|
|
#include <algorithm>
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtGlobal>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QPair>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QList>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
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 <QtAlgorithms>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QJsonParseError>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QJsonArray>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QJsonValue>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include "acoustidclient.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2019-08-20 23:31:23 +02:00
|
|
|
#include "core/networktimeouts.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/timeconstants.h"
|
2019-07-07 21:14:24 +02:00
|
|
|
#include "core/logging.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 13:46:10 +02:00
|
|
|
const char *AcoustidClient::kClientId = "0qjUoxbowg";
|
2019-09-20 23:22:27 +02:00
|
|
|
const char *AcoustidClient::kUrl = "https://api.acoustid.org/v2/lookup";
|
2018-02-27 18:06:05 +01:00
|
|
|
const int AcoustidClient::kDefaultTimeout = 5000; // msec
|
|
|
|
|
|
|
|
AcoustidClient::AcoustidClient(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
network_(new NetworkAccessManager(this)),
|
|
|
|
timeouts_(new NetworkTimeouts(kDefaultTimeout, this)) {}
|
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
AcoustidClient::~AcoustidClient() {
|
|
|
|
|
|
|
|
CancelAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
void AcoustidClient::SetTimeout(const int msec) { timeouts_->SetTimeout(msec); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
void AcoustidClient::Start(const int id, const QString &fingerprint, int duration_msec) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2022-10-13 22:39:31 +02:00
|
|
|
using Param = QPair<QString, QString>;
|
|
|
|
using ParamList = QList<Param>;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
const ParamList params = ParamList() << Param("format", "json")
|
|
|
|
<< Param("client", kClientId)
|
|
|
|
<< Param("duration", QString::number(duration_msec / kMsecPerSec))
|
|
|
|
<< Param("meta", "recordingids+sources")
|
|
|
|
<< Param("fingerprint", fingerprint);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
2019-06-29 19:57:20 +02:00
|
|
|
url_query.setQueryItems(params);
|
|
|
|
QUrl url(kUrl);
|
2018-02-27 18:06:05 +01:00
|
|
|
url.setQuery(url_query);
|
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
QNetworkRequest req(url);
|
2020-08-14 20:20:41 +02:00
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
2018-02-27 18:06:05 +01:00
|
|
|
QNetworkReply *reply = network_->get(req);
|
2021-03-21 00:37:17 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id]() { RequestFinished(reply, id); });
|
2018-02-27 18:06:05 +01:00
|
|
|
requests_[id] = reply;
|
|
|
|
|
|
|
|
timeouts_->AddReply(reply);
|
2020-05-12 21:28:42 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
void AcoustidClient::Cancel(const int id) {
|
2020-05-12 21:28:42 +02:00
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
if (requests_.contains(id)) delete requests_.take(id);
|
2020-05-12 21:28:42 +02:00
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
void AcoustidClient::CancelAll() {
|
2019-06-29 19:57:20 +02:00
|
|
|
|
2021-03-21 04:47:11 +01:00
|
|
|
QList<QNetworkReply*> replies = requests_.values();
|
|
|
|
qDeleteAll(replies);
|
2018-02-27 18:06:05 +01:00
|
|
|
requests_.clear();
|
2019-06-29 19:57:20 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Struct used when extracting results in RequestFinished
|
|
|
|
struct IdSource {
|
2021-07-11 09:49:38 +02:00
|
|
|
IdSource(const QString &id, const int source) : id_(id), nb_sources_(source) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
bool operator<(const IdSource &other) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
// 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_;
|
|
|
|
};
|
2021-06-20 23:55:02 +02:00
|
|
|
|
|
|
|
} // namespace
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
void AcoustidClient::RequestFinished(QNetworkReply *reply, const int request_id) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
requests_.remove(request_id);
|
|
|
|
|
2019-06-29 19:57:20 +02:00
|
|
|
if (reply->error() != QNetworkReply::NoError || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
2019-07-07 21:14:24 +02:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
qLog(Error) << QString("Acoustid: %1 (%2)").arg(reply->errorString()).arg(reply->error());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Error) << QString("Acoustid: Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
emit Finished(request_id, QStringList());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument json_document = QJsonDocument::fromJson(reply->readAll(), &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
emit Finished(request_id, QStringList());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_object = json_document.object();
|
|
|
|
|
|
|
|
QString status = json_object["status"].toString();
|
|
|
|
if (status != "ok") {
|
2019-06-29 19:57:20 +02:00
|
|
|
emit Finished(request_id, QStringList(), status);
|
2018-02-27 18:06:05 +01:00
|
|
|
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
|
|
|
|
QJsonArray json_results = json_object["results"].toArray();
|
|
|
|
|
|
|
|
// List of <id, nb of sources> pairs
|
|
|
|
QList<IdSource> id_source_list;
|
|
|
|
|
2021-03-26 22:10:43 +01:00
|
|
|
for (const QJsonValueRef v : json_results) {
|
2018-02-27 18:06:05 +01:00
|
|
|
QJsonObject r = v.toObject();
|
|
|
|
if (!r["recordings"].isUndefined()) {
|
|
|
|
QJsonArray json_recordings = r["recordings"].toArray();
|
2021-03-26 22:10:43 +01:00
|
|
|
for (const QJsonValueRef recording : json_recordings) {
|
2018-02-27 18:06:05 +01:00
|
|
|
QJsonObject o = recording.toObject();
|
|
|
|
if (!o["id"].isUndefined()) {
|
|
|
|
id_source_list << IdSource(o["id"].toString(), o["sources"].toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:18:46 +02:00
|
|
|
std::stable_sort(id_source_list.begin(), id_source_list.end());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QList<QString> id_list;
|
2021-06-20 19:04:08 +02:00
|
|
|
id_list.reserve(id_source_list.count());
|
2021-06-12 20:53:23 +02:00
|
|
|
for (const IdSource &is : id_source_list) {
|
2018-02-27 18:06:05 +01:00
|
|
|
id_list << is.id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit Finished(request_id, id_list);
|
|
|
|
|
|
|
|
}
|
|
|
|
|