Add HTML lyrics provider

This commit is contained in:
Jonas Kvinge
2022-10-17 21:34:18 +02:00
parent 50502ce720
commit 1c90b03476
10 changed files with 294 additions and 284 deletions

View File

@@ -22,95 +22,24 @@
#include <QVariant>
#include <QString>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QRegularExpression>
#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[] = "<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()) {
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);
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;
}