From ec99df3144d4df10ae985453df0065c91c61845d Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 18 Oct 2022 22:49:15 +0200 Subject: [PATCH] Create common class for Musixmatch --- src/CMakeLists.txt | 2 ++ src/covermanager/musixmatchcoverprovider.cpp | 19 +++------- src/covermanager/musixmatchcoverprovider.h | 3 +- src/lyrics/musixmatchlyricsprovider.cpp | 16 +-------- src/lyrics/musixmatchlyricsprovider.h | 6 ++-- src/providers/musixmatchprovider.cpp | 38 ++++++++++++++++++++ src/providers/musixmatchprovider.h | 36 +++++++++++++++++++ 7 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 src/providers/musixmatchprovider.cpp create mode 100644 src/providers/musixmatchprovider.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 17a602cf8..e64762277 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -162,6 +162,8 @@ set(SOURCES lyrics/musixmatchlyricsprovider.cpp lyrics/chartlyricsprovider.cpp + providers/musixmatchprovider.cpp + settings/settingsdialog.cpp settings/settingspage.cpp settings/behavioursettingspage.cpp diff --git a/src/covermanager/musixmatchcoverprovider.cpp b/src/covermanager/musixmatchcoverprovider.cpp index e6760b24b..0bcc424ec 100644 --- a/src/covermanager/musixmatchcoverprovider.cpp +++ b/src/covermanager/musixmatchcoverprovider.cpp @@ -34,6 +34,7 @@ #include "core/logging.h" #include "core/networkaccessmanager.h" +#include "providers/musixmatchprovider.h" #include "albumcoverfetcher.h" #include "jsoncoverprovider.h" #include "musixmatchcoverprovider.h" @@ -56,22 +57,10 @@ bool MusixmatchCoverProvider::StartSearch(const QString &artist, const QString & Q_UNUSED(title); - QString artist_stripped = artist; - QString album_stripped = album; + if (artist.isEmpty() || album.isEmpty()) return false; - artist_stripped = artist_stripped.replace('/', '-') - .remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption)) - .simplified() - .replace(' ', '-') - .replace(QRegularExpression("(-)\\1+"), "-") - .toLower(); - - album_stripped = album_stripped.replace('/', '-') - .remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption)) - .simplified() - .replace(' ', '-') - .replace(QRegularExpression("(-)\\1+"), "-") - .toLower(); + QString artist_stripped = StringFixup(artist); + QString album_stripped = StringFixup(album); if (artist_stripped.isEmpty() || album_stripped.isEmpty()) return false; diff --git a/src/covermanager/musixmatchcoverprovider.h b/src/covermanager/musixmatchcoverprovider.h index ccd3bb12e..827a9f8e7 100644 --- a/src/covermanager/musixmatchcoverprovider.h +++ b/src/covermanager/musixmatchcoverprovider.h @@ -29,11 +29,12 @@ #include #include "jsoncoverprovider.h" +#include "providers/musixmatchprovider.h" class QNetworkReply; class NetworkAccessManager; -class MusixmatchCoverProvider : public JsonCoverProvider { +class MusixmatchCoverProvider : public JsonCoverProvider, MusixmatchProvider { Q_OBJECT public: diff --git a/src/lyrics/musixmatchlyricsprovider.cpp b/src/lyrics/musixmatchlyricsprovider.cpp index 331a7601a..5b764e7b6 100644 --- a/src/lyrics/musixmatchlyricsprovider.cpp +++ b/src/lyrics/musixmatchlyricsprovider.cpp @@ -39,9 +39,7 @@ #include "jsonlyricsprovider.h" #include "lyricsfetcher.h" #include "musixmatchlyricsprovider.h" - -const char *MusixmatchLyricsProvider::kApiUrl = "https://api.musixmatch.com/ws/1.1"; -const char *MusixmatchLyricsProvider::kApiKey = "Y2FhMDRlN2Y4OWE5OTIxYmZlOGMzOWQzOGI3ZGU4MjE="; +#include "providers/musixmatchprovider.h" MusixmatchLyricsProvider::MusixmatchLyricsProvider(NetworkAccessManager *network, QObject *parent) : JsonLyricsProvider("Musixmatch", true, false, network, parent), rate_limit_exceeded_(false) {} @@ -247,18 +245,6 @@ void MusixmatchLyricsProvider::HandleSearchReply(QNetworkReply *reply, LyricsSea } -QString MusixmatchLyricsProvider::StringFixup(QString string) { - - return string.replace('/', '-') - .replace('\'', '-') - .remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption)) - .simplified() - .replace(' ', '-') - .replace(QRegularExpression("(-)\\1+"), "-") - .toLower(); - -} - bool MusixmatchLyricsProvider::CreateLyricsRequest(LyricsSearchContextPtr search) { QString artist_stripped = StringFixup(search->artist); diff --git a/src/lyrics/musixmatchlyricsprovider.h b/src/lyrics/musixmatchlyricsprovider.h index 5271b40cc..5320161a8 100644 --- a/src/lyrics/musixmatchlyricsprovider.h +++ b/src/lyrics/musixmatchlyricsprovider.h @@ -33,11 +33,12 @@ #include "jsonlyricsprovider.h" #include "lyricsfetcher.h" +#include "providers/musixmatchprovider.h" class QNetworkReply; class NetworkAccessManager; -class MusixmatchLyricsProvider : public JsonLyricsProvider { +class MusixmatchLyricsProvider : public JsonLyricsProvider, public MusixmatchProvider { Q_OBJECT public: @@ -60,7 +61,6 @@ class MusixmatchLyricsProvider : public JsonLyricsProvider { using LyricsSearchContextPtr = std::shared_ptr; - QString StringFixup(QString string); bool SendSearchRequest(LyricsSearchContextPtr search); bool CreateLyricsRequest(LyricsSearchContextPtr search); bool SendLyricsRequest(LyricsSearchContextPtr search, const QUrl &url); @@ -72,8 +72,6 @@ class MusixmatchLyricsProvider : public JsonLyricsProvider { void HandleLyricsReply(QNetworkReply *reply, LyricsSearchContextPtr search, const QUrl &url); private: - static const char *kApiUrl; - static const char *kApiKey; QList requests_search_; QList replies_; bool rate_limit_exceeded_; diff --git a/src/providers/musixmatchprovider.cpp b/src/providers/musixmatchprovider.cpp new file mode 100644 index 000000000..f8987076d --- /dev/null +++ b/src/providers/musixmatchprovider.cpp @@ -0,0 +1,38 @@ +/* + * Strawberry Music Player + * Copyright 2020-2022, 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 "musixmatchprovider.h" + +const char *MusixmatchProvider::kApiUrl = "https://api.musixmatch.com/ws/1.1"; +const char *MusixmatchProvider::kApiKey = "Y2FhMDRlN2Y4OWE5OTIxYmZlOGMzOWQzOGI3ZGU4MjE="; + +QString MusixmatchProvider::StringFixup(QString string) { + + return string.replace('/', '-') + .replace('\'', '-') + .remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption)) + .simplified() + .replace(' ', '-') + .replace(QRegularExpression("(-)\\1+"), "-") + .toLower(); + +} diff --git a/src/providers/musixmatchprovider.h b/src/providers/musixmatchprovider.h new file mode 100644 index 000000000..d3a4f5acf --- /dev/null +++ b/src/providers/musixmatchprovider.h @@ -0,0 +1,36 @@ +/* + * Strawberry Music Player + * Copyright 2020-2022, 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 MUSIXMATCHPROVIDER_H +#define MUSIXMATCHPROVIDER_H + +#include +#include + +class MusixmatchProvider { + + protected: + QString StringFixup(QString string); + + protected: + static const char *kApiUrl; + static const char *kApiKey; +}; + +#endif // MUSIXMATCHPROVIDER_H