Refactor Tidal, Spotify, Qobuz, Subsonic and cover providers
Use common HTTP, Json and OAuthenticator class
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,20 +19,17 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QScopeGuard>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
@@ -47,20 +44,33 @@ using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
namespace {
|
||||
constexpr int kLimit = 10;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TidalCoverProvider::TidalCoverProvider(const TidalServicePtr service, const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: JsonCoverProvider(u"Tidal"_s, true, true, 2.5, true, true, network, parent),
|
||||
service_(service) {}
|
||||
|
||||
TidalCoverProvider::~TidalCoverProvider() {
|
||||
bool TidalCoverProvider::authenticated() const {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
return service_->authenticated();
|
||||
|
||||
}
|
||||
|
||||
bool TidalCoverProvider::use_authorization_header() const {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
QByteArray TidalCoverProvider::authorization_header() const {
|
||||
|
||||
return service_->authorization_header();
|
||||
|
||||
}
|
||||
|
||||
void TidalCoverProvider::ClearSession() {
|
||||
|
||||
service_->ClearSession();
|
||||
|
||||
}
|
||||
|
||||
@@ -89,20 +99,7 @@ bool TidalCoverProvider::StartSearch(const QString &artist, const QString &album
|
||||
<< Param(u"limit"_s, QString::number(kLimit))
|
||||
<< Param(u"countryCode"_s, service_->country_code());
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : params) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
QUrl url(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + resource);
|
||||
url.setQuery(url_query);
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (!service_->access_token().isEmpty()) req.setRawHeader("authorization", "Bearer " + service_->access_token().toUtf8());
|
||||
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
replies_ << reply;
|
||||
QNetworkReply *reply = CreateGetRequest(QUrl(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + resource), params);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id]() { HandleSearchReply(reply, id); });
|
||||
|
||||
return true;
|
||||
@@ -111,52 +108,62 @@ bool TidalCoverProvider::StartSearch(const QString &artist, const QString &album
|
||||
|
||||
void TidalCoverProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
||||
|
||||
QByteArray TidalCoverProvider::GetReplyData(QNetworkReply *reply) {
|
||||
JsonBaseRequest::JsonObjectResult TidalCoverProvider::ParseJsonObject(QNetworkReply *reply) {
|
||||
|
||||
QByteArray data;
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
|
||||
data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
||||
return ReplyDataResult(ErrorCode::NetworkError, QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
}
|
||||
else {
|
||||
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
||||
// This is a network error, there is nothing more to do.
|
||||
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
|
||||
JsonObjectResult result(ErrorCode::Success);
|
||||
result.network_error = reply->error();
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid()) {
|
||||
result.http_status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
}
|
||||
|
||||
const QByteArray data = reply->readAll();
|
||||
bool clear_session = false;
|
||||
if (!data.isEmpty()) {
|
||||
QJsonParseError json_parse_error;
|
||||
const QJsonDocument json_document = QJsonDocument::fromJson(data, &json_parse_error);
|
||||
if (json_parse_error.error == QJsonParseError::NoError) {
|
||||
const QJsonObject json_object = json_document.object();
|
||||
if (json_object.contains("status"_L1) && json_object.contains("subStatus"_L1) && json_object.contains("userMessage"_L1)) {
|
||||
const int status = json_object["status"_L1].toInt();
|
||||
const int sub_status = json_object["subStatus"_L1].toInt();
|
||||
const QString user_message = json_object["userMessage"_L1].toString();
|
||||
result.error_code = ErrorCode::APIError;
|
||||
result.api_error = status;
|
||||
result.error_message = QStringLiteral("%1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
||||
if (status == 401 && sub_status == 6001) {
|
||||
clear_session = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.json_object = json_document.object();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// See if there is Json data containing "status" and "userMessage" - then use that instead.
|
||||
data = reply->readAll();
|
||||
QJsonParseError parse_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &parse_error);
|
||||
int status = 0;
|
||||
int sub_status = 0;
|
||||
QString error;
|
||||
if (parse_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (!json_obj.isEmpty() && json_obj.contains("status"_L1) && json_obj.contains("userMessage"_L1)) {
|
||||
status = json_obj["status"_L1].toInt();
|
||||
sub_status = json_obj["subStatus"_L1].toInt();
|
||||
QString user_message = json_obj["userMessage"_L1].toString();
|
||||
error = QStringLiteral("%1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
||||
}
|
||||
}
|
||||
if (error.isEmpty()) {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
error = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
else {
|
||||
error = QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
||||
}
|
||||
}
|
||||
if (status == 401 && sub_status == 6001) { // User does not have a valid session
|
||||
service_->Logout();
|
||||
}
|
||||
Error(error);
|
||||
result.error_code = ErrorCode::ParseError;
|
||||
result.error_message = json_parse_error.errorString();
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
return data;
|
||||
if (result.error_code != ErrorCode::APIError) {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
result.error_code = ErrorCode::NetworkError;
|
||||
result.error_message = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
else if (result.http_status_code != 200) {
|
||||
result.error_code = ErrorCode::HttpError;
|
||||
result.error_message = QStringLiteral("Received HTTP code %1").arg(result.http_status_code);
|
||||
}
|
||||
}
|
||||
|
||||
if (reply->error() == QNetworkReply::AuthenticationRequiredError || clear_session) {
|
||||
service_->ClearSession();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@@ -167,36 +174,34 @@ void TidalCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id) {
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
if (data.isEmpty()) {
|
||||
Q_EMIT SearchFinished(id, CoverProviderSearchResults());
|
||||
CoverProviderSearchResults results;
|
||||
const QScopeGuard search_finished = qScopeGuard([this, id, &results]() { Q_EMIT SearchFinished(id, results); });
|
||||
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
Q_EMIT SearchFinished(id, CoverProviderSearchResults());
|
||||
const QJsonObject json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object is missing items."_s, json_obj);
|
||||
Q_EMIT SearchFinished(id, CoverProviderSearchResults());
|
||||
if (!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object is missing items."_s, json_object);
|
||||
return;
|
||||
}
|
||||
QJsonValue value_items = json_obj["items"_L1];
|
||||
const QJsonValue value_items = json_object["items"_L1];
|
||||
|
||||
if (!value_items.isArray()) {
|
||||
Q_EMIT SearchFinished(id, CoverProviderSearchResults());
|
||||
return;
|
||||
}
|
||||
const QJsonArray array_items = value_items.toArray();
|
||||
if (array_items.isEmpty()) {
|
||||
Q_EMIT SearchFinished(id, CoverProviderSearchResults());
|
||||
return;
|
||||
}
|
||||
|
||||
CoverProviderSearchResults results;
|
||||
int i = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
@@ -204,29 +209,29 @@ void TidalCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id) {
|
||||
Error(u"Invalid Json reply, items array item is not a object."_s);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_item = value_item.toObject();
|
||||
const QJsonObject object_item = value_item.toObject();
|
||||
|
||||
if (!obj_item.contains("artist"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item is missing artist."_s, obj_item);
|
||||
if (!object_item.contains("artist"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item is missing artist."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
QJsonValue value_artist = obj_item["artist"_L1];
|
||||
const QJsonValue value_artist = object_item["artist"_L1];
|
||||
if (!value_artist.isObject()) {
|
||||
Error(u"Invalid Json reply, items array item artist is not a object."_s, value_artist);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_artist = value_artist.toObject();
|
||||
if (!obj_artist.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item artist is missing name."_s, obj_artist);
|
||||
const QJsonObject object_artist = value_artist.toObject();
|
||||
if (!object_artist.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item artist is missing name."_s, object_artist);
|
||||
continue;
|
||||
}
|
||||
QString artist = obj_artist["name"_L1].toString();
|
||||
const QString artist = object_artist["name"_L1].toString();
|
||||
|
||||
QJsonObject obj_album;
|
||||
if (obj_item.contains("album"_L1)) {
|
||||
QJsonValue value_album = obj_item["album"_L1];
|
||||
QJsonObject object_album;
|
||||
if (object_item.contains("album"_L1)) {
|
||||
QJsonValue value_album = object_item["album"_L1];
|
||||
if (value_album.isObject()) {
|
||||
obj_album = value_album.toObject();
|
||||
object_album = value_album.toObject();
|
||||
}
|
||||
else {
|
||||
Error(u"Invalid Json reply, items array item album is not a object."_s, value_album);
|
||||
@@ -234,15 +239,15 @@ void TidalCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
obj_album = obj_item;
|
||||
object_album = object_item;
|
||||
}
|
||||
|
||||
if (!obj_album.contains("title"_L1) || !obj_album.contains("cover"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item album is missing title or cover."_s, obj_album);
|
||||
if (!object_album.contains("title"_L1) || !object_album.contains("cover"_L1)) {
|
||||
Error(u"Invalid Json reply, items array item album is missing title or cover."_s, object_album);
|
||||
continue;
|
||||
}
|
||||
QString album = obj_album["title"_L1].toString();
|
||||
QString cover = obj_album["cover"_L1].toString().replace("-"_L1, "/"_L1);
|
||||
const QString album = object_album["title"_L1].toString();
|
||||
const QString cover = object_album["cover"_L1].toString().replace("-"_L1, "/"_L1);
|
||||
|
||||
CoverProviderSearchResult cover_result;
|
||||
cover_result.artist = artist;
|
||||
@@ -260,7 +265,6 @@ void TidalCoverProvider::HandleSearchReply(QNetworkReply *reply, const int id) {
|
||||
}
|
||||
|
||||
}
|
||||
Q_EMIT SearchFinished(id, results);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user