From 78088987b41100e32738c0602a1f8f27834a6a3d Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Sun, 30 Jun 2024 15:46:48 +0100 Subject: [PATCH 1/2] Change lyrics search button to go to Google instead Musixmatch changed their API, so the existing implementation doesn't work --- src/QUMainWindow.cpp | 78 +++++++------------------------------------- src/QUMainWindow.ui | 4 +-- 2 files changed, 14 insertions(+), 68 deletions(-) diff --git a/src/QUMainWindow.cpp b/src/QUMainWindow.cpp index e9098a3..d6e7d28 100644 --- a/src/QUMainWindow.cpp +++ b/src/QUMainWindow.cpp @@ -1380,73 +1380,19 @@ void QUMainWindow::handleMP3() { void QUMainWindow::on_pushButton_GetLyrics_clicked() { setCursor(Qt::WaitCursor); - - QUrl url("https://www.musixmatch.com/search/" + ui->lineEdit_Artist->text() + " " + ui->lineEdit_Title->text()); - - QNetworkAccessManager *m_NetworkMngr = new QNetworkAccessManager(this); - QNetworkReply *reply = m_NetworkMngr->get(QNetworkRequest(url)); + QUrl url("https://www.google.com/search"); + QUrlQuery urlQuery; + QRegularExpression whitespace = QRegularExpression("(\\s+)"); + QStringList queryStrings; + queryStrings << ui->lineEdit_Artist->text().split(whitespace) << ui->lineEdit_Title->text().split(whitespace) << "lyrics"; + QByteArray encodedQuery; + foreach (QString queryString, queryStrings) + encodedQuery += QUrl::toPercentEncoding(queryString + " ", QString("+").toLatin1()); + urlQuery.addQueryItem("q", encodedQuery); + url.setQuery(urlQuery); - QEventLoop loop; - connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - if(reply->error() != QNetworkReply::NoError) { - QUMessageBox::warning(this, - tr("Lyrics retrieval failed."), - QString(tr("Is your internet connection working?")), - BTN << ":/icons/accept.png" << "OK", - 240); - logSrv->add(QString(tr("Lyrics retrieval failed. Host unreachable.")), QU::Error); - return; - } - - QString searchresult = reply->readAll(); - - QRegularExpression re = QRegularExpression("", QRegularExpression::InvertedGreedinessOption); - QRegularExpressionMatch match = re.match(searchresult); - - QString lyr; - bool lyricsFound = false; - - if (match.hasMatch()) { - QString relative_url = match.captured(1); - url.setUrl("https://www.musixmatch.com" + relative_url); - - reply = m_NetworkMngr->get(QNetworkRequest(url)); - - QEventLoop loop; - connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - if(reply->error() != QNetworkReply::NoError) { - QUMessageBox::warning(this, - tr("Lyrics retrieval failed."), - QString(tr("Is your internet connection working?")), - BTN << ":/icons/accept.png" << "OK", - 240); - logSrv->add(QString(tr("Lyrics retrieval failed. Host unreachable.")), QU::Error); - return; - } - - QString musixmatch_page = reply->readAll(); - - re.setPattern("\"body\":\"(.*),\"language\":\"[a-z]+\",\"languageDescription\":\"(.*)\","); - match = re.match(musixmatch_page); - - if (match.hasMatch()) { - lyr = match.captured(1).replace("\\n","\n").replace("\\\"", "\"").replace("´", "'").replace("`", "'"); - lyr.chop(1); //todo: change above regex to not include the final " - ui->plainTextEdit_InputLyrics->setPlainText(lyr); - lyricsFound = true; - } - } else { - ui->plainTextEdit_InputLyrics->setPlainText("Lyrics could not be retrieved automatically. Please try to adjust artist/title or paste them in this field manually."); - } - - if(!lyricsFound) { - setCursor(Qt::ArrowCursor); - return; - } else { - handleLyrics(ui->plainTextEdit_InputLyrics->toPlainText()); - } + QDesktopServices::openUrl(url); + setCursor(Qt::ArrowCursor); } void QUMainWindow::handleLyrics(QString lyrics) { diff --git a/src/QUMainWindow.ui b/src/QUMainWindow.ui index 380235f..5257aeb 100644 --- a/src/QUMainWindow.ui +++ b/src/QUMainWindow.ui @@ -1485,10 +1485,10 @@ - fetch lyrics from musixmatch.com + Search lyrics on Google - web + Google Search From 9ac4da4dcc231395c9ccc64ed6996f4b440533ce Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Sun, 30 Jun 2024 15:54:46 +0100 Subject: [PATCH 2/2] Change apostrophes in lyrics to typographer's apostrophes --- src/QUMainWindow.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/QUMainWindow.cpp b/src/QUMainWindow.cpp index d6e7d28..fe7dacb 100644 --- a/src/QUMainWindow.cpp +++ b/src/QUMainWindow.cpp @@ -547,6 +547,7 @@ QString QUMainWindow::cleanLyrics(QString rawLyricsString) { // replace misused accents (´,`) by the correct apostrophe (') rawLyricsString = rawLyricsString.replace("´", "'"); rawLyricsString = rawLyricsString.replace("`", "'"); + rawLyricsString = rawLyricsString.replace("'", "’"); // delete leading and trailing whitespace from each line, change line beginning to uppercase if selected QStringList rawLyricsStringList = rawLyricsString.split(QRegularExpression("\\n"));