Disable automatic conversions from 8-bit strings

This commit is contained in:
Jonas Kvinge
2024-04-11 02:56:01 +02:00
parent 58944993b8
commit 0c6872b352
310 changed files with 2501 additions and 2332 deletions

View File

@@ -28,17 +28,19 @@
#include "lyricssearchrequest.h"
#include "azlyricscomlyricsprovider.h"
const char AzLyricsComLyricsProvider::kUrl[] = "https://www.azlyrics.com/lyrics/";
const char AzLyricsComLyricsProvider::kStartTag[] = "<div>";
const char AzLyricsComLyricsProvider::kEndTag[] = "</div>";
const char AzLyricsComLyricsProvider::kLyricsStart[] = "<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->";
namespace {
constexpr char kUrl[] = "https://www.azlyrics.com/lyrics/";
constexpr char kStartTag[] = "<div>";
constexpr char kEndTag[] = "</div>";
constexpr char kLyricsStart[] = "<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->";
} // namespace
AzLyricsComLyricsProvider::AzLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
: HtmlLyricsProvider(QStringLiteral("azlyrics.com"), true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
: HtmlLyricsProvider(QStringLiteral("azlyrics.com"), true, QLatin1String(kStartTag), QLatin1String(kEndTag), QLatin1String(kLyricsStart), false, network, parent) {}
QUrl AzLyricsComLyricsProvider::Url(const LyricsSearchRequest &request) {
return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + ".html");
return QUrl(QLatin1String(kUrl) + StringFixup(request.artist) + QStringLiteral("/") + StringFixup(request.title) + QStringLiteral(".html"));
}

View File

@@ -41,12 +41,6 @@ class AzLyricsComLyricsProvider : public HtmlLyricsProvider {
private:
QString StringFixup(const QString &text);
private:
static const char kUrl[];
static const char kStartTag[];
static const char kEndTag[];
static const char kLyricsStart[];
};
#endif // AZLYRICSCOMLYRICSPROVIDER_H

View File

