Refactor Tidal, Spotify, Qobuz, Subsonic and cover providers

Use common HTTP, Json and OAuthenticator class
This commit is contained in:
Jonas Kvinge
2025-03-08 23:11:07 +01:00
parent 7de8a44709
commit cd516c37b9
81 changed files with 2429 additions and 3968 deletions

View File

@@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2024-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,11 +19,6 @@
#include "config.h"
#include <utility>
#include <QtGlobal>
#include <QObject>
#include <QList>
#include <QVariant>
#include <QByteArray>
#include <QString>
@@ -36,13 +31,13 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QTimer>
#include <QScopeGuard>
#include "includes/shared_ptr.h"
#include "core/networkaccessmanager.h"
#include "core/logging.h"
#include "core/settings.h"
#include "core/song.h"
#include "constants/timeconstants.h"
#include "core/oauthenticator.h"
#include "albumcoverfetcher.h"
#include "jsoncoverprovider.h"
#include "opentidalcoverprovider.h"
@@ -51,8 +46,8 @@ using namespace Qt::Literals::StringLiterals;
namespace {
constexpr char kSettingsGroup[] = "OpenTidal";
constexpr char kAuthUrl[] = "https://auth.tidal.com/v1/oauth2/token";
constexpr char kApiUrl[] = "https://openapi.tidal.com";
constexpr char kOAuthAccessTokenUrl[] = "https://auth.tidal.com/v1/oauth2/token";
constexpr char kApiUrl[] = "https://openapi.tidal.com/v2";
constexpr char kApiClientIdB64[] = "RHBwV3FpTEM4ZFJSV1RJaQ==";
constexpr char kApiClientSecretB64[] = "cGk0QmxpclZXQWlteWpBc0RnWmZ5RmVlRzA2b3E1blVBVTljUW1IdFhDST0=";
constexpr int kLimit = 10;
@@ -63,32 +58,24 @@ using std::make_shared;
OpenTidalCoverProvider::OpenTidalCoverProvider(const SharedPtr<NetworkAccessManager> network, QObject *parent)
: JsonCoverProvider(u"OpenTidal"_s, true, false, 2.5, true, false, network, parent),
login_timer_(new QTimer(this)),
oauth_(new OAuthenticator(network, this)),
timer_flush_requests_(new QTimer(this)),
login_in_progress_(false),
have_login_(false),
login_time_(0),
expires_in_(0) {
login_in_progress_(false) {
login_timer_->setSingleShot(true);
QObject::connect(login_timer_, &QTimer::timeout, this, &OpenTidalCoverProvider::Login);
oauth_->set_settings_group(QLatin1String(kSettingsGroup));
oauth_->set_type(OAuthenticator::Type::Client_Credentials);
oauth_->set_access_token_url(QUrl(QLatin1String(kOAuthAccessTokenUrl)));
oauth_->set_client_id(QString::fromLatin1(QByteArray::fromBase64(kApiClientIdB64)));
oauth_->set_client_secret(QString::fromLatin1(QByteArray::fromBase64(kApiClientSecretB64)));
oauth_->set_use_local_redirect_server(false);
oauth_->set_random_port(false);
QObject::connect(oauth_, &OAuthenticator::AuthenticationFinished, this, &OpenTidalCoverProvider::OAuthFinished);
timer_flush_requests_->setInterval(kRequestsDelay);
timer_flush_requests_->setSingleShot(false);
QObject::connect(timer_flush_requests_, &QTimer::timeout, this, &OpenTidalCoverProvider::FlushRequests);
LoadSession();
}
OpenTidalCoverProvider::~OpenTidalCoverProvider() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
QObject::disconnect(reply, nullptr, this, nullptr);
reply->abort();
reply->deleteLater();
}
oauth_->LoadSession();
}
@@ -96,7 +83,7 @@ bool OpenTidalCoverProvider::StartSearch(const QString &artist, const QString &a
if (artist.isEmpty() || album.isEmpty()) return false;
if (!have_login_ && !login_in_progress_ && QDateTime::currentSecsSinceEpoch() - last_login_attempt_.toSecsSinceEpoch() < 120) {
if (!oauth_->authenticated() && !login_in_progress_ && (last_login_attempt_.isValid() && (QDateTime::currentSecsSinceEpoch() - last_login_attempt_.toSecsSinceEpoch()) < 120)) {
return false;
}
@@ -115,32 +102,10 @@ void OpenTidalCoverProvider::CancelSearch(const int id) {
Q_UNUSED(id);
}
void OpenTidalCoverProvider::LoadSession() {
Settings s;
s.beginGroup(kSettingsGroup);
token_type_ = s.value("token_type").toString();
access_token_ = s.value("access_token").toString();
expires_in_ = s.value("expires_in", 0).toLongLong();
login_time_ = s.value("login_time", 0).toLongLong();
s.endGroup();
if (!token_type_.isEmpty() && !access_token_.isEmpty() && (login_time_ + expires_in_) > (QDateTime::currentSecsSinceEpoch() + 30)) {
have_login_ = true;
}
}
void OpenTidalCoverProvider::FlushRequests() {
if (have_login_ && (login_time_ + expires_in_) < QDateTime::currentSecsSinceEpoch()) {
have_login_ = false;
}
if (!have_login_) {
if (!login_in_progress_) {
Login();
}
if (!oauth_->authenticated()) {
LoginCheck();
return;
}
@@ -155,7 +120,7 @@ void OpenTidalCoverProvider::FlushRequests() {
void OpenTidalCoverProvider::LoginCheck() {
if (!login_in_progress_ && (!last_login_attempt_.isValid() || QDateTime::currentSecsSinceEpoch() - last_login_attempt_.toSecsSinceEpoch() > 120)) {
if (!oauth_->authenticated() && !login_in_progress_ && (!last_login_attempt_.isValid() || QDateTime::currentSecsSinceEpoch() - last_login_attempt_.toSecsSinceEpoch() > 120)) {
Login();
}
@@ -165,156 +130,93 @@ void OpenTidalCoverProvider::Login() {
qLog(Debug) << "Authenticating...";
if (login_timer_->isActive()) {
login_timer_->stop();
}
have_login_ = false;
login_in_progress_ = true;
last_login_attempt_ = QDateTime::currentDateTime();
QUrl url(QString::fromLatin1(kAuthUrl));
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
req.setRawHeader("Authorization", "Basic " + QByteArray(QByteArray::fromBase64(kApiClientIdB64) + ":" + QByteArray::fromBase64(kApiClientSecretB64)).toBase64());
QUrlQuery url_query;
url_query.addQueryItem(u"grant_type"_s, u"client_credentials"_s);
QNetworkReply *reply = network_->post(req, url_query.toString(QUrl::FullyEncoded).toUtf8());
replies_ << reply;
QObject::connect(reply, &QNetworkReply::sslErrors, this, &OpenTidalCoverProvider::HandleLoginSSLErrors);
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() { LoginFinished(reply); });
oauth_->Authenticate();
}
void OpenTidalCoverProvider::HandleLoginSSLErrors(const QList<QSslError> &ssl_errors) {
for (const QSslError &ssl_error : ssl_errors) {
qLog(Error) << "OpenTidal:" << ssl_error.errorString();
}
}
void OpenTidalCoverProvider::LoginFinished(QNetworkReply *reply) {
if (!replies_.contains(reply)) return;
replies_.removeAll(reply);
QObject::disconnect(reply, nullptr, this, nullptr);
reply->deleteLater();
void OpenTidalCoverProvider::OAuthFinished(const bool success, const QString &error) {
login_in_progress_ = false;
last_login_attempt_ = QDateTime();
QJsonObject json_obj = GetJsonObject(reply);
if (json_obj.isEmpty()) {
FinishAllSearches();
return;
if (success) {
qLog(Debug) << "OpenTidal: Authentication successful";
last_login_attempt_ = QDateTime();
if (!timer_flush_requests_->isActive()) {
timer_flush_requests_->start();
}
}
if (!json_obj.contains("access_token"_L1) ||
!json_obj.contains("token_type"_L1) ||
!json_obj.contains("expires_in"_L1) ||
!json_obj["access_token"_L1].isString() ||
!json_obj["token_type"_L1].isString()) {
qLog(Error) << "OpenTidal: Invalid login reply.";
FinishAllSearches();
return;
else {
qLog(Debug) << "OpenTidal: Authentication failed" << error;
last_login_attempt_ = QDateTime::currentDateTime();
}
have_login_ = true;
token_type_ = json_obj["token_type"_L1].toString();
access_token_ = json_obj["access_token"_L1].toString();
login_time_ = QDateTime::currentSecsSinceEpoch();
expires_in_ = json_obj["expires_in"_L1].toInt();
Settings s;
s.beginGroup(kSettingsGroup);
s.setValue("token_type", token_type_);
s.setValue("access_token", access_token_);
s.setValue("expires_in", expires_in_);
s.setValue("login_time", login_time_);
s.endGroup();
if (expires_in_ <= 300) {
expires_in_ = 300;
}
expires_in_ -= 30;
login_timer_->setInterval(static_cast<int>(expires_in_ * kMsecPerSec));
login_timer_->start();
if (!timer_flush_requests_->isActive()) {
timer_flush_requests_->start();
}
qLog(Debug) << "Authentication successful";
}
QJsonObject OpenTidalCoverProvider::ExtractJsonObj(const QByteArray &data) {
JsonBaseRequest::JsonObjectResult OpenTidalCoverProvider::ParseJsonObject(QNetworkReply *reply) {
QJsonParseError json_parse_error;
const QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_parse_error);
if (json_parse_error.error != QJsonParseError::NoError) {
qLog(Error) << "OpenTidal:" << json_parse_error.errorString();
return QJsonObject();
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
return ReplyDataResult(ErrorCode::NetworkError, QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
}
if (!json_doc.isObject()) {
return QJsonObject();
}
return json_doc.object();
}
QJsonObject OpenTidalCoverProvider::GetJsonObject(QNetworkReply *reply) {
const int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error() != QNetworkReply::NoError || (http_code != 200 && http_code != 207)) {
if (reply->error() != QNetworkReply::NoError) {
qLog(Error) << "OpenTidal:" << reply->errorString();
if (reply->error() < 200) {
return QJsonObject();
}
if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
LoginCheck();
}
}
else if (http_code != 200 && http_code != 207) {
qLog(Error) << "OpenTidal: Received HTTP code" << http_code;
}
const QByteArray data = reply->readAll();
if (data.isEmpty()) {
return QJsonObject();
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.contains("errors"_L1) && json_obj["errors"_L1].isArray()) {
const QJsonArray array = json_obj["errors"_L1].toArray();
for (const QJsonValue &value : array) {
if (!value.isObject()) continue;
QJsonObject obj = value.toObject();
if (!obj.contains("category"_L1) ||
!obj.contains("code"_L1) ||
!obj.contains("detail"_L1)) {
continue;
}
QString category = obj["category"_L1].toString();
QString code = obj["code"_L1].toString();
QString detail = obj["detail"_L1].toString();
qLog(Error) << "OpenTidal:" << category << code << detail;
if (category == "AUTHENTICATION_ERROR"_L1) {
LoginCheck();
}
}
}
return QJsonObject();
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();
if (data.isEmpty()) {
return QJsonObject();
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("errors"_L1) && json_object["errors"_L1].isArray()) {
const QJsonArray array_errors = json_object["errors"_L1].toArray();
for (const auto &value : array_errors) {
if (!value.isObject()) continue;
const QJsonObject object_error = value.toObject();
if (!object_error.contains("category"_L1) || !object_error.contains("code"_L1) || !object_error.contains("detail"_L1)) {
continue;
}
const QString category = object_error["category"_L1].toString();
const QString code = object_error["code"_L1].toString();
const QString detail = object_error["detail"_L1].toString();
result.error_code = ErrorCode::APIError;
result.error_message = QStringLiteral("%1 (%2) (%3)").arg(category, code, detail);
if (category == "AUTHENTICATION_ERROR"_L1) {
clear_session = true;
}
}
}
else {
result.json_object = json_document.object();
}
}
else {
result.error_code = ErrorCode::ParseError;
result.error_message = json_parse_error.errorString();
}
}
return ExtractJsonObj(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) {
oauth_->ClearSession();
}
return result;
}
@@ -331,17 +233,19 @@ void OpenTidalCoverProvider::SendSearchRequest(SearchRequestPtr search_request)
}
QUrlQuery url_query;
url_query.addQueryItem(u"query"_s, QString::fromUtf8(QUrl::toPercentEncoding(query)));
url_query.addQueryItem(u"include"_s, u"albums"_s);
url_query.addQueryItem(u"limit"_s, QString::number(kLimit));
url_query.addQueryItem(u"countryCode"_s, u"US"_s);
QUrl url(QLatin1String(kApiUrl) + "/search"_L1);
QUrl url(QLatin1String(kApiUrl) + "/searchresults/"_L1 + QString::fromUtf8(QUrl::toPercentEncoding(query)));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/vnd.tidal.v1+json"_s);
req.setRawHeader("Authorization", token_type_.toUtf8() + " " + access_token_.toUtf8());
QNetworkRequest network_request(url);
network_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
network_request.setHeader(QNetworkRequest::ContentTypeHeader, u"application/vnd.tidal.v1+json"_s);
if (oauth_->authenticated()) {
network_request.setRawHeader("Authorization", oauth_->authorization_header());
}
QNetworkReply *reply = network_->get(req);
QNetworkReply *reply = network_->get(network_request);
replies_ << reply;
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, search_request]() { HandleSearchReply(reply, search_request); });
@@ -354,92 +258,76 @@ void OpenTidalCoverProvider::HandleSearchReply(QNetworkReply *reply, SearchReque
QObject::disconnect(reply, nullptr, this, nullptr);
reply->deleteLater();
QJsonObject json_obj = GetJsonObject(reply);
if (json_obj.isEmpty()) {
CoverProviderSearchResults results;
const QScopeGuard search_finished = qScopeGuard([this, search_request, &results]() { Q_EMIT SearchFinished(search_request->id, results); });
const JsonObjectResult json_object_result = ParseJsonObject(reply);
if (!json_object_result.success()) {
Error(json_object_result.error_message);
if (login_in_progress_) {
search_requests_queue_.prepend(search_request);
}
else {
Q_EMIT SearchFinished(search_request->id, CoverProviderSearchResults());
}
return;
}
const QJsonObject &json_object = json_object_result.json_object;
if (!json_object.contains("included"_L1) || !json_object["included"_L1].isArray()) {
qLog(Error) << "OpenTidal: Json object is missing included.";
return;
}
if (!json_obj.contains("albums"_L1) || !json_obj["albums"_L1].isArray()) {
qLog(Debug) << "OpenTidal: Json object is missing albums.";
Q_EMIT SearchFinished(search_request->id, CoverProviderSearchResults());
const QJsonArray array_included = json_object["included"_L1].toArray();
if (array_included.isEmpty()) {
return;
}
const QJsonArray array_albums = json_obj["albums"_L1].toArray();
if (array_albums.isEmpty()) {
Q_EMIT SearchFinished(search_request->id, CoverProviderSearchResults());
return;
}
CoverProviderSearchResults results;
int i = 0;
for (const QJsonValue &value_album : array_albums) {
for (const auto &value_included : array_included) {
if (!value_album.isObject()) {
qLog(Debug) << "OpenTidal: Invalid Json reply: Albums array value is not a object.";
if (!value_included.isObject()) {
qLog(Error) << "OpenTidal: Invalid Json reply: Albums array value is not a object.";
continue;
}
QJsonObject obj_album = value_album.toObject();
const QJsonObject object_included = value_included.toObject();
if (!obj_album.contains("resource"_L1) || !obj_album["resource"_L1].isObject()) {
qLog(Debug) << "OpenTidal: Invalid Json reply: Albums array album is missing resource object.";
if (!object_included.contains("attributes"_L1) || !object_included["attributes"_L1].isObject()) {
qLog(Error) << "OpenTidal: Invalid Json reply: Included array item is missing attributes object." << object_included;
continue;
}
QJsonObject obj_resource = obj_album["resource"_L1].toObject();
const QJsonObject object_attributes = object_included["attributes"_L1].toObject();
if (!obj_resource.contains("artists"_L1) || !obj_resource["artists"_L1].isArray()) {
qLog(Debug) << "OpenTidal: Invalid Json reply: Resource is missing artists array.";
if (!object_attributes.contains("title"_L1) || !object_attributes["title"_L1].isString()) {
qLog(Error) << "OpenTidal: Invalid Json reply: Attributes is missing title string." << object_attributes;
continue;
}
if (!obj_resource.contains("title"_L1) || !obj_resource["title"_L1].isString()) {
qLog(Debug) << "OpenTidal: Invalid Json reply: Resource is missing title.";
if (!object_attributes.contains("imageLinks"_L1) || !object_attributes["imageLinks"_L1].isArray()) {
qLog(Error) << "OpenTidal: Invalid Json reply: Attributes is missing imageLinks object." << object_attributes;
continue;
}
if (!obj_resource.contains("imageCover"_L1) || !obj_resource["imageCover"_L1].isArray()) {
qLog(Debug) << "OpenTidal: Invalid Json reply: Resource is missing imageCover array.";
continue;
}
const QString album = object_attributes["title"_L1].toString();
const QJsonArray array_imagelinks = object_attributes["imageLinks"_L1].toArray();
QString artist;
const QString album = obj_resource["title"_L1].toString();
const QJsonArray array_artists = obj_resource["artists"_L1].toArray();
for (const QJsonValue &value_artist : array_artists) {
if (!value_artist.isObject()) {
for (const auto &value_imagelink : array_imagelinks) {
if (!value_imagelink.isObject()) {
continue;
}
QJsonObject obj_artist = value_artist.toObject();
if (!obj_artist.contains("name"_L1)) {
const QJsonObject object_imagelink = value_imagelink.toObject();
if (!object_imagelink.contains("href"_L1) || !object_imagelink.contains("meta"_L1) || !object_imagelink["meta"_L1].isObject()) {
continue;
}
artist = obj_artist["name"_L1].toString();
break;
}
const QJsonArray array_covers = obj_resource["imageCover"_L1].toArray();
for (const QJsonValue &value_cover : array_covers) {
if (!value_cover.isObject()) {
QJsonObject object_meta = object_imagelink["meta"_L1].toObject();
if (!object_meta.contains("width"_L1) || !object_meta.contains("height"_L1)) {
continue;
}
QJsonObject obj_cover = value_cover.toObject();
if (!obj_cover.contains("url"_L1) || !obj_cover.contains("width"_L1) || !obj_cover.contains("height"_L1)) {
continue;
}
const QUrl url(obj_cover["url"_L1].toString());
const int width = obj_cover["width"_L1].toInt();
const int height = obj_cover["height"_L1].toInt();
const QUrl url(object_imagelink["href"_L1].toString());
const int width = object_meta["width"_L1].toInt();
const int height = object_meta["height"_L1].toInt();
if (!url.isValid()) continue;
if (width < 640 || height < 640) continue;
CoverProviderSearchResult cover_result;
cover_result.artist = artist;
cover_result.artist = search_request->artist;
cover_result.album = Song::AlbumRemoveDiscMisc(album);
cover_result.image_url = url;
cover_result.image_size = QSize(width, height);
@@ -448,8 +336,6 @@ void OpenTidalCoverProvider::HandleSearchReply(QNetworkReply *reply, SearchReque
}
}
Q_EMIT SearchFinished(search_request->id, results);
}
void OpenTidalCoverProvider::FinishAllSearches() {
@@ -465,7 +351,7 @@ void OpenTidalCoverProvider::FinishAllSearches() {
void OpenTidalCoverProvider::Error(const QString &error, const QVariant &debug) {
qLog(Error) << "Tidal:" << error;
qLog(Error) << "OpenTidal:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}