2019-08-12 18:11:01 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2019-08-12 18:11:01 +02: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"
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2021-08-12 23:00:10 +02:00
|
|
|
#include "core/networkaccessmanager.h"
|
2019-08-12 18:11:01 +02:00
|
|
|
#include "jsonlyricsprovider.h"
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
JsonLyricsProvider::JsonLyricsProvider(const QString &name, const bool enabled, const bool authentication_required, SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider(name, enabled, authentication_required, network, parent) {}
|
2019-08-12 18:11:01 +02:00
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
QByteArray JsonLyricsProvider::ExtractData(QNetworkReply *reply) {
|
2019-08-12 18:11:01 +02:00
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
2019-08-12 22:24:05 +02:00
|
|
|
if (reply->error() < 200) {
|
2020-05-08 18:35:36 +02:00
|
|
|
return QByteArray();
|
2019-08-12 22:24:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
|
2019-08-12 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
return reply->readAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject JsonLyricsProvider::ExtractJsonObj(const QByteArray &data) {
|
|
|
|
|
2019-08-12 22:24:05 +02:00
|
|
|
if (data.isEmpty()) {
|
2019-08-12 18:11:01 +02:00
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:48:48 +02:00
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
2019-08-12 18:11:01 +02:00
|
|
|
|
2020-05-10 12:48:48 +02:00
|
|
|
if (json_error.error != QJsonParseError::NoError) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Failed to parse json data: %1").arg(json_error.errorString()));
|
2019-08-12 18:11:01 +02:00
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
if (json_doc.isEmpty()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received empty Json document."), data);
|
2019-08-12 18:11:01 +02:00
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Json document is not an object."), json_doc);
|
2019-08-12 18:11:01 +02:00
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Received empty Json object."), json_doc);
|
2019-08-12 18:11:01 +02:00
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_obj;
|
|
|
|
|
|
|
|
}
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
QJsonObject JsonLyricsProvider::ExtractJsonObj(QNetworkReply *reply) {
|
|
|
|
|
|
|
|
return ExtractJsonObj(ExtractData(reply));
|
|
|
|
|
|
|
|
}
|