LyricsProvider: Parse multiple sections of the same tag
This commit is contained in:
parent
43c14ae71b
commit
bf904a6afa
@ -480,9 +480,9 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString content = QString::fromUtf8(data);
|
QString content = QString::fromUtf8(data);
|
||||||
QString lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div data-lyrics-container=[^>]+>"));
|
QString lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div data-lyrics-container=[^>]+>"), true);
|
||||||
if (lyrics.isEmpty()) {
|
if (lyrics.isEmpty()) {
|
||||||
lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div class=\"lyrics\">"));
|
lyrics = ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div class=\"lyrics\">"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lyrics.isEmpty()) {
|
if (!lyrics.isEmpty()) {
|
||||||
|
@ -30,40 +30,58 @@
|
|||||||
LyricsProvider::LyricsProvider(const QString &name, const bool enabled, const bool authentication_required, NetworkAccessManager *network, QObject *parent)
|
LyricsProvider::LyricsProvider(const QString &name, const bool enabled, const bool authentication_required, NetworkAccessManager *network, QObject *parent)
|
||||||
: QObject(parent), network_(network), name_(name), enabled_(enabled), order_(0), authentication_required_(authentication_required) {}
|
: QObject(parent), network_(network), name_(name), enabled_(enabled), order_(0), authentication_required_(authentication_required) {}
|
||||||
|
|
||||||
QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start) {
|
QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple) {
|
||||||
|
|
||||||
QRegularExpressionMatch rematch = lyrics_start.match(content);
|
QString lyrics;
|
||||||
if (!rematch.hasMatch()) return QString();
|
qint64 start_idx = 0;
|
||||||
|
|
||||||
const qint64 start_lyrics_tag_idx = rematch.capturedEnd();
|
|
||||||
|
|
||||||
// Find the index of the end tag.
|
|
||||||
qint64 start_tag_idx = 0;
|
|
||||||
qint64 end_tag_idx = 0;
|
|
||||||
qint64 idx = start_lyrics_tag_idx;
|
|
||||||
int tags = 1;
|
|
||||||
do {
|
do {
|
||||||
QRegularExpressionMatch rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
|
||||||
start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
QRegularExpressionMatch rematch = lyrics_start.match(content, start_idx);
|
||||||
QRegularExpressionMatch rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
if (!rematch.hasMatch()) break;
|
||||||
end_tag_idx = rematch_end_tag.hasMatch() ? rematch_end_tag.capturedStart() : -1;
|
|
||||||
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
const qint64 start_lyrics_tag_idx = rematch.capturedEnd();
|
||||||
++tags;
|
|
||||||
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
// Find the index of the end tag.
|
||||||
|
qint64 start_tag_idx = 0;
|
||||||
|
qint64 end_tag_idx = 0;
|
||||||
|
qint64 end_tag_length = 0;
|
||||||
|
qint64 idx = start_lyrics_tag_idx;
|
||||||
|
int tags = 1;
|
||||||
|
do {
|
||||||
|
QRegularExpressionMatch rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
||||||
|
start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
||||||
|
QRegularExpressionMatch rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
||||||
|
if (rematch_end_tag.hasMatch()) {
|
||||||
|
end_tag_idx = rematch_end_tag.capturedStart();
|
||||||
|
end_tag_length = rematch_end_tag.capturedLength();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
end_tag_idx = -1;
|
||||||
|
end_tag_length = 0;
|
||||||
|
}
|
||||||
|
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
||||||
|
++tags;
|
||||||
|
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
||||||
|
}
|
||||||
|
else if (rematch_end_tag.hasMatch()) {
|
||||||
|
--tags;
|
||||||
|
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (rematch_end_tag.hasMatch()) {
|
while (tags > 0 || end_tag_idx >= start_tag_idx);
|
||||||
--tags;
|
|
||||||
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
start_idx = end_tag_idx + end_tag_length;
|
||||||
|
|
||||||
|
if (end_tag_idx > 0 || start_lyrics_tag_idx < end_tag_idx) {
|
||||||
|
lyrics.append(content.mid(start_lyrics_tag_idx, end_tag_idx - start_lyrics_tag_idx)
|
||||||
|
.replace(QRegularExpression("<br[^>]+>"), "\n")
|
||||||
|
.remove(QRegularExpression("<[^>]*>"))
|
||||||
|
.trimmed());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
while (tags > 0 || end_tag_idx >= start_tag_idx);
|
while (start_idx > 0 && multiple);
|
||||||
|
|
||||||
if (end_tag_idx <= 0 || start_lyrics_tag_idx >= end_tag_idx) return QString();
|
|
||||||
|
|
||||||
QString lyrics = content.mid(start_lyrics_tag_idx, end_tag_idx - start_lyrics_tag_idx)
|
|
||||||
.replace(QRegularExpression("<br[^>]+>"), "\n")
|
|
||||||
.remove(QRegularExpression("<[^>]*>"))
|
|
||||||
.trimmed();
|
|
||||||
|
|
||||||
if (lyrics.length() > 6000 || lyrics.contains("there are no lyrics to", Qt::CaseInsensitive)) {
|
if (lyrics.length() > 6000 || lyrics.contains("there are no lyrics to", Qt::CaseInsensitive)) {
|
||||||
return QString();
|
return QString();
|
||||||
|
@ -57,7 +57,7 @@ class LyricsProvider : public QObject {
|
|||||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QString ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start);
|
QString ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start, const bool multiple);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void AuthenticationComplete(bool, QStringList = QStringList());
|
void AuthenticationComplete(bool, QStringList = QStringList());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user