mirror of
https://github.com/strawberrymusicplayer/strawberry
synced 2024-12-16 10:38:53 +01:00
Scrobbler: Refactor and add MusicBrainz integration
This commit is contained in:
parent
aedbd52e9d
commit
fab38f693d
@ -270,6 +270,7 @@ set(SOURCES
|
||||
scrobbler/scrobblerservice.cpp
|
||||
scrobbler/scrobblercache.cpp
|
||||
scrobbler/scrobblercacheitem.cpp
|
||||
scrobbler/scrobblemetadata.cpp
|
||||
scrobbler/scrobblingapi20.cpp
|
||||
scrobbler/lastfmscrobbler.cpp
|
||||
scrobbler/librefmscrobbler.cpp
|
||||
@ -502,7 +503,6 @@ set(HEADERS
|
||||
scrobbler/scrobblerservices.h
|
||||
scrobbler/scrobblerservice.h
|
||||
scrobbler/scrobblercache.h
|
||||
scrobbler/scrobblercacheitem.h
|
||||
scrobbler/scrobblingapi20.h
|
||||
scrobbler/lastfmscrobbler.h
|
||||
scrobbler/librefmscrobbler.h
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -22,10 +22,7 @@
|
||||
#include <QObject>
|
||||
|
||||
#include "core/application.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
|
||||
#include "scrobblercache.h"
|
||||
#include "scrobblingapi20.h"
|
||||
#include "lastfmscrobbler.h"
|
||||
|
||||
const char *LastFMScrobbler::kName = "Last.fm";
|
||||
@ -35,20 +32,4 @@ const char *LastFMScrobbler::kApiUrl = "https://ws.audioscrobbler.com/2.0/";
|
||||
const char *LastFMScrobbler::kCacheFile = "lastfmscrobbler.cache";
|
||||
|
||||
LastFMScrobbler::LastFMScrobbler(Application *app, QObject *parent)
|
||||
: ScrobblingAPI20(kName, kSettingsGroup, kAuthUrl, kApiUrl, true, app, parent),
|
||||
auth_url_(kAuthUrl),
|
||||
api_url_(kApiUrl),
|
||||
app_(app),
|
||||
network_(new NetworkAccessManager(this)),
|
||||
cache_(new ScrobblerCache(kCacheFile, this)),
|
||||
enabled_(false),
|
||||
subscriber_(false),
|
||||
submitted_(false),
|
||||
timestamp_(0) {
|
||||
|
||||
ReloadSettings();
|
||||
LoadSession();
|
||||
|
||||
}
|
||||
|
||||
LastFMScrobbler::~LastFMScrobbler() = default;
|
||||
: ScrobblingAPI20(kName, kSettingsGroup, kAuthUrl, kApiUrl, true, kCacheFile, app, parent) {}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -24,49 +24,25 @@
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include "core/song.h"
|
||||
#include "scrobblingapi20.h"
|
||||
|
||||
class Application;
|
||||
class NetworkAccessManager;
|
||||
class ScrobblerCache;
|
||||
|
||||
class LastFMScrobbler : public ScrobblingAPI20 {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LastFMScrobbler(Application *app, QObject *parent = nullptr);
|
||||
~LastFMScrobbler() override;
|
||||
|
||||
static const char *kName;
|
||||
static const char *kSettingsGroup;
|
||||
static const char *kApiUrl;
|
||||
|
||||
NetworkAccessManager *network() const override { return network_; }
|
||||
ScrobblerCache *cache() const override { return cache_; }
|
||||
|
||||
private:
|
||||
static const char *kAuthUrl;
|
||||
static const char *kCacheFile;
|
||||
|
||||
QString settings_group_;
|
||||
QString auth_url_;
|
||||
QString api_url_;
|
||||
QString api_key_;
|
||||
QString secret_;
|
||||
Application *app_;
|
||||
NetworkAccessManager *network_;
|
||||
ScrobblerCache *cache_;
|
||||
bool enabled_;
|
||||
bool subscriber_;
|
||||
QString username_;
|
||||
QString session_key_;
|
||||
bool submitted_;
|
||||
Song song_playing_;
|
||||
quint64 timestamp_;
|
||||
|
||||
};
|
||||
|
||||
#endif // LASTFMSCROBBLER_H
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -22,9 +22,7 @@
|
||||
#include <QObject>
|
||||
|
||||
#include "core/application.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
|
||||
#include "scrobblercache.h"
|
||||
#include "scrobblingapi20.h"
|
||||
#include "librefmscrobbler.h"
|
||||
|
||||
@ -35,20 +33,4 @@ const char *LibreFMScrobbler::kApiUrl = "https://libre.fm/2.0/";
|
||||
const char *LibreFMScrobbler::kCacheFile = "librefmscrobbler.cache";
|
||||
|
||||
LibreFMScrobbler::LibreFMScrobbler(Application *app, QObject *parent)
|
||||
: ScrobblingAPI20(kName, kSettingsGroup, kAuthUrl, kApiUrl, false, app, parent),
|
||||
auth_url_(kAuthUrl),
|
||||
api_url_(kApiUrl),
|
||||
app_(app),
|
||||
network_(new NetworkAccessManager(this)),
|
||||
cache_(new ScrobblerCache(kCacheFile, this)),
|
||||
enabled_(false),
|
||||
subscriber_(false),
|
||||
submitted_(false),
|
||||
timestamp_(0) {
|
||||
|
||||
ScrobblingAPI20::ReloadSettings();
|
||||
LoadSession();
|
||||
|
||||
}
|
||||
|
||||
LibreFMScrobbler::~LibreFMScrobbler() = default;
|
||||
: ScrobblingAPI20(kName, kSettingsGroup, kAuthUrl, kApiUrl, false, kCacheFile, app, parent) {}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -24,49 +24,25 @@
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include "core/song.h"
|
||||
#include "scrobblingapi20.h"
|
||||
|
||||
class Application;
|
||||
class NetworkAccessManager;
|
||||
class ScrobblerCache;
|
||||
|
||||
class LibreFMScrobbler : public ScrobblingAPI20 {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LibreFMScrobbler(Application *app, QObject *parent = nullptr);
|
||||
~LibreFMScrobbler() override;
|
||||
|
||||
static const char *kName;
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
NetworkAccessManager *network() const override { return network_; }
|
||||
ScrobblerCache *cache() const override { return cache_; }
|
||||
|
||||
private:
|
||||
static const char *kAuthUrl;
|
||||
static const char *kApiUrl;
|
||||
static const char *kCacheFile;
|
||||
|
||||
QString settings_group_;
|
||||
QString auth_url_;
|
||||
QString api_url_;
|
||||
QString api_key_;
|
||||
QString secret_;
|
||||
Application *app_;
|
||||
NetworkAccessManager *network_;
|
||||
ScrobblerCache *cache_;
|
||||
bool enabled_;
|
||||
bool subscriber_;
|
||||
QString username_;
|
||||
QString session_key_;
|
||||
bool submitted_;
|
||||
Song song_playing_;
|
||||
quint64 timestamp_;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBREFMSCROBBLER_H
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -52,6 +52,7 @@
|
||||
#include "scrobblerservice.h"
|
||||
#include "scrobblercache.h"
|
||||
#include "scrobblercacheitem.h"
|
||||
#include "scrobblemetadata.h"
|
||||
#include "listenbrainzscrobbler.h"
|
||||
|
||||
const char *ListenBrainzScrobbler::kName = "ListenBrainz";
|
||||
@ -417,6 +418,72 @@ QByteArray ListenBrainzScrobbler::GetReplyData(QNetworkReply *reply) {
|
||||
|
||||
}
|
||||
|
||||
QJsonObject ListenBrainzScrobbler::JsonTrackMetadata(const ScrobbleMetadata &metadata) const {
|
||||
|
||||
QJsonObject object_track_metadata;
|
||||
if (prefer_albumartist_) {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(metadata.effective_albumartist()));
|
||||
}
|
||||
else {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(metadata.artist));
|
||||
}
|
||||
|
||||
if (!metadata.album.isEmpty()) {
|
||||
object_track_metadata.insert("release_name", QJsonValue::fromVariant(StripAlbum(metadata.album)));
|
||||
}
|
||||
|
||||
object_track_metadata.insert("track_name", QJsonValue::fromVariant(StripTitle(metadata.title)));
|
||||
|
||||
QJsonObject object_additional_info;
|
||||
|
||||
object_additional_info.insert("duration_ms", metadata.length_nanosec / kNsecPerMsec);
|
||||
|
||||
if (metadata.track > 0) {
|
||||
object_additional_info.insert("tracknumber", metadata.track);
|
||||
}
|
||||
|
||||
object_additional_info.insert("media_player", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("media_player_version", QCoreApplication::applicationVersion());
|
||||
object_additional_info.insert("submission_client", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("submission_client_version", QCoreApplication::applicationVersion());
|
||||
|
||||
QJsonArray artist_mbids;
|
||||
if (!metadata.musicbrainz_album_artist_id.isEmpty()) {
|
||||
artist_mbids.append(metadata.musicbrainz_album_artist_id);
|
||||
}
|
||||
if (!metadata.musicbrainz_artist_id.isEmpty()) {
|
||||
artist_mbids.append(metadata.musicbrainz_artist_id);
|
||||
}
|
||||
if (!metadata.musicbrainz_original_artist_id.isEmpty()) {
|
||||
artist_mbids.append(metadata.musicbrainz_original_artist_id);
|
||||
}
|
||||
if (!artist_mbids.isEmpty()) {
|
||||
object_additional_info.insert("artist_mbids", artist_mbids);
|
||||
}
|
||||
|
||||
if (!metadata.musicbrainz_album_id.isEmpty()) {
|
||||
object_additional_info.insert("release_mbid", metadata.musicbrainz_album_id);
|
||||
}
|
||||
else if (!metadata.musicbrainz_original_album_id.isEmpty()) {
|
||||
object_additional_info.insert("release_mbid", metadata.musicbrainz_original_album_id);
|
||||
}
|
||||
|
||||
if (!metadata.musicbrainz_recording_id.isEmpty()) {
|
||||
object_additional_info.insert("recording_mbid", metadata.musicbrainz_recording_id);
|
||||
}
|
||||
if (!metadata.musicbrainz_track_id.isEmpty()) {
|
||||
object_additional_info.insert("track_mbid", metadata.musicbrainz_track_id);
|
||||
}
|
||||
if (!metadata.musicbrainz_work_id.isEmpty()) {
|
||||
object_additional_info.insert("work_mbids", QJsonArray() << metadata.musicbrainz_work_id);
|
||||
}
|
||||
|
||||
object_track_metadata.insert("additional_info", object_additional_info);
|
||||
|
||||
return object_track_metadata;
|
||||
|
||||
}
|
||||
|
||||
void ListenBrainzScrobbler::UpdateNowPlaying(const Song &song) {
|
||||
|
||||
CheckScrobblePrevSong();
|
||||
@ -427,48 +494,10 @@ void ListenBrainzScrobbler::UpdateNowPlaying(const Song &song) {
|
||||
|
||||
if (!song.is_metadata_good() || !IsAuthenticated() || app_->scrobbler()->IsOffline()) return;
|
||||
|
||||
QString album = song.album();
|
||||
QString title = song.title();
|
||||
|
||||
album = album.remove(Song::kAlbumRemoveDisc)
|
||||
.remove(Song::kAlbumRemoveMisc);
|
||||
title = title.remove(Song::kTitleRemoveMisc);
|
||||
|
||||
QJsonObject object_track_metadata;
|
||||
if (!prefer_albumartist_ || song.albumartist().isEmpty()) {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(song.artist()));
|
||||
}
|
||||
else {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(song.albumartist()));
|
||||
}
|
||||
|
||||
if (!album.isEmpty()) {
|
||||
object_track_metadata.insert("release_name", QJsonValue::fromVariant(album));
|
||||
}
|
||||
|
||||
object_track_metadata.insert("track_name", QJsonValue::fromVariant(title));
|
||||
|
||||
QJsonObject object_additional_info;
|
||||
|
||||
object_additional_info.insert("duration_ms", song.length_nanosec() / kNsecPerMsec);
|
||||
|
||||
if (const int track = song.track(); track > 0) {
|
||||
object_additional_info.insert("tracknumber", track);
|
||||
}
|
||||
|
||||
object_additional_info.insert("media_player", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("media_player_version", QCoreApplication::applicationVersion());
|
||||
object_additional_info.insert("submission_client", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("submission_client_version", QCoreApplication::applicationVersion());
|
||||
|
||||
object_track_metadata.insert("additional_info", object_additional_info);
|
||||
|
||||
QJsonObject object_listen;
|
||||
object_listen.insert("track_metadata", object_track_metadata);
|
||||
|
||||
object_listen.insert("track_metadata", JsonTrackMetadata(ScrobbleMetadata(song)));
|
||||
QJsonArray array_payload;
|
||||
array_payload.append(object_listen);
|
||||
|
||||
QJsonObject object;
|
||||
object.insert("listen_type", "playing_now");
|
||||
object.insert("payload", array_payload);
|
||||
@ -563,45 +592,17 @@ void ListenBrainzScrobbler::Submit() {
|
||||
if (!IsEnabled() || !IsAuthenticated() || app_->scrobbler()->IsOffline()) return;
|
||||
|
||||
QJsonArray array;
|
||||
int i(0);
|
||||
int i = 0;
|
||||
QList<quint64> list;
|
||||
QList<ScrobblerCacheItemPtr> items = cache_->List();
|
||||
for (ScrobblerCacheItemPtr item : items) { // clazy:exclude=range-loop-reference
|
||||
if (item->sent_) continue;
|
||||
item->sent_ = true;
|
||||
ScrobblerCacheItemPtrList cache_items = cache_->List();
|
||||
for (ScrobblerCacheItemPtr cache_item : cache_items) {
|
||||
if (cache_item->sent) continue;
|
||||
cache_item->sent = true;
|
||||
++i;
|
||||
list << item->timestamp_;
|
||||
list << cache_item->timestamp;
|
||||
QJsonObject object_listen;
|
||||
object_listen.insert("listened_at", QJsonValue::fromVariant(item->timestamp_));
|
||||
QJsonObject object_track_metadata;
|
||||
if (!prefer_albumartist_ || item->albumartist_.isEmpty()) {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(item->artist_));
|
||||
}
|
||||
else {
|
||||
object_track_metadata.insert("artist_name", QJsonValue::fromVariant(item->albumartist_));
|
||||
}
|
||||
|
||||
if (!item->album_.isEmpty()) {
|
||||
object_track_metadata.insert("release_name", QJsonValue::fromVariant(item->album_));
|
||||
}
|
||||
|
||||
QJsonObject object_additional_info;
|
||||
|
||||
object_additional_info.insert("duration_ms", item->duration_ / kNsecPerMsec);
|
||||
|
||||
if (item->track_ > 0) {
|
||||
object_additional_info.insert("tracknumber", item->track_);
|
||||
}
|
||||
|
||||
object_additional_info.insert("media_player", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("media_player_version", QCoreApplication::applicationVersion());
|
||||
object_additional_info.insert("submission_client", QCoreApplication::applicationName());
|
||||
object_additional_info.insert("submission_client_version", QCoreApplication::applicationVersion());
|
||||
|
||||
object_track_metadata.insert("additional_info", object_additional_info);
|
||||
|
||||
object_track_metadata.insert("track_name", QJsonValue::fromVariant(item->song_));
|
||||
object_listen.insert("track_metadata", object_track_metadata);
|
||||
object_listen.insert("listened_at", QJsonValue::fromVariant(cache_item->timestamp));
|
||||
object_listen.insert("track_metadata", JsonTrackMetadata(cache_item->metadata));
|
||||
array.append(QJsonValue::fromVariant(object_listen));
|
||||
if (i >= kScrobblesPerRequest) break;
|
||||
}
|
||||
@ -666,6 +667,58 @@ void ListenBrainzScrobbler::ScrobbleRequestFinished(QNetworkReply *reply, const
|
||||
|
||||
}
|
||||
|
||||
void ListenBrainzScrobbler::Love() {
|
||||
|
||||
if (!song_playing_.is_valid() || !song_playing_.is_metadata_good()) return;
|
||||
|
||||
if (!IsAuthenticated()) app_->scrobbler()->ShowConfig();
|
||||
|
||||
if (song_playing_.musicbrainz_recording_id().isEmpty()) {
|
||||
qLog(Error) << "ListenBrainz: Missing MusicBrainz recording ID for" << song_playing_.artist() << song_playing_.album() << song_playing_.title();
|
||||
return;
|
||||
}
|
||||
|
||||
qLog(Debug) << "ListenBrainz: Sending love for song" << song_playing_.artist() << song_playing_.album() << song_playing_.title();
|
||||
|
||||
QJsonObject object;
|
||||
object.insert("recording_mbid", song_playing_.musicbrainz_recording_id());
|
||||
object.insert("score", 1);
|
||||
|
||||
QUrl url(QString("%1/1/feedback/recording-feedback").arg(kApiUrl));
|
||||
QNetworkReply *reply = CreateRequest(url, QJsonDocument(object));
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() { LoveRequestFinished(reply); });
|
||||
|
||||
}
|
||||
|
||||
void ListenBrainzScrobbler::LoveRequestFinished(QNetworkReply *reply) {
|
||||
|
||||
if (!replies_.contains(reply)) return;
|
||||
replies_.removeAll(reply);
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
if (data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_obj.contains("code") && json_obj.contains("error_description")) {
|
||||
Error(json_obj["error_description"].toString());
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_obj.contains("status")) {
|
||||
QString status = json_obj["status"].toString();
|
||||
qLog(Debug) << "ListenBrainz: Received recording-feedback status:" << status;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ListenBrainzScrobbler::AuthError(const QString &error) {
|
||||
emit AuthenticationComplete(false, error);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -35,6 +35,7 @@
|
||||
#include "core/song.h"
|
||||
#include "scrobblerservice.h"
|
||||
#include "scrobblercache.h"
|
||||
#include "scrobblemetadata.h"
|
||||
|
||||
class QNetworkReply;
|
||||
|
||||
@ -68,6 +69,7 @@ class ListenBrainzScrobbler : public ScrobblerService {
|
||||
void UpdateNowPlaying(const Song &song) override;
|
||||
void ClearPlaying() override;
|
||||
void Scrobble(const Song &song) override;
|
||||
void Love() override;
|
||||
|
||||
signals:
|
||||
void AuthenticationComplete(bool success, QString error = QString());
|
||||
@ -81,11 +83,12 @@ class ListenBrainzScrobbler : public ScrobblerService {
|
||||
void RequestNewAccessToken() { RequestAccessToken(); }
|
||||
void UpdateNowPlayingRequestFinished(QNetworkReply *reply);
|
||||
void ScrobbleRequestFinished(QNetworkReply *reply, const QList<quint64> &list);
|
||||
void LoveRequestFinished(QNetworkReply *reply);
|
||||
|
||||
private:
|
||||
QNetworkReply *CreateRequest(const QUrl &url, const QJsonDocument &json_doc);
|
||||
QByteArray GetReplyData(QNetworkReply *reply);
|
||||
|
||||
QJsonObject JsonTrackMetadata(const ScrobbleMetadata &metadata) const;
|
||||
void AuthError(const QString &error);
|
||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||
void RequestAccessToken(const QUrl &redirect_url = QUrl(), const QString &code = QString());
|
||||
|
41
src/scrobbler/scrobblemetadata.cpp
Normal file
41
src/scrobbler/scrobblemetadata.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* 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 "core/song.h"
|
||||
|
||||
#include "scrobblemetadata.h"
|
||||
|
||||
ScrobbleMetadata::ScrobbleMetadata(const Song &song)
|
||||
: title(song.title()),
|
||||
album(song.album()),
|
||||
artist(song.artist()),
|
||||
albumartist(song.albumartist()),
|
||||
track(song.track()),
|
||||
grouping(song.grouping()),
|
||||
musicbrainz_album_artist_id(song.musicbrainz_album_artist_id()),
|
||||
musicbrainz_artist_id(song.musicbrainz_artist_id()),
|
||||
musicbrainz_original_artist_id(song.musicbrainz_original_artist_id()),
|
||||
musicbrainz_album_id(song.musicbrainz_album_id()),
|
||||
musicbrainz_original_album_id(song.musicbrainz_original_album_id()),
|
||||
musicbrainz_recording_id(song.musicbrainz_recording_id()),
|
||||
musicbrainz_track_id(song.musicbrainz_track_id()),
|
||||
musicbrainz_disc_id(song.musicbrainz_disc_id()),
|
||||
musicbrainz_release_group_id(song.musicbrainz_release_group_id()),
|
||||
musicbrainz_work_id(song.musicbrainz_work_id()),
|
||||
length_nanosec(song.length_nanosec()) {}
|
54
src/scrobbler/scrobblemetadata.h
Normal file
54
src/scrobbler/scrobblemetadata.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* 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 SCROBBLEMETADATA_H
|
||||
#define SCROBBLEMETADATA_H
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QString>
|
||||
|
||||
#include "core/song.h"
|
||||
|
||||
class ScrobbleMetadata {
|
||||
public:
|
||||
explicit ScrobbleMetadata(const Song &song = Song());
|
||||
|
||||
QString title;
|
||||
QString album;
|
||||
QString artist;
|
||||
QString albumartist;
|
||||
int track;
|
||||
QString grouping;
|
||||
QString musicbrainz_album_artist_id; // artist_mbids
|
||||
QString musicbrainz_artist_id; // artist_mbids
|
||||
QString musicbrainz_original_artist_id; // artist_mbids
|
||||
QString musicbrainz_album_id; // release_mbid
|
||||
QString musicbrainz_original_album_id; // release_mbid
|
||||
QString musicbrainz_recording_id; // recording_mbid
|
||||
QString musicbrainz_track_id; // track_mbid
|
||||
QString musicbrainz_disc_id;
|
||||
QString musicbrainz_release_group_id; // release_group_mbid
|
||||
QString musicbrainz_work_id; // work_mbids
|
||||
qint64 length_nanosec;
|
||||
|
||||
QString effective_albumartist() const { return albumartist.isEmpty() ? artist : albumartist; }
|
||||
|
||||
};
|
||||
|
||||
#endif // SCROBBLEMETADATA_H
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -70,7 +70,9 @@ void ScrobblerCache::ReadCache() {
|
||||
if (!result) return;
|
||||
|
||||
QTextStream stream(&file);
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
stream.setEncoding(QStringConverter::Encoding::Utf8);
|
||||
#else
|
||||
stream.setCodec("UTF-8");
|
||||
#endif
|
||||
QString data = stream.readAll();
|
||||
@ -120,32 +122,70 @@ void ScrobblerCache::ReadCache() {
|
||||
QJsonObject json_obj_track = value.toObject();
|
||||
if (
|
||||
!json_obj_track.contains("timestamp") ||
|
||||
!json_obj_track.contains("song") ||
|
||||
!json_obj_track.contains("album") ||
|
||||
!json_obj_track.contains("artist") ||
|
||||
!json_obj_track.contains("albumartist") ||
|
||||
!json_obj_track.contains("album") ||
|
||||
!json_obj_track.contains("title") ||
|
||||
!json_obj_track.contains("track") ||
|
||||
!json_obj_track.contains("duration")
|
||||
!json_obj_track.contains("albumartist") ||
|
||||
!json_obj_track.contains("length_nanosec")
|
||||
) {
|
||||
qLog(Error) << "Scrobbler cache JSON tracks array value is missing data.";
|
||||
qLog(Debug) << value;
|
||||
continue;
|
||||
}
|
||||
|
||||
ScrobbleMetadata metadata;
|
||||
quint64 timestamp = json_obj_track["timestamp"].toVariant().toULongLong();
|
||||
QString artist = json_obj_track["artist"].toString();
|
||||
QString album = json_obj_track["album"].toString();
|
||||
QString song = json_obj_track["song"].toString();
|
||||
QString albumartist = json_obj_track["albumartist"].toString();
|
||||
int track = json_obj_track["track"].toInt();
|
||||
qint64 duration = json_obj_track["duration"].toVariant().toLongLong();
|
||||
metadata.artist = json_obj_track["artist"].toString();
|
||||
metadata.album = json_obj_track["album"].toString();
|
||||
metadata.title = json_obj_track["title"].toString();
|
||||
metadata.track = json_obj_track["track"].toInt();
|
||||
metadata.albumartist = json_obj_track["albumartist"].toString();
|
||||
metadata.length_nanosec = json_obj_track["length_nanosec"].toVariant().toLongLong();
|
||||
|
||||
if (timestamp <= 0 || artist.isEmpty() || song.isEmpty() || duration <= 0) {
|
||||
qLog(Error) << "Invalid cache data" << "for song" << song;
|
||||
if (timestamp <= 0 || metadata.artist.isEmpty() || metadata.title.isEmpty() || metadata.length_nanosec <= 0) {
|
||||
qLog(Error) << "Invalid cache data" << "for song" << metadata.title;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (json_obj_track.contains("grouping")) {
|
||||
metadata.grouping = json_obj_track["grouping"].toString();
|
||||
}
|
||||
|
||||
if (json_obj_track.contains("musicbrainz_album_artist_id")) {
|
||||
metadata.musicbrainz_album_artist_id = json_obj_track["musicbrainz_album_artist_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_artist_id")) {
|
||||
metadata.musicbrainz_artist_id = json_obj_track["musicbrainz_artist_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_original_artist_id")) {
|
||||
metadata.musicbrainz_original_artist_id = json_obj_track["musicbrainz_original_artist_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_album_id")) {
|
||||
metadata.musicbrainz_album_id = json_obj_track["musicbrainz_album_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_original_album_id")) {
|
||||
metadata.musicbrainz_original_album_id = json_obj_track["musicbrainz_original_album_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_recording_id")) {
|
||||
metadata.musicbrainz_recording_id = json_obj_track["musicbrainz_recording_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_track_id")) {
|
||||
metadata.musicbrainz_track_id = json_obj_track["musicbrainz_track_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_disc_id")) {
|
||||
metadata.musicbrainz_disc_id = json_obj_track["musicbrainz_disc_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_release_group_id")) {
|
||||
metadata.musicbrainz_release_group_id = json_obj_track["musicbrainz_release_group_id"].toString();
|
||||
}
|
||||
if (json_obj_track.contains("musicbrainz_work_id")) {
|
||||
metadata.musicbrainz_work_id = json_obj_track["musicbrainz_work_id"].toString();
|
||||
}
|
||||
|
||||
if (scrobbler_cache_.contains(timestamp)) continue;
|
||||
scrobbler_cache_.insert(timestamp, std::make_shared<ScrobblerCacheItem>(artist, album, song, albumartist, track, duration, timestamp));
|
||||
std::shared_ptr<ScrobblerCacheItem> cache_item = std::make_shared<ScrobblerCacheItem>(metadata, timestamp);
|
||||
scrobbler_cache_.insert(timestamp, cache_item);
|
||||
|
||||
}
|
||||
|
||||
@ -164,18 +204,27 @@ void ScrobblerCache::WriteCache() {
|
||||
}
|
||||
|
||||
QJsonArray array;
|
||||
|
||||
QHash <quint64, std::shared_ptr<ScrobblerCacheItem>> ::iterator i;
|
||||
for (i = scrobbler_cache_.begin(); i != scrobbler_cache_.end(); ++i) {
|
||||
ScrobblerCacheItemPtr item = i.value();
|
||||
for (QHash <quint64, std::shared_ptr<ScrobblerCacheItem>> ::iterator i = scrobbler_cache_.begin(); i != scrobbler_cache_.end(); ++i) {
|
||||
ScrobblerCacheItemPtr cache_item = i.value();
|
||||
QJsonObject object;
|
||||
object.insert("timestamp", QJsonValue::fromVariant(item->timestamp_));
|
||||
object.insert("artist", QJsonValue::fromVariant(item->artist_));
|
||||
object.insert("album", QJsonValue::fromVariant(item->album_));
|
||||
object.insert("song", QJsonValue::fromVariant(item->song_));
|
||||
object.insert("albumartist", QJsonValue::fromVariant(item->albumartist_));
|
||||
object.insert("track", QJsonValue::fromVariant(item->track_));
|
||||
object.insert("duration", QJsonValue::fromVariant(item->duration_));
|
||||
object.insert("timestamp", QJsonValue::fromVariant(cache_item->timestamp));
|
||||
object.insert("artist", QJsonValue::fromVariant(cache_item->metadata.artist));
|
||||
object.insert("album", QJsonValue::fromVariant(cache_item->metadata.album));
|
||||
object.insert("title", QJsonValue::fromVariant(cache_item->metadata.title));
|
||||
object.insert("track", QJsonValue::fromVariant(cache_item->metadata.track));
|
||||
object.insert("albumartist", QJsonValue::fromVariant(cache_item->metadata.albumartist));
|
||||
object.insert("grouping", QJsonValue::fromVariant(cache_item->metadata.grouping));
|
||||
object.insert("musicbrainz_album_artist_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_album_artist_id));
|
||||
object.insert("musicbrainz_artist_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_artist_id));
|
||||
object.insert("musicbrainz_original_artist_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_original_artist_id));
|
||||
object.insert("musicbrainz_album_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_album_id));
|
||||
object.insert("musicbrainz_original_album_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_original_album_id));
|
||||
object.insert("musicbrainz_recording_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_recording_id));
|
||||
object.insert("musicbrainz_track_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_track_id));
|
||||
object.insert("musicbrainz_disc_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_disc_id));
|
||||
object.insert("musicbrainz_release_group_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_release_group_id));
|
||||
object.insert("musicbrainz_work_id", QJsonValue::fromVariant(cache_item->metadata.musicbrainz_work_id));
|
||||
object.insert("length_nanosec", QJsonValue::fromVariant(cache_item->metadata.length_nanosec));
|
||||
array.append(QJsonValue::fromVariant(object));
|
||||
}
|
||||
|
||||
@ -190,7 +239,9 @@ void ScrobblerCache::WriteCache() {
|
||||
return;
|
||||
}
|
||||
QTextStream stream(&file);
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
stream.setEncoding(QStringConverter::Encoding::Utf8);
|
||||
#else
|
||||
stream.setCodec("UTF-8");
|
||||
#endif
|
||||
stream << doc.toJson();
|
||||
@ -202,21 +253,15 @@ ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 timest
|
||||
|
||||
if (scrobbler_cache_.contains(timestamp)) return nullptr;
|
||||
|
||||
QString album = song.album();
|
||||
QString title = song.title();
|
||||
ScrobblerCacheItemPtr cache_item = std::make_shared<ScrobblerCacheItem>(ScrobbleMetadata(song), timestamp);
|
||||
|
||||
album.remove(Song::kAlbumRemoveDisc)
|
||||
.remove(Song::kAlbumRemoveMisc);
|
||||
title.remove(Song::kTitleRemoveMisc);
|
||||
|
||||
ScrobblerCacheItemPtr item = std::make_shared<ScrobblerCacheItem>(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
|
||||
scrobbler_cache_.insert(timestamp, item);
|
||||
scrobbler_cache_.insert(timestamp, cache_item);
|
||||
|
||||
if (loaded_ && !timer_flush_->isActive()) {
|
||||
timer_flush_->start();
|
||||
}
|
||||
|
||||
return item;
|
||||
return cache_item;
|
||||
|
||||
}
|
||||
|
||||
@ -239,7 +284,7 @@ void ScrobblerCache::Remove(const quint64 hash) {
|
||||
}
|
||||
|
||||
void ScrobblerCache::Remove(ScrobblerCacheItemPtr item) {
|
||||
scrobbler_cache_.remove(item->timestamp_);
|
||||
scrobbler_cache_.remove(item->timestamp);
|
||||
}
|
||||
|
||||
void ScrobblerCache::ClearSent(const QList<quint64> &list) {
|
||||
@ -247,7 +292,7 @@ void ScrobblerCache::ClearSent(const QList<quint64> &list) {
|
||||
for (const quint64 timestamp : list) {
|
||||
if (!scrobbler_cache_.contains(timestamp)) continue;
|
||||
ScrobblerCacheItemPtr item = scrobbler_cache_.value(timestamp);
|
||||
item->sent_ = false;
|
||||
item->sent = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -49,7 +49,7 @@ class ScrobblerCache : public QObject {
|
||||
void Remove(const quint64 hash);
|
||||
void Remove(ScrobblerCacheItemPtr item);
|
||||
int Count() const { return scrobbler_cache_.size(); };
|
||||
QList<ScrobblerCacheItemPtr> List() const { return scrobbler_cache_.values(); }
|
||||
ScrobblerCacheItemPtrList List() const { return scrobbler_cache_.values(); }
|
||||
void ClearSent(const QList<quint64> &list);
|
||||
void Flush(const QList<quint64> &list);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -20,17 +20,11 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QString>
|
||||
|
||||
#include "scrobblercacheitem.h"
|
||||
#include "scrobblemetadata.h"
|
||||
|
||||
ScrobblerCacheItem::ScrobblerCacheItem(const QString &artist, const QString &album, const QString &song, const QString &albumartist, const int track, const qint64 duration, const quint64 timestamp, QObject *parent)
|
||||
: QObject(parent),
|
||||
artist_(artist),
|
||||
album_(album),
|
||||
song_(song),
|
||||
albumartist_(albumartist),
|
||||
track_(track),
|
||||
duration_(duration),
|
||||
timestamp_(timestamp),
|
||||
sent_(false) {}
|
||||
ScrobblerCacheItem::ScrobblerCacheItem(const ScrobbleMetadata &_metadata, const quint64 _timestamp)
|
||||
: metadata(_metadata),
|
||||
timestamp(_timestamp),
|
||||
sent(false) {}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -25,33 +25,23 @@
|
||||
#include <memory>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class ScrobblerCacheItem : public QObject {
|
||||
Q_OBJECT
|
||||
#include "scrobblemetadata.h"
|
||||
|
||||
class ScrobblerCacheItem {
|
||||
|
||||
public:
|
||||
explicit ScrobblerCacheItem(const QString &artist, const QString &album, const QString &song, const QString &albumartist, const int track, const qint64 duration, const quint64 timestamp, QObject *parent = nullptr);
|
||||
|
||||
QString effective_albumartist() const { return albumartist_.isEmpty() ? artist_ : albumartist_; }
|
||||
|
||||
public:
|
||||
QString artist_;
|
||||
QString album_;
|
||||
QString song_;
|
||||
QString albumartist_;
|
||||
int track_;
|
||||
qint64 duration_;
|
||||
quint64 timestamp_;
|
||||
bool sent_;
|
||||
explicit ScrobblerCacheItem(const ScrobbleMetadata &_metadata, const quint64 _timestamp);
|
||||
|
||||
ScrobbleMetadata metadata;
|
||||
quint64 timestamp;
|
||||
bool sent;
|
||||
};
|
||||
|
||||
using ScrobblerCacheItemPtr = std::shared_ptr<ScrobblerCacheItem>;
|
||||
using ScrobblerCacheItemList = QList<ScrobblerCacheItemPtr>;
|
||||
using ScrobblerCacheItemPtrList = QList<ScrobblerCacheItemPtr>;
|
||||
|
||||
Q_DECLARE_METATYPE(ScrobblerCacheItemPtr)
|
||||
Q_DECLARE_METATYPE(ScrobblerCacheItemList)
|
||||
Q_DECLARE_METATYPE(ScrobblerCacheItemPtrList)
|
||||
|
||||
#endif // SCROBBLERCACHEITEM_H
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -28,6 +28,8 @@
|
||||
|
||||
#include "scrobblerservice.h"
|
||||
|
||||
#include "core/song.h"
|
||||
|
||||
ScrobblerService::ScrobblerService(const QString &name, Application *app, QObject *parent) : QObject(parent), name_(name) {
|
||||
|
||||
Q_UNUSED(app);
|
||||
@ -53,11 +55,24 @@ QJsonObject ScrobblerService::ExtractJsonObj(const QByteArray &data, const bool
|
||||
}
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (json_obj.isEmpty()) {
|
||||
if (!ignore_empty)
|
||||
if (!ignore_empty) {
|
||||
Error("Received empty Json object.", json_doc);
|
||||
}
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
return json_obj;
|
||||
|
||||
}
|
||||
|
||||
QString ScrobblerService::StripAlbum(QString album) const {
|
||||
|
||||
return album.remove(Song::kAlbumRemoveDisc).remove(Song::kAlbumRemoveMisc);
|
||||
|
||||
}
|
||||
|
||||
QString ScrobblerService::StripTitle(QString title) const {
|
||||
|
||||
return title.remove(Song::kTitleRemoveMisc);
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -30,8 +30,9 @@
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "core/song.h"
|
||||
|
||||
class Application;
|
||||
class Song;
|
||||
|
||||
class ScrobblerService : public QObject {
|
||||
Q_OBJECT
|
||||
@ -50,17 +51,21 @@ class ScrobblerService : public QObject {
|
||||
virtual void ClearPlaying() = 0;
|
||||
virtual void Scrobble(const Song &song) = 0;
|
||||
virtual void Love() {}
|
||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||
|
||||
virtual void StartSubmit(const bool initial = false) = 0;
|
||||
virtual void Submitted() = 0;
|
||||
virtual bool IsSubmitted() const { return false; }
|
||||
|
||||
protected:
|
||||
using Param = QPair<QString, QString>;
|
||||
using EncodedParam = QPair<QByteArray, QByteArray>;
|
||||
using ParamList = QList<Param>;
|
||||
using EncodedParam = QPair<QByteArray, QByteArray>;
|
||||
|
||||
QJsonObject ExtractJsonObj(const QByteArray &data, const bool ignore_empty = false);
|
||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||
|
||||
QString StripAlbum(QString album) const;
|
||||
QString StripTitle(QString title) const;
|
||||
|
||||
public slots:
|
||||
virtual void Submit() = 0;
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -55,13 +55,15 @@
|
||||
#include "audioscrobbler.h"
|
||||
#include "scrobblerservice.h"
|
||||
#include "scrobblingapi20.h"
|
||||
#include "scrobblercache.h"
|
||||
#include "scrobblercacheitem.h"
|
||||
#include "scrobblemetadata.h"
|
||||
|
||||
const char *ScrobblingAPI20::kApiKey = "211990b4c96782c05d1536e7219eb56e";
|
||||
const char *ScrobblingAPI20::kSecret = "80fd738f49596e9709b1bf9319c444a8";
|
||||
const int ScrobblingAPI20::kScrobblesPerRequest = 50;
|
||||
|
||||
ScrobblingAPI20::ScrobblingAPI20(const QString &name, const QString &settings_group, const QString &auth_url, const QString &api_url, const bool batch, Application *app, QObject *parent)
|
||||
ScrobblingAPI20::ScrobblingAPI20(const QString &name, const QString &settings_group, const QString &auth_url, const QString &api_url, const bool batch, const QString &cache_file, Application *app, QObject *parent)
|
||||
: ScrobblerService(name, app, parent),
|
||||
name_(name),
|
||||
settings_group_(settings_group),
|
||||
@ -69,6 +71,8 @@ ScrobblingAPI20::ScrobblingAPI20(const QString &name, const QString &settings_gr
|
||||
api_url_(api_url),
|
||||
batch_(batch),
|
||||
app_(app),
|
||||
network_(new NetworkAccessManager(this)),
|
||||
cache_(new ScrobblerCache(cache_file, this)),
|
||||
server_(nullptr),
|
||||
enabled_(false),
|
||||
https_(false),
|
||||
@ -82,6 +86,9 @@ ScrobblingAPI20::ScrobblingAPI20(const QString &name, const QString &settings_gr
|
||||
timer_submit_.setSingleShot(true);
|
||||
QObject::connect(&timer_submit_, &QTimer::timeout, this, &ScrobblingAPI20::Submit);
|
||||
|
||||
ReloadSettings();
|
||||
LoadSession();
|
||||
|
||||
}
|
||||
|
||||
ScrobblingAPI20::~ScrobblingAPI20() {
|
||||
@ -230,7 +237,7 @@ void ScrobblingAPI20::RequestSession(const QString &token) {
|
||||
session_url_query.addQueryItem("method", "auth.getSession");
|
||||
session_url_query.addQueryItem("token", token);
|
||||
QString data_to_sign;
|
||||
for (const QPair<QString, QString> ¶m : session_url_query.queryItems()) {
|
||||
for (const Param ¶m : session_url_query.queryItems()) {
|
||||
data_to_sign += param.first + param.second;
|
||||
}
|
||||
data_to_sign += kSecret;
|
||||
@ -242,7 +249,7 @@ void ScrobblingAPI20::RequestSession(const QString &token) {
|
||||
|
||||
QNetworkRequest req(session_url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
QNetworkReply *reply = network()->get(req);
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() { AuthenticateReplyFinished(reply); });
|
||||
|
||||
@ -376,7 +383,7 @@ QNetworkReply *ScrobblingAPI20::CreateRequest(const ParamList &request_params) {
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
||||
QNetworkReply *reply = network()->post(req, query);
|
||||
QNetworkReply *reply = network_->post(req, query);
|
||||
replies_ << reply;
|
||||
|
||||
//qLog(Debug) << name_ << "Sending request" << url_query.toString(QUrl::FullyDecoded);
|
||||
@ -452,20 +459,13 @@ void ScrobblingAPI20::UpdateNowPlaying(const Song &song) {
|
||||
|
||||
if (!IsAuthenticated() || !song.is_metadata_good() || app_->scrobbler()->IsOffline()) return;
|
||||
|
||||
QString album = song.album();
|
||||
QString title = song.title();
|
||||
|
||||
album = album.remove(Song::kAlbumRemoveDisc)
|
||||
.remove(Song::kAlbumRemoveMisc);
|
||||
title = title.remove(Song::kTitleRemoveMisc);
|
||||
|
||||
ParamList params = ParamList()
|
||||
<< Param("method", "track.updateNowPlaying")
|
||||
<< Param("artist", prefer_albumartist_ ? song.effective_albumartist() : song.artist())
|
||||
<< Param("track", title);
|
||||
<< Param("track", StripTitle(song.title()));
|
||||
|
||||
if (!album.isEmpty()) {
|
||||
params << Param("album", album);
|
||||
if (!song.album().isEmpty()) {
|
||||
params << Param("album", StripAlbum(song.album()));
|
||||
}
|
||||
|
||||
if (!prefer_albumartist_ && !song.albumartist().isEmpty()) {
|
||||
@ -525,7 +525,7 @@ void ScrobblingAPI20::Scrobble(const Song &song) {
|
||||
|
||||
scrobbled_ = true;
|
||||
|
||||
cache()->Add(song, timestamp_);
|
||||
cache_->Add(song, timestamp_);
|
||||
|
||||
if (app_->scrobbler()->IsOffline()) return;
|
||||
|
||||
@ -541,7 +541,7 @@ void ScrobblingAPI20::Scrobble(const Song &song) {
|
||||
|
||||
void ScrobblingAPI20::StartSubmit(const bool initial) {
|
||||
|
||||
if (!submitted_ && cache()->Count() > 0) {
|
||||
if (!submitted_ && cache_->Count() > 0) {
|
||||
if (initial && (!batch_ || app_->scrobbler()->SubmitDelay() <= 0) && !submit_error_) {
|
||||
if (timer_submit_.isActive()) {
|
||||
timer_submit_.stop();
|
||||
@ -567,27 +567,27 @@ void ScrobblingAPI20::Submit() {
|
||||
|
||||
int i = 0;
|
||||
QList<quint64> list;
|
||||
QList<ScrobblerCacheItemPtr> items = cache()->List();
|
||||
for (ScrobblerCacheItemPtr item : items) { // clazy:exclude=range-loop-reference
|
||||
if (item->sent_) continue;
|
||||
item->sent_ = true;
|
||||
ScrobblerCacheItemPtrList items = cache_->List();
|
||||
for (ScrobblerCacheItemPtr item : items) {
|
||||
if (item->sent) continue;
|
||||
item->sent = true;
|
||||
if (!batch_) {
|
||||
SendSingleScrobble(item);
|
||||
continue;
|
||||
}
|
||||
list << item->timestamp_;
|
||||
params << Param(QString("%1[%2]").arg("artist").arg(i), prefer_albumartist_ ? item->effective_albumartist() : item->artist_);
|
||||
params << Param(QString("%1[%2]").arg("track").arg(i), item->song_);
|
||||
params << Param(QString("%1[%2]").arg("timestamp").arg(i), QString::number(item->timestamp_));
|
||||
params << Param(QString("%1[%2]").arg("duration").arg(i), QString::number(item->duration_ / kNsecPerSec));
|
||||
if (!item->album_.isEmpty()) {
|
||||
params << Param(QString("%1[%2]").arg("album").arg(i), item->album_);
|
||||
list << item->timestamp;
|
||||
params << Param(QString("%1[%2]").arg("artist").arg(i), prefer_albumartist_ ? item->metadata.effective_albumartist() : item->metadata.artist);
|
||||
params << Param(QString("%1[%2]").arg("track").arg(i), StripTitle(item->metadata.title));
|
||||
params << Param(QString("%1[%2]").arg("timestamp").arg(i), QString::number(item->timestamp));
|
||||
params << Param(QString("%1[%2]").arg("duration").arg(i), QString::number(item->metadata.length_nanosec / kNsecPerSec));
|
||||
if (!item->metadata.album.isEmpty()) {
|
||||
params << Param(QString("%1[%2]").arg("album").arg(i), StripAlbum(item->metadata.album));
|
||||
}
|
||||
if (!prefer_albumartist_ && !item->albumartist_.isEmpty()) {
|
||||
params << Param(QString("%1[%2]").arg("albumArtist").arg(i), item->albumartist_);
|
||||
if (!prefer_albumartist_ && !item->metadata.albumartist.isEmpty()) {
|
||||
params << Param(QString("%1[%2]").arg("albumArtist").arg(i), item->metadata.albumartist);
|
||||
}
|
||||
if (item->track_ > 0) {
|
||||
params << Param(QString("%1[%2]").arg("trackNumber").arg(i), QString::number(item->track_));
|
||||
if (item->metadata.track > 0) {
|
||||
params << Param(QString("%1[%2]").arg("trackNumber").arg(i), QString::number(item->metadata.track));
|
||||
}
|
||||
++i;
|
||||
if (i >= kScrobblesPerRequest) break;
|
||||
@ -613,7 +613,7 @@ void ScrobblingAPI20::ScrobbleRequestFinished(QNetworkReply *reply, const QList<
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
if (data.isEmpty()) {
|
||||
cache()->ClearSent(list);
|
||||
cache_->ClearSent(list);
|
||||
submit_error_ = true;
|
||||
StartSubmit();
|
||||
return;
|
||||
@ -621,7 +621,7 @@ void ScrobblingAPI20::ScrobbleRequestFinished(QNetworkReply *reply, const QList<
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
cache()->ClearSent(list);
|
||||
cache_->ClearSent(list);
|
||||
submit_error_ = true;
|
||||
StartSubmit();
|
||||
return;
|
||||
@ -632,13 +632,13 @@ void ScrobblingAPI20::ScrobbleRequestFinished(QNetworkReply *reply, const QList<
|
||||
QString error_message = json_obj["message"].toString();
|
||||
QString error_reason = QString("%1 (%2)").arg(error_message).arg(error_code);
|
||||
Error(error_reason);
|
||||
cache()->ClearSent(list);
|
||||
cache_->ClearSent(list);
|
||||
submit_error_ = true;
|
||||
StartSubmit();
|
||||
return;
|
||||
}
|
||||
|
||||
cache()->Flush(list);
|
||||
cache_->Flush(list);
|
||||
submit_error_ = false;
|
||||
|
||||
if (!json_obj.contains("scrobbles")) {
|
||||
@ -713,7 +713,7 @@ void ScrobblingAPI20::ScrobbleRequestFinished(QNetworkReply *reply, const QList<
|
||||
return;
|
||||
}
|
||||
|
||||
for (const QJsonValueRef value : array_scrobble) { // clazy:exclude=range-loop-detach
|
||||
for (const QJsonValueRef value : array_scrobble) {
|
||||
|
||||
if (!value.isObject()) {
|
||||
Error("Json scrobbles scrobble array value is not an object.");
|
||||
@ -783,23 +783,23 @@ void ScrobblingAPI20::SendSingleScrobble(ScrobblerCacheItemPtr item) {
|
||||
|
||||
ParamList params = ParamList()
|
||||
<< Param("method", "track.scrobble")
|
||||
<< Param("artist", prefer_albumartist_ ? item->effective_albumartist() : item->artist_)
|
||||
<< Param("track", item->song_)
|
||||
<< Param("timestamp", QString::number(item->timestamp_))
|
||||
<< Param("duration", QString::number(item->duration_ / kNsecPerSec));
|
||||
<< Param("artist", prefer_albumartist_ ? item->metadata.effective_albumartist() : item->metadata.artist)
|
||||
<< Param("track", StripTitle(item->metadata.title))
|
||||
<< Param("timestamp", QString::number(item->timestamp))
|
||||
<< Param("duration", QString::number(item->metadata.length_nanosec / kNsecPerSec));
|
||||
|
||||
if (!item->album_.isEmpty()) {
|
||||
params << Param("album", item->album_);
|
||||
if (!item->metadata.album.isEmpty()) {
|
||||
params << Param("album", StripAlbum(item->metadata.album));
|
||||
}
|
||||
if (!prefer_albumartist_ && !item->albumartist_.isEmpty()) {
|
||||
params << Param("albumArtist", item->albumartist_);
|
||||
if (!prefer_albumartist_ && !item->metadata.albumartist.isEmpty()) {
|
||||
params << Param("albumArtist", item->metadata.albumartist);
|
||||
}
|
||||
if (item->track_ > 0) {
|
||||
params << Param("trackNumber", QString::number(item->track_));
|
||||
if (item->metadata.track > 0) {
|
||||
params << Param("trackNumber", QString::number(item->metadata.track));
|
||||
}
|
||||
|
||||
QNetworkReply *reply = CreateRequest(params);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, item]() { SingleScrobbleRequestFinished(reply, item->timestamp_); });
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, item]() { SingleScrobbleRequestFinished(reply, item->timestamp); });
|
||||
|
||||
}
|
||||
|
||||
@ -810,7 +810,7 @@ void ScrobblingAPI20::SingleScrobbleRequestFinished(QNetworkReply *reply, const
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
ScrobblerCacheItemPtr item = cache()->Get(timestamp);
|
||||
ScrobblerCacheItemPtr item = cache_->Get(timestamp);
|
||||
if (!item) {
|
||||
Error(QString("Received reply for non-existing cache entry %1.").arg(timestamp));
|
||||
return;
|
||||
@ -818,13 +818,13 @@ void ScrobblingAPI20::SingleScrobbleRequestFinished(QNetworkReply *reply, const
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
if (data.isEmpty()) {
|
||||
item->sent_ = false;
|
||||
item->sent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
item->sent_ = false;
|
||||
item->sent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -833,17 +833,17 @@ void ScrobblingAPI20::SingleScrobbleRequestFinished(QNetworkReply *reply, const
|
||||
QString error_message = json_obj["message"].toString();
|
||||
QString error_reason = QString("%1 (%2)").arg(error_message).arg(error_code);
|
||||
Error(error_reason);
|
||||
item->sent_ = false;
|
||||
item->sent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("scrobbles")) {
|
||||
Error("Json reply from server is missing scrobbles.", json_obj);
|
||||
item->sent_ = false;
|
||||
item->sent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
cache()->Remove(timestamp);
|
||||
cache_->Remove(timestamp);
|
||||
item = nullptr;
|
||||
|
||||
QJsonValue value_scrobbles = json_obj["scrobbles"];
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -45,7 +45,7 @@ class ScrobblingAPI20 : public ScrobblerService {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ScrobblingAPI20(const QString &name, const QString &settings_group, const QString &auth_url, const QString &api_url, const bool batch, Application *app, QObject *parent = nullptr);
|
||||
explicit ScrobblingAPI20(const QString &name, const QString &settings_group, const QString &auth_url, const QString &api_url, const bool batch, const QString &cache_file, Application *app, QObject *parent = nullptr);
|
||||
~ScrobblingAPI20() override;
|
||||
|
||||
static const char *kApiKey;
|
||||
@ -53,9 +53,6 @@ class ScrobblingAPI20 : public ScrobblerService {
|
||||
void ReloadSettings() override;
|
||||
void LoadSession();
|
||||
|
||||
virtual NetworkAccessManager *network() const = 0;
|
||||
virtual ScrobblerCache *cache() const = 0;
|
||||
|
||||
bool IsEnabled() const override { return enabled_; }
|
||||
bool IsUseHTTPS() const { return https_; }
|
||||
bool IsAuthenticated() const override { return !username_.isEmpty() && !session_key_.isEmpty(); }
|
||||
@ -76,7 +73,7 @@ class ScrobblingAPI20 : public ScrobblerService {
|
||||
void AuthenticationComplete(bool success, QString error = QString());
|
||||
|
||||
public slots:
|
||||
void WriteCache() override { cache()->WriteCache(); }
|
||||
void WriteCache() override { cache_->WriteCache(); }
|
||||
|
||||
private slots:
|
||||
void RedirectArrived();
|
||||
@ -133,6 +130,7 @@ class ScrobblingAPI20 : public ScrobblerService {
|
||||
void StartSubmit(const bool initial = false) override;
|
||||
void CheckScrobblePrevSong();
|
||||
|
||||
protected:
|
||||
QString name_;
|
||||
QString settings_group_;
|
||||
QString auth_url_;
|
||||
@ -140,6 +138,8 @@ class ScrobblingAPI20 : public ScrobblerService {
|
||||
bool batch_;
|
||||
|
||||
Application *app_;
|
||||
NetworkAccessManager *network_;
|
||||
ScrobblerCache *cache_;
|
||||
LocalRedirectServer *server_;
|
||||
|
||||
bool enabled_;
|
||||
|
Loading…
Reference in New Issue
Block a user