Skip to content

Commit

Permalink
Merge pull request #48 from UltraStar-Deluxe/adapt-lyricssearch-button
Browse files Browse the repository at this point in the history
Change lyrics search button to go to Google instead
  • Loading branch information
DeinAlptraum authored Jun 30, 2024
2 parents 592df78 + 9ac4da4 commit 2e54a0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 68 deletions.
79 changes: 13 additions & 66 deletions src/QUMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -1380,73 +1381,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("<a class=\"title\" href=\"(.*)\"><span>", 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) {
Expand Down
4 changes: 2 additions & 2 deletions src/QUMainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1485,10 +1485,10 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>fetch lyrics from musixmatch.com</string>
<string>Search lyrics on Google</string>
</property>
<property name="text">
<string>web</string>
<string>Google Search</string>
</property>
<property name="icon">
<iconset resource="resources/UltraStar-Creator.qrc">
Expand Down

0 comments on commit 2e54a0c

Please sign in to comment.