strawberry-audio-player-win.../src/musicbrainz/acoustidclient.cpp

194 lines
6.0 KiB
C++
Raw Normal View History

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>
2020-02-09 02:29:35 +01:00
#include <QtGlobal>
#include <QObject>
#include <QPair>
2020-02-09 02:29:35 +01:00
#include <QList>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QStringList>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include <QUrlQuery>
#include <QtAlgorithms>
#include <QNetworkRequest>
#include <QNetworkReply>
2018-02-27 18:06:05 +01:00
#include <QJsonParseError>
#include <QJsonArray>
2018-02-27 18:06:05 +01:00
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/shared_ptr.h"
2021-01-11 16:48:46 +01:00
#include "core/networkaccessmanager.h"
#include "core/networktimeouts.h"
#include "utilities/timeconstants.h"
2018-02-27 18:06:05 +01:00
#include "acoustidclient.h"
namespace {
constexpr char kClientId[] = "0qjUoxbowg";
constexpr char kUrl[] = "https://api.acoustid.org/v2/lookup";
constexpr int kDefaultTimeout = 5000; // msec
} // namespace
2018-02-27 18:06:05 +01:00
AcoustidClient::AcoustidClient(SharedPtr<NetworkAccessManager> network, QObject *parent)
2018-02-27 18:06:05 +01:00
: QObject(parent),
network_(network),
2018-02-27 18:06:05 +01:00
timeouts_(new NetworkTimeouts(kDefaultTimeout, this)) {}
AcoustidClient::~AcoustidClient() {
CancelAll();
}
void AcoustidClient::SetTimeout(const int msec) { timeouts_->SetTimeout(msec); }
2018-02-27 18:06:05 +01: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
const ParamList params = ParamList() << Param(QStringLiteral("format"), QStringLiteral("json"))
<< Param(QStringLiteral("client"), QLatin1String(kClientId))
<< Param(QStringLiteral("duration"), QString::number(duration_msec / kMsecPerSec))
<< Param(QStringLiteral("meta"), QStringLiteral("recordingids+sources"))
<< Param(QStringLiteral("fingerprint"), fingerprint);
2018-02-27 18:06:05 +01:00
QUrlQuery url_query;
url_query.setQueryItems(params);
QUrl url(QString::fromLatin1(kUrl));
2018-02-27 18:06:05 +01:00
url.setQuery(url_query);
QNetworkRequest req(url);
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);
2018-02-27 18:06:05 +01:00
}
void AcoustidClient::Cancel(const int id) {
if (requests_.contains(id)) delete requests_.take(id);
}
2018-02-27 18:06:05 +01:00
void AcoustidClient::CancelAll() {
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();
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
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);
if (reply->error() != QNetworkReply::NoError || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
if (reply->error() != QNetworkReply::NoError) {
2024-04-09 23:20:26 +02:00
qLog(Error) << QStringLiteral("Acoustid: %1 (%2)").arg(reply->errorString()).arg(reply->error());
}
else {
2024-04-09 23:20:26 +02:00
qLog(Error) << QStringLiteral("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();
2024-04-09 23:20:26 +02:00
QString status = json_object[QStringLiteral("status")].toString();
if (status != QStringLiteral("ok")) {
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
2024-04-09 23:20:26 +02:00
QJsonArray json_results = json_object[QStringLiteral("results")].toArray();
2018-02-27 18:06:05 +01:00
// 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();
2024-04-09 23:20:26 +02:00
if (!r[QStringLiteral("recordings")].isUndefined()) {
QJsonArray json_recordings = r[QStringLiteral("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();
2024-04-09 23:20:26 +02:00
if (!o[QStringLiteral("id")].isUndefined()) {
id_source_list << IdSource(o[QStringLiteral("id")].toString(), o[QStringLiteral("sources")].toInt());
2018-02-27 18:06:05 +01:00
}
}
}
}
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
2024-04-23 16:51:42 +02:00
QStringList 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);
}