2018-12-23 18:54:27 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-12-23 18:54:27 +01:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-06-25 22:51:00 +02:00
|
|
|
#include <memory>
|
2021-01-30 21:48:45 +01:00
|
|
|
#include <functional>
|
2021-06-20 19:04:08 +02:00
|
|
|
#include <chrono>
|
2020-05-10 14:59:04 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QStandardPaths>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QHash>
|
|
|
|
#include <QVariant>
|
2018-12-23 18:54:27 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QTextStream>
|
2021-01-30 21:48:45 +01:00
|
|
|
#include <QTimer>
|
2018-12-23 18:54:27 +01:00
|
|
|
#include <QJsonDocument>
|
2018-12-26 15:05:32 +01:00
|
|
|
#include <QJsonValue>
|
2018-12-23 18:54:27 +01:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtDebug>
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
#include "scrobblercache.h"
|
|
|
|
#include "scrobblercacheitem.h"
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
timer_flush_(new QTimer(this)),
|
|
|
|
filename_(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + filename),
|
|
|
|
loaded_(false) {
|
2021-01-30 21:48:45 +01:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
ReadCache();
|
|
|
|
loaded_ = true;
|
2021-01-30 21:48:45 +01:00
|
|
|
|
|
|
|
timer_flush_->setSingleShot(true);
|
2021-06-20 19:04:08 +02:00
|
|
|
timer_flush_->setInterval(10min);
|
2021-01-30 21:48:45 +01:00
|
|
|
QObject::connect(timer_flush_, &QTimer::timeout, this, &ScrobblerCache::WriteCache);
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
ScrobblerCache::~ScrobblerCache() {
|
|
|
|
scrobbler_cache_.clear();
|
|
|
|
}
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
void ScrobblerCache::ReadCache() {
|
|
|
|
|
|
|
|
QFile file(filename_);
|
|
|
|
bool result = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
|
|
|
if (!result) return;
|
|
|
|
|
|
|
|
QTextStream stream(&file);
|
2020-07-18 04:27:21 +02:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
2018-12-23 18:54:27 +01:00
|
|
|
stream.setCodec("UTF-8");
|
2020-07-18 04:27:21 +02:00
|
|
|
#endif
|
2018-12-23 18:54:27 +01:00
|
|
|
QString data = stream.readAll();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (data.isEmpty()) return;
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data.toUtf8(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
qLog(Error) << "Scrobbler cache is missing JSON data.";
|
|
|
|
return;
|
|
|
|
}
|
2020-10-02 19:27:47 +02:00
|
|
|
if (json_doc.isEmpty()) {
|
2018-12-23 18:54:27 +01:00
|
|
|
qLog(Error) << "Scrobbler cache has empty JSON document.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_doc.isObject()) {
|
|
|
|
qLog(Error) << "Scrobbler cache JSON document is not an object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
|
|
|
qLog(Error) << "Scrobbler cache has empty JSON object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_obj.contains("tracks")) {
|
|
|
|
qLog(Error) << "Scrobbler cache is missing JSON tracks.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonValue json_tracks = json_obj["tracks"];
|
|
|
|
if (!json_tracks.isArray()) {
|
|
|
|
qLog(Error) << "Scrobbler cache JSON tracks is not an array.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray json_array = json_tracks.toArray();
|
|
|
|
if (json_array.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:10:43 +01:00
|
|
|
for (const QJsonValueRef value : json_array) {
|
2018-12-23 18:54:27 +01:00
|
|
|
if (!value.isObject()) {
|
|
|
|
qLog(Error) << "Scrobbler cache JSON tracks array value is not an object.";
|
|
|
|
qLog(Debug) << value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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("track") ||
|
|
|
|
!json_obj_track.contains("duration")
|
|
|
|
) {
|
|
|
|
qLog(Error) << "Scrobbler cache JSON tracks array value is missing data.";
|
|
|
|
qLog(Debug) << value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-08-03 21:50:26 +02:00
|
|
|
if (timestamp <= 0 || artist.isEmpty() || song.isEmpty() || duration <= 0) {
|
2018-12-23 18:54:27 +01:00
|
|
|
qLog(Error) << "Invalid cache data" << "for song" << song;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (scrobbler_cache_.contains(timestamp)) continue;
|
2020-05-10 14:59:04 +02:00
|
|
|
scrobbler_cache_.insert(timestamp, std::make_shared<ScrobblerCacheItem>(artist, album, song, albumartist, track, duration, timestamp));
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrobblerCache::WriteCache() {
|
|
|
|
|
|
|
|
if (!loaded_) return;
|
|
|
|
|
|
|
|
qLog(Debug) << "Writing scrobbler cache file" << filename_;
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
if (scrobbler_cache_.isEmpty()) {
|
|
|
|
QFile file(filename_);
|
|
|
|
if (file.exists()) file.remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
QJsonArray array;
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
QHash <quint64, std::shared_ptr<ScrobblerCacheItem>> ::iterator i;
|
2021-08-23 21:21:08 +02:00
|
|
|
for (i = scrobbler_cache_.begin(); i != scrobbler_cache_.end(); ++i) {
|
2020-05-10 14:59:04 +02:00
|
|
|
ScrobblerCacheItemPtr item = i.value();
|
2018-12-23 18:54:27 +01:00
|
|
|
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_));
|
|
|
|
array.append(QJsonValue::fromVariant(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject object;
|
|
|
|
object.insert("tracks", array);
|
|
|
|
QJsonDocument doc(object);
|
|
|
|
|
|
|
|
QFile file(filename_);
|
|
|
|
bool result = file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
if (!result) {
|
|
|
|
qLog(Error) << "Unable to open scrobbler cache file" << filename_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTextStream stream(&file);
|
2020-07-18 04:27:21 +02:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
2018-12-23 18:54:27 +01:00
|
|
|
stream.setCodec("UTF-8");
|
2020-07-18 04:27:21 +02:00
|
|
|
#endif
|
2018-12-23 18:54:27 +01:00
|
|
|
stream << doc.toJson();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 timestamp) {
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
if (scrobbler_cache_.contains(timestamp)) return nullptr;
|
|
|
|
|
2018-12-26 13:33:56 +01:00
|
|
|
QString album = song.album();
|
2019-01-22 22:49:48 +01:00
|
|
|
QString title = song.title();
|
2018-12-26 13:33:56 +01:00
|
|
|
|
2019-01-22 22:49:48 +01:00
|
|
|
album.remove(Song::kAlbumRemoveDisc);
|
|
|
|
album.remove(Song::kAlbumRemoveMisc);
|
|
|
|
title.remove(Song::kTitleRemoveMisc);
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
ScrobblerCacheItemPtr item = std::make_shared<ScrobblerCacheItem>(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
|
2018-12-23 18:54:27 +01:00
|
|
|
scrobbler_cache_.insert(timestamp, item);
|
|
|
|
|
2021-01-30 21:48:45 +01:00
|
|
|
if (loaded_ && !timer_flush_->isActive()) {
|
|
|
|
timer_flush_->start();
|
|
|
|
}
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
return item;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
ScrobblerCacheItemPtr ScrobblerCache::Get(const quint64 hash) {
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
if (scrobbler_cache_.contains(hash)) { return scrobbler_cache_.value(hash); }
|
|
|
|
else return nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrobblerCache::Remove(const quint64 hash) {
|
|
|
|
|
|
|
|
if (!scrobbler_cache_.contains(hash)) {
|
|
|
|
qLog(Error) << "Tried to remove non-existing hash" << hash;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
scrobbler_cache_.remove(hash);
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
void ScrobblerCache::Remove(ScrobblerCacheItemPtr item) {
|
|
|
|
scrobbler_cache_.remove(item->timestamp_);
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
void ScrobblerCache::ClearSent(const QList<quint64> &list) {
|
|
|
|
|
|
|
|
for (const quint64 timestamp : list) {
|
2018-12-23 18:54:27 +01:00
|
|
|
if (!scrobbler_cache_.contains(timestamp)) continue;
|
2020-09-24 17:02:43 +02:00
|
|
|
ScrobblerCacheItemPtr item = scrobbler_cache_.value(timestamp);
|
2018-12-23 18:54:27 +01:00
|
|
|
item->sent_ = false;
|
|
|
|
}
|
2020-05-10 14:59:04 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
void ScrobblerCache::Flush(const QList<quint64> &list) {
|
2018-12-23 18:54:27 +01:00
|
|
|
|
2020-05-10 14:59:04 +02:00
|
|
|
for (const quint64 timestamp : list) {
|
2018-12-23 18:54:27 +01:00
|
|
|
if (!scrobbler_cache_.contains(timestamp)) continue;
|
2020-05-10 14:59:04 +02:00
|
|
|
scrobbler_cache_.remove(timestamp);
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
2021-01-30 21:48:45 +01:00
|
|
|
|
|
|
|
if (!timer_flush_->isActive()) {
|
|
|
|
timer_flush_->start();
|
|
|
|
}
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
}
|