Add HTML lyrics provider
This commit is contained in:
@@ -178,6 +178,7 @@ set(SOURCES
|
|||||||
lyrics/lyricsfetcher.cpp
|
lyrics/lyricsfetcher.cpp
|
||||||
lyrics/lyricsfetchersearch.cpp
|
lyrics/lyricsfetchersearch.cpp
|
||||||
lyrics/jsonlyricsprovider.cpp
|
lyrics/jsonlyricsprovider.cpp
|
||||||
|
lyrics/htmllyricsprovider.cpp
|
||||||
lyrics/ovhlyricsprovider.cpp
|
lyrics/ovhlyricsprovider.cpp
|
||||||
lyrics/lololyricsprovider.cpp
|
lyrics/lololyricsprovider.cpp
|
||||||
lyrics/geniuslyricsprovider.cpp
|
lyrics/geniuslyricsprovider.cpp
|
||||||
@@ -417,6 +418,7 @@ set(HEADERS
|
|||||||
lyrics/lyricsfetcher.h
|
lyrics/lyricsfetcher.h
|
||||||
lyrics/lyricsfetchersearch.h
|
lyrics/lyricsfetchersearch.h
|
||||||
lyrics/jsonlyricsprovider.h
|
lyrics/jsonlyricsprovider.h
|
||||||
|
lyrics/htmllyricsprovider.h
|
||||||
lyrics/ovhlyricsprovider.h
|
lyrics/ovhlyricsprovider.h
|
||||||
lyrics/lololyricsprovider.h
|
lyrics/lololyricsprovider.h
|
||||||
lyrics/geniuslyricsprovider.h
|
lyrics/geniuslyricsprovider.h
|
||||||
|
|||||||
@@ -22,95 +22,24 @@
|
|||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QNetworkReply>
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include "core/logging.h"
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
#include "core/networkaccessmanager.h"
|
#include "core/networkaccessmanager.h"
|
||||||
#include "lyricssearchrequest.h"
|
#include "lyricssearchrequest.h"
|
||||||
#include "lyricssearchresult.h"
|
|
||||||
#include "azlyricscomlyricsprovider.h"
|
#include "azlyricscomlyricsprovider.h"
|
||||||
|
|
||||||
const char *AzLyricsComLyricsProvider::kUrl = "https://www.azlyrics.com/lyrics/";
|
const char AzLyricsComLyricsProvider::kUrl[] = "https://www.azlyrics.com/lyrics/";
|
||||||
|
const char AzLyricsComLyricsProvider::kStartTag[] = "<div>";
|
||||||
|
const char AzLyricsComLyricsProvider::kEndTag[] = "</div>";
|
||||||
|
const char AzLyricsComLyricsProvider::kLyricsStart[] = "<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->";
|
||||||
|
|
||||||
AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider("azlyrics.com", true, false, network, parent) {}
|
AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||||
|
: HtmlLyricsProvider("azlyrics.com", true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
|
||||||
|
|
||||||
AzLyricsComLyricsProvider::~AzLyricsComLyricsProvider() {
|
QUrl AzLyricsComLyricsProvider::GetUrl(const LyricsSearchRequest &request) {
|
||||||
|
|
||||||
while (!replies_.isEmpty()) {
|
return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + ".html");
|
||||||
QNetworkReply *reply = replies_.takeFirst();
|
|
||||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
|
||||||
reply->abort();
|
|
||||||
reply->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AzLyricsComLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
|
|
||||||
|
|
||||||
SendRequest(id, request, request.artist, request.album, request.title);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void AzLyricsComLyricsProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
|
||||||
|
|
||||||
void AzLyricsComLyricsProvider::SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url) {
|
|
||||||
|
|
||||||
if (url.isEmpty() || !url.isValid()) {
|
|
||||||
url.setUrl(kUrl + StringFixup(result_artist) + "/" + StringFixup(result_title) + ".html");
|
|
||||||
}
|
|
||||||
|
|
||||||
QNetworkRequest req(url);
|
|
||||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
||||||
QNetworkReply *reply = network_->get(req);
|
|
||||||
replies_ << reply;
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request, result_artist, result_album, result_title]() { HandleLyricsReply(reply, id, request, result_artist, result_album, result_title); });
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void AzLyricsComLyricsProvider::HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title) {
|
|
||||||
|
|
||||||
if (!replies_.contains(reply)) return;
|
|
||||||
replies_.removeAll(reply);
|
|
||||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
if (reply->error() != QNetworkReply::NoError) {
|
|
||||||
qLog(Error) << "azlyrics.com:" << reply->errorString() << reply->error();
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|
||||||
qLog(Error) << "azlyrics.com: Received HTTP code" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QByteArray data = reply->readAll();
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
qLog(Error) << "azlyrics.com: Empty reply received from server.";
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QString lyrics = ParseLyricsFromHTML(QString::fromUtf8(data), QRegularExpression("<div>"), QRegularExpression("</div>"), QRegularExpression("<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->"), false);
|
|
||||||
if (lyrics.isEmpty()) {
|
|
||||||
qLog(Debug) << "azlyrics.com: No lyrics for" << request.artist << request.album << request.title;
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
qLog(Debug) << "azlyrics.com: Got lyrics for" << request.artist << request.album << request.title;
|
|
||||||
|
|
||||||
LyricsSearchResult result(lyrics);
|
|
||||||
result.artist = result_artist;
|
|
||||||
result.album = result_album;
|
|
||||||
result.title = result_title;
|
|
||||||
emit SearchFinished(id, LyricsSearchResults() << result);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,10 +48,3 @@ QString AzLyricsComLyricsProvider::StringFixup(QString string) {
|
|||||||
return string.remove(QRegularExpression("[^\\w0-9\\-]", QRegularExpression::UseUnicodePropertiesOption)).simplified().toLower();
|
return string.remove(QRegularExpression("[^\\w0-9\\-]", QRegularExpression::UseUnicodePropertiesOption)).simplified().toLower();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AzLyricsComLyricsProvider::Error(const QString &error, const QVariant &debug) {
|
|
||||||
|
|
||||||
qLog(Error) << "azlyrics.com:" << error;
|
|
||||||
if (debug.isValid()) qLog(Debug) << debug;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,33 +28,25 @@
|
|||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
#include "lyricsprovider.h"
|
#include "core/networkaccessmanager.h"
|
||||||
|
#include "htmllyricsprovider.h"
|
||||||
#include "lyricssearchrequest.h"
|
#include "lyricssearchrequest.h"
|
||||||
|
|
||||||
class QNetworkReply;
|
class AzLyricsComLyricsProvider : public HtmlLyricsProvider {
|
||||||
class NetworkAccessManager;
|
|
||||||
|
|
||||||
class AzLyricsComLyricsProvider : public LyricsProvider {
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AzLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
explicit AzLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||||
~AzLyricsComLyricsProvider() override;
|
|
||||||
|
|
||||||
bool StartSearch(const int id, const LyricsSearchRequest &request) override;
|
protected:
|
||||||
void CancelSearch(const int id) override;
|
QUrl GetUrl(const LyricsSearchRequest &request) override;
|
||||||
|
QString StringFixup(QString string) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url = QUrl());
|
static const char kUrl[];
|
||||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
static const char kStartTag[];
|
||||||
static QString StringFixup(QString string);
|
static const char kEndTag[];
|
||||||
|
static const char kLyricsStart[];
|
||||||
private slots:
|
|
||||||
void HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static const char *kUrl;
|
|
||||||
QList<QNetworkReply*> replies_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AZLYRICSCOMLYRICSPROVIDER_H
|
#endif // AZLYRICSCOMLYRICSPROVIDER_H
|
||||||
|
|||||||
@@ -48,6 +48,7 @@
|
|||||||
#include "utilities/randutils.h"
|
#include "utilities/randutils.h"
|
||||||
#include "internet/localredirectserver.h"
|
#include "internet/localredirectserver.h"
|
||||||
#include "jsonlyricsprovider.h"
|
#include "jsonlyricsprovider.h"
|
||||||
|
#include "htmllyricsprovider.h"
|
||||||
#include "geniuslyricsprovider.h"
|
#include "geniuslyricsprovider.h"
|
||||||
|
|
||||||
using std::make_shared;
|
using std::make_shared;
|
||||||
@@ -467,9 +468,9 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString content = QString::fromUtf8(data);
|
QString content = QString::fromUtf8(data);
|
||||||
QString lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div data-lyrics-container=[^>]+>"), true);
|
QString lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div data-lyrics-container=[^>]+>"), true);
|
||||||
if (lyrics.isEmpty()) {
|
if (lyrics.isEmpty()) {
|
||||||
lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div class=\"lyrics\">"), true);
|
lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div class=\"lyrics\">"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lyrics.isEmpty()) {
|
if (!lyrics.isEmpty()) {
|
||||||
|
|||||||
183
src/lyrics/htmllyricsprovider.cpp
Normal file
183
src/lyrics/htmllyricsprovider.cpp
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
/*
|
||||||
|
* Strawberry Music Player
|
||||||
|
* Copyright 2022, 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/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QVariant>
|
||||||
|
#include <QString>
|
||||||
|
#include <QUrl>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
|
||||||
|
#include "core/logging.h"
|
||||||
|
#include "core/shared_ptr.h"
|
||||||
|
#include "core/networkaccessmanager.h"
|
||||||
|
#include "utilities/strutils.h"
|
||||||
|
#include "htmllyricsprovider.h"
|
||||||
|
#include "lyricssearchrequest.h"
|
||||||
|
|
||||||
|
HtmlLyricsProvider::HtmlLyricsProvider(const QString &name, const bool enabled, const QString &start_tag, const QString &end_tag, const QString &lyrics_start, const bool multiple, SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||||
|
: LyricsProvider(name, enabled, false, network, parent), start_tag_(start_tag), end_tag_(end_tag), lyrics_start_(lyrics_start), multiple_(multiple) {}
|
||||||
|
|
||||||
|
HtmlLyricsProvider::~HtmlLyricsProvider() {
|
||||||
|
|
||||||
|
while (!replies_.isEmpty()) {
|
||||||
|
QNetworkReply *reply = replies_.takeFirst();
|
||||||
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||||
|
reply->abort();
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HtmlLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
|
||||||
|
|
||||||
|
QUrl url(GetUrl(request));
|
||||||
|
QNetworkRequest req(url);
|
||||||
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||||
|
QNetworkReply *reply = network_->get(req);
|
||||||
|
replies_ << reply;
|
||||||
|
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request]() { HandleLyricsReply(reply, id, request); });
|
||||||
|
|
||||||
|
qLog(Debug) << name_ << "Sending request for" << url;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtmlLyricsProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
||||||
|
|
||||||
|
void HtmlLyricsProvider::HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request) {
|
||||||
|
|
||||||
|
if (!replies_.contains(reply)) return;
|
||||||
|
replies_.removeAll(reply);
|
||||||
|
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
|
if (reply->error() == QNetworkReply::ContentNotFoundError) {
|
||||||
|
qLog(Debug) << name_ << "No lyrics for" << request.artist << request.album << request.title;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qLog(Error) << name_ << reply->errorString() << reply->error();
|
||||||
|
}
|
||||||
|
emit SearchFinished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
||||||
|
qLog(Error) << name_ << "Received HTTP code" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
emit SearchFinished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray data = reply->readAll();
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
qLog(Error) << name_ << "Empty reply received from server.";
|
||||||
|
emit SearchFinished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString lyrics = ParseLyricsFromHTML(QString::fromUtf8(data), QRegularExpression(start_tag_), QRegularExpression(end_tag_), QRegularExpression(lyrics_start_), multiple_);
|
||||||
|
if (lyrics.isEmpty() || lyrics.contains("we do not have the lyrics for", Qt::CaseInsensitive)) {
|
||||||
|
qLog(Debug) << name_ << "No lyrics for" << request.artist << request.album << request.title;
|
||||||
|
emit SearchFinished(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
qLog(Debug) << name_ << "Got lyrics for" << request.artist << request.album << request.title;
|
||||||
|
|
||||||
|
LyricsSearchResult result(lyrics);
|
||||||
|
emit SearchFinished(id, LyricsSearchResults() << result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QString HtmlLyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple) {
|
||||||
|
|
||||||
|
QString lyrics;
|
||||||
|
qint64 start_idx = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
QRegularExpressionMatch rematch = lyrics_start.match(content, start_idx);
|
||||||
|
if (!rematch.hasMatch()) break;
|
||||||
|
|
||||||
|
const qint64 start_lyrics_idx = rematch.capturedEnd();
|
||||||
|
qint64 end_lyrics_idx = -1;
|
||||||
|
|
||||||
|
// Find the index of the end tag.
|
||||||
|
qint64 idx = start_lyrics_idx;
|
||||||
|
QRegularExpressionMatch rematch_start_tag;
|
||||||
|
QRegularExpressionMatch rematch_end_tag;
|
||||||
|
int tags = 1;
|
||||||
|
do {
|
||||||
|
rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
||||||
|
const qint64 start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
||||||
|
rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
||||||
|
const qint64 end_tag_idx = rematch_end_tag.hasMatch() ? rematch_end_tag.capturedStart() : -1;
|
||||||
|
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
||||||
|
++tags;
|
||||||
|
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
||||||
|
}
|
||||||
|
else if (rematch_end_tag.hasMatch()) {
|
||||||
|
--tags;
|
||||||
|
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
||||||
|
if (tags == 0) {
|
||||||
|
end_lyrics_idx = rematch_end_tag.capturedStart();
|
||||||
|
start_idx = rematch_end_tag.capturedEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (tags > 0 && (rematch_start_tag.hasMatch() || rematch_end_tag.hasMatch()));
|
||||||
|
|
||||||
|
if (end_lyrics_idx != -1 && start_lyrics_idx < end_lyrics_idx) {
|
||||||
|
if (!lyrics.isEmpty()) {
|
||||||
|
lyrics.append("\n");
|
||||||
|
}
|
||||||
|
lyrics.append(content.mid(start_lyrics_idx, end_lyrics_idx - start_lyrics_idx)
|
||||||
|
.remove('\r')
|
||||||
|
.remove('\n')
|
||||||
|
.replace(QRegularExpression("<br[^>]*>"), "\n")
|
||||||
|
.remove(QRegularExpression("<[^>]*>"))
|
||||||
|
.trimmed());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
start_idx = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
while (start_idx > 0 && multiple);
|
||||||
|
|
||||||
|
if (lyrics.length() > 6000 || lyrics.contains("there are no lyrics to", Qt::CaseInsensitive)) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Utilities::DecodeHtmlEntities(lyrics);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtmlLyricsProvider::Error(const QString &error, const QVariant &debug) {
|
||||||
|
|
||||||
|
qLog(Error) << name_ << error;
|
||||||
|
if (debug.isValid()) qLog(Debug) << name_ << debug;
|
||||||
|
|
||||||
|
}
|
||||||
67
src/lyrics/htmllyricsprovider.h
Normal file
67
src/lyrics/htmllyricsprovider.h
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Strawberry Music Player
|
||||||
|
* Copyright 2022, 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 HTMLLYRICSPROVIDER_H
|
||||||
|
#define HTMLLYRICSPROVIDER_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <QtGlobal>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QList>
|
||||||
|
#include <QVariant>
|
||||||
|
#include <QString>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
#include "core/shared_ptr.h"
|
||||||
|
#include "core/networkaccessmanager.h"
|
||||||
|
#include "lyricsprovider.h"
|
||||||
|
#include "lyricssearchrequest.h"
|
||||||
|
|
||||||
|
class QNetworkReply;
|
||||||
|
|
||||||
|
class HtmlLyricsProvider : public LyricsProvider {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit HtmlLyricsProvider(const QString &name, const bool enabled, const QString &start_tag, const QString &end_tag, const QString &lyrics_start, const bool multiple, SharedPtr<NetworkAccessManager> network, QObject *parent);
|
||||||
|
~HtmlLyricsProvider();
|
||||||
|
|
||||||
|
virtual bool StartSearch(const int id, const LyricsSearchRequest &request) override;
|
||||||
|
virtual void CancelSearch(const int id) override;
|
||||||
|
|
||||||
|
static QString ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual QUrl GetUrl(const LyricsSearchRequest &request) = 0;
|
||||||
|
virtual QString StringFixup(QString string) = 0;
|
||||||
|
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
virtual void HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QList<QNetworkReply*> replies_;
|
||||||
|
const QString start_tag_;
|
||||||
|
const QString end_tag_;
|
||||||
|
const QString lyrics_start_;
|
||||||
|
const bool multiple_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HTMLLYRICSPROVIDER_H
|
||||||
@@ -21,76 +21,10 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QRegularExpression>
|
|
||||||
|
|
||||||
#include "utilities/strutils.h"
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
#include "core/networkaccessmanager.h"
|
#include "core/networkaccessmanager.h"
|
||||||
#include "lyricsprovider.h"
|
#include "lyricsprovider.h"
|
||||||
|
|
||||||
LyricsProvider::LyricsProvider(const QString &name, const bool enabled, const bool authentication_required, SharedPtr<NetworkAccessManager> network, QObject *parent)
|
LyricsProvider::LyricsProvider(const QString &name, const bool enabled, const bool authentication_required, SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||||
: QObject(parent), network_(network), name_(name), enabled_(enabled), order_(0), authentication_required_(authentication_required) {}
|
: QObject(parent), network_(network), name_(name), enabled_(enabled), order_(0), authentication_required_(authentication_required) {}
|
||||||
|
|
||||||
QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple) {
|
|
||||||
|
|
||||||
QString lyrics;
|
|
||||||
qint64 start_idx = 0;
|
|
||||||
|
|
||||||
do {
|
|
||||||
|
|
||||||
QRegularExpressionMatch rematch = lyrics_start.match(content, start_idx);
|
|
||||||
if (!rematch.hasMatch()) break;
|
|
||||||
|
|
||||||
const qint64 start_lyrics_idx = rematch.capturedEnd();
|
|
||||||
qint64 end_lyrics_idx = -1;
|
|
||||||
|
|
||||||
// Find the index of the end tag.
|
|
||||||
qint64 idx = start_lyrics_idx;
|
|
||||||
QRegularExpressionMatch rematch_start_tag;
|
|
||||||
QRegularExpressionMatch rematch_end_tag;
|
|
||||||
int tags = 1;
|
|
||||||
do {
|
|
||||||
rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
|
||||||
const qint64 start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
|
||||||
rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
|
||||||
const qint64 end_tag_idx = rematch_end_tag.hasMatch() ? rematch_end_tag.capturedStart() : -1;
|
|
||||||
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
|
||||||
++tags;
|
|
||||||
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
|
||||||
}
|
|
||||||
else if (rematch_end_tag.hasMatch()) {
|
|
||||||
--tags;
|
|
||||||
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
|
||||||
if (tags == 0) {
|
|
||||||
end_lyrics_idx = rematch_end_tag.capturedStart();
|
|
||||||
start_idx = rematch_end_tag.capturedEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (tags > 0 && (rematch_start_tag.hasMatch() || rematch_end_tag.hasMatch()));
|
|
||||||
|
|
||||||
if (end_lyrics_idx != -1 && start_lyrics_idx < end_lyrics_idx) {
|
|
||||||
if (!lyrics.isEmpty()) {
|
|
||||||
lyrics.append("\n");
|
|
||||||
}
|
|
||||||
lyrics.append(content.mid(start_lyrics_idx, end_lyrics_idx - start_lyrics_idx)
|
|
||||||
.remove('\r')
|
|
||||||
.remove('\n')
|
|
||||||
.replace(QRegularExpression("<br[^>]*>"), "\n")
|
|
||||||
.remove(QRegularExpression("<[^>]*>"))
|
|
||||||
.trimmed());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
start_idx = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
while (start_idx > 0 && multiple);
|
|
||||||
|
|
||||||
if (lyrics.length() > 6000 || lyrics.contains("there are no lyrics to", Qt::CaseInsensitive)) {
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Utilities::DecodeHtmlEntities(lyrics);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -30,11 +30,10 @@
|
|||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
|
#include "core/networkaccessmanager.h"
|
||||||
#include "lyricssearchrequest.h"
|
#include "lyricssearchrequest.h"
|
||||||
#include "lyricssearchresult.h"
|
#include "lyricssearchresult.h"
|
||||||
|
|
||||||
class NetworkAccessManager;
|
|
||||||
|
|
||||||
class LyricsProvider : public QObject {
|
class LyricsProvider : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -57,9 +56,6 @@ class LyricsProvider : public QObject {
|
|||||||
|
|
||||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||||
|
|
||||||
protected:
|
|
||||||
QString ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple);
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void AuthenticationComplete(const bool success, const QStringList &errors = QStringList());
|
void AuthenticationComplete(const bool success, const QStringList &errors = QStringList());
|
||||||
void AuthenticationSuccess();
|
void AuthenticationSuccess();
|
||||||
@@ -68,10 +64,10 @@ class LyricsProvider : public QObject {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
SharedPtr<NetworkAccessManager> network_;
|
SharedPtr<NetworkAccessManager> network_;
|
||||||
QString name_;
|
const QString name_;
|
||||||
bool enabled_;
|
bool enabled_;
|
||||||
int order_;
|
int order_;
|
||||||
bool authentication_required_;
|
const bool authentication_required_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LYRICSPROVIDER_H
|
#endif // LYRICSPROVIDER_H
|
||||||
|
|||||||
@@ -22,96 +22,24 @@
|
|||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QUrlQuery>
|
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QNetworkReply>
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include "core/logging.h"
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
#include "core/networkaccessmanager.h"
|
#include "core/networkaccessmanager.h"
|
||||||
#include "lyricssearchrequest.h"
|
#include "lyricssearchrequest.h"
|
||||||
#include "lyricssearchresult.h"
|
|
||||||
#include "songlyricscomlyricsprovider.h"
|
#include "songlyricscomlyricsprovider.h"
|
||||||
|
|
||||||
const char *SongLyricsComLyricsProvider::kUrl = "https://www.songlyrics.com/";
|
const char SongLyricsComLyricsProvider::kUrl[] = "https://www.songlyrics.com/";
|
||||||
|
const char SongLyricsComLyricsProvider::kStartTag[] = "<p[^>]*>";
|
||||||
|
const char SongLyricsComLyricsProvider::kEndTag[] = "<\\/p>";
|
||||||
|
const char SongLyricsComLyricsProvider::kLyricsStart[] = "<p id=\"songLyricsDiv\"[^>]+>";
|
||||||
|
|
||||||
SongLyricsComLyricsProvider::SongLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider("songlyrics.com", true, false, network, parent) {}
|
SongLyricsComLyricsProvider::SongLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||||
|
: HtmlLyricsProvider("songlyrics.com", true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
|
||||||
|
|
||||||
SongLyricsComLyricsProvider::~SongLyricsComLyricsProvider() {
|
QUrl SongLyricsComLyricsProvider::GetUrl(const LyricsSearchRequest &request) {
|
||||||
|
|
||||||
while (!replies_.isEmpty()) {
|
return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + "-lyrics/");
|
||||||
QNetworkReply *reply = replies_.takeFirst();
|
|
||||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
|
||||||
reply->abort();
|
|
||||||
reply->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SongLyricsComLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
|
|
||||||
|
|
||||||
SendRequest(id, request, request.artist, request.album, request.title);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void SongLyricsComLyricsProvider::CancelSearch(const int id) { Q_UNUSED(id); }
|
|
||||||
|
|
||||||
void SongLyricsComLyricsProvider::SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url) {
|
|
||||||
|
|
||||||
if (url.isEmpty() || !url.isValid()) {
|
|
||||||
url.setUrl(kUrl + StringFixup(result_artist) + "/" + StringFixup(result_title) + "-lyrics/");
|
|
||||||
}
|
|
||||||
|
|
||||||
QNetworkRequest req(url);
|
|
||||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
||||||
QNetworkReply *reply = network_->get(req);
|
|
||||||
replies_ << reply;
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request, result_artist, result_album, result_title]() { HandleLyricsReply(reply, id, request, result_artist, result_album, result_title); });
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void SongLyricsComLyricsProvider::HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title) {
|
|
||||||
|
|
||||||
if (!replies_.contains(reply)) return;
|
|
||||||
replies_.removeAll(reply);
|
|
||||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
if (reply->error() != QNetworkReply::NoError) {
|
|
||||||
qLog(Error) << "songlyrics.com:" << reply->errorString() << reply->error();
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|
||||||
qLog(Error) << "songlyrics.com: Received HTTP code" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QByteArray data = reply->readAll();
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
qLog(Error) << "songlyrics.com: Empty reply received from server.";
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QString lyrics = ParseLyricsFromHTML(QString::fromUtf8(data), QRegularExpression("<p[^>]*>"), QRegularExpression("<\\/p>"), QRegularExpression("<p id=\"songLyricsDiv\"[^>]+>"), false);
|
|
||||||
if (lyrics.isEmpty()) {
|
|
||||||
qLog(Debug) << "songlyrics.com: No lyrics for" << request.artist << request.album << request.title;
|
|
||||||
emit SearchFinished(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
qLog(Debug) << "songlyrics.com: Got lyrics for" << request.artist << request.album << request.title;
|
|
||||||
|
|
||||||
LyricsSearchResult result(lyrics);
|
|
||||||
result.artist = result_artist;
|
|
||||||
result.album = result_album;
|
|
||||||
result.title = result_title;
|
|
||||||
emit SearchFinished(id, LyricsSearchResults() << result);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,10 +54,3 @@ QString SongLyricsComLyricsProvider::StringFixup(QString string) {
|
|||||||
.toLower();
|
.toLower();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SongLyricsComLyricsProvider::Error(const QString &error, const QVariant &debug) {
|
|
||||||
|
|
||||||
qLog(Error) << "songlyrics.com:" << error;
|
|
||||||
if (debug.isValid()) qLog(Debug) << debug;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,33 +28,25 @@
|
|||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
#include "core/shared_ptr.h"
|
#include "core/shared_ptr.h"
|
||||||
#include "lyricsprovider.h"
|
#include "core/networkaccessmanager.h"
|
||||||
|
#include "htmllyricsprovider.h"
|
||||||
#include "lyricssearchrequest.h"
|
#include "lyricssearchrequest.h"
|
||||||
|
|
||||||
class QNetworkReply;
|
class SongLyricsComLyricsProvider : public HtmlLyricsProvider {
|
||||||
class NetworkAccessManager;
|
|
||||||
|
|
||||||
class SongLyricsComLyricsProvider : public LyricsProvider {
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SongLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
explicit SongLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||||
~SongLyricsComLyricsProvider() override;
|
|
||||||
|
|
||||||
bool StartSearch(const int id, const LyricsSearchRequest &request) override;
|
protected:
|
||||||
void CancelSearch(const int id) override;
|
QUrl GetUrl(const LyricsSearchRequest &request) override;
|
||||||
|
QString StringFixup(QString string) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url = QUrl());
|
static const char kUrl[];
|
||||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
static const char kStartTag[];
|
||||||
static QString StringFixup(QString string);
|
static const char kEndTag[];
|
||||||
|
static const char kLyricsStart[];
|
||||||
private slots:
|
|
||||||
void HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static const char *kUrl;
|
|
||||||
QList<QNetworkReply*> replies_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SONGLYRICSCOMLYRICSPROVIDER_H
|
#endif // SONGLYRICSCOMLYRICSPROVIDER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user