strawberry-audio-player-win.../src/lyrics/ovhlyricsprovider.cpp

125 lines
3.8 KiB
C++
Raw Normal View History

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 <QVariant>
#include <QString>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
2020-02-09 02:29:35 +01:00
#include <QJsonValue>
#include <QtDebug>
2019-08-12 18:11:01 +02:00
#include "core/logging.h"
2021-01-11 16:48:46 +01:00
#include "core/networkaccessmanager.h"
2021-01-11 16:05:39 +01:00
#include "core/utilities.h"
2019-08-12 18:11:01 +02:00
#include "lyricsfetcher.h"
#include "jsonlyricsprovider.h"
#include "ovhlyricsprovider.h"
const char *OVHLyricsProvider::kUrlSearch = "https://api.lyrics.ovh/v1/";
OVHLyricsProvider::OVHLyricsProvider(QObject *parent) : JsonLyricsProvider("Lyrics.ovh", true, false, parent), network_(new NetworkAccessManager(this)) {}
2019-08-12 18:11:01 +02:00
OVHLyricsProvider::~OVHLyricsProvider() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply, nullptr, this, nullptr);
reply->abort();
reply->deleteLater();
}
}
2019-08-12 18:11:01 +02:00
bool OVHLyricsProvider::StartSearch(const QString &artist, const QString &album, const QString &title, const quint64 id) {
2019-09-15 20:27:32 +02:00
Q_UNUSED(album);
2019-08-12 18:11:01 +02:00
QUrl url(kUrlSearch + QString(QUrl::toPercentEncoding(artist)) + "/" + QString(QUrl::toPercentEncoding(title)));
QNetworkRequest req(url);
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
#else
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
#endif
QNetworkReply *reply = network_->get(req);
replies_ << reply;
2021-03-21 00:37:17 +01:00
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, artist, title]() { HandleSearchReply(reply, id, artist, title); });
2019-08-12 18:11:01 +02:00
//qLog(Debug) << "OVHLyrics: Sending request for" << url;
return true;
}
2019-09-15 20:27:32 +02:00
void OVHLyricsProvider::CancelSearch(const quint64 id) { Q_UNUSED(id); }
2019-08-12 18:11:01 +02:00
void OVHLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64 id, const QString &artist, const QString &title) {
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 18:11:01 +02:00
reply->deleteLater();
QJsonObject json_obj = ExtractJsonObj(reply);
2019-08-12 18:11:01 +02:00
if (json_obj.isEmpty()) {
emit SearchFinished(id, LyricsSearchResults());
return;
}
if (json_obj.contains("error")) {
Error(json_obj["error"].toString());
2019-08-12 18:11:01 +02:00
qLog(Debug) << "OVHLyrics: No lyrics for" << artist << title;
emit SearchFinished(id, LyricsSearchResults());
return;
}
if (!json_obj.contains("lyrics")) {
emit SearchFinished(id, LyricsSearchResults());
return;
}
LyricsSearchResult result;
result.lyrics = json_obj["lyrics"].toString();
2020-12-20 07:09:12 +01:00
if (result.lyrics.isEmpty()) {
qLog(Debug) << "OVHLyrics: No lyrics for" << artist << title;
emit SearchFinished(id, LyricsSearchResults());
}
else {
2021-01-11 16:05:39 +01:00
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);
2020-12-20 07:09:12 +01:00
qLog(Debug) << "OVHLyrics: Got lyrics for" << artist << title;
emit SearchFinished(id, LyricsSearchResults() << result);
}
2019-08-12 18:11:01 +02:00
}
void OVHLyricsProvider::Error(const QString &error, const QVariant &debug) {
2019-08-12 18:11:01 +02:00
qLog(Error) << "OVHLyrics:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}