Refactor subsonic, tidal and qobuz code

This commit is contained in:
Jonas Kvinge
2021-07-01 02:01:38 +02:00
parent b9f3f80d50
commit b5f4df0912
31 changed files with 183 additions and 203 deletions

View File

@@ -41,8 +41,6 @@
#include "qobuzservice.h"
#include "qobuzbaserequest.h"
const char *QobuzBaseRequest::kApiUrl = "https://www.qobuz.com/api.json/0.2";
QobuzBaseRequest::QobuzBaseRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent) :
QObject(parent),
service_(service),
@@ -63,7 +61,7 @@ QNetworkReply *QobuzBaseRequest::CreateRequest(const QString &ressource_name, co
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
}
QUrl url(kApiUrl + QString("/") + ressource_name);
QUrl url(QobuzService::kApiUrl + QString("/") + ressource_name);
url.setQuery(url_query);
QNetworkRequest req(url);
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)

View File

@@ -44,6 +44,8 @@ class QobuzBaseRequest : public QObject {
Q_OBJECT
public:
explicit QobuzBaseRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent = nullptr);
~QobuzBaseRequest();
enum QueryType {
QueryType_None,
@@ -56,14 +58,10 @@ class QobuzBaseRequest : public QObject {
QueryType_StreamURL,
};
explicit QobuzBaseRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent);
~QobuzBaseRequest();
protected:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
static const char *kApiUrl;
QNetworkReply *CreateRequest(const QString &ressource_name, const QList<Param> &params_provided);
QByteArray GetReplyData(QNetworkReply *reply);
QJsonObject ExtractJsonObj(QByteArray &data);
@@ -73,7 +71,6 @@ class QobuzBaseRequest : public QObject {
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
static QString ErrorsToHTML(const QStringList &errors);
QString api_url() { return QString(kApiUrl); }
QString app_id() { return service_->app_id(); }
QString app_secret() { return service_->app_secret(); }
QString username() { return service_->username(); }

View File

@@ -38,7 +38,7 @@ class QobuzFavoriteRequest : public QobuzBaseRequest {
Q_OBJECT
public:
explicit QobuzFavoriteRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent);
explicit QobuzFavoriteRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent = nullptr);
~QobuzFavoriteRequest();
enum FavoriteType {

View File

@@ -904,7 +904,7 @@ void QobuzRequest::SongsReceived(QNetworkReply *reply, const QString &artist_id_
}
bool compilation = false;
//bool multidisc = false;
bool multidisc = false;
SongList songs;
int songs_received = 0;
for (const QJsonValueRef value_item : array_items) {
@@ -919,18 +919,15 @@ void QobuzRequest::SongsReceived(QNetworkReply *reply, const QString &artist_id_
Song song(Song::Source_Qobuz);
ParseSong(song, obj_item, artist_id, album_id, album_artist, album, cover_url);
if (!song.is_valid()) continue;
//if (song.disc() >= 2) multidisc = true;
if (song.disc() >= 2) multidisc = true;
if (song.is_compilation()) compilation = true;
songs << song;
}
for (Song &song : songs) {
for (Song song : songs) {
if (compilation) song.set_compilation_detected(true);
//if (multidisc) {
//QString album_full(QString("%1 - (Disc %2)").arg(song.album()).arg(song.disc()));
//song.set_album(album_full);
//}
songs_ << song;
if (!multidisc) song.set_disc(0);
songs_.insert(song.song_id(), song);
}
SongsFinishCheck(artist_id, album_id, limit_requested, offset_requested, songs_total, songs_received, album_artist, album);
@@ -1140,7 +1137,7 @@ QString QobuzRequest::ParseSong(Song &song, const QJsonObject &json_obj, QString
void QobuzRequest::GetAlbumCovers() {
for (Song &song : songs_) {
for (const Song &song : songs_) {
AddAlbumCoverRequest(song);
}
FlushAlbumCoverRequests();
@@ -1152,13 +1149,13 @@ void QobuzRequest::GetAlbumCovers() {
}
void QobuzRequest::AddAlbumCoverRequest(Song &song) {
void QobuzRequest::AddAlbumCoverRequest(const Song &song) {
QUrl cover_url = song.art_automatic();
if (!cover_url.isValid()) return;
if (album_covers_requests_sent_.contains(cover_url)) {
album_covers_requests_sent_.insert(cover_url, &song);
album_covers_requests_sent_.insert(cover_url, song.song_id());
return;
}
@@ -1167,7 +1164,7 @@ void QobuzRequest::AddAlbumCoverRequest(Song &song) {
request.filename = app_->album_cover_loader()->CoverFilePath(song.source(), song.effective_albumartist(), song.effective_album(), song.album_id(), QString(), cover_url);
if (request.filename.isEmpty()) return;
album_covers_requests_sent_.insert(cover_url, &song);
album_covers_requests_sent_.insert(cover_url, song.song_id());
++album_covers_requested_;
album_cover_requests_queue_.enqueue(request);
@@ -1261,8 +1258,10 @@ void QobuzRequest::AlbumCoverReceived(QNetworkReply *reply, const QUrl &cover_ur
if (image.loadFromData(data, format)) {
if (image.save(filename, format)) {
while (album_covers_requests_sent_.contains(cover_url)) {
Song *song = album_covers_requests_sent_.take(cover_url);
song->set_art_automatic(QUrl::fromLocalFile(filename));
const QString song_id = album_covers_requests_sent_.take(cover_url);
if (songs_.contains(song_id)) {
songs_[song_id].set_art_automatic(QUrl::fromLocalFile(filename));
}
}
}
else {
@@ -1321,9 +1320,9 @@ void QobuzRequest::FinishCheck() {
}
else {
if (songs_.isEmpty() && errors_.isEmpty())
emit Results(query_id_, songs_, tr("Unknown error"));
emit Results(query_id_, songs_.values(), tr("Unknown error"));
else
emit Results(query_id_, songs_, ErrorsToHTML(errors_));
emit Results(query_id_, songs_.values(), ErrorsToHTML(errors_));
}
}

View File

@@ -51,7 +51,7 @@ class QobuzRequest : public QobuzBaseRequest {
public:
explicit QobuzRequest(QobuzService *service, QobuzUrlHandler *url_handler, Application *app, NetworkAccessManager *network, QueryType type, QObject *parent);
explicit QobuzRequest(QobuzService *service, QobuzUrlHandler *url_handler, Application *app, NetworkAccessManager *network, QueryType type, QObject *parent = nullptr);
~QobuzRequest();
void ReloadSettings();
@@ -134,7 +134,7 @@ class QobuzRequest : public QobuzBaseRequest {
QString AlbumCoverFileName(const Song &song);
void GetAlbumCovers();
void AddAlbumCoverRequest(Song &song);
void AddAlbumCoverRequest(const Song &song);
void FlushAlbumCoverRequests();
void AlbumCoverFinishCheck();
@@ -170,7 +170,7 @@ class QobuzRequest : public QobuzBaseRequest {
QList<QString> artist_albums_requests_pending_;
QHash<QString, Request> album_songs_requests_pending_;
QMultiMap<QUrl, Song*> album_covers_requests_sent_;
QMultiMap<QUrl, QString> album_covers_requests_sent_;
int artists_requests_active_;
int artists_total_;
@@ -191,7 +191,7 @@ class QobuzRequest : public QobuzBaseRequest {
int album_covers_requested_;
int album_covers_received_;
SongList songs_;
QMap<QString, Song> songs_;
QStringList errors_;
bool no_results_;
QList<QNetworkReply*> replies_;

View File

@@ -60,6 +60,7 @@
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;
@@ -101,8 +102,8 @@ QobuzService::QobuzService(Application *app, QObject *parent)
pending_search_type_(InternetSearchView::SearchType_Artists),
search_id_(0),
login_sent_(false),
login_attempts_(0)
{
login_attempts_(0),
next_stream_url_request_id_(0) {
app->player()->RegisterUrlHandler(url_handler_);
@@ -176,8 +177,8 @@ QobuzService::QobuzService(Application *app, QObject *parent)
QobuzService::~QobuzService() {
while (!stream_url_requests_.isEmpty()) {
QobuzStreamURLRequest *stream_url_req = stream_url_requests_.takeFirst();
QObject::disconnect(stream_url_req, nullptr, this, nullptr);
std::shared_ptr<QobuzStreamURLRequest> stream_url_req = stream_url_requests_.take(stream_url_requests_.firstKey());
QObject::disconnect(stream_url_req.get(), nullptr, this, nullptr);
stream_url_req->deleteLater();
}
@@ -506,7 +507,7 @@ void QobuzService::GetArtists() {
ResetArtistsRequest();
artists_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Artists, this));
artists_request_ = std::make_shared<QobuzRequest>(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Artists);
QObject::connect(artists_request_.get(), &QobuzRequest::Results, this, &QobuzService::ArtistsResultsReceived);
QObject::connect(artists_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::ArtistsUpdateStatusReceived);
@@ -560,7 +561,7 @@ void QobuzService::GetAlbums() {
}
ResetAlbumsRequest();
albums_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Albums, this));
albums_request_ = std::make_shared<QobuzRequest>(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Albums);
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);
@@ -613,7 +614,7 @@ void QobuzService::GetSongs() {
}
ResetSongsRequest();
songs_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Songs, this));
songs_request_ = std::make_shared<QobuzRequest>(this, url_handler_, app_, network_, QobuzBaseRequest::QueryType_Songs);
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);
@@ -695,7 +696,7 @@ void QobuzService::SendSearch() {
break;
}
search_request_.reset(new QobuzRequest(this, url_handler_, app_, network_, type, this));
search_request_ = std::make_shared<QobuzRequest>(this, url_handler_, app_, network_, type);
QObject::connect(search_request_.get(), &QobuzRequest::Results, this, &QobuzService::SearchResultsReceived);
QObject::connect(search_request_.get(), &QobuzRequest::UpdateStatus, this, &QobuzService::SearchUpdateStatus);
@@ -718,23 +719,22 @@ void QobuzService::GetStreamURL(const QUrl &url) {
return;
}
QobuzStreamURLRequest *stream_url_req = new QobuzStreamURLRequest(this, network_, url, this);
stream_url_requests_ << stream_url_req;
const int id = ++next_stream_url_request_id_;
std::shared_ptr<QobuzStreamURLRequest> stream_url_req = std::make_shared<QobuzStreamURLRequest>(this, network_, url, id);
stream_url_requests_.insert(id, stream_url_req);
QObject::connect(stream_url_req, &QobuzStreamURLRequest::TryLogin, this, &QobuzService::TryLogin);
QObject::connect(stream_url_req, &QobuzStreamURLRequest::StreamURLFinished, this, &QobuzService::HandleStreamURLFinished);
QObject::connect(this, &QobuzService::LoginComplete, stream_url_req, &QobuzStreamURLRequest::LoginComplete);
QObject::connect(stream_url_req.get(), &QobuzStreamURLRequest::TryLogin, this, &QobuzService::TryLogin);
QObject::connect(stream_url_req.get(), &QobuzStreamURLRequest::StreamURLFinished, this, &QobuzService::HandleStreamURLFinished);
QObject::connect(this, &QobuzService::LoginComplete, stream_url_req.get(), &QobuzStreamURLRequest::LoginComplete);
stream_url_req->Process();
}
void QobuzService::HandleStreamURLFinished(const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration, const QString &error) {
void QobuzService::HandleStreamURLFinished(const int id, const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration, const QString &error) {
QobuzStreamURLRequest *stream_url_req = qobject_cast<QobuzStreamURLRequest*>(sender());
if (!stream_url_req || !stream_url_requests_.contains(stream_url_req)) return;
stream_url_req->deleteLater();
stream_url_requests_.removeAll(stream_url_req);
if (!stream_url_requests_.contains(id)) return;
std::shared_ptr<QobuzStreamURLRequest> stream_url_req = stream_url_requests_.take(id);
emit StreamURLFinished(original_url, stream_url, filetype, samplerate, bit_depth, duration, error);

View File

@@ -29,6 +29,7 @@
#include <QPair>
#include <QSet>
#include <QList>
#include <QMap>
#include <QVariant>
#include <QByteArray>
#include <QString>
@@ -60,6 +61,7 @@ class QobuzService : public InternetService {
~QobuzService();
static const Song::Source kSource;
static const char *kApiUrl;
void Exit() override;
void ReloadSettings() override;
@@ -105,15 +107,6 @@ class QobuzService : public InternetService {
QSortFilterProxyModel *albums_collection_sort_model() override { return albums_collection_sort_model_; }
QSortFilterProxyModel *songs_collection_sort_model() override { return songs_collection_sort_model_; }
enum QueryType {
QueryType_Artists,
QueryType_Albums,
QueryType_Songs,
QueryType_SearchArtists,
QueryType_SearchAlbums,
QueryType_SearchSongs,
};
public slots:
void ShowConfig() override;
void TryLogin();
@@ -145,7 +138,7 @@ class QobuzService : public InternetService {
void ArtistsUpdateProgressReceived(const int id, const int progress);
void AlbumsUpdateProgressReceived(const int id, const int progress);
void SongsUpdateProgressReceived(const int id, const int progress);
void HandleStreamURLFinished(const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration, const QString &error);
void HandleStreamURLFinished(const int id, const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration, const QString &error);
private:
typedef QPair<QString, QString> Param;
@@ -155,6 +148,7 @@ class QobuzService : public InternetService {
void LoginError(const QString &error = QString(), const QVariant &debug = QVariant());
static const char *kAuthUrl;
static const int kLoginAttempts;
static const int kTimeResetLoginAttempts;
@@ -217,7 +211,8 @@ class QobuzService : public InternetService {
bool login_sent_;
int login_attempts_;
QList<QobuzStreamURLRequest*> stream_url_requests_;
int next_stream_url_request_id_;
QMap<int, std::shared_ptr<QobuzStreamURLRequest>> stream_url_requests_;
QStringList login_errors_;

View File

@@ -45,11 +45,12 @@
#include "qobuzbaserequest.h"
#include "qobuzstreamurlrequest.h"
QobuzStreamURLRequest::QobuzStreamURLRequest(QobuzService *service, NetworkAccessManager *network, const QUrl &original_url, QObject *parent)
QobuzStreamURLRequest::QobuzStreamURLRequest(QobuzService *service, NetworkAccessManager *network, const QUrl &original_url, const int id, QObject *parent)
: QobuzBaseRequest(service, network, parent),
service_(service),
reply_(nullptr),
original_url_(original_url),
id_(id),
song_id_(original_url.path().toInt()),
tries_(0),
need_login_(false) {}
@@ -70,7 +71,7 @@ void QobuzStreamURLRequest::LoginComplete(const bool success, const QString &err
need_login_ = false;
if (!success) {
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, error);
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, error);
return;
}
@@ -81,7 +82,7 @@ void QobuzStreamURLRequest::LoginComplete(const bool success, const QString &err
void QobuzStreamURLRequest::Process() {
if (app_id().isEmpty() || app_secret().isEmpty()) {
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, tr("Missing Qobuz app ID or secret."));
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, tr("Missing Qobuz app ID or secret."));
return;
}
@@ -100,7 +101,7 @@ void QobuzStreamURLRequest::Cancel() {
reply_->abort();
}
else {
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, tr("Cancelled."));
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, tr("Cancelled."));
}
}
@@ -171,32 +172,32 @@ void QobuzStreamURLRequest::StreamURLReceived() {
need_login_ = true;
return;
}
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}
if (!json_obj.contains("track_id")) {
Error("Invalid Json reply, stream url is missing track_id.", json_obj);
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}
int track_id = json_obj["track_id"].toInt();
if (track_id != song_id_) {
Error("Incorrect track ID returned.", json_obj);
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}
if (!json_obj.contains("mime_type") || !json_obj.contains("url")) {
Error("Invalid Json reply, stream url is missing url or mime_type.", json_obj);
emit StreamURLFinished(original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}
@@ -217,7 +218,7 @@ void QobuzStreamURLRequest::StreamURLReceived() {
if (!url.isValid()) {
Error("Returned stream url is invalid.", json_obj);
emit StreamURLFinished(original_url_, original_url_, filetype, -1, -1, -1, errors_.first());
emit StreamURLFinished(id_, original_url_, original_url_, filetype, -1, -1, -1, errors_.first());
return;
}
@@ -234,7 +235,7 @@ void QobuzStreamURLRequest::StreamURLReceived() {
bit_depth = static_cast<int>(json_obj["bit_depth"].toDouble());
}
emit StreamURLFinished(original_url_, url, filetype, samplerate, bit_depth, duration);
emit StreamURLFinished(id_, original_url_, url, filetype, samplerate, bit_depth, duration);
}

View File

@@ -40,7 +40,7 @@ class QobuzStreamURLRequest : public QobuzBaseRequest {
Q_OBJECT
public:
explicit QobuzStreamURLRequest(QobuzService *service, NetworkAccessManager *network, const QUrl &original_url, QObject *parent);
explicit QobuzStreamURLRequest(QobuzService *service, NetworkAccessManager *network, const QUrl &original_url, const int id, QObject *parent = nullptr);
~QobuzStreamURLRequest();
void GetStreamURL();
@@ -54,7 +54,7 @@ class QobuzStreamURLRequest : public QobuzBaseRequest {
signals:
void TryLogin();
void StreamURLFinished(QUrl original_url, QUrl stream_url, Song::FileType filetype, int samplerate, int bit_depth, qint64 duration, QString error = QString());
void StreamURLFinished(int id, QUrl original_url, QUrl stream_url, Song::FileType filetype, int samplerate, int bit_depth, qint64 duration, QString error = QString());
private slots:
void StreamURLReceived();
@@ -68,6 +68,7 @@ class QobuzStreamURLRequest : public QobuzBaseRequest {
QobuzService *service_;
QNetworkReply *reply_;
QUrl original_url_;
int id_;
int song_id_;
int tries_;
bool need_login_;