Lyrics: Refactor

This commit is contained in:
Jonas Kvinge
2025-03-08 23:26:44 +01:00
parent b02b114caf
commit 1173d5f865
23 changed files with 544 additions and 836 deletions

View File

@@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2019-2025, 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
@@ -46,17 +46,6 @@ constexpr char kUrlSearch[] = "http://api.lololyrics.com/0.5/getLyric";
LoloLyricsProvider::LoloLyricsProvider(const SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider(u"LoloLyrics"_s, true, false, network, parent) {}
LoloLyricsProvider::~LoloLyricsProvider() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
QObject::disconnect(reply, nullptr, this, nullptr);
reply->abort();
reply->deleteLater();
}
}
void LoloLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
Q_ASSERT(QThread::currentThread() != qApp->thread());
@@ -65,12 +54,7 @@ void LoloLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &re
url_query.addQueryItem(u"artist"_s, QString::fromLatin1(QUrl::toPercentEncoding(request.artist)));
url_query.addQueryItem(u"track"_s, QString::fromLatin1(QUrl::toPercentEncoding(request.title)));
QUrl url(QString::fromLatin1(kUrlSearch));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
QNetworkReply *reply = network_->get(req);
replies_ << reply;
QNetworkReply *reply = CreateGetRequest(QUrl(QLatin1String(kUrlSearch)), url_query);
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request]() { HandleSearchReply(reply, id, request); });
}
@@ -82,24 +66,19 @@ void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, c
QObject::disconnect(reply, nullptr, this, nullptr);
reply->deleteLater();
QString failure_reason;
if (reply->error() != QNetworkReply::NoError) {
failure_reason = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
if (reply->error() < 200) {
Error(failure_reason);
Q_EMIT SearchFinished(id);
return;
}
}
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
failure_reason = QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
}
QByteArray data = reply->readAll();
LyricsSearchResults results;
const QScopeGuard search_finished = qScopeGuard([this, id, &results]() { Q_EMIT SearchFinished(id, results); });
if (!data.isEmpty()) {
QXmlStreamReader reader(data);
const ReplyDataResult reply_data_result = GetReplyData(reply);
if (!reply_data_result.success()) {
Error(reply_data_result.error_message);
return;
}
QString error_message = reply_data_result.error_message;
if (!reply_data_result.data.isEmpty()) {
QXmlStreamReader reader(reply_data_result.data);
LyricsSearchResult result;
QString status;
while (!reader.atEnd()) {
@@ -118,7 +97,7 @@ void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, c
result.lyrics = reader.readElementText();
}
else {
failure_reason = reader.readElementText();
error_message = reader.readElementText();
result = LyricsSearchResult();
}
}
@@ -136,19 +115,10 @@ void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, c
}
if (results.isEmpty()) {
qLog(Debug) << "LoloLyrics: No lyrics for" << request.artist << request.title << failure_reason;
qLog(Debug) << "LoloLyrics: No lyrics for" << request.artist << request.title << error_message;
}
else {
qLog(Debug) << "LoloLyrics: Got lyrics for" << request.artist << request.title;
}
Q_EMIT SearchFinished(id, results);
}
void LoloLyricsProvider::Error(const QString &error, const QVariant &debug) {
qLog(Error) << "LoloLyrics:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}