diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e9f10972..f1719f7d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -178,6 +178,7 @@ set(SOURCES lyrics/lyricsfetcher.cpp lyrics/lyricsfetchersearch.cpp lyrics/jsonlyricsprovider.cpp + lyrics/htmllyricsprovider.cpp lyrics/ovhlyricsprovider.cpp lyrics/lololyricsprovider.cpp lyrics/geniuslyricsprovider.cpp @@ -417,6 +418,7 @@ set(HEADERS lyrics/lyricsfetcher.h lyrics/lyricsfetchersearch.h lyrics/jsonlyricsprovider.h + lyrics/htmllyricsprovider.h lyrics/ovhlyricsprovider.h lyrics/lololyricsprovider.h lyrics/geniuslyricsprovider.h diff --git a/src/lyrics/azlyricscomlyricsprovider.cpp b/src/lyrics/azlyricscomlyricsprovider.cpp index f8b9e2cc..1eabe690 100644 --- a/src/lyrics/azlyricscomlyricsprovider.cpp +++ b/src/lyrics/azlyricscomlyricsprovider.cpp @@ -22,95 +22,24 @@ #include #include #include -#include -#include #include -#include "core/logging.h" #include "core/shared_ptr.h" #include "core/networkaccessmanager.h" #include "lyricssearchrequest.h" -#include "lyricssearchresult.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[] = "
"; +const char AzLyricsComLyricsProvider::kEndTag[] = "
"; +const char AzLyricsComLyricsProvider::kLyricsStart[] = ""; -AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr network, QObject *parent) : LyricsProvider("azlyrics.com", true, false, network, parent) {} +AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr network, QObject *parent) + : HtmlLyricsProvider("azlyrics.com", true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {} -AzLyricsComLyricsProvider::~AzLyricsComLyricsProvider() { +QUrl AzLyricsComLyricsProvider::GetUrl(const LyricsSearchRequest &request) { - while (!replies_.isEmpty()) { - 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("
"), QRegularExpression("
"), QRegularExpression(""), 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); + return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + ".html"); } @@ -119,10 +48,3 @@ QString AzLyricsComLyricsProvider::StringFixup(QString string) { 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; - -} diff --git a/src/lyrics/azlyricscomlyricsprovider.h b/src/lyrics/azlyricscomlyricsprovider.h index c4a12d7b..752304dc 100644 --- a/src/lyrics/azlyricscomlyricsprovider.h +++ b/src/lyrics/azlyricscomlyricsprovider.h @@ -28,33 +28,25 @@ #include #include "core/shared_ptr.h" -#include "lyricsprovider.h" +#include "core/networkaccessmanager.h" +#include "htmllyricsprovider.h" #include "lyricssearchrequest.h" -class QNetworkReply; -class NetworkAccessManager; - -class AzLyricsComLyricsProvider : public LyricsProvider { +class AzLyricsComLyricsProvider : public HtmlLyricsProvider { Q_OBJECT public: explicit AzLyricsComLyricsProvider(SharedPtr network, QObject *parent = nullptr); - ~AzLyricsComLyricsProvider() override; - bool StartSearch(const int id, const LyricsSearchRequest &request) override; - void CancelSearch(const int id) override; + protected: + QUrl GetUrl(const LyricsSearchRequest &request) override; + QString StringFixup(QString string) override; private: - void SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url = QUrl()); - void Error(const QString &error, const QVariant &debug = QVariant()) override; - static QString StringFixup(QString string); - - 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 replies_; + static const char kUrl[]; + static const char kStartTag[]; + static const char kEndTag[]; + static const char kLyricsStart[]; }; #endif // AZLYRICSCOMLYRICSPROVIDER_H diff --git a/src/lyrics/geniuslyricsprovider.cpp b/src/lyrics/geniuslyricsprovider.cpp index 8b3338f3..f36ed611 100644 --- a/src/lyrics/geniuslyricsprovider.cpp +++ b/src/lyrics/geniuslyricsprovider.cpp @@ -48,6 +48,7 @@ #include "utilities/randutils.h" #include "internet/localredirectserver.h" #include "jsonlyricsprovider.h" +#include "htmllyricsprovider.h" #include "geniuslyricsprovider.h" using std::make_shared; @@ -467,9 +468,9 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear } QString content = QString::fromUtf8(data); - QString lyrics = ParseLyricsFromHTML(content, QRegularExpression("]*>"), QRegularExpression("<\\/div>"), QRegularExpression("
]+>"), true); + QString lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("]*>"), QRegularExpression("<\\/div>"), QRegularExpression("
]+>"), true); if (lyrics.isEmpty()) { - lyrics = ParseLyricsFromHTML(content, QRegularExpression("]*>"), QRegularExpression("<\\/div>"), QRegularExpression("
"), true); + lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("]*>"), QRegularExpression("<\\/div>"), QRegularExpression("
"), true); } if (!lyrics.isEmpty()) { diff --git a/src/lyrics/htmllyricsprovider.cpp b/src/lyrics/htmllyricsprovider.cpp new file mode 100644 index 00000000..7c896f57 --- /dev/null +++ b/src/lyrics/htmllyricsprovider.cpp @@ -0,0 +1,183 @@ +/* + * Strawberry Music Player + * Copyright 2022, Jonas Kvinge + * + * 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 . + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 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("]*>"), "\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; + +} diff --git a/src/lyrics/htmllyricsprovider.h b/src/lyrics/htmllyricsprovider.h new file mode 100644 index 00000000..f14d2f82 --- /dev/null +++ b/src/lyrics/htmllyricsprovider.h @@ -0,0 +1,67 @@ +/* + * Strawberry Music Player + * Copyright 2022, Jonas Kvinge + * + * 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 . + * + */ + +#ifndef HTMLLYRICSPROVIDER_H +#define HTMLLYRICSPROVIDER_H + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#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 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 replies_; + const QString start_tag_; + const QString end_tag_; + const QString lyrics_start_; + const bool multiple_; +}; + +#endif // HTMLLYRICSPROVIDER_H diff --git a/src/lyrics/lyricsprovider.cpp b/src/lyrics/lyricsprovider.cpp index d7cf5068..30ab8f1d 100644 --- a/src/lyrics/lyricsprovider.cpp +++ b/src/lyrics/lyricsprovider.cpp @@ -21,76 +21,10 @@ #include #include -#include -#include "utilities/strutils.h" #include "core/shared_ptr.h" #include "core/networkaccessmanager.h" #include "lyricsprovider.h" LyricsProvider::LyricsProvider(const QString &name, const bool enabled, const bool authentication_required, SharedPtr network, QObject *parent) : 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("]*>"), "\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); - -} diff --git a/src/lyrics/lyricsprovider.h b/src/lyrics/lyricsprovider.h index 1931607d..c8442336 100644 --- a/src/lyrics/lyricsprovider.h +++ b/src/lyrics/lyricsprovider.h @@ -30,11 +30,10 @@ #include #include "core/shared_ptr.h" +#include "core/networkaccessmanager.h" #include "lyricssearchrequest.h" #include "lyricssearchresult.h" -class NetworkAccessManager; - class LyricsProvider : public QObject { Q_OBJECT @@ -57,9 +56,6 @@ class LyricsProvider : public QObject { 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: void AuthenticationComplete(const bool success, const QStringList &errors = QStringList()); void AuthenticationSuccess(); @@ -68,10 +64,10 @@ class LyricsProvider : public QObject { protected: SharedPtr network_; - QString name_; + const QString name_; bool enabled_; int order_; - bool authentication_required_; + const bool authentication_required_; }; #endif // LYRICSPROVIDER_H diff --git a/src/lyrics/songlyricscomlyricsprovider.cpp b/src/lyrics/songlyricscomlyricsprovider.cpp index 562cf36b..32dd9b64 100644 --- a/src/lyrics/songlyricscomlyricsprovider.cpp +++ b/src/lyrics/songlyricscomlyricsprovider.cpp @@ -22,96 +22,24 @@ #include #include #include -#include -#include -#include #include -#include "core/logging.h" #include "core/shared_ptr.h" #include "core/networkaccessmanager.h" #include "lyricssearchrequest.h" -#include "lyricssearchresult.h" #include "songlyricscomlyricsprovider.h" -const char *SongLyricsComLyricsProvider::kUrl = "https://www.songlyrics.com/"; +const char SongLyricsComLyricsProvider::kUrl[] = "https://www.songlyrics.com/"; +const char SongLyricsComLyricsProvider::kStartTag[] = "]*>"; +const char SongLyricsComLyricsProvider::kEndTag[] = "<\\/p>"; +const char SongLyricsComLyricsProvider::kLyricsStart[] = "

]+>"; -SongLyricsComLyricsProvider::SongLyricsComLyricsProvider(SharedPtr network, QObject *parent) : LyricsProvider("songlyrics.com", true, false, network, parent) {} +SongLyricsComLyricsProvider::SongLyricsComLyricsProvider(SharedPtr network, QObject *parent) + : HtmlLyricsProvider("songlyrics.com", true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {} -SongLyricsComLyricsProvider::~SongLyricsComLyricsProvider() { +QUrl SongLyricsComLyricsProvider::GetUrl(const LyricsSearchRequest &request) { - while (!replies_.isEmpty()) { - 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("]*>"), QRegularExpression("<\\/p>"), QRegularExpression("

]+>"), 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); + return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + "-lyrics/"); } @@ -126,10 +54,3 @@ QString SongLyricsComLyricsProvider::StringFixup(QString string) { .toLower(); } - -void SongLyricsComLyricsProvider::Error(const QString &error, const QVariant &debug) { - - qLog(Error) << "songlyrics.com:" << error; - if (debug.isValid()) qLog(Debug) << debug; - -} diff --git a/src/lyrics/songlyricscomlyricsprovider.h b/src/lyrics/songlyricscomlyricsprovider.h index 8ef39cc0..b4dec2a3 100644 --- a/src/lyrics/songlyricscomlyricsprovider.h +++ b/src/lyrics/songlyricscomlyricsprovider.h @@ -28,33 +28,25 @@ #include #include "core/shared_ptr.h" -#include "lyricsprovider.h" +#include "core/networkaccessmanager.h" +#include "htmllyricsprovider.h" #include "lyricssearchrequest.h" -class QNetworkReply; -class NetworkAccessManager; - -class SongLyricsComLyricsProvider : public LyricsProvider { +class SongLyricsComLyricsProvider : public HtmlLyricsProvider { Q_OBJECT public: explicit SongLyricsComLyricsProvider(SharedPtr network, QObject *parent = nullptr); - ~SongLyricsComLyricsProvider() override; - bool StartSearch(const int id, const LyricsSearchRequest &request) override; - void CancelSearch(const int id) override; + protected: + QUrl GetUrl(const LyricsSearchRequest &request) override; + QString StringFixup(QString string) override; private: - void SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url = QUrl()); - void Error(const QString &error, const QVariant &debug = QVariant()) override; - static QString StringFixup(QString string); - - 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 replies_; + static const char kUrl[]; + static const char kStartTag[]; + static const char kEndTag[]; + static const char kLyricsStart[]; }; #endif // SONGLYRICSCOMLYRICSPROVIDER_H