Add Subsonic scrobble support (#545)

* add SubsonicScrobbler, add Scrobble method in SubsonicService

* new class SubsonicScrobbleRequest, use queue again, clean up

* add checkbox to enable server-side scrobbling to Subsonic settings page

* Check serversidescrobbling in SubsonicScrobbler::ReloadSettings instead of SubsonicService

TODO: SubsonicScrobbler::ReloadSettings needs to be called when
SubsonicSettings change.
This commit is contained in:
Pascal Below
2020-09-23 17:55:12 +02:00
committed by GitHub
parent b2fc41a911
commit 45bc353341
10 changed files with 491 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2020, Pascal Below <spezifisch@below.fr>
*
* 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"
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QDateTime>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QtDebug>
#include "core/application.h"
#include "core/logging.h"
#include "subsonicservice.h"
#include "subsonicbaserequest.h"
#include "subsonicscrobblerequest.h"
const int SubsonicScrobbleRequest::kMaxConcurrentScrobbleRequests = 3;
SubsonicScrobbleRequest::SubsonicScrobbleRequest(SubsonicService *service, SubsonicUrlHandler *url_handler, Application *app, QObject *parent)
: SubsonicBaseRequest(service, parent),
service_(service),
url_handler_(url_handler),
app_(app),
scrobble_requests_active_(0)
{
}
SubsonicScrobbleRequest::~SubsonicScrobbleRequest() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
disconnect(reply, nullptr, this, nullptr);
if (reply->isRunning()) reply->abort();
reply->deleteLater();
}
}
void SubsonicScrobbleRequest::CreateScrobbleRequest(const QString song_id, const bool submission, const QDateTime start_time) {
Request request;
request.song_id = song_id;
request.submission = submission;
request.time_ms = start_time.toMSecsSinceEpoch();
scrobble_requests_queue_.enqueue(request);
if (scrobble_requests_active_ < kMaxConcurrentScrobbleRequests) FlushScrobbleRequests();
}
void SubsonicScrobbleRequest::FlushScrobbleRequests() {
while (!scrobble_requests_queue_.isEmpty() && scrobble_requests_active_ < kMaxConcurrentScrobbleRequests) {
Request request = scrobble_requests_queue_.dequeue();
++scrobble_requests_active_;
ParamList params = ParamList() <<
Param("id", request.song_id) <<
Param("submission", QVariant(request.submission).toString()) <<
Param("time", QVariant(request.time_ms).toString());
QNetworkReply *reply;
reply = CreateGetRequest(QString("scrobble"), params);
replies_ << reply;
connect(reply, &QNetworkReply::finished, [=] { ScrobbleReplyReceived(reply); });
}
}
void SubsonicScrobbleRequest::ScrobbleReplyReceived(QNetworkReply *reply) {
if (!replies_.contains(reply)) return;
replies_.removeAll(reply);
disconnect(reply, nullptr, this, nullptr);
reply->deleteLater();
--scrobble_requests_active_;
// "subsonic-response" is empty on success, but some keys like status, version, or type might be present.
// Therefore we can only check for errors.
QByteArray data = GetReplyData(reply);
if (data.isEmpty()) {
FinishCheck();
return;
}
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) {
FinishCheck();
return;
}
if (json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (!json_error.isObject()) {
Error("Json error is not an object.", json_obj);
FinishCheck();
return;
}
json_obj = json_error.toObject();
if (!json_obj.isEmpty() && json_obj.contains("code") && json_obj.contains("message")) {
int code = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
Error(QString("%1 (%2)").arg(message).arg(code));
FinishCheck();
}
else {
Error("Json error object is missing code or message.", json_obj);
FinishCheck();
return;
}
return;
}
FinishCheck();
}
void SubsonicScrobbleRequest::FinishCheck() {
if (!scrobble_requests_queue_.isEmpty() && scrobble_requests_active_ < kMaxConcurrentScrobbleRequests) FlushScrobbleRequests();
}
void SubsonicScrobbleRequest::Error(const QString &error, const QVariant &debug) {
if (!error.isEmpty()) {
qLog(Error) << "SubsonicScrobbleRequest:" << error;
errors_ << error;
}
if (debug.isValid()) qLog(Debug) << debug;
FinishCheck();
}

