Lyrics: Refactor
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2024-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
|
||||
@@ -47,17 +47,6 @@ constexpr char kLyricsEnd[] = "</script>";
|
||||
|
||||
LyricFindLyricsProvider::LyricFindLyricsProvider(const SharedPtr<NetworkAccessManager> network, QObject *parent) : JsonLyricsProvider(u"lyricfind.com"_s, true, false, network, parent) {}
|
||||
|
||||
LyricFindLyricsProvider::~LyricFindLyricsProvider() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QUrl LyricFindLyricsProvider::Url(const LyricsSearchRequest &request) {
|
||||
|
||||
return QUrl(QLatin1String(kUrl) + QLatin1Char('/') + StringFixup(request.artist) + QLatin1Char('-') + StringFixup(request.title));
|
||||
@@ -85,11 +74,7 @@ void LyricFindLyricsProvider::StartSearch(const int id, const LyricsSearchReques
|
||||
Q_ASSERT(QThread::currentThread() != qApp->thread());
|
||||
|
||||
const QUrl url = Url(request);
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::UserAgentHeader, u"Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"_s);
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
replies_ << reply;
|
||||
QNetworkReply *reply = CreateGetRequest(url, true);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request]() { HandleSearchReply(reply, id, request); });
|
||||
|
||||
qLog(Debug) << "LyricFind: Sending request for" << url;
|
||||
@@ -108,20 +93,13 @@ void LyricFindLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int
|
||||
LyricsSearchResults results;
|
||||
const QScopeGuard end_search = qScopeGuard([this, id, request, &results]() { EndSearch(id, request, results); });
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
const ReplyDataResult reply_data_result = GetReplyData(reply);
|
||||
if (!reply_data_result.success()) {
|
||||
Error(reply_data_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid()) {
|
||||
const int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (http_code != 200 && http_code != 201 && http_code != 202) {
|
||||
Error(QStringLiteral("Received HTTP code %1").arg(http_code));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const QByteArray data = reply->readAll();
|
||||
const QByteArray &data = reply_data_result.data;
|
||||
if (data.isEmpty()) {
|
||||
Error(u"Empty reply received from server."_s);
|
||||
return;
|
||||
@@ -149,63 +127,63 @@ void LyricFindLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject obj = ExtractJsonObj(content_json.toUtf8());
|
||||
if (obj.isEmpty()) {
|
||||
const JsonObjectResult json_object_result = GetJsonObject(content_json.toUtf8());
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
if (!obj.contains("props"_L1) || !obj["props"_L1].isObject()) {
|
||||
|
||||
QJsonObject json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_object.contains("props"_L1) || !json_object["props"_L1].isObject()) {
|
||||
Error(u"Missing props."_s);
|
||||
return;
|
||||
}
|
||||
obj = obj["props"_L1].toObject();
|
||||
if (!obj.contains("pageProps"_L1) || !obj["pageProps"_L1].isObject()) {
|
||||
json_object = json_object["props"_L1].toObject();
|
||||
if (!json_object.contains("pageProps"_L1) || !json_object["pageProps"_L1].isObject()) {
|
||||
Error(u"Missing pageProps."_s);
|
||||
return;
|
||||
}
|
||||
obj = obj["pageProps"_L1].toObject();
|
||||
if (!obj.contains("songData"_L1) || !obj["songData"_L1].isObject()) {
|
||||
json_object = json_object["pageProps"_L1].toObject();
|
||||
if (!json_object.contains("songData"_L1) || !json_object["songData"_L1].isObject()) {
|
||||
Error(u"Missing songData."_s);
|
||||
return;
|
||||
}
|
||||
obj = obj["songData"_L1].toObject();
|
||||
json_object = json_object["songData"_L1].toObject();
|
||||
|
||||
if (!obj.contains("response"_L1) || !obj["response"_L1].isObject()) {
|
||||
if (!json_object.contains("response"_L1) || !json_object["response"_L1].isObject()) {
|
||||
Error(u"Missing response."_s);
|
||||
return;
|
||||
}
|
||||
//const QJsonObject obj_response = obj[QLatin1String("response")].toObject();
|
||||
|
||||
if (!obj.contains("track"_L1) || !obj["track"_L1].isObject()) {
|
||||
if (!json_object.contains("track"_L1) || !json_object["track"_L1].isObject()) {
|
||||
Error(u"Missing track."_s);
|
||||
return;
|
||||
}
|
||||
const QJsonObject obj_track = obj["track"_L1].toObject();
|
||||
const QJsonObject object_track = json_object["track"_L1].toObject();
|
||||
|
||||
if (!obj_track.contains("title"_L1) ||
|
||||
!obj_track.contains("lyrics"_L1)) {
|
||||
if (!object_track.contains("title"_L1) ||
|
||||
!object_track.contains("lyrics"_L1)) {
|
||||
Error(u"Missing title or lyrics."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
LyricsSearchResult result;
|
||||
|
||||
const QJsonObject obj_artist = obj["artist"_L1].toObject();
|
||||
if (obj_artist.contains("name"_L1)) {
|
||||
result.artist = obj_artist["name"_L1].toString();
|
||||
const QJsonObject object_artist = json_object["artist"_L1].toObject();
|
||||
if (object_artist.contains("name"_L1)) {
|
||||
result.artist = object_artist["name"_L1].toString();
|
||||
}
|
||||
result.title = obj_track["title"_L1].toString();
|
||||
result.lyrics = obj_track["lyrics"_L1].toString();
|
||||
result.title = object_track["title"_L1].toString();
|
||||
result.lyrics = object_track["lyrics"_L1].toString();
|
||||
results << result;
|
||||
|
||||
}
|
||||
|
||||
void LyricFindLyricsProvider::Error(const QString &error, const QVariant &debug) {
|
||||
|
||||
qLog(Error) << "LyricFind:" << error;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
}
|
||||
|
||||
void LyricFindLyricsProvider::EndSearch(const int id, const LyricsSearchRequest &request, const LyricsSearchResults &results) {
|
||||
|
||||
if (results.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user