2019-08-12 22:06: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 22:06: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 <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/strutils.h"
|
2023-03-06 21:42:59 +01:00
|
|
|
#include "lyricssearchrequest.h"
|
|
|
|
#include "lyricssearchresult.h"
|
2019-08-12 22:06:01 +02:00
|
|
|
#include "lololyricsprovider.h"
|
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
namespace {
|
|
|
|
constexpr char kUrlSearch[] = "http://api.lololyrics.com/0.5/getLyric";
|
|
|
|
}
|
2019-08-12 22:06:01 +02:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
LoloLyricsProvider::LoloLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider(QStringLiteral("LoloLyrics"), true, false, network, parent) {}
|
2019-08-12 22:06:01 +02:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
LoloLyricsProvider::~LoloLyricsProvider() {
|
|
|
|
|
|
|
|
while (!replies_.isEmpty()) {
|
|
|
|
QNetworkReply *reply = replies_.takeFirst();
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
2020-05-12 21:28:42 +02:00
|
|
|
reply->abort();
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:42:59 +01:00
|
|
|
bool LoloLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
|
2019-08-12 22:06:01 +02:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
2024-04-11 02:56:01 +02:00
|
|
|
url_query.addQueryItem(QStringLiteral("artist"), QString::fromLatin1(QUrl::toPercentEncoding(request.artist)));
|
|
|
|
url_query.addQueryItem(QStringLiteral("track"), QString::fromLatin1(QUrl::toPercentEncoding(request.title)));
|
2019-08-12 22:06:01 +02:00
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
QUrl url(QString::fromLatin1(kUrlSearch));
|
2019-08-12 22:06:01 +02:00
|
|
|
url.setQuery(url_query);
|
2019-08-22 19:28:54 +02:00
|
|
|
QNetworkRequest req(url);
|
2020-08-14 20:20:41 +02:00
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
2019-08-22 19:28:54 +02:00
|
|
|
QNetworkReply *reply = network_->get(req);
|
2020-05-12 21:28:42 +02:00
|
|
|
replies_ << reply;
|
2023-03-06 21:42:59 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request]() { HandleSearchReply(reply, id, request); });
|
2019-08-12 22:06:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
void LoloLyricsProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
2019-08-12 22:06:01 +02:00
|
|
|
|
2023-03-06 21:42:59 +01:00
|
|
|
void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request) {
|
2019-08-12 22:06:01 +02:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
if (!replies_.contains(reply)) return;
|
|
|
|
replies_.removeAll(reply);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
2019-08-12 22:06:01 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
QString failure_reason;
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2024-04-09 23:20:26 +02:00
|
|
|
failure_reason = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
2019-08-12 22:06:01 +02:00
|
|
|
if (reply->error() < 200) {
|
2020-05-08 18:35:36 +02:00
|
|
|
Error(failure_reason);
|
2023-03-06 21:42:59 +01:00
|
|
|
emit SearchFinished(id);
|
2019-08-12 22:06:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
2024-04-09 23:20:26 +02:00
|
|
|
failure_reason = QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
2019-08-12 22:06:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 12:48:48 +02:00
|
|
|
QByteArray data = reply->readAll();
|
2019-08-12 22:06:01 +02:00
|
|
|
LyricsSearchResults results;
|
|
|
|
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
QXmlStreamReader reader(data);
|
|
|
|
LyricsSearchResult result;
|
|
|
|
QString status;
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
2020-08-21 22:39:01 +02:00
|
|
|
QString name = reader.name().toString();
|
2019-08-12 22:06:01 +02:00
|
|
|
if (type == QXmlStreamReader::StartElement) {
|
2024-06-12 02:13:27 +02:00
|
|
|
if (name == QLatin1String("result")) {
|
2019-08-12 22:06:01 +02:00
|
|
|
status.clear();
|
|
|
|
result = LyricsSearchResult();
|
|
|
|
}
|
2024-06-12 02:13:27 +02:00
|
|
|
else if (name == QLatin1String("status")) {
|
2019-08-12 22:06:01 +02:00
|
|
|
status = reader.readElementText();
|
|
|
|
}
|
2024-06-12 02:13:27 +02:00
|
|
|
else if (name == QLatin1String("response")) {
|
|
|
|
if (status == QLatin1String("OK")) {
|
2019-08-12 22:06:01 +02:00
|
|
|
result.lyrics = reader.readElementText();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failure_reason = reader.readElementText();
|
|
|
|
result = LyricsSearchResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == QXmlStreamReader::EndElement) {
|
2024-06-12 02:13:27 +02:00
|
|
|
if (name == QLatin1String("result")) {
|
2021-07-11 09:49:38 +02:00
|
|
|
if (!result.lyrics.isEmpty()) {
|
|
|
|
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);
|
|
|
|
results << result;
|
|
|
|
}
|
|
|
|
result = LyricsSearchResult();
|
|
|
|
}
|
2019-08-12 22:06:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:42:59 +01:00
|
|
|
if (results.isEmpty()) {
|
|
|
|
qLog(Debug) << "LoloLyrics: No lyrics for" << request.artist << request.title << failure_reason;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Debug) << "LoloLyrics: Got lyrics for" << request.artist << request.title;
|
|
|
|
}
|
2019-08-12 22:06:01 +02:00
|
|
|
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
void LoloLyricsProvider::Error(const QString &error, const QVariant &debug) {
|
2019-08-12 22:06:01 +02:00
|
|
|
|
|
|
|
qLog(Error) << "LoloLyrics:" << error;
|
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
|
|
|
|
|
|
|
}
|