strawberry-audio-player-win.../src/tidal/tidalrequest.cpp

1408 lines
43 KiB
C++
Raw Permalink Normal View History

2020-04-13 19:04:06 +02:00
/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2020-04-13 19:04:06 +02: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/>.
*
*/
#include "config.h"
2024-08-12 18:12:26 +02:00
#include <utility>
2020-04-13 19:04:06 +02:00
#include <QObject>
#include <QList>
2020-04-13 19:04:06 +02:00
#include <QByteArray>
#include <QByteArrayList>
2020-04-13 19:04:06 +02:00
#include <QString>
#include <QUrl>
#include <QImage>
#include <QImageReader>
2020-04-13 19:04:06 +02:00
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
2022-10-31 06:12:02 +01:00
#include <QTimer>
2020-04-13 19:04:06 +02:00
2024-10-22 18:12:33 +02:00
#include "includes/shared_ptr.h"
2020-04-13 19:04:06 +02:00
#include "core/logging.h"
2021-01-11 16:48:46 +01:00
#include "core/networkaccessmanager.h"
2020-04-13 19:04:06 +02:00
#include "core/song.h"
2024-10-22 18:12:33 +02:00
#include "constants/timeconstants.h"
#include "utilities/imageutils.h"
#include "utilities/coverutils.h"
2020-04-13 19:04:06 +02:00
#include "tidalservice.h"
#include "tidalurlhandler.h"
#include "tidalbaserequest.h"
#include "tidalrequest.h"
2024-10-18 20:17:23 +02:00
using namespace Qt::Literals::StringLiterals;
namespace {
constexpr char kResourcesUrl[] = "https://resources.tidal.com";
constexpr int kMaxConcurrentArtistsRequests = 3;
constexpr int kMaxConcurrentAlbumsRequests = 3;
constexpr int kMaxConcurrentSongsRequests = 3;
constexpr int kMaxConcurrentArtistAlbumsRequests = 3;
constexpr int kMaxConcurrentAlbumSongsRequests = 3;
constexpr int kMaxConcurrentAlbumCoverRequests = 1;
constexpr int kFlushRequestsDelay = 200;
} // namespace
2020-04-13 19:04:06 +02:00
2024-10-22 18:12:33 +02:00
TidalRequest::TidalRequest(TidalService *service, TidalUrlHandler *url_handler, const SharedPtr<NetworkAccessManager> network, const Type query_type, QObject *parent)
2020-04-13 19:04:06 +02:00
: TidalBaseRequest(service, network, parent),
service_(service),
url_handler_(url_handler),
network_(network),
2022-10-31 06:12:02 +01:00
timer_flush_requests_(new QTimer(this)),
2023-02-18 14:09:27 +01:00
query_type_(query_type),
2020-04-13 19:04:06 +02:00
fetchalbums_(service->fetchalbums()),
2022-10-31 06:12:02 +01:00
coversize_(service->coversize()),
2020-04-13 19:04:06 +02:00
query_id_(-1),
finished_(false),
2022-10-31 06:12:02 +01:00
artists_requests_total_(0),
2020-04-13 19:04:06 +02:00
artists_requests_active_(0),
2022-10-31 06:12:02 +01:00
artists_requests_received_(0),
2020-04-13 19:04:06 +02:00
artists_total_(0),
artists_received_(0),
2022-10-31 06:12:02 +01:00
albums_requests_total_(0),
2020-04-13 19:04:06 +02:00
albums_requests_active_(0),
2022-10-31 06:12:02 +01:00
albums_requests_received_(0),
albums_total_(0),
albums_received_(0),
songs_requests_total_(0),
2020-04-13 19:04:06 +02:00
songs_requests_active_(0),
2022-10-31 06:12:02 +01:00
songs_requests_received_(0),
songs_total_(0),
songs_received_(0),
artist_albums_requests_total_(),
2020-04-13 19:04:06 +02:00
artist_albums_requests_active_(0),
2022-10-31 06:12:02 +01:00
artist_albums_requests_received_(0),
artist_albums_total_(0),
2020-04-13 19:04:06 +02:00
artist_albums_received_(0),
album_songs_requests_active_(0),
2022-10-31 06:12:02 +01:00
album_songs_requests_received_(0),
album_songs_requests_total_(0),
album_songs_total_(0),
2020-04-13 19:04:06 +02:00
album_songs_received_(0),
2022-10-31 06:12:02 +01:00
album_covers_requests_total_(0),
album_covers_requests_active_(0),
album_covers_requests_received_(0),
need_login_(false) {
timer_flush_requests_->setInterval(kFlushRequestsDelay);
timer_flush_requests_->setSingleShot(false);
QObject::connect(timer_flush_requests_, &QTimer::timeout, this, &TidalRequest::FlushRequests);
}
2020-04-13 19:04:06 +02:00
TidalRequest::~TidalRequest() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
if (reply->isRunning()) reply->abort();
reply->deleteLater();
}
while (!album_cover_replies_.isEmpty()) {
QNetworkReply *reply = album_cover_replies_.takeFirst();
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
if (reply->isRunning()) reply->abort();
reply->deleteLater();
}
}
2021-06-20 19:04:08 +02:00
void TidalRequest::LoginComplete(const bool success, const QString &error) {
2020-04-13 19:04:06 +02:00
if (!need_login_) return;
need_login_ = false;
if (!success) {
Error(error);
return;
}
Process();
}
void TidalRequest::Process() {
if (!service_->authenticated()) {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Authenticating..."));
2020-04-13 19:04:06 +02:00
need_login_ = true;
service_->TryLogin();
return;
}
2023-02-18 14:09:27 +01:00
switch (query_type_) {
2024-08-05 17:53:07 +02:00
case Type::FavouriteArtists:
2020-04-13 19:04:06 +02:00
GetArtists();
break;
2024-08-05 17:53:07 +02:00
case Type::FavouriteAlbums:
2020-04-13 19:04:06 +02:00
GetAlbums();
break;
2024-08-05 17:53:07 +02:00
case Type::FavouriteSongs:
2020-04-13 19:04:06 +02:00
GetSongs();
break;
2024-07-02 18:34:27 +02:00
case Type::SearchArtists:
2020-04-13 19:04:06 +02:00
ArtistsSearch();
break;
2024-07-02 18:34:27 +02:00
case Type::SearchAlbums:
2020-04-13 19:04:06 +02:00
AlbumsSearch();
break;
2024-07-02 18:34:27 +02:00
case Type::SearchSongs:
2020-04-13 19:04:06 +02:00
SongsSearch();
break;
default:
2024-10-20 06:38:55 +02:00
Error(u"Invalid query type."_s);
2020-04-13 19:04:06 +02:00
break;
}
}
2022-10-31 06:12:02 +01:00
void TidalRequest::StartRequests() {
if (!timer_flush_requests_->isActive()) {
timer_flush_requests_->start();
}
}
void TidalRequest::FlushRequests() {
if (!artists_requests_queue_.isEmpty()) {
FlushArtistsRequests();
return;
}
if (!albums_requests_queue_.isEmpty()) {
FlushAlbumsRequests();
return;
}
if (!artist_albums_requests_queue_.isEmpty()) {
FlushArtistAlbumsRequests();
return;
}
if (!album_songs_requests_queue_.isEmpty()) {
FlushAlbumSongsRequests();
return;
}
if (!songs_requests_queue_.isEmpty()) {
FlushSongsRequests();
return;
}
if (!album_cover_requests_queue_.isEmpty()) {
FlushAlbumCoverRequests();
return;
}
timer_flush_requests_->stop();
}
2020-04-13 19:04:06 +02:00
void TidalRequest::Search(const int query_id, const QString &search_text) {
query_id_ = query_id;
search_text_ = search_text;
}
void TidalRequest::GetArtists() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Receiving artists..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddArtistsRequest();
}
void TidalRequest::AddArtistsRequest(const int offset, const int limit) {
Request request;
request.limit = limit;
request.offset = offset;
artists_requests_queue_.enqueue(request);
2022-10-31 06:12:02 +01:00
++artists_requests_total_;
StartRequests();
2020-04-13 19:04:06 +02:00
}
void TidalRequest::FlushArtistsRequests() {
while (!artists_requests_queue_.isEmpty() && artists_requests_active_ < kMaxConcurrentArtistsRequests) {
Request request = artists_requests_queue_.dequeue();
ParamList parameters;
2024-10-20 06:38:55 +02:00
if (query_type_ == Type::SearchArtists) parameters << Param(u"query"_s, search_text_);
if (request.limit > 0) parameters << Param(u"limit"_s, QString::number(request.limit));
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
2022-10-31 06:12:02 +01:00
QNetworkReply *reply = nullptr;
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteArtists) {
2024-04-09 23:20:26 +02:00
reply = CreateRequest(QStringLiteral("users/%1/favorites/artists").arg(service_->user_id()), parameters);
2020-04-13 19:04:06 +02:00
}
2024-07-02 18:34:27 +02:00
if (query_type_ == Type::SearchArtists) {
2024-10-20 06:38:55 +02:00
reply = CreateRequest(u"search/artists"_s, parameters);
2020-04-13 19:04:06 +02:00
}
if (!reply) continue;
replies_ << reply;
2021-03-21 00:37:17 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { ArtistsReplyReceived(reply, request.limit, request.offset); });
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
++artists_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
void TidalRequest::GetAlbums() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Receiving albums..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddAlbumsRequest();
}
void TidalRequest::AddAlbumsRequest(const int offset, const int limit) {
Request request;
request.limit = limit;
request.offset = offset;
albums_requests_queue_.enqueue(request);
2022-10-31 06:12:02 +01:00
++albums_requests_total_;
StartRequests();
2020-04-13 19:04:06 +02:00
}
void TidalRequest::FlushAlbumsRequests() {
while (!albums_requests_queue_.isEmpty() && albums_requests_active_ < kMaxConcurrentAlbumsRequests) {
Request request = albums_requests_queue_.dequeue();
ParamList parameters;
2024-10-20 06:38:55 +02:00
if (query_type_ == Type::SearchAlbums) parameters << Param(u"query"_s, search_text_);
if (request.limit > 0) parameters << Param(u"limit"_s, QString::number(request.limit));
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
2022-10-31 06:12:02 +01:00
QNetworkReply *reply = nullptr;
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteAlbums) {
2024-04-09 23:20:26 +02:00
reply = CreateRequest(QStringLiteral("users/%1/favorites/albums").arg(service_->user_id()), parameters);
2020-04-13 19:04:06 +02:00
}
2024-07-02 18:34:27 +02:00
if (query_type_ == Type::SearchAlbums) {
2024-10-20 06:38:55 +02:00
reply = CreateRequest(u"search/albums"_s, parameters);
2020-04-13 19:04:06 +02:00
}
if (!reply) continue;
replies_ << reply;
2021-03-21 00:37:17 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumsReplyReceived(reply, request.limit, request.offset); });
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
++albums_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
void TidalRequest::GetSongs() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Receiving songs..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddSongsRequest();
}
void TidalRequest::AddSongsRequest(const int offset, const int limit) {
Request request;
request.limit = limit;
request.offset = offset;
songs_requests_queue_.enqueue(request);
2022-10-31 06:12:02 +01:00
++songs_requests_total_;
StartRequests();
2020-04-13 19:04:06 +02:00
}
void TidalRequest::FlushSongsRequests() {
while (!songs_requests_queue_.isEmpty() && songs_requests_active_ < kMaxConcurrentSongsRequests) {
Request request = songs_requests_queue_.dequeue();
ParamList parameters;
2024-10-20 06:38:55 +02:00
if (query_type_ == Type::SearchSongs) parameters << Param(u"query"_s, search_text_);
if (request.limit > 0) parameters << Param(u"limit"_s, QString::number(request.limit));
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
2022-10-31 06:12:02 +01:00
QNetworkReply *reply = nullptr;
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteSongs) {
2024-04-09 23:20:26 +02:00
reply = CreateRequest(QStringLiteral("users/%1/favorites/tracks").arg(service_->user_id()), parameters);
2020-04-13 19:04:06 +02:00
}
2024-07-02 18:34:27 +02:00
if (query_type_ == Type::SearchSongs) {
2024-10-20 06:38:55 +02:00
reply = CreateRequest(u"search/tracks"_s, parameters);
2020-04-13 19:04:06 +02:00
}
if (!reply) continue;
replies_ << reply;
2021-03-21 00:37:17 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { SongsReplyReceived(reply, request.limit, request.offset); });
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
++songs_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
void TidalRequest::ArtistsSearch() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Searching..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddArtistsSearchRequest();
}
void TidalRequest::AddArtistsSearchRequest(const int offset) {
AddArtistsRequest(offset, service_->artistssearchlimit());
}
void TidalRequest::AlbumsSearch() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Searching..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddAlbumsSearchRequest();
}
void TidalRequest::AddAlbumsSearchRequest(const int offset) {
AddAlbumsRequest(offset, service_->albumssearchlimit());
}
void TidalRequest::SongsSearch() {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateStatus(query_id_, tr("Searching..."));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
AddSongsSearchRequest();
}
void TidalRequest::AddSongsSearchRequest(const int offset) {
AddSongsRequest(offset, service_->songssearchlimit());
}
void TidalRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested) {
if (!replies_.contains(reply)) return;
replies_.removeAll(reply);
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
reply->deleteLater();
QByteArray data = GetReplyData(reply, (offset_requested == 0));
--artists_requests_active_;
2022-10-31 06:12:02 +01:00
++artists_requests_received_;
2020-04-13 19:04:06 +02:00
if (finished_) return;
if (data.isEmpty()) {
ArtistsFinishCheck();
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
ArtistsFinishCheck();
return;
}
if (!json_obj.contains("limit"_L1) ||
!json_obj.contains("offset"_L1) ||
!json_obj.contains("totalNumberOfItems"_L1) ||
!json_obj.contains("items"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Json object missing values."_s, json_obj);
2022-10-31 06:12:02 +01:00
ArtistsFinishCheck();
2020-04-13 19:04:06 +02:00
return;
}
//int limit = json_obj["limit"].toInt();
int offset = json_obj["offset"_L1].toInt();
int artists_total = json_obj["totalNumberOfItems"_L1].toInt();
2020-04-13 19:04:06 +02:00
if (offset_requested == 0) {
artists_total_ = artists_total;
}
else if (artists_total != artists_total_) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("totalNumberOfItems returned does not match previous totalNumberOfItems! %1 != %2").arg(artists_total).arg(artists_total_));
2020-04-13 19:04:06 +02:00
ArtistsFinishCheck();
return;
}
if (offset != offset_requested) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
2020-04-13 19:04:06 +02:00
ArtistsFinishCheck();
return;
}
if (offset_requested == 0) {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(artists_received_, artists_total_));
2020-04-13 19:04:06 +02:00
}
2020-04-23 21:05:57 +02:00
QJsonValue value_items = ExtractItems(json_obj);
if (!value_items.isArray()) {
2020-04-13 19:04:06 +02:00
ArtistsFinishCheck();
return;
}
2024-08-12 18:12:26 +02:00
const QJsonArray array_items = value_items.toArray();
2020-04-23 21:05:57 +02:00
if (array_items.isEmpty()) { // Empty array means no results
2020-04-13 19:04:06 +02:00
ArtistsFinishCheck();
return;
}
int artists_received = 0;
2024-08-12 18:12:26 +02:00
for (const QJsonValue &value_item : array_items) {
2020-04-13 19:04:06 +02:00
++artists_received;
2020-04-23 21:05:57 +02:00
if (!value_item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item in array is not a object."_s);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_item = value_item.toObject();
2020-04-13 19:04:06 +02:00
if (obj_item.contains("item"_L1)) {
QJsonValue json_item = obj_item["item"_L1];
2020-04-13 19:04:06 +02:00
if (!json_item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item in array is not a object."_s, json_item);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
obj_item = json_item.toObject();
2020-04-13 19:04:06 +02:00
}
if (!obj_item.contains("id"_L1) || !obj_item.contains("name"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item missing id or album."_s, obj_item);
2020-04-13 19:04:06 +02:00
continue;
}
2022-10-31 06:12:02 +01:00
Artist artist;
if (obj_item["id"_L1].isString()) {
artist.artist_id = obj_item["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
artist.artist_id = QString::number(obj_item["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
artist.artist = obj_item["name"_L1].toString();
2022-10-31 06:12:02 +01:00
if (artist_albums_requests_pending_.contains(artist.artist_id)) continue;
ArtistAlbumsRequest request;
request.artist = artist;
artist_albums_requests_pending_.insert(artist.artist_id, request);
2020-04-13 19:04:06 +02:00
}
artists_received_ += artists_received;
2024-08-25 01:06:30 +02:00
if (offset_requested != 0) Q_EMIT UpdateProgress(query_id_, GetProgress(artists_received_, artists_total_));
2020-04-13 19:04:06 +02:00
ArtistsFinishCheck(limit_requested, offset, artists_received);
}
void TidalRequest::ArtistsFinishCheck(const int limit, const int offset, const int artists_received) {
if (finished_) return;
if ((limit == 0 || limit > artists_received) && artists_received_ < artists_total_) {
int offset_next = offset + artists_received;
if (offset_next > 0 && offset_next < artists_total_) {
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteArtists) AddArtistsRequest(offset_next);
2024-07-02 18:34:27 +02:00
else if (query_type_ == Type::SearchArtists) AddArtistsSearchRequest(offset_next);
2020-04-13 19:04:06 +02:00
}
}
if (artists_requests_queue_.isEmpty() && artists_requests_active_ <= 0) { // Artist query is finished, get all albums for all artists.
// Get artist albums
2024-08-12 18:12:26 +02:00
const QList<ArtistAlbumsRequest> requests = artist_albums_requests_pending_.values();
2022-10-31 06:12:02 +01:00
for (const ArtistAlbumsRequest &request : requests) {
AddArtistAlbumsRequest(request.artist);
2020-04-13 19:04:06 +02:00
}
artist_albums_requests_pending_.clear();
2022-10-31 06:12:02 +01:00
if (artist_albums_requests_total_ > 0) {
2024-08-25 01:06:30 +02:00
if (artist_albums_requests_total_ == 1) Q_EMIT UpdateStatus(query_id_, tr("Receiving albums for %1 artist...").arg(artist_albums_requests_total_));
else Q_EMIT UpdateStatus(query_id_, tr("Receiving albums for %1 artists...").arg(artist_albums_requests_total_));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
}
}
FinishCheck();
}
void TidalRequest::AlbumsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested) {
2022-10-31 06:12:02 +01:00
2020-04-13 19:04:06 +02:00
--albums_requests_active_;
2022-10-31 06:12:02 +01:00
++albums_requests_received_;
AlbumsReceived(reply, Artist(), limit_requested, offset_requested, offset_requested == 0);
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
void TidalRequest::AddArtistAlbumsRequest(const Artist &artist, const int offset) {
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
ArtistAlbumsRequest request;
request.artist = artist;
2020-04-13 19:04:06 +02:00
request.offset = offset;
artist_albums_requests_queue_.enqueue(request);
2022-10-31 06:12:02 +01:00
++artist_albums_requests_total_;
StartRequests();
2020-04-13 19:04:06 +02:00
}
void TidalRequest::FlushArtistAlbumsRequests() {
while (!artist_albums_requests_queue_.isEmpty() && artist_albums_requests_active_ < kMaxConcurrentArtistAlbumsRequests) {
2022-10-31 06:12:02 +01:00
const ArtistAlbumsRequest request = artist_albums_requests_queue_.dequeue();
2020-04-13 19:04:06 +02:00
ParamList parameters;
2024-10-20 06:38:55 +02:00
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
2024-04-09 23:20:26 +02:00
QNetworkReply *reply = CreateRequest(QStringLiteral("artists/%1/albums").arg(request.artist.artist_id), parameters);
2022-10-31 06:12:02 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { ArtistAlbumsReplyReceived(reply, request.artist, request.offset); });
2020-04-13 19:04:06 +02:00
replies_ << reply;
2022-10-31 06:12:02 +01:00
++artist_albums_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
2022-10-31 06:12:02 +01:00
void TidalRequest::ArtistAlbumsReplyReceived(QNetworkReply *reply, const Artist &artist, const int offset_requested) {
2020-04-13 19:04:06 +02:00
--artist_albums_requests_active_;
2022-10-31 06:12:02 +01:00
++artist_albums_requests_received_;
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(artist_albums_requests_received_, artist_albums_requests_total_));
2022-10-31 06:12:02 +01:00
AlbumsReceived(reply, artist, 0, offset_requested, false);
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
void TidalRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_requested, const int limit_requested, const int offset_requested, const bool auto_login) {
2020-04-13 19:04:06 +02:00
if (!replies_.contains(reply)) return;
replies_.removeAll(reply);
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
reply->deleteLater();
QByteArray data = GetReplyData(reply, auto_login);
if (finished_) return;
if (data.isEmpty()) {
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
if (!json_obj.contains("limit"_L1) ||
!json_obj.contains("offset"_L1) ||
!json_obj.contains("totalNumberOfItems"_L1) ||
!json_obj.contains("items"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Json object missing values."_s, json_obj);
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
//int limit = json_obj["limit"].toInt();
int offset = json_obj["offset"_L1].toInt();
int albums_total = json_obj["totalNumberOfItems"_L1].toInt();
2020-04-13 19:04:06 +02:00
if (offset != offset_requested) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
2020-04-23 21:05:57 +02:00
QJsonValue value_items = ExtractItems(json_obj);
if (!value_items.isArray()) {
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
2024-08-12 18:12:26 +02:00
const QJsonArray array_items = value_items.toArray();
2020-04-23 21:05:57 +02:00
if (array_items.isEmpty()) {
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested);
2020-04-13 19:04:06 +02:00
return;
}
int albums_received = 0;
2024-08-12 18:12:26 +02:00
for (const QJsonValue &value_item : array_items) {
2020-04-13 19:04:06 +02:00
++albums_received;
2020-04-23 21:05:57 +02:00
if (!value_item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item in array is not a object."_s);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_item = value_item.toObject();
2020-04-13 19:04:06 +02:00
if (obj_item.contains("item"_L1)) {
QJsonValue json_item = obj_item["item"_L1];
2020-04-13 19:04:06 +02:00
if (!json_item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item in array is not a object."_s, json_item);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
obj_item = json_item.toObject();
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
Album album;
if (obj_item.contains("type"_L1)) { // This was an albums request or search
if (!obj_item.contains("id"_L1) || !obj_item.contains("title"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item is missing ID or title."_s, obj_item);
2020-04-13 19:04:06 +02:00
continue;
}
if (obj_item["id"_L1].isString()) {
album.album_id = obj_item["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
album.album_id = QString::number(obj_item["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
album.album = obj_item["title"_L1].toString();
if (service_->album_explicit() && obj_item.contains("explicit"_L1)) {
album.album_explicit = obj_item["explicit"_L1].toVariant().toBool();
2022-10-31 06:12:02 +01:00
if (album.album_explicit && !album.album.isEmpty()) {
album.album.append(" (Explicit)"_L1);
}
}
2020-04-13 19:04:06 +02:00
}
else if (obj_item.contains("album"_L1)) { // This was a tracks request or search
QJsonValue value_album = obj_item["album"_L1];
2020-04-23 21:05:57 +02:00
if (!value_album.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item album is not a object."_s, value_album);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_album = value_album.toObject();
if (!obj_album.contains("id"_L1) || !obj_album.contains("title"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item album is missing ID or title."_s, obj_album);
2020-04-13 19:04:06 +02:00
continue;
}
if (obj_album["id"_L1].isString()) {
album.album_id = obj_album["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
album.album_id = QString::number(obj_album["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
album.album = obj_album["title"_L1].toString();
if (service_->album_explicit() && obj_album.contains("explicit"_L1)) {
album.album_explicit = obj_album["explicit"_L1].toVariant().toBool();
2022-10-31 06:12:02 +01:00
if (album.album_explicit && !album.album.isEmpty()) {
album.album.append(" (Explicit)"_L1);
}
}
2020-04-13 19:04:06 +02:00
}
else {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item missing type or album."_s, obj_item);
2020-04-13 19:04:06 +02:00
continue;
}
2022-10-31 06:12:02 +01:00
if (album_songs_requests_pending_.contains(album.album_id)) continue;
2020-04-13 19:04:06 +02:00
if (!obj_item.contains("artist"_L1) || !obj_item.contains("title"_L1) || !obj_item.contains("audioQuality"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item missing artist, title or audioQuality."_s, obj_item);
2020-04-13 19:04:06 +02:00
continue;
}
QJsonValue value_artist = obj_item["artist"_L1];
2020-04-23 21:05:57 +02:00
if (!value_artist.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item artist is not a object."_s, value_artist);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_artist = value_artist.toObject();
if (!obj_artist.contains("id"_L1) || !obj_artist.contains("name"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item artist missing id or name."_s, obj_artist);
2020-04-13 19:04:06 +02:00
continue;
}
2022-10-31 06:12:02 +01:00
Artist album_artist;
if (obj_artist["id"_L1].isString()) {
album_artist.artist_id = obj_artist["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
album_artist.artist_id = QString::number(obj_artist["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
album_artist.artist = obj_artist["name"_L1].toString();
2020-04-13 19:04:06 +02:00
2021-03-21 04:43:55 +01:00
//QString quality = obj_item["audioQuality"].toString();
//QString copyright = obj_item["copyright"].toString();
2020-04-13 19:04:06 +02:00
//qLog(Debug) << "Tidal:" << artist << album << quality << copyright;
2022-10-31 06:12:02 +01:00
AlbumSongsRequest request;
request.artist = album_artist;
request.album = album;
2022-10-31 06:12:02 +01:00
album_songs_requests_pending_.insert(album.album_id, request);
}
2020-04-13 19:04:06 +02:00
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteAlbums || query_type_ == Type::SearchAlbums) {
2022-10-31 06:12:02 +01:00
albums_received_ += albums_received;
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(albums_received_, albums_total_));
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
AlbumsFinishCheck(artist_requested, limit_requested, offset, albums_total, albums_received);
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
void TidalRequest::AlbumsFinishCheck(const Artist &artist, const int limit, const int offset, const int albums_total, const int albums_received) {
2020-04-13 19:04:06 +02:00
if (finished_) return;
if (limit == 0 || limit > albums_received) {
int offset_next = offset + albums_received;
if (offset_next > 0 && offset_next < albums_total) {
2023-02-18 14:09:27 +01:00
switch (query_type_) {
2024-08-05 17:53:07 +02:00
case Type::FavouriteAlbums:
2020-04-13 19:04:06 +02:00
AddAlbumsRequest(offset_next);
break;
2024-07-02 18:34:27 +02:00
case Type::SearchAlbums:
2020-04-13 19:04:06 +02:00
AddAlbumsSearchRequest(offset_next);
break;
2024-08-05 17:53:07 +02:00
case Type::FavouriteArtists:
2024-07-02 18:34:27 +02:00
case Type::SearchArtists:
2022-10-31 06:12:02 +01:00
AddArtistAlbumsRequest(artist, offset_next);
2020-04-13 19:04:06 +02:00
break;
default:
break;
}
}
}
if (
2022-10-31 06:12:02 +01:00
artists_requests_queue_.isEmpty() &&
artists_requests_active_ <= 0 &&
2020-04-13 19:04:06 +02:00
albums_requests_queue_.isEmpty() &&
albums_requests_active_ <= 0 &&
artist_albums_requests_queue_.isEmpty() &&
artist_albums_requests_active_ <= 0
) { // Artist albums query is finished, get all songs for all albums.
// Get songs for all the albums.
2024-08-23 20:30:59 +02:00
for (QHash<QString, AlbumSongsRequest>::const_iterator it = album_songs_requests_pending_.constBegin(); it != album_songs_requests_pending_.constEnd(); ++it) {
2022-10-31 06:12:02 +01:00
const AlbumSongsRequest &request = it.value();
AddAlbumSongsRequest(request.artist, request.album);
2020-04-13 19:04:06 +02:00
}
album_songs_requests_pending_.clear();
2022-10-31 06:12:02 +01:00
if (album_songs_requests_total_ > 0) {
2024-08-25 01:06:30 +02:00
if (album_songs_requests_total_ == 1) Q_EMIT UpdateStatus(query_id_, tr("Receiving songs for %1 album...").arg(album_songs_requests_total_));
else Q_EMIT UpdateStatus(query_id_, tr("Receiving songs for %1 albums...").arg(album_songs_requests_total_));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
}
}
2022-10-31 06:12:02 +01:00
GetAlbumCoversCheck();
2020-04-13 19:04:06 +02:00
FinishCheck();
}
void TidalRequest::SongsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested) {
--songs_requests_active_;
2022-10-31 06:12:02 +01:00
++songs_requests_received_;
2024-07-02 18:34:27 +02:00
if (query_type_ == Type::SearchSongs && fetchalbums_) {
2022-10-31 06:12:02 +01:00
AlbumsReceived(reply, Artist(), limit_requested, offset_requested, offset_requested == 0);
2020-04-13 19:04:06 +02:00
}
else {
2022-10-31 06:12:02 +01:00
SongsReceived(reply, Artist(), Album(), limit_requested, offset_requested, offset_requested == 0);
2020-04-13 19:04:06 +02:00
}
}
2022-10-31 06:12:02 +01:00
void TidalRequest::AddAlbumSongsRequest(const Artist &artist, const Album &album, const int offset) {
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
AlbumSongsRequest request;
request.artist = artist;
request.album = album;
2020-04-13 19:04:06 +02:00
request.offset = offset;
album_songs_requests_queue_.enqueue(request);
2022-10-31 06:12:02 +01:00
++album_songs_requests_total_;
StartRequests();
2020-04-13 19:04:06 +02:00
}
void TidalRequest::FlushAlbumSongsRequests() {
while (!album_songs_requests_queue_.isEmpty() && album_songs_requests_active_ < kMaxConcurrentAlbumSongsRequests) {
2022-10-31 06:12:02 +01:00
AlbumSongsRequest request = album_songs_requests_queue_.dequeue();
2020-04-13 19:04:06 +02:00
ParamList parameters;
2024-10-20 06:38:55 +02:00
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
2024-04-09 23:20:26 +02:00
QNetworkReply *reply = CreateRequest(QStringLiteral("albums/%1/tracks").arg(request.album.album_id), parameters);
2020-04-13 19:04:06 +02:00
replies_ << reply;
2022-10-31 06:12:02 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumSongsReplyReceived(reply, request.artist, request.album, request.offset); });
++album_songs_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
2022-10-31 06:12:02 +01:00
void TidalRequest::AlbumSongsReplyReceived(QNetworkReply *reply, const Artist &artist, const Album &album, const int offset_requested) {
2020-04-13 19:04:06 +02:00
--album_songs_requests_active_;
2022-10-31 06:12:02 +01:00
++album_songs_requests_received_;
2020-04-13 19:04:06 +02:00
if (offset_requested == 0) {
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(album_songs_requests_received_, album_songs_requests_total_));
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
SongsReceived(reply, artist, album, 0, offset_requested, false);
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
void TidalRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, const Album &album, const int limit_requested, const int offset_requested, const bool auto_login) {
2020-04-13 19:04:06 +02:00
if (!replies_.contains(reply)) return;
replies_.removeAll(reply);
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
reply->deleteLater();
QByteArray data = GetReplyData(reply, auto_login);
if (finished_) return;
if (data.isEmpty()) {
2022-10-31 06:12:02 +01:00
SongsFinishCheck(artist, album, limit_requested, offset_requested);
2020-04-13 19:04:06 +02:00
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
2022-10-31 06:12:02 +01:00
SongsFinishCheck(artist, album, limit_requested, offset_requested);
2020-04-13 19:04:06 +02:00
return;
}
if (!json_obj.contains("limit"_L1) ||
!json_obj.contains("offset"_L1) ||
!json_obj.contains("totalNumberOfItems"_L1) ||
!json_obj.contains("items"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Json object missing values."_s, json_obj);
2022-10-31 06:12:02 +01:00
SongsFinishCheck(artist, album, limit_requested, offset_requested);
2020-04-13 19:04:06 +02:00
return;
}
//int limit = json_obj["limit"].toInt();
int offset = json_obj["offset"_L1].toInt();
int songs_total = json_obj["totalNumberOfItems"_L1].toInt();
2020-04-13 19:04:06 +02:00
if (offset != offset_requested) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
2022-10-31 06:12:02 +01:00
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
2020-04-13 19:04:06 +02:00
return;
}
QJsonValue json_value = ExtractItems(json_obj);
if (!json_value.isArray()) {
2022-10-31 06:12:02 +01:00
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
2020-04-13 19:04:06 +02:00
return;
}
2024-08-12 18:12:26 +02:00
const QJsonArray array_items = json_value.toArray();
2022-10-31 06:12:02 +01:00
if (array_items.isEmpty()) {
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
2020-04-13 19:04:06 +02:00
return;
}
bool compilation = false;
bool multidisc = false;
SongList songs;
int songs_received = 0;
2024-08-12 18:12:26 +02:00
for (const QJsonValue &value_item : array_items) {
2020-04-13 19:04:06 +02:00
2020-04-23 21:05:57 +02:00
if (!value_item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track is not a object."_s);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_item = value_item.toObject();
2020-04-13 19:04:06 +02:00
if (obj_item.contains("item"_L1)) {
QJsonValue item = obj_item["item"_L1];
2020-04-23 21:05:57 +02:00
if (!item.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, item is not a object."_s, item);
2020-04-13 19:04:06 +02:00
continue;
}
2020-04-23 21:05:57 +02:00
obj_item = item.toObject();
2020-04-13 19:04:06 +02:00
}
++songs_received;
2023-02-18 14:09:27 +01:00
Song song(Song::Source::Tidal);
2022-10-31 06:12:02 +01:00
ParseSong(song, obj_item, artist, album);
2020-04-13 19:04:06 +02:00
if (!song.is_valid()) continue;
if (song.disc() >= 2) multidisc = true;
if (song.is_compilation()) compilation = true;
songs << song;
}
2024-08-12 18:12:26 +02:00
for (Song song : std::as_const(songs)) {
2020-04-13 19:04:06 +02:00
if (compilation) song.set_compilation_detected(true);
if (!multidisc) song.set_disc(0);
songs_.insert(song.song_id(), song);
2020-04-13 19:04:06 +02:00
}
2024-08-05 17:53:07 +02:00
if (query_type_ == Type::FavouriteSongs || query_type_ == Type::SearchSongs) {
2022-10-31 06:12:02 +01:00
songs_received_ += songs_received;
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(songs_received_, songs_total_));
2022-10-31 06:12:02 +01:00
}
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, songs_received);
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
void TidalRequest::SongsFinishCheck(const Artist &artist, const Album &album, const int limit, const int offset, const int songs_total, const int songs_received) {
2020-04-13 19:04:06 +02:00
if (finished_) return;
if (limit == 0 || limit > songs_received) {
int offset_next = offset + songs_received;
if (offset_next > 0 && offset_next < songs_total) {
2023-02-18 14:09:27 +01:00
switch (query_type_) {
2024-08-05 17:53:07 +02:00
case Type::FavouriteSongs:
2020-04-13 19:04:06 +02:00
AddSongsRequest(offset_next);
break;
2024-07-02 18:34:27 +02:00
case Type::SearchSongs:
2020-04-13 19:04:06 +02:00
// If artist_id and album_id isn't zero it means that it's a songs search where we fetch all albums too. So fallthrough.
2022-10-31 06:12:02 +01:00
if (artist.artist_id.isEmpty() && album.album_id.isEmpty()) {
2020-04-13 19:04:06 +02:00
AddSongsSearchRequest(offset_next);
break;
}
2022-07-26 20:37:06 +02:00
[[fallthrough]];
2024-08-05 17:53:07 +02:00
case Type::FavouriteArtists:
2024-07-02 18:34:27 +02:00
case Type::SearchArtists:
2024-08-05 17:53:07 +02:00
case Type::FavouriteAlbums:
2024-07-02 18:34:27 +02:00
case Type::SearchAlbums:
2022-10-31 06:12:02 +01:00
AddAlbumSongsRequest(artist, album, offset_next);
2020-04-13 19:04:06 +02:00
break;
default:
break;
}
}
}
2022-10-31 06:12:02 +01:00
GetAlbumCoversCheck();
2020-04-13 19:04:06 +02:00
FinishCheck();
}
2022-10-31 06:12:02 +01:00
void TidalRequest::ParseSong(Song &song, const QJsonObject &json_obj, const Artist &album_artist, const Album &album) {
2020-04-13 19:04:06 +02:00
if (
!json_obj.contains("album"_L1) ||
!json_obj.contains("allowStreaming"_L1) ||
!json_obj.contains("artist"_L1) ||
!json_obj.contains("artists"_L1) ||
!json_obj.contains("audioQuality"_L1) ||
!json_obj.contains("duration"_L1) ||
!json_obj.contains("id"_L1) ||
!json_obj.contains("streamReady"_L1) ||
!json_obj.contains("title"_L1) ||
!json_obj.contains("trackNumber"_L1) ||
!json_obj.contains("url"_L1) ||
!json_obj.contains("volumeNumber"_L1) ||
!json_obj.contains("copyright"_L1)
2020-04-13 19:04:06 +02:00
) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track is missing one or more values."_s, json_obj);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
QJsonValue value_artist = json_obj["artist"_L1];
QJsonValue value_album = json_obj["album"_L1];
QJsonValue json_duration = json_obj["duration"_L1];
2021-03-21 04:43:55 +01:00
//QJsonArray array_artists = json_obj["artists"].toArray();
2020-04-13 19:04:06 +02:00
QString song_id;
if (json_obj["id"_L1].isString()) {
song_id = json_obj["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
song_id = QString::number(json_obj["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
QString title = json_obj["title"_L1].toString();
2021-03-21 04:43:55 +01:00
//QString urlstr = json_obj["url"].toString();
int track = json_obj["trackNumber"_L1].toInt();
int disc = json_obj["volumeNumber"_L1].toInt();
bool allow_streaming = json_obj["allowStreaming"_L1].toBool();
bool stream_ready = json_obj["streamReady"_L1].toBool();
QString copyright = json_obj["copyright"_L1].toString();
2020-04-13 19:04:06 +02:00
2020-04-23 21:05:57 +02:00
if (!value_artist.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track artist is not a object."_s, value_artist);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_artist = value_artist.toObject();
if (!obj_artist.contains("id"_L1) || !obj_artist.contains("name"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track artist is missing id or name."_s, obj_artist);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
QString artist_id;
if (obj_artist["id"_L1].isString()) {
artist_id = obj_artist["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
artist_id = QString::number(obj_artist["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
QString artist = obj_artist["name"_L1].toString();
2020-04-13 19:04:06 +02:00
2020-04-23 21:05:57 +02:00
if (!value_album.isObject()) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track album is not a object."_s, value_album);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
2020-04-23 21:05:57 +02:00
QJsonObject obj_album = value_album.toObject();
if (!obj_album.contains("id"_L1) || !obj_album.contains("title"_L1)) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track album is missing ID or title."_s, obj_album);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
QString album_id;
if (obj_album["id"_L1].isString()) {
album_id = obj_album["id"_L1].toString();
2020-04-13 19:04:06 +02:00
}
else {
album_id = QString::number(obj_album["id"_L1].toInt());
2020-04-13 19:04:06 +02:00
}
2022-10-31 06:12:02 +01:00
if (!album.album_id.isEmpty() && album.album_id != album_id) {
2024-10-20 06:38:55 +02:00
Error(u"Invalid Json reply, track album id is wrong."_s, obj_album);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
QString album_title = obj_album["title"_L1].toString();
if (album.album_explicit) album_title.append(" (Explicit)"_L1);
2020-04-13 19:04:06 +02:00
if (!allow_streaming) {
2024-04-09 23:20:26 +02:00
Warn(QStringLiteral("Song %1 %2 %3 is not allowStreaming").arg(artist, album_title, title));
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
if (!stream_ready) {
2024-04-09 23:20:26 +02:00
Warn(QStringLiteral("Song %1 %2 %3 is not streamReady").arg(artist, album_title, title));
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
QUrl url;
url.setScheme(url_handler_->scheme());
url.setPath(song_id);
QVariant q_duration = json_duration.toVariant();
2021-10-30 02:21:29 +02:00
qint64 duration = 0;
if (q_duration.isValid()) {
2020-04-13 19:04:06 +02:00
duration = q_duration.toLongLong() * kNsecPerSec;
}
else {
2024-10-20 06:38:55 +02:00
Error(u"Invalid duration for song."_s, json_duration);
2022-10-31 06:12:02 +01:00
return;
2020-04-13 19:04:06 +02:00
}
2023-01-20 23:35:47 +01:00
QUrl cover_url;
if (obj_album.contains("cover"_L1)) {
const QString cover = obj_album["cover"_L1].toString().replace(u'-', u'/');
2023-01-20 23:35:47 +01:00
if (!cover.isEmpty()) {
cover_url.setUrl(QStringLiteral("%1/images/%2/%3.jpg").arg(QLatin1String(kResourcesUrl), cover, coversize_));
2023-01-20 23:35:47 +01:00
}
}
2020-04-13 19:04:06 +02:00
title = Song::TitleRemoveMisc(title);
2020-04-13 19:04:06 +02:00
//qLog(Debug) << "id" << song_id << "track" << track << "disc" << disc << "title" << title << "album" << album << "album artist" << album_artist << "artist" << artist << cover << allow_streaming << url;
2023-02-18 14:09:27 +01:00
song.set_source(Song::Source::Tidal);
2020-04-13 19:04:06 +02:00
song.set_song_id(song_id);
song.set_album_id(album_id);
song.set_artist_id(artist_id);
2022-10-31 06:12:02 +01:00
if (album_artist.artist != artist) song.set_albumartist(album_artist.artist);
song.set_album(album_title);
2020-04-13 19:04:06 +02:00
song.set_artist(artist);
song.set_title(title);
song.set_track(track);
song.set_disc(disc);
song.set_url(url);
song.set_length_nanosec(duration);
2023-01-20 23:35:47 +01:00
if (cover_url.isValid()) {
song.set_art_automatic(cover_url);
}
2020-04-13 19:04:06 +02:00
song.set_comment(copyright);
song.set_directory_id(0);
2023-02-18 14:09:27 +01:00
song.set_filetype(Song::FileType::Stream);
2020-04-13 19:04:06 +02:00
song.set_filesize(0);
song.set_mtime(0);
song.set_ctime(0);
song.set_valid(true);
2022-10-31 06:12:02 +01:00
}
void TidalRequest::GetAlbumCoversCheck() {
if (
!finished_ &&
service_->download_album_covers() &&
IsQuery() &&
artists_requests_queue_.isEmpty() &&
albums_requests_queue_.isEmpty() &&
songs_requests_queue_.isEmpty() &&
artist_albums_requests_queue_.isEmpty() &&
album_songs_requests_queue_.isEmpty() &&
album_cover_requests_queue_.isEmpty() &&
artist_albums_requests_pending_.isEmpty() &&
album_songs_requests_pending_.isEmpty() &&
album_covers_requests_sent_.isEmpty() &&
artists_requests_active_ <= 0 &&
albums_requests_active_ <= 0 &&
songs_requests_active_ <= 0 &&
artist_albums_requests_active_ <= 0 &&
album_songs_requests_active_ <= 0 &&
album_covers_requests_active_ <= 0
) {
GetAlbumCovers();
}
2020-04-13 19:04:06 +02:00
}
void TidalRequest::GetAlbumCovers() {
2021-07-12 07:38:49 +02:00
const SongList songs = songs_.values();
for (const Song &song : songs) {
2020-04-13 19:04:06 +02:00
AddAlbumCoverRequest(song);
}
2024-08-25 01:06:30 +02:00
if (album_covers_requests_total_ == 1) Q_EMIT UpdateStatus(query_id_, tr("Receiving album cover for %1 album...").arg(album_covers_requests_total_));
else Q_EMIT UpdateStatus(query_id_, tr("Receiving album covers for %1 albums...").arg(album_covers_requests_total_));
Q_EMIT UpdateProgress(query_id_, 0);
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
StartRequests();
2020-04-13 19:04:06 +02:00
}
2021-07-12 07:38:49 +02:00
void TidalRequest::AddAlbumCoverRequest(const Song &song) {
2020-04-13 19:04:06 +02:00
if (album_covers_requests_sent_.contains(song.album_id())) {
album_covers_requests_sent_.insert(song.album_id(), song.song_id());
2020-04-13 19:04:06 +02:00
return;
}
AlbumCoverRequest request;
request.album_id = song.album_id();
request.url = song.art_automatic();
request.filename = CoverUtils::CoverFilePath(CoverOptions(), song.source(), song.effective_albumartist(), song.effective_album(), song.album_id(), QString(), request.url);
2020-04-13 19:04:06 +02:00
if (request.filename.isEmpty()) return;
album_covers_requests_sent_.insert(song.album_id(), song.song_id());
2022-10-31 06:12:02 +01:00
++album_covers_requests_total_;
2020-04-13 19:04:06 +02:00
album_cover_requests_queue_.enqueue(request);
}
void TidalRequest::FlushAlbumCoverRequests() {
while (!album_cover_requests_queue_.isEmpty() && album_covers_requests_active_ < kMaxConcurrentAlbumCoverRequests) {
AlbumCoverRequest request = album_cover_requests_queue_.dequeue();
QNetworkRequest req(request.url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
2020-04-13 19:04:06 +02:00
QNetworkReply *reply = network_->get(req);
album_cover_replies_ << reply;
2021-03-21 00:37:17 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumCoverReceived(reply, request.album_id, request.url, request.filename); });
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
++album_covers_requests_active_;
2020-04-13 19:04:06 +02:00
}
}
void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album_id, const QUrl &url, const QString &filename) {
if (album_cover_replies_.contains(reply)) {
album_cover_replies_.removeAll(reply);
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
2020-04-13 19:04:06 +02:00
reply->deleteLater();
}
else {
AlbumCoverFinishCheck();
return;
}
--album_covers_requests_active_;
2022-10-31 06:12:02 +01:00
++album_covers_requests_received_;
2020-04-13 19:04:06 +02:00
if (finished_) return;
2024-08-25 01:06:30 +02:00
Q_EMIT UpdateProgress(query_id_, GetProgress(album_covers_requests_received_, album_covers_requests_total_));
2020-04-13 19:04:06 +02:00
if (!album_covers_requests_sent_.contains(album_id)) {
AlbumCoverFinishCheck();
return;
}
if (reply->error() != QNetworkReply::NoError) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
2020-04-13 19:04:06 +02:00
album_covers_requests_sent_.remove(album_id);
AlbumCoverFinishCheck();
return;
}
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Received HTTP code %1 for %2.").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()).arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
AlbumCoverFinishCheck();
return;
}
QString mimetype = reply->header(QNetworkRequest::ContentTypeHeader).toString();
if (mimetype.contains(u';')) {
mimetype = mimetype.left(mimetype.indexOf(u';'));
2021-05-29 23:27:40 +02:00
}
if (!ImageUtils::SupportedImageMimeTypes().contains(mimetype, Qt::CaseInsensitive) && !ImageUtils::SupportedImageFormats().contains(mimetype, Qt::CaseInsensitive)) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Unsupported mimetype for image reader %1 for %2").arg(mimetype, url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
AlbumCoverFinishCheck();
return;
}
const QByteArray data = reply->readAll();
if (data.isEmpty()) {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Received empty image data for %1").arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
2020-04-13 19:04:06 +02:00
AlbumCoverFinishCheck();
return;
}
2022-01-29 01:33:40 +01:00
QByteArrayList format_list = QImageReader::imageFormatsForMimeType(mimetype.toUtf8());
2020-10-13 01:38:09 +02:00
char *format = nullptr;
if (!format_list.isEmpty()) {
2024-08-23 20:30:59 +02:00
format = format_list[0].data();
}
2020-04-13 19:04:06 +02:00
QImage image;
if (image.loadFromData(data, format)) {
if (image.save(filename, format)) {
2020-04-13 19:04:06 +02:00
while (album_covers_requests_sent_.contains(album_id)) {
const QString song_id = album_covers_requests_sent_.take(album_id);
if (songs_.contains(song_id)) {
songs_[song_id].set_art_automatic(QUrl::fromLocalFile(filename));
}
2020-04-13 19:04:06 +02:00
}
}
else {
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Error saving image data to %1").arg(filename));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
}
2020-04-13 19:04:06 +02:00
}
else {
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
2024-04-09 23:20:26 +02:00
Error(QStringLiteral("Error decoding image data from %1").arg(url.toString()));
2020-04-13 19:04:06 +02:00
}
AlbumCoverFinishCheck();
}
void TidalRequest::AlbumCoverFinishCheck() {
FinishCheck();
}
void TidalRequest::FinishCheck() {
if (
!finished_ &&
!need_login_ &&
artists_requests_queue_.isEmpty() &&
2022-10-31 06:12:02 +01:00
albums_requests_queue_.isEmpty() &&
2020-04-13 19:04:06 +02:00
songs_requests_queue_.isEmpty() &&
artist_albums_requests_queue_.isEmpty() &&
album_songs_requests_queue_.isEmpty() &&
album_cover_requests_queue_.isEmpty() &&
artist_albums_requests_pending_.isEmpty() &&
album_songs_requests_pending_.isEmpty() &&
album_covers_requests_sent_.isEmpty() &&
artists_requests_active_ <= 0 &&
albums_requests_active_ <= 0 &&
songs_requests_active_ <= 0 &&
artist_albums_requests_active_ <= 0 &&
album_songs_requests_active_ <= 0 &&
2022-10-31 06:12:02 +01:00
album_covers_requests_active_ <= 0
2020-04-13 19:04:06 +02:00
) {
2022-10-31 06:12:02 +01:00
if (timer_flush_requests_->isActive()) {
timer_flush_requests_->stop();
}
2020-04-13 19:04:06 +02:00
finished_ = true;
if (songs_.isEmpty()) {
if (errors_.isEmpty()) {
if (IsSearch()) {
2024-08-25 01:06:30 +02:00
Q_EMIT Results(query_id_, SongMap(), tr("No match."));
}
else {
2024-08-25 01:06:30 +02:00
Q_EMIT Results(query_id_);
}
}
else {
2024-08-25 01:06:30 +02:00
Q_EMIT Results(query_id_, SongMap(), ErrorsToHTML(errors_));
}
2020-04-13 19:04:06 +02:00
}
else {
2024-08-25 01:06:30 +02:00
Q_EMIT Results(query_id_, songs_);
}
2020-04-13 19:04:06 +02:00
}
}
2022-10-31 06:12:02 +01:00
int TidalRequest::GetProgress(const int count, const int total) {
return static_cast<int>((static_cast<float>(count) / static_cast<float>(total)) * 100.0F);
}
2020-04-13 19:04:06 +02:00
void TidalRequest::Error(const QString &error, const QVariant &debug) {
if (!error.isEmpty()) {
errors_ << error;
qLog(Error) << "Tidal:" << error;
}
if (debug.isValid()) qLog(Debug) << debug;
FinishCheck();
}
2020-04-23 21:05:57 +02:00
void TidalRequest::Warn(const QString &error, const QVariant &debug) {
2020-04-13 19:04:06 +02:00
qLog(Error) << "Tidal:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}