@@ -37,7 +37,9 @@
#include "lyricssearchresult.h"
#include "chartlyricsprovider.h"
const char *ChartLyricsProvider::kUrlSearch = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect";
namespace {
constexpr char kUrlSearch[] = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect";
}
ChartLyricsProvider::ChartLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider(QStringLiteral("ChartLyrics"), false, false, network, parent) {}
@@ -55,10 +57,10 @@ ChartLyricsProvider::~ChartLyricsProvider() {
bool ChartLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
QUrlQuery url_query;
url_query.addQueryItem(QStringLiteral("artist"), QUrl::toPercentEncoding(request.artist));
url_query.addQueryItem(QStringLiteral("song"), QUrl::toPercentEncoding(request.title));
url_query.addQueryItem(QStringLiteral("artist"), QString::fromUtf8(QUrl::toPercentEncoding(request.artist)));
url_query.addQueryItem(QStringLiteral("song"), QString::fromUtf8(QUrl::toPercentEncoding(request.title)));
QUrl url(kUrlSearch);
QUrl url(QString::fromUtf8(kUrlSearch));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
@@ -99,21 +101,21 @@ void ChartLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id,
QXmlStreamReader::TokenType type = reader.readNext();
QString name = reader.name().toString();
if (type == QXmlStreamReader::StartElement) {
if (name == "GetLyricResult") {
if (name == QStringLiteral("GetLyricResult")) {
result = LyricsSearchResult();
}
if (name == "LyricArtist") {
if (name == QStringLiteral("LyricArtist")) {
result.artist = reader.readElementText();
}
else if (name == "LyricSong") {
else if (name == QStringLiteral("LyricSong")) {
result.title = reader.readElementText();
}
else if (name == "Lyric") {
else if (name == QStringLiteral("Lyric")) {
result.lyrics = reader.readElementText();
}
}
else if (type == QXmlStreamReader::EndElement) {
if (name == "GetLyricResult") {
if (name == QStringLiteral("GetLyricResult")) {
if (!result.artist.isEmpty() && !result.title.isEmpty() && !result.lyrics.isEmpty() &&
(result.artist.compare(request.albumartist, Qt::CaseInsensitive) == 0 ||
result.artist.compare(request.artist, Qt::CaseInsensitive) == 0 ||

View File

@@ -51,7 +51,6 @@ class ChartLyricsProvider : public LyricsProvider {
void HandleSearchReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request);
private:
static const char *kUrlSearch;
QList<QNetworkReply*> replies_;
};

View File

@@ -28,17 +28,19 @@
#include "lyricssearchrequest.h"
#include "elyricsnetlyricsprovider.h"
const char ElyricsNetLyricsProvider::kUrl[] = "https://www.elyrics.net/read/";
const char ElyricsNetLyricsProvider::kStartTag[] = "<div[^>]*>";
const char ElyricsNetLyricsProvider::kEndTag[] = "<\\/div>";
const char ElyricsNetLyricsProvider::kLyricsStart[] = "<div id='inlyr'>";
namespace {
constexpr char kUrl[] = "https://www.elyrics.net/read/";
constexpr char kStartTag[] = "<div[^>]*>";
constexpr char kEndTag[] = "<\\/div>";
constexpr char kLyricsStart[] = "<div id='inlyr'>";
} // namespace
ElyricsNetLyricsProvider::ElyricsNetLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
: HtmlLyricsProvider(QStringLiteral("elyrics.net"), true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
: HtmlLyricsProvider(QStringLiteral("elyrics.net"), true, QLatin1String(kStartTag), QLatin1String(kEndTag), QLatin1String(kLyricsStart), false, network, parent) {}
QUrl ElyricsNetLyricsProvider::Url(const LyricsSearchRequest &request) {
return QUrl(kUrl + request.artist[0].toLower() + "/" + StringFixup(request.artist) + "-lyrics/" + StringFixup(request.title) + "-lyrics.html");
return QUrl(QLatin1String(kUrl) + request.artist[0].toLower() + QLatin1Char('/') + StringFixup(request.artist) + QStringLiteral("-lyrics/") + StringFixup(request.title) + QStringLiteral("-lyrics.html"));
}
@@ -48,7 +50,7 @@ QString ElyricsNetLyricsProvider::StringFixup(const QString &text) {
.replace(QRegularExpression(QStringLiteral("[^\\w0-9_,&\\-\\(\\) ]")), QStringLiteral("_"))
.replace(QRegularExpression(QStringLiteral(" {2,}")), QStringLiteral(" "))
.simplified()
.replace(' ', '-')
.replace(QLatin1Char(' '), QLatin1Char('-'))
.toLower();
}

View File

@@ -41,12 +41,6 @@ class ElyricsNetLyricsProvider : public HtmlLyricsProvider {
private:
QString StringFixup(const QString &text);
private:
static const char kUrl[];
static const char kStartTag[];
static const char kEndTag[];
static const char kLyricsStart[];
};
#endif // ELYRICSNETLYRICSPROVIDER_H

View File

@@ -45,6 +45,7 @@
#include "core/logging.h"
#include "core/shared_ptr.h"
#include "core/networkaccessmanager.h"
#include "core/settings.h"
#include "utilities/randutils.h"
#include "internet/localredirectserver.h"
#include "jsonlyricsprovider.h"
@@ -53,17 +54,19 @@
using std::make_shared;
const char *GeniusLyricsProvider::kSettingsGroup = "GeniusLyrics";
const char *GeniusLyricsProvider::kOAuthAuthorizeUrl = "https://api.genius.com/oauth/authorize";
const char *GeniusLyricsProvider::kOAuthAccessTokenUrl = "https://api.genius.com/oauth/token";
const char *GeniusLyricsProvider::kOAuthRedirectUrl = "http://localhost:63111/"; // Genius does not accept a random port number. This port must match the URL of the ClientID.
const char *GeniusLyricsProvider::kUrlSearch = "https://api.genius.com/search/";
const char *GeniusLyricsProvider::kClientIDB64 = "RUNTNXU4U1VyMU1KUU5hdTZySEZteUxXY2hkanFiY3lfc2JjdXBpNG5WMU9SNUg4dTBZelEtZTZCdFg2dl91SQ==";
const char *GeniusLyricsProvider::kClientSecretB64 = "VE9pMU9vUjNtTXZ3eFR3YVN0QVRyUjVoUlhVWDI1Ylp5X240eEt1M0ZkYlNwRG5JUnd0LXFFbHdGZkZkRWY2VzJ1S011UnQzM3c2Y3hqY0tVZ3NGN2c=";
namespace {
constexpr char kSettingsGroup[] = "GeniusLyrics";
constexpr char kOAuthAuthorizeUrl[] = "https://api.genius.com/oauth/authorize";
constexpr char kOAuthAccessTokenUrl[] = "https://api.genius.com/oauth/token";
constexpr char kOAuthRedirectUrl[] = "http://localhost:63111/"; // Genius does not accept a random port number. This port must match the URL of the ClientID.
constexpr char kUrlSearch[] = "https://api.genius.com/search/";
constexpr char kClientIDB64[] = "RUNTNXU4U1VyMU1KUU5hdTZySEZteUxXY2hkanFiY3lfc2JjdXBpNG5WMU9SNUg4dTBZelEtZTZCdFg2dl91SQ==";
constexpr char kClientSecretB64[] = "VE9pMU9vUjNtTXZ3eFR3YVN0QVRyUjVoUlhVWDI1Ylp5X240eEt1M0ZkYlNwRG5JUnd0LXFFbHdGZkZkRWY2VzJ1S011UnQzM3c2Y3hqY0tVZ3NGN2c=";
} // namespace
GeniusLyricsProvider::GeniusLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : JsonLyricsProvider(QStringLiteral("Genius"), true, true, network, parent), server_(nullptr) {
QSettings s;
Settings s;
s.beginGroup(kSettingsGroup);
if (s.contains("access_token")) {
access_token_ = s.value("access_token").toString();
@@ -85,7 +88,7 @@ GeniusLyricsProvider::~GeniusLyricsProvider() {
void GeniusLyricsProvider::Authenticate() {
QUrl redirect_url(kOAuthRedirectUrl);
QUrl redirect_url(QString::fromLatin1(kOAuthRedirectUrl));
if (!server_) {
server_ = new LocalRedirectServer(this);
@@ -100,19 +103,19 @@ void GeniusLyricsProvider::Authenticate() {
}
code_verifier_ = Utilities::CryptographicRandomString(44);
code_challenge_ = QString(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
if (code_challenge_.lastIndexOf(QChar('=')) == code_challenge_.length() - 1) {
code_challenge_ = QString::fromLatin1(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
if (code_challenge_.lastIndexOf(QLatin1Char('=')) == code_challenge_.length() - 1) {
code_challenge_.chop(1);
}
QUrlQuery url_query;
url_query.addQueryItem(QStringLiteral("client_id"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
url_query.addQueryItem(QStringLiteral("redirect_uri"), QUrl::toPercentEncoding(redirect_url.toString()));
url_query.addQueryItem(QStringLiteral("client_id"), QString::fromLatin1(QUrl::toPercentEncoding(QLatin1String(kClientIDB64))));
url_query.addQueryItem(QStringLiteral("redirect_uri"), QString::fromLatin1(QUrl::toPercentEncoding(redirect_url.toString())));
url_query.addQueryItem(QStringLiteral("scope"), QStringLiteral("me"));
url_query.addQueryItem(QStringLiteral("state"), QUrl::toPercentEncoding(code_challenge_));
url_query.addQueryItem(QStringLiteral("state"), QString::fromLatin1(QUrl::toPercentEncoding(code_challenge_)));
url_query.addQueryItem(QStringLiteral("response_type"), QStringLiteral("code"));
QUrl url(kOAuthAuthorizeUrl);
QUrl url(QString::fromLatin1(kOAuthAuthorizeUrl));
url.setQuery(url_query);
const bool result = QDesktopServices::openUrl(url);
@@ -136,7 +139,7 @@ void GeniusLyricsProvider::RedirectArrived() {
AuthError(QUrlQuery(url).queryItemValue(QStringLiteral("error")));
}
else if (url_query.hasQueryItem(QStringLiteral("code"))) {
QUrl redirect_url(kOAuthRedirectUrl);
QUrl redirect_url(QString::fromLatin1(kOAuthRedirectUrl));
redirect_url.setPort(server_->url().port());
RequestAccessToken(url, redirect_url);
}
@@ -169,14 +172,14 @@ void GeniusLyricsProvider::RequestAccessToken(const QUrl &url, const QUrl &redir
const QString code = url_query.queryItemValue(QStringLiteral("code"));
QUrlQuery new_url_query;
new_url_query.addQueryItem(QStringLiteral("code"), QUrl::toPercentEncoding(code));
new_url_query.addQueryItem(QStringLiteral("client_id"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
new_url_query.addQueryItem(QStringLiteral("client_secret"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientSecretB64)));
new_url_query.addQueryItem(QStringLiteral("redirect_uri"), QUrl::toPercentEncoding(redirect_url.toString()));
new_url_query.addQueryItem(QStringLiteral("code"), QString::fromLatin1(QUrl::toPercentEncoding(code)));
new_url_query.addQueryItem(QStringLiteral("client_id"), QString::fromLatin1(QUrl::toPercentEncoding(QLatin1String(kClientIDB64))));
new_url_query.addQueryItem(QStringLiteral("client_secret"), QString::fromLatin1(QUrl::toPercentEncoding(QLatin1String(kClientSecretB64))));
new_url_query.addQueryItem(QStringLiteral("redirect_uri"), QString::fromLatin1(QUrl::toPercentEncoding(redirect_url.toString())));
new_url_query.addQueryItem(QStringLiteral("grant_type"), QStringLiteral("authorization_code"));
new_url_query.addQueryItem(QStringLiteral("response_type"), QStringLiteral("code"));
QUrl new_url(kOAuthAccessTokenUrl);
QUrl new_url(QString::fromLatin1(kOAuthAccessTokenUrl));
QNetworkRequest req(new_url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
QByteArray query = new_url_query.toString(QUrl::FullyEncoded).toUtf8();
@@ -218,7 +221,7 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
}
else {
// See if there is Json data containing "status" and "userMessage" then use that instead.
QByteArray data = reply->readAll();
const QByteArray data = reply->readAll();
QJsonParseError json_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
@@ -242,7 +245,7 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
}
}
QByteArray data = reply->readAll();
const QByteArray data = reply->readAll();
QJsonParseError json_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
@@ -275,7 +278,7 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
access_token_ = json_obj[QStringLiteral("access_token")].toString();
QSettings s;
Settings s;
s.beginGroup(kSettingsGroup);
s.setValue("access_token", access_token_);
s.endGroup();
@@ -297,9 +300,9 @@ bool GeniusLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &
requests_search_.insert(id, search);
QUrlQuery url_query;
url_query.addQueryItem(QStringLiteral("q"), QUrl::toPercentEncoding(QStringLiteral("%1 %2").arg(request.artist, request.title)));
url_query.addQueryItem(QStringLiteral("q"), QString::fromLatin1(QUrl::toPercentEncoding(QStringLiteral("%1 %2").arg(request.artist, request.title))));
QUrl url(kUrlSearch);
QUrl url(QString::fromLatin1(kUrlSearch));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
@@ -460,14 +463,14 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear
return;
}
QByteArray data = reply->readAll();
const QByteArray data = reply->readAll();
if (data.isEmpty()) {
Error(QStringLiteral("Empty reply received from server."));
EndSearch(search, lyric);
return;
}
QString content = QString::fromUtf8(data);
const QString content = QString::fromUtf8(data);
QString lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression(QStringLiteral("<div[^>]*>")), QRegularExpression(QStringLiteral("<\\/div>")), QRegularExpression(QStringLiteral("<div data-lyrics-container=[^>]+>")), true);
if (lyrics.isEmpty()) {
lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression(QStringLiteral("<div[^>]*>")), QRegularExpression(QStringLiteral("<\\/div>")), QRegularExpression(QStringLiteral("<div class=\"lyrics\">")), true);

View File

@@ -86,15 +86,6 @@ class GeniusLyricsProvider : public JsonLyricsProvider {
void HandleSearchReply(QNetworkReply *reply, const int id);
void HandleLyricReply(QNetworkReply *reply, const int search_id, const QUrl &url);
private:
static const char *kSettingsGroup;
static const char *kClientIDB64;
static const char *kClientSecretB64;
static const char *kOAuthAuthorizeUrl;
static const char *kOAuthAccessTokenUrl;
static const char *kOAuthRedirectUrl;
static const char *kUrlSearch;
private:
LocalRedirectServer *server_;
QString code_verifier_;

View File

@@ -56,7 +56,7 @@ bool HtmlLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &re
QUrl url(Url(request));
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
req.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0");
req.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"));
QNetworkReply *reply = network_->get(req);
replies_ << reply;
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, id, request]() { HandleLyricsReply(reply, id, request); });
@@ -154,11 +154,11 @@ QString HtmlLyricsProvider::ParseLyricsFromHTML(const QString &content, const QR
if (end_lyrics_idx != -1 && start_lyrics_idx < end_lyrics_idx) {
if (!lyrics.isEmpty()) {
lyrics.append("\n");
lyrics.append(QLatin1Char('\n'));
}
lyrics.append(content.mid(start_lyrics_idx, end_lyrics_idx - start_lyrics_idx)
.remove('\r')
.remove('\n')
.remove(QLatin1Char('\r'))
.remove(QLatin1Char('\n'))
.remove(QRegularExpression(QStringLiteral("<a [^>]*>[^<]*</a>")))
.remove(QRegularExpression(QStringLiteral("<script>[^>]*</script>")))
.remove(QRegularExpression(QStringLiteral("<div [^>]*>×</div>")))

View File

@@ -29,28 +29,30 @@
#include "lyricssearchrequest.h"
#include "letraslyricsprovider.h"
const char LetrasLyricsProvider::kUrl[] = "https://www.letras.mus.br/winamp.php";
const char LetrasLyricsProvider::kStartTag[] = "<div[^>]*>";
const char LetrasLyricsProvider::kEndTag[] = "<\\/div>";
const char LetrasLyricsProvider::kLyricsStart[] = "<div id=\"letra-cnt\">";
namespace {
constexpr char kUrl[] = "https://www.letras.mus.br/winamp.php";
constexpr char kStartTag[] = "<div[^>]*>";
constexpr char kEndTag[] = "<\\/div>";
constexpr char kLyricsStart[] = "<div id=\"letra-cnt\">";
} // namespace
LetrasLyricsProvider::LetrasLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
: HtmlLyricsProvider(QStringLiteral("letras.mus.br"), true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
: HtmlLyricsProvider(QStringLiteral("letras.mus.br"), true, QLatin1String(kStartTag), QLatin1String(kEndTag), QLatin1String(kLyricsStart), false, network, parent) {}
QUrl LetrasLyricsProvider::Url(const LyricsSearchRequest &request) {
return QUrl(QString(kUrl) + QStringLiteral("?musica=") + StringFixup(request.artist) + "&artista=" + StringFixup(request.title));
return QUrl(QLatin1String(kUrl) + QStringLiteral("?musica=") + StringFixup(request.artist) + QStringLiteral("&artista=") + StringFixup(request.title));
}
QString LetrasLyricsProvider::StringFixup(const QString &text) {
return QUrl::toPercentEncoding(Utilities::Transliterate(text)
return QString::fromLatin1(QUrl::toPercentEncoding(Utilities::Transliterate(text)
.replace(QRegularExpression(QStringLiteral("[^\\w0-9_,&\\-\\(\\) ]")), QStringLiteral("_"))
.replace(QRegularExpression(QStringLiteral(" {2,}")), QStringLiteral(" "))
.simplified()
.replace(' ', '-')
.replace(QLatin1Char(' '), QLatin1Char('-'))
.toLower()
);
));
}

View File

@@ -41,12 +41,6 @@ class LetrasLyricsProvider : public HtmlLyricsProvider {
private:
QString StringFixup(const QString &text);
private:
static const char kUrl[];
static const char kStartTag[];
static const char kEndTag[];
static const char kLyricsStart[];
};
#endif // LETRASLYRICSPROVIDER_H

View File

@@ -37,7 +37,9 @@
#include "lyricssearchresult.h"
#include "lololyricsprovider.h"
const char *LoloLyricsProvider::kUrlSearch = "http://api.lololyrics.com/0.5/getLyric";
namespace {
constexpr char kUrlSearch[] = "http://api.lololyrics.com/0.5/getLyric";
}
LoloLyricsProvider::LoloLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : LyricsProvider(QStringLiteral("LoloLyrics"), true, false, network, parent) {}
@@ -55,10 +57,10 @@ LoloLyricsProvider::~LoloLyricsProvider() {
bool LoloLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
QUrlQuery url_query;
url_query.addQueryItem(QStringLiteral("artist"), QUrl::toPercentEncoding(request.artist));
url_query.addQueryItem(QStringLiteral("track"), QUrl::toPercentEncoding(request.title));
url_query.addQueryItem(QStringLiteral("artist"), QString::fromLatin1(QUrl::toPercentEncoding(request.artist)));
url_query.addQueryItem(QStringLiteral("track"), QString::fromLatin1(QUrl::toPercentEncoding(request.title)));
QUrl url(kUrlSearch);
QUrl url(QString::fromLatin1(kUrlSearch));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
@@ -103,15 +105,15 @@ void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, c
QXmlStreamReader::TokenType type = reader.readNext();
QString name = reader.name().toString();
if (type == QXmlStreamReader::StartElement) {
if (name == "result") {
if (name == QStringLiteral("result")) {
status.clear();
result = LyricsSearchResult();
}
else if (name == "status") {
else if (name == QStringLiteral("status")) {
status = reader.readElementText();
}
else if (name == "response") {
if (status == "OK") {
else if (name == QStringLiteral("response")) {
if (status == QStringLiteral("OK")) {
result.lyrics = reader.readElementText();
}
else {
@@ -121,7 +123,7 @@ void LoloLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id, c
}
}
else if (type == QXmlStreamReader::EndElement) {
if (name == "result") {
if (name == QStringLiteral("result")) {
if (!result.lyrics.isEmpty()) {
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);
results << result;

View File

@@ -52,7 +52,6 @@ class LoloLyricsProvider : public LyricsProvider {
void HandleSearchReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request);
private:
static const char *kUrlSearch;
QList<QNetworkReply*> replies_;
};

View File

@@ -28,6 +28,7 @@
#include <QSettings>
#include "core/logging.h"
#include "core/settings.h"
#include "lyricsprovider.h"
#include "lyricsproviders.h"
@@ -55,7 +56,7 @@ void LyricsProviders::ReloadSettings() {
all_providers.insert(provider->order(), provider->name());
}
QSettings s;
Settings s;
s.beginGroup(LyricsSettingsPage::kSettingsGroup);
QStringList providers_enabled = s.value("providers", QStringList() << all_providers.values()).toStringList();
s.endGroup();

View File

@@ -79,12 +79,12 @@ void MusixmatchLyricsProvider::CancelSearch(const int id) { Q_UNUSED(id); }
bool MusixmatchLyricsProvider::SendSearchRequest(LyricsSearchContextPtr search) {
QUrlQuery url_query;
url_query.addQueryItem(QStringLiteral("apikey"), QByteArray::fromBase64(kApiKey));
url_query.addQueryItem(QStringLiteral("q_artist"), QUrl::toPercentEncoding(search->request.artist));
url_query.addQueryItem(QStringLiteral("q_track"), QUrl::toPercentEncoding(search->request.title));
url_query.addQueryItem(QStringLiteral("apikey"), QString::fromLatin1(QByteArray::fromBase64(kApiKey)));
url_query.addQueryItem(QStringLiteral("q_artist"), QString::fromLatin1(QUrl::toPercentEncoding(search->request.artist)));
url_query.addQueryItem(QStringLiteral("q_track"), QString::fromLatin1(QUrl::toPercentEncoding(search->request.title)));
url_query.addQueryItem(QStringLiteral("f_has_lyrics"), QStringLiteral("1"));
QUrl url(QString(kApiUrl) + QStringLiteral("/track.search"));
QUrl url(QString::fromLatin1(kApiUrl) + QStringLiteral("/track.search"));
url.setQuery(url_query);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
@@ -290,16 +290,16 @@ void MusixmatchLyricsProvider::HandleLyricsReply(QNetworkReply *reply, LyricsSea
return;
}
QByteArray data = reply->readAll();
const QByteArray data = reply->readAll();
if (data.isEmpty()) {
Error(QStringLiteral("Empty reply received from server."));
EndSearch(search, url);
return;
}
QString content = data;
QString data_begin = QStringLiteral("<script id=\"__NEXT_DATA__\" type=\"application/json\">");
QString data_end = QStringLiteral("</script>");
const QString content = QString::fromUtf8(data);
const QString data_begin = QStringLiteral("<script id=\"__NEXT_DATA__\" type=\"application/json\">");
const QString data_end = QStringLiteral("</script>");
qint64 begin_idx = content.indexOf(data_begin);
QString content_json;
if (begin_idx > 0) {

View File

@@ -36,7 +36,9 @@
#include "jsonlyricsprovider.h"
#include "ovhlyricsprovider.h"
const char *OVHLyricsProvider::kUrlSearch = "https://api.lyrics.ovh/v1/";
namespace {
constexpr char kUrlSearch[] = "https://api.lyrics.ovh/v1/";
}
OVHLyricsProvider::OVHLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : JsonLyricsProvider(QStringLiteral("Lyrics.ovh"), true, false, network, parent) {}
@@ -53,7 +55,7 @@ OVHLyricsProvider::~OVHLyricsProvider() {
bool OVHLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &request) {
QUrl url(kUrlSearch + QString(QUrl::toPercentEncoding(request.artist)) + "/" + QString(QUrl::toPercentEncoding(request.title)));
QUrl url(QString::fromLatin1(kUrlSearch) + QString::fromLatin1(QUrl::toPercentEncoding(request.artist)) + QLatin1Char('/') + QString::fromLatin1(QUrl::toPercentEncoding(request.title)));
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
QNetworkReply *reply = network_->get(req);

View File

@@ -52,7 +52,6 @@ class OVHLyricsProvider : public JsonLyricsProvider {
void HandleSearchReply(QNetworkReply *reply, const int id, const LyricsSearchRequest &request);
private:
static const char *kUrlSearch;
QList<QNetworkReply*> replies_;
};

View File

@@ -27,28 +27,30 @@
#include "lyricssearchrequest.h"
#include "songlyricscomlyricsprovider.h"
const char SongLyricsComLyricsProvider::kUrl[] = "https://www.songlyrics.com/";
const char SongLyricsComLyricsProvider::kStartTag[] = "<p[^>]*>";
const char SongLyricsComLyricsProvider::kEndTag[] = "<\\/p>";
const char SongLyricsComLyricsProvider::kLyricsStart[] = "<p id=\"songLyricsDiv\"[^>]+>";
namespace {
constexpr char kUrl[] = "https://www.songlyrics.com/";
constexpr char kStartTag[] = "<p[^>]*>";
constexpr char kEndTag[] = "<\\/p>";
constexpr char kLyricsStart[] = "<p id=\"songLyricsDiv\"[^>]+>";
} // namespace
SongLyricsComLyricsProvider::SongLyricsComLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent)
: HtmlLyricsProvider(QStringLiteral("songlyrics.com"), true, kStartTag, kEndTag, kLyricsStart, false, network, parent) {}
: HtmlLyricsProvider(QStringLiteral("songlyrics.com"), true, QLatin1String(kStartTag), QLatin1String(kEndTag), QLatin1String(kLyricsStart), false, network, parent) {}
QUrl SongLyricsComLyricsProvider::Url(const LyricsSearchRequest &request) {
return QUrl(kUrl + StringFixup(request.artist) + "/" + StringFixup(request.title) + "-lyrics/");
return QUrl(QLatin1String(kUrl) + StringFixup(request.artist) + QLatin1Char('/') + StringFixup(request.title) + QStringLiteral("-lyrics/"));
}
QString SongLyricsComLyricsProvider::StringFixup(QString text) {
return text.replace('/', '-')
.replace('\'', '-')
return text.replace(QLatin1Char('/'), QLatin1Char('-'))
.replace(QLatin1Char('\''), QLatin1Char('-'))
.remove(QRegularExpression(QStringLiteral("[^\\w0-9\\- ]")))
.replace(QRegularExpression(QStringLiteral(" {2,}")), QStringLiteral(" "))
.simplified()
.replace(' ', '-')
.replace(QLatin1Char(' '), QLatin1Char('-'))
.replace(QRegularExpression(QStringLiteral("(-)\\1+")), QStringLiteral("-"))
.toLower();

View File

@@ -41,12 +41,6 @@ class SongLyricsComLyricsProvider : public HtmlLyricsProvider {
private:
QString StringFixup(QString text);
private:
static const char kUrl[];
static const char kStartTag[];
static const char kEndTag[];
static const char kLyricsStart[];
};
#endif // SONGLYRICSCOMLYRICSPROVIDER_H