Files
strawberry/src/scrobbler/lastfmimport.h
copilot-swe-agent[bot] 4c8103ef6d Add custom API key support and retry logic for Last.fm import
- Add API key field to Last.fm settings UI with helpful info text
- Store and load custom API key from settings
- Use custom API key in lastfmimport if provided, fall back to default
- Implement exponential backoff retry logic (up to 5 retries)
- Retry on HTTP 500/503 errors with increasing delays (5s, 10s, 20s, 40s, 80s)
- Add retry count tracking to request structures

Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com>
2026-01-03 21:24:41 +00:00

120 lines
3.7 KiB
C++

/*
* Strawberry Music Player
* Copyright 2020-2025, 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 LASTFMIMPORT_H
#define LASTFMIMPORT_H
#include "config.h"
#include <QList>
#include <QVariant>
#include <QByteArray>
#include <QString>
#include <QQueue>
#include <QDateTime>
#include "core/jsonbaserequest.h"
#include "includes/shared_ptr.h"
#include "core/jsonbaserequest.h"
class QTimer;
class QNetworkReply;
class NetworkAccessManager;
class LastFMImport : public JsonBaseRequest {
Q_OBJECT
public:
explicit LastFMImport(const SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
~LastFMImport() override;
QString service_name() const override { return QStringLiteral("LastFMImport"); }
bool authentication_required() const override { return false; }
bool authenticated() const override { return false; }
bool use_authorization_header() const override { return false; }
QByteArray authorization_header() const override { return QByteArray(); }
void ReloadSettings();
void ImportData(const bool lastplayed = true, const bool playcount = true);
void AbortAll();
private:
using Param = QPair<QString, QString>;
using ParamList = QList<Param>;
struct GetRecentTracksRequest {
explicit GetRecentTracksRequest(const int _page, const int _retry_count = 0) : page(_page), retry_count(_retry_count) {}
int page;
int retry_count;
};
struct GetTopTracksRequest {
explicit GetTopTracksRequest(const int _page, const int _retry_count = 0) : page(_page), retry_count(_retry_count) {}
int page;
int retry_count;
};
private:
QNetworkReply *CreateRequest(const ParamList &request_params);
JsonObjectResult ParseJsonObject(QNetworkReply *reply);
void AddGetRecentTracksRequest(const int page = 0);
void AddGetTopTracksRequest(const int page = 0);
void SendGetRecentTracksRequest(GetRecentTracksRequest request);
void SendGetTopTracksRequest(GetTopTracksRequest request);
void Error(const QString &error, const QVariant &debug = QVariant()) override;
void UpdateTotalCheck();
void UpdateProgressCheck();
void FinishCheck();
Q_SIGNALS:
void UpdatePlayCount(const QString&, const QString&, const int, const bool = false);
void UpdateLastPlayed(const QString&, const QString&, const QString&, const qint64);
void UpdateTotal(const int, const int);
void UpdateProgress(const int, const int);
void Finished();
void FinishedWithError(const QString &error);
private Q_SLOTS:
void FlushRequests();
void GetRecentTracksRequestFinished(QNetworkReply *reply, GetRecentTracksRequest request);
void GetTopTracksRequestFinished(QNetworkReply *reply, GetTopTracksRequest request);
private:
SharedPtr<NetworkAccessManager> network_;
QTimer *timer_flush_requests_;
QString username_;
QString api_key_;
bool lastplayed_;
bool playcount_;
int playcount_total_;
int lastplayed_total_;
int playcount_received_;
int lastplayed_received_;
QQueue<GetRecentTracksRequest> recent_tracks_requests_;
QQueue<GetTopTracksRequest> top_tracks_requests_;
};
#endif // LASTFMIMPORT_H