1 /*
2  * Strawberry Music Player
3  * Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
4  *
5  * Strawberry is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Strawberry is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include "config.h"
21 
22 #include <QObject>
23 #include <QByteArray>
24 #include <QString>
25 #include <QNetworkRequest>
26 #include <QNetworkReply>
27 #include <QJsonDocument>
28 #include <QJsonObject>
29 
30 #include "core/networkaccessmanager.h"
31 #include "lyricsprovider.h"
32 #include "jsonlyricsprovider.h"
33 
JsonLyricsProvider(const QString & name,const bool enabled,const bool authentication_required,NetworkAccessManager * network,QObject * parent)34 JsonLyricsProvider::JsonLyricsProvider(const QString &name, const bool enabled, const bool authentication_required, NetworkAccessManager *network, QObject *parent) : LyricsProvider(name, enabled, authentication_required, network, parent) {}
35 
ExtractData(QNetworkReply * reply)36 QByteArray JsonLyricsProvider::ExtractData(QNetworkReply *reply) {
37 
38   if (reply->error() != QNetworkReply::NoError) {
39     Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
40     if (reply->error() < 200) {
41       return QByteArray();
42     }
43   }
44   else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
45     Error(QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
46   }
47 
48   return reply->readAll();
49 
50 }
51 
ExtractJsonObj(const QByteArray & data)52 QJsonObject JsonLyricsProvider::ExtractJsonObj(const QByteArray &data) {
53 
54   if (data.isEmpty()) {
55     return QJsonObject();
56   }
57 
58   QJsonParseError json_error;
59   QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
60 
61   if (json_error.error != QJsonParseError::NoError) {
62     Error(QString("Failed to parse json data: %1").arg(json_error.errorString()));
63     return QJsonObject();
64   }
65 
66   if (json_doc.isEmpty()) {
67     Error("Received empty Json document.", data);
68     return QJsonObject();
69   }
70 
71   if (!json_doc.isObject()) {
72     Error("Json document is not an object.", json_doc);
73     return QJsonObject();
74   }
75 
76   QJsonObject json_obj = json_doc.object();
77   if (json_obj.isEmpty()) {
78     Error("Received empty Json object.", json_doc);
79     return QJsonObject();
80   }
81 
82   return json_obj;
83 
84 }
85 
ExtractJsonObj(QNetworkReply * reply)86 QJsonObject JsonLyricsProvider::ExtractJsonObj(QNetworkReply *reply) {
87 
88   return ExtractJsonObj(ExtractData(reply));
89 
90 }
91