Improve Tidal and Qobuz request

This commit is contained in:
Jonas Kvinge
2022-10-31 06:12:02 +01:00
parent 7c0c9fccdb
commit 157de12bdf
14 changed files with 861 additions and 615 deletions

View File

@@ -60,7 +60,7 @@ QNetworkReply *QobuzBaseRequest::CreateRequest(const QString &ressource_name, co
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
}
QUrl url(QobuzService::kApiUrl + QString("/") + ressource_name);
QUrl url(QString(QobuzService::kApiUrl) + QString("/") + ressource_name);
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);

File diff suppressed because it is too large Load Diff

View File

@@ -41,6 +41,7 @@
#include "qobuzbaserequest.h"
class QNetworkReply;
class QTimer;
class Application;
class NetworkAccessManager;
class QobuzService;
@@ -52,19 +53,55 @@ class QobuzRequest : public QobuzBaseRequest {
public:
explicit QobuzRequest(QobuzService *service, QobuzUrlHandler *url_handler, Application *app, NetworkAccessManager *network, QueryType type, QObject *parent = nullptr);
~QobuzRequest();
~QobuzRequest() override;
void ReloadSettings();
void Process();
void Search(const int query_id, const QString &search_text);
private:
struct Artist {
QString artist_id;
QString artist;
};
struct Album {
Album() : album_explicit(false) {}
QString album_id;
QString album;
QUrl cover_url;
bool album_explicit;
};
struct Request {
Request() : offset(0), limit(0) {}
int offset;
int limit;
};
struct ArtistAlbumsRequest {
ArtistAlbumsRequest() : offset(0), limit(0) {}
Artist artist;
int offset;
int limit;
};
struct AlbumSongsRequest {
AlbumSongsRequest() : offset(0), limit(0) {}
Artist artist;
Album album;
int offset;
int limit;
};
struct AlbumCoverRequest {
QString artist_id;
QString album_id;
QUrl url;
QString filename;
};
signals:
void LoginSuccess();
void LoginFailure(QString failure_reason);
void Results(int id, SongMap songs, QString error);
void UpdateStatus(int id, QString text);
void ProgressSetMaximum(int id, int max);
void UpdateProgress(int id, int max);
void StreamURLFinished(QUrl original_url, QUrl url, Song::FileType, QString error = QString());
@@ -72,35 +109,23 @@ class QobuzRequest : public QobuzBaseRequest {
void ArtistsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested);
void AlbumsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested);
void AlbumsReceived(QNetworkReply *reply, const QString &artist_id_requested, const int limit_requested, const int offset_requested);
void AlbumsReceived(QNetworkReply *reply, const Artist &artist_requested, const int limit_requested, const int offset_requested);
void SongsReplyReceived(QNetworkReply *reply, const int limit_requested, const int offset_requested);
void SongsReceived(QNetworkReply *reply, const QString &artist_id_requested, const QString &album_id_requested, const int limit_requested, const int offset_requested, const QString &album_artist_requested = QString(), const QString &album_requested = QString());
void SongsReceived(QNetworkReply *reply, const Artist &artist_requested, const Album &album_requested, const int limit_requested, const int offset_requested);
void ArtistAlbumsReplyReceived(QNetworkReply *reply, const QString &artist_id, const int offset_requested);
void AlbumSongsReplyReceived(QNetworkReply *reply, const QString &artist_id, const QString &album_id, const int offset_requested, const QString &album_artist, const QString &album);
void ArtistAlbumsReplyReceived(QNetworkReply *reply, const Artist &artist, const int offset_requested);
void AlbumSongsReplyReceived(QNetworkReply *reply, const Artist &artist, const Album &album, const int offset_requested);
void AlbumCoverReceived(QNetworkReply *reply, const QUrl &cover_url, const QString &filename);
private:
struct Request {
Request() : offset(0), limit(0) {}
QString artist_id;
QString album_id;
QString song_id;
int offset;
int limit;
QString album_artist;
QString album;
};
struct AlbumCoverRequest {
QUrl url;
QString filename;
};
bool IsQuery() { return (type_ == QueryType_Artists || type_ == QueryType_Albums || type_ == QueryType_Songs); }
bool IsSearch() { return (type_ == QueryType_SearchArtists || type_ == QueryType_SearchAlbums || type_ == QueryType_SearchSongs); }
void StartRequests();
void FlushRequests();
void GetArtists();
void GetAlbums();
void GetSongs();
@@ -120,24 +145,27 @@ class QobuzRequest : public QobuzBaseRequest {
void FlushSongsRequests();
void ArtistsFinishCheck(const int limit = 0, const int offset = 0, const int artists_received = 0);
void AlbumsFinishCheck(const QString &artist_id, const int limit = 0, const int offset = 0, const int albums_total = 0, const int albums_received = 0);
void SongsFinishCheck(const QString &artist_id, const QString &album_id, const int limit, const int offset, const int songs_total, const int songs_received, const QString &album_artist, const QString &album);
void AlbumsFinishCheck(const Artist &artist, const int limit = 0, const int offset = 0, const int albums_total = 0, const int albums_received = 0);
void SongsFinishCheck(const Artist &artist, const Album &album, const int limit = 0, const int offset = 0, const int songs_total = 0, const int songs_received = 0);
void AddArtistAlbumsRequest(const QString &artist_id, const int offset = 0);
void AddArtistAlbumsRequest(const Artist &artist, const int offset = 0);
void FlushArtistAlbumsRequests();
void AddAlbumSongsRequest(const QString &artist_id, const QString &album_id, const QString &album_artist, const QString &album, const int offset = 0);
void AddAlbumSongsRequest(const Artist &artist, const Album &album, const int offset = 0);
void FlushAlbumSongsRequests();
QString ParseSong(Song &song, const QJsonObject &json_obj, QString artist_id, QString album_id, QString album_artist, QString album, QUrl cover_url);
void ParseSong(Song &song, const QJsonObject &json_obj, const Artist &album_artist, const Album &album);
QString AlbumCoverFileName(const Song &song);
void GetAlbumCoversCheck();
void GetAlbumCovers();
void AddAlbumCoverRequest(const Song &song);
void FlushAlbumCoverRequests();
void AlbumCoverFinishCheck();
int GetProgress(const int count, const int total);
void FinishCheck();
static void Warn(const QString &error, const QVariant &debug = QVariant());
void Error(const QString &error, const QVariant &debug = QVariant()) override;
@@ -148,13 +176,15 @@ class QobuzRequest : public QobuzBaseRequest {
static const int kMaxConcurrentArtistAlbumsRequests;
static const int kMaxConcurrentAlbumSongsRequests;
static const int kMaxConcurrentAlbumCoverRequests;
static const int kFlushRequestsDelay;
QobuzService *service_;
QobuzUrlHandler *url_handler_;
Application *app_;
NetworkAccessManager *network_;
QTimer *timer_flush_requests_;
QueryType type_;
const QueryType type_;
int query_id_;
QString search_text_;
@@ -164,32 +194,47 @@ class QobuzRequest : public QobuzBaseRequest {
QQueue<Request> albums_requests_queue_;
QQueue<Request> songs_requests_queue_;
QQueue<Request> artist_albums_requests_queue_;
QQueue<Request> album_songs_requests_queue_;
QQueue<ArtistAlbumsRequest> artist_albums_requests_queue_;
QQueue<AlbumSongsRequest> album_songs_requests_queue_;
QQueue<AlbumCoverRequest> album_cover_requests_queue_;
QList<QString> artist_albums_requests_pending_;
QHash<QString, Request> album_songs_requests_pending_;
QHash<QString, ArtistAlbumsRequest> artist_albums_requests_pending_;
QHash<QString, AlbumSongsRequest> album_songs_requests_pending_;
QMultiMap<QUrl, QString> album_covers_requests_sent_;
int artists_requests_total_;
int artists_requests_active_;
int artists_requests_received_;
int artists_total_;
int artists_received_;
int albums_requests_total_;
int albums_requests_active_;
int songs_requests_active_;
int albums_requests_received_;
int albums_total_;
int albums_received_;
int songs_requests_total_;
int songs_requests_active_;
int songs_requests_received_;
int songs_total_;
int songs_received_;
int artist_albums_requests_total_;
int artist_albums_requests_active_;
int artist_albums_requested_;
int artist_albums_requests_received_;
int artist_albums_total_;
int artist_albums_received_;
int album_songs_requests_active_;
int album_songs_requested_;
int album_songs_requests_received_;
int album_songs_requests_total_;
int album_songs_total_;
int album_songs_received_;
int album_covers_requests_total_;
int album_covers_requests_active_;
int album_covers_requested_;
int album_covers_received_;
int album_covers_requests_received_;
SongMap songs_;
QStringList errors_;

View File

@@ -57,19 +57,19 @@
#include "settings/settingsdialog.h"
#include "settings/qobuzsettingspage.h"
const Song::Source QobuzService::kSource = Song::Source_Qobuz;
const char *QobuzService::kAuthUrl = "https://www.qobuz.com/api.json/0.2/user/login";
const char *QobuzService::kApiUrl = "https://www.qobuz.com/api.json/0.2";
const int QobuzService::kLoginAttempts = 2;
const int QobuzService::kTimeResetLoginAttempts = 60000;
constexpr Song::Source QobuzService::kSource = Song::Source_Qobuz;
constexpr char QobuzService::kAuthUrl[] = "https://www.qobuz.com/api.json/0.2/user/login";
constexpr char QobuzService::kApiUrl[] = "https://www.qobuz.com/api.json/0.2";
constexpr int QobuzService::kLoginAttempts = 2;
constexpr int QobuzService::kTimeResetLoginAttempts = 60000;
const char *QobuzService::kArtistsSongsTable = "qobuz_artists_songs";
const char *QobuzService::kAlbumsSongsTable = "qobuz_albums_songs";
const char *QobuzService::kSongsTable = "qobuz_songs";
constexpr char QobuzService::kArtistsSongsTable[] = "qobuz_artists_songs";
constexpr char QobuzService::kAlbumsSongsTable[] = "qobuz_albums_songs";
constexpr char QobuzService::kSongsTable[] = "qobuz_songs";
const char *QobuzService::kArtistsSongsFtsTable = "qobuz_artists_songs_fts";
const char *QobuzService::kAlbumsSongsFtsTable = "qobuz_albums_songs_fts";
const char *QobuzService::kSongsFtsTable = "qobuz_songs_fts";
constexpr char QobuzService::kArtistsSongsFtsTable[] = "qobuz_artists_songs_fts";
constexpr char QobuzService::kAlbumsSongsFtsTable[] = "qobuz_albums_songs_fts";
constexpr char QobuzService::kSongsFtsTable[] = "qobuz_songs_fts";
QobuzService::QobuzService(Application *app, QObject *parent)
: InternetService(Song::Source_Qobuz, "Qobuz", "qobuz", QobuzSettingsPage::kSettingsGroup, SettingsDialog::Page_Qobuz, app, parent),
@@ -536,7 +536,6 @@ void QobuzService::GetArtists() {
artists_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Artists), [](QobuzRequest *request) { request->deleteLater(); });
QObject::connect(artists_request_.get(), &QobuzRequest::Results, this, &QobuzService::ArtistsResultsReceived);
QObject::connect(artists_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::ArtistsUpdateStatusReceived);
QObject::connect(artists_request_.get(), &QobuzRequest::ProgressSetMaximum, this, &QobuzService::ArtistsProgressSetMaximumReceived);
QObject::connect(artists_request_.get(), &QobuzRequest::UpdateProgress, this, &QobuzService::ArtistsUpdateProgressReceived);
artists_request_->Process();
@@ -556,11 +555,6 @@ void QobuzService::ArtistsUpdateStatusReceived(const int id, const QString &text
emit ArtistsUpdateStatus(text);
}
void QobuzService::ArtistsProgressSetMaximumReceived(const int id, const int max) {
Q_UNUSED(id);
emit ArtistsProgressSetMaximum(max);
}
void QobuzService::ArtistsUpdateProgressReceived(const int id, const int progress) {
Q_UNUSED(id);
emit ArtistsUpdateProgress(progress);
@@ -592,7 +586,6 @@ void QobuzService::GetAlbums() {
albums_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Albums), [](QobuzRequest *request) { request->deleteLater(); });
QObject::connect(albums_request_.get(), &QobuzRequest::Results, this, &QobuzService::AlbumsResultsReceived);
QObject::connect(albums_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::AlbumsUpdateStatusReceived);
QObject::connect(albums_request_.get(), &QobuzRequest::ProgressSetMaximum, this, &QobuzService::AlbumsProgressSetMaximumReceived);
QObject::connect(albums_request_.get(), &QobuzRequest::UpdateProgress, this, &QobuzService::AlbumsUpdateProgressReceived);
albums_request_->Process();
@@ -612,11 +605,6 @@ void QobuzService::AlbumsUpdateStatusReceived(const int id, const QString &text)
emit AlbumsUpdateStatus(text);
}
void QobuzService::AlbumsProgressSetMaximumReceived(const int id, const int max) {
Q_UNUSED(id);
emit AlbumsProgressSetMaximum(max);
}
void QobuzService::AlbumsUpdateProgressReceived(const int id, const int progress) {
Q_UNUSED(id);
emit AlbumsUpdateProgress(progress);
@@ -648,7 +636,6 @@ void QobuzService::GetSongs() {
songs_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Songs), [](QobuzRequest *request) { request->deleteLater(); });
QObject::connect(songs_request_.get(), &QobuzRequest::Results, this, &QobuzService::SongsResultsReceived);
QObject::connect(songs_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::SongsUpdateStatusReceived);
QObject::connect(songs_request_.get(), &QobuzRequest::ProgressSetMaximum, this, &QobuzService::SongsProgressSetMaximumReceived);
QObject::connect(songs_request_.get(), &QobuzRequest::UpdateProgress, this, &QobuzService::SongsUpdateProgressReceived);
songs_request_->Process();
@@ -668,11 +655,6 @@ void QobuzService::SongsUpdateStatusReceived(const int id, const QString &text)
emit SongsUpdateStatus(text);
}
void QobuzService::SongsProgressSetMaximumReceived(const int id, const int max) {
Q_UNUSED(id);
emit SongsProgressSetMaximum(max);
}
void QobuzService::SongsUpdateProgressReceived(const int id, const int progress) {
Q_UNUSED(id);
emit SongsUpdateProgress(progress);
@@ -734,7 +716,6 @@ void QobuzService::SendSearch() {
QObject::connect(search_request_.get(), &QobuzRequest::Results, this, &QobuzService::SearchResultsReceived);
QObject::connect(search_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::SearchUpdateStatus);
QObject::connect(search_request_.get(), &QobuzRequest::ProgressSetMaximum, this, &QobuzService::SearchProgressSetMaximum);
QObject::connect(search_request_.get(), &QobuzRequest::UpdateProgress, this, &QobuzService::SearchUpdateProgress);
search_request_->Search(search_id_, search_text_);

View File

@@ -61,7 +61,7 @@ class QobuzService : public InternetService {
~QobuzService();
static const Song::Source kSource;
static const char *kApiUrl;
static const char kApiUrl[];
void Exit() override;
void ReloadSettings() override;
@@ -72,26 +72,26 @@ class QobuzService : public InternetService {
int max_login_attempts() { return kLoginAttempts; }
Application *app() { return app_; }
QString app_id() { return app_id_; }
QString app_secret() { return app_secret_; }
QString username() { return username_; }
QString password() { return password_; }
int format() { return format_; }
int search_delay() { return search_delay_; }
int artistssearchlimit() { return artistssearchlimit_; }
int albumssearchlimit() { return albumssearchlimit_; }
int songssearchlimit() { return songssearchlimit_; }
bool download_album_covers() { return download_album_covers_; }
Application *app() const { return app_; }
QString app_id() const { return app_id_; }
QString app_secret() const { return app_secret_; }
QString username() const { return username_; }
QString password() const { return password_; }
int format() const { return format_; }
int search_delay() const { return search_delay_; }
int artistssearchlimit() const { return artistssearchlimit_; }
int albumssearchlimit() const { return albumssearchlimit_; }
int songssearchlimit() const { return songssearchlimit_; }
bool download_album_covers() const { return download_album_covers_; }
QString user_auth_token() { return user_auth_token_; }
qint64 user_id() { return user_id_; }
QString device_id() { return device_id_; }
qint64 credential_id() { return credential_id_; }
QString user_auth_token() const { return user_auth_token_; }
qint64 user_id() const { return user_id_; }
QString device_id() const { return device_id_; }
qint64 credential_id() const { return credential_id_; }
bool authenticated() override { return (!app_id_.isEmpty() && !app_secret_.isEmpty() && !user_auth_token_.isEmpty()); }
bool login_sent() { return login_sent_; }
bool login_attempts() { return login_attempts_; }
bool authenticated() const override { return (!app_id_.isEmpty() && !app_secret_.isEmpty() && !user_auth_token_.isEmpty()); }
bool login_sent() const { return login_sent_; }
bool login_attempts() const { return login_attempts_; }
uint GetStreamURL(const QUrl &url, QString &error);
@@ -132,9 +132,6 @@ class QobuzService : public InternetService {
void ArtistsUpdateStatusReceived(const int id, const QString &text);
void AlbumsUpdateStatusReceived(const int id, const QString &text);
void SongsUpdateStatusReceived(const int id, const QString &text);
void ArtistsProgressSetMaximumReceived(const int id, const int max);
void AlbumsProgressSetMaximumReceived(const int id, const int max);
void SongsProgressSetMaximumReceived(const int id, const int max);
void ArtistsUpdateProgressReceived(const int id, const int progress);
void AlbumsUpdateProgressReceived(const int id, const int progress);
void SongsUpdateProgressReceived(const int id, const int progress);
@@ -149,18 +146,18 @@ class QobuzService : public InternetService {
void SendSearch();
void LoginError(const QString &error = QString(), const QVariant &debug = QVariant());
static const char *kAuthUrl;
static const char kAuthUrl[];
static const int kLoginAttempts;
static const int kTimeResetLoginAttempts;
static const char *kArtistsSongsTable;
static const char *kAlbumsSongsTable;
static const char *kSongsTable;
static const char kArtistsSongsTable[];
static const char kAlbumsSongsTable[];
static const char kSongsTable[];
static const char *kArtistsSongsFtsTable;
static const char *kAlbumsSongsFtsTable;
static const char *kSongsFtsTable;
static const char kArtistsSongsFtsTable[];
static const char kAlbumsSongsFtsTable[];
static const char kSongsFtsTable[];
Application *app_;
NetworkAccessManager *network_;