View File

@@ -0,0 +1,90 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2020, Pascal Below <spezifisch@below.fr>
*
* 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/>.
*
*/
#ifndef SUBSONICSCROBBLEREQUEST_H
#define SUBSONICSCROBBLEREQUEST_H
#include "config.h"
#include <memory>
#include <QtGlobal>
#include <QObject>
#include <QDateTime>
#include <QPair>
#include <QQueue>
#include <QVariant>
#include <QString>
#include <QStringList>
#include "subsonicbaserequest.h"
class QNetworkReply;
class Application;
class SubsonicService;
class SubsonicUrlHandler;
class SubsonicScrobbleRequest : public SubsonicBaseRequest {
Q_OBJECT
public:
explicit SubsonicScrobbleRequest(SubsonicService *service, SubsonicUrlHandler *url_handler, Application *app, QObject *parent);
~SubsonicScrobbleRequest() override;
void CreateScrobbleRequest(const QString song_id, const bool submission, const QDateTime start_time);
private slots:
void ScrobbleReplyReceived(QNetworkReply *reply);
private:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
struct Request {
explicit Request() : submission(false) {}
// subsonic song id
QString song_id;
// submission: true=Submission, false=NowPlaying
bool submission;
// song start time
qint64 time_ms;
};
void FlushScrobbleRequests();
void FinishCheck();
void Error(const QString &error, const QVariant &debug = QVariant()) override;
static const int kMaxConcurrentScrobbleRequests;
SubsonicService *service_;
SubsonicUrlHandler *url_handler_;
Application *app_;
QQueue<Request> scrobble_requests_queue_;
int scrobble_requests_active_;
QStringList errors_;
QList<QNetworkReply*> replies_;
};
#endif // SUBSONICSCROBBLEREQUEST_H

View File

@@ -27,6 +27,7 @@
#include <QPair>
#include <QList>
#include <QString>
#include <QVariant>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkAccessManager>
@@ -53,6 +54,7 @@
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
#include "subsonicrequest.h"
#include "subsonicscrobblerequest.h"
#include "settings/settingsdialog.h"
#include "settings/subsonicsettingspage.h"
@@ -379,6 +381,21 @@ void SubsonicService::CheckConfiguration() {
}
void SubsonicService::Scrobble(QString song_id, bool submission, QDateTime time) {
if (!server_url().isValid() || username().isEmpty() || password().isEmpty()) {
return;
}
if (!scrobble_request_.get()) {
// we're doing requests every 30-240s the whole time, so keep reusing this instance
scrobble_request_.reset(new SubsonicScrobbleRequest(this, url_handler_, app_, this));
}
scrobble_request_->CreateScrobbleRequest(song_id, submission, time);
}
void SubsonicService::ResetSongsRequest() {
if (songs_request_.get()) {

View File

@@ -35,6 +35,7 @@
#include <QUrl>
#include <QNetworkAccessManager>
#include <QSslError>
#include <QDateTime>
#include "core/song.h"
#include "internet/internetservice.h"
@@ -45,6 +46,7 @@ class QNetworkReply;
class Application;
class SubsonicUrlHandler;
class SubsonicRequest;
class SubsonicScrobbleRequest;
class CollectionBackend;
class CollectionModel;
@@ -79,6 +81,7 @@ class SubsonicService : public InternetService {
QSortFilterProxyModel *songs_collection_sort_model() override { return collection_sort_model_; }
void CheckConfiguration();
void Scrobble(QString song_id, bool submission, QDateTime time);
public slots:
void ShowConfig() override;
@@ -116,6 +119,7 @@ class SubsonicService : public InternetService {
QSortFilterProxyModel *collection_sort_model_;
std::shared_ptr<SubsonicRequest> songs_request_;
std::shared_ptr<SubsonicScrobbleRequest> scrobble_request_;
QUrl server_url_;
QString username_;