From 50502ce720e9a73e38a6dd728e36050f29482850 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 19 Sep 2023 22:47:07 +0200 Subject: [PATCH] Add azlyrics.com lyrics provider --- README.md | 2 +- debian/control.in | 2 +- ...rawberrymusicplayer.strawberry.appdata.xml | 2 +- dist/unix/strawberry.spec.in | 2 +- src/CMakeLists.txt | 2 + src/core/application.cpp | 2 + src/lyrics/azlyricscomlyricsprovider.cpp | 128 ++++++++++++++++++ src/lyrics/azlyricscomlyricsprovider.h | 60 ++++++++ 8 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 src/lyrics/azlyricscomlyricsprovider.cpp create mode 100644 src/lyrics/azlyricscomlyricsprovider.h diff --git a/README.md b/README.md index 9d3b1f8a..974e1cfc 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Funding developers is a way to contribute to open source projects you appreciate * Edit tags on audio files * Fetch tags from MusicBrainz * Album cover art from [Last.fm](https://www.last.fm/), [Musicbrainz](https://musicbrainz.org/), [Discogs](https://www.discogs.com/), [Musixmatch](https://www.musixmatch.com/), [Deezer](https://www.deezer.com/), [Tidal](https://www.tidal.com/), [Qobuz](https://www.qobuz.com/) and [Spotify](https://www.spotify.com/) - * Song lyrics from [Genius](https://genius.com/), [Musixmatch](https://www.musixmatch.com/), [ChartLyrics](http://www.chartlyrics.com/), [lyrics.ovh](https://lyrics.ovh/), [lololyrics.com](https://www.lololyrics.com/) and [songlyrics.com](https://www.songlyrics.com/) + * Song lyrics from [Genius](https://genius.com/), [Musixmatch](https://www.musixmatch.com/), [ChartLyrics](http://www.chartlyrics.com/), [lyrics.ovh](https://lyrics.ovh/), [lololyrics.com](https://www.lololyrics.com/), [songlyrics.com](https://www.songlyrics.com/) and [azlyrics.com](https://www.azlyrics.com/) * Support for multiple backends * Audio analyzer * Audio equalizer diff --git a/debian/control.in b/debian/control.in index 60acf126..e84dabc1 100644 --- a/debian/control.in +++ b/debian/control.in @@ -52,7 +52,7 @@ Description: music player and music collection organizer - Edit tags on audio files - Automatically retrieve tags from MusicBrainz - Album cover art from Last.fm, Musicbrainz, Discogs, Musixmatch, Deezer, Tidal, Qobuz and Spotify - - Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com and songlyrics.com + - Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com, songlyrics.com and azlyrics.com - Audio analyzer - Audio equalizer - Transfer music to mass-storage USB players, MTP compatible devices and iPod Nano/Classic diff --git a/dist/unix/org.strawberrymusicplayer.strawberry.appdata.xml b/dist/unix/org.strawberrymusicplayer.strawberry.appdata.xml index 43a17762..81007353 100644 --- a/dist/unix/org.strawberrymusicplayer.strawberry.appdata.xml +++ b/dist/unix/org.strawberrymusicplayer.strawberry.appdata.xml @@ -29,7 +29,7 @@
  • Edit tags on audio files
  • Automatically retrieve tags from MusicBrainz
  • Album cover art from Last.fm, Musicbrainz, Discogs, Musixmatch, Deezer, Tidal, Qobuz and Spotify
  • -
  • Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com and songlyrics.com
  • +
  • Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com, songlyrics.com and azlyrics.com
  • Support for multiple backends
  • Audio analyzer and equalizer
  • Transfer music to mass-storage USB players, MTP compatible devices and iPod Nano/Classic
  • diff --git a/dist/unix/strawberry.spec.in b/dist/unix/strawberry.spec.in index 9d01419b..9a6aa7fe 100644 --- a/dist/unix/strawberry.spec.in +++ b/dist/unix/strawberry.spec.in @@ -118,7 +118,7 @@ Features: - Edit tags on audio files - Automatically retrieve tags from MusicBrainz - Album cover art from Last.fm, Musicbrainz, Discogs, Musixmatch, Deezer, Tidal, Qobuz and Spotify - - Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com and songlyrics.com + - Song lyrics from Genius, Musixmatch, ChartLyrics, lyrics.ovh, lololyrics.com, songlyrics.com and azlyrics.com - Support for multiple backends - Audio analyzer - Audio equalizer diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f43395c..e9f10972 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -184,6 +184,7 @@ set(SOURCES lyrics/musixmatchlyricsprovider.cpp lyrics/chartlyricsprovider.cpp lyrics/songlyricscomlyricsprovider.cpp + lyrics/azlyricscomlyricsprovider.cpp providers/musixmatchprovider.cpp @@ -422,6 +423,7 @@ set(HEADERS lyrics/musixmatchlyricsprovider.h lyrics/chartlyricsprovider.h lyrics/songlyricscomlyricsprovider.h + lyrics/azlyricscomlyricsprovider.h settings/settingsdialog.h settings/settingspage.h diff --git a/src/core/application.cpp b/src/core/application.cpp index ec591a3e..a481d213 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -65,6 +65,7 @@ #include "lyrics/musixmatchlyricsprovider.h" #include "lyrics/chartlyricsprovider.h" #include "lyrics/songlyricscomlyricsprovider.h" +#include "lyrics/azlyricscomlyricsprovider.h" #include "scrobbler/audioscrobbler.h" #include "scrobbler/lastfmscrobbler.h" @@ -164,6 +165,7 @@ class ApplicationImpl { lyrics_providers->AddProvider(new MusixmatchLyricsProvider(app->network())); lyrics_providers->AddProvider(new ChartLyricsProvider(app->network())); lyrics_providers->AddProvider(new SongLyricsComLyricsProvider(app->network())); + lyrics_providers->AddProvider(new AzLyricsComLyricsProvider(app->network())); lyrics_providers->ReloadSettings(); return lyrics_providers; }), diff --git a/src/lyrics/azlyricscomlyricsprovider.cpp b/src/lyrics/azlyricscomlyricsprovider.cpp new file mode 100644 index 00000000..f8b9e2cc --- /dev/null +++ b/src/lyrics/azlyricscomlyricsprovider.cpp @@ -0,0 +1,128 @@ +/* + * Strawberry Music Player + * Copyright 2023, Jonas Kvinge + * + * Strawberry is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Strawberry is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Strawberry. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#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/"; + +AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr network, QObject *parent) : LyricsProvider("azlyrics.com", true, false, network, parent) {} + +AzLyricsComLyricsProvider::~AzLyricsComLyricsProvider() { + + 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("
    "), QRegularExpression("
    "), QRegularExpression(""), 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); + +} + +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; + +} diff --git a/src/lyrics/azlyricscomlyricsprovider.h b/src/lyrics/azlyricscomlyricsprovider.h new file mode 100644 index 00000000..c4a12d7b --- /dev/null +++ b/src/lyrics/azlyricscomlyricsprovider.h @@ -0,0 +1,60 @@ +/* + * Strawberry Music Player + * Copyright 2023, Jonas Kvinge + * + * Strawberry is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Strawberry is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Strawberry. If not, see . + * + */ + +#ifndef AZLYRICSCOMLYRICSPROVIDER_H +#define AZLYRICSCOMLYRICSPROVIDER_H + +#include +#include +#include +#include +#include +#include + +#include "core/shared_ptr.h" +#include "lyricsprovider.h" +#include "lyricssearchrequest.h" + +class QNetworkReply; +class NetworkAccessManager; + +class AzLyricsComLyricsProvider : public LyricsProvider { + Q_OBJECT + + public: + explicit AzLyricsComLyricsProvider(SharedPtr network, QObject *parent = nullptr); + ~AzLyricsComLyricsProvider() override; + + bool StartSearch(const int id, const LyricsSearchRequest &request) override; + void CancelSearch(const int id) override; + + private: + void SendRequest(const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title, QUrl url = QUrl()); + void Error(const QString &error, const QVariant &debug = QVariant()) override; + static QString StringFixup(QString string); + + private slots: + void HandleLyricsReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request, const QString &result_artist, const QString &result_album, const QString &result_title); + + private: + static const char *kUrl; + QList replies_; +}; + +#endif // AZLYRICSCOMLYRICSPROVIDER_H