LyricsProvider: Add function for parsing lyrics from HTML
This commit is contained in:
parent
3f4bf5f512
commit
1a25faa5b9
|
@ -21,9 +21,55 @@
|
|||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include "core/utilities.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "lyricsprovider.h"
|
||||
|
||||
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) {}
|
||||
|
||||
QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start) {
|
||||
|
||||
QRegularExpressionMatch rematch = lyrics_start.match(content);
|
||||
if (!rematch.hasMatch()) return QString();
|
||||
|
||||
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 {
|
||||
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);
|
||||
end_tag_idx = rematch_end_tag.hasMatch() ? rematch_end_tag.capturedStart() : -1;
|
||||
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();
|
||||
}
|
||||
}
|
||||
while (tags > 0 || end_tag_idx >= start_tag_idx);
|
||||
|
||||
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)) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return Utilities::DecodeHtmlEntities(lyrics);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include "lyricsfetcher.h"
|
||||
|
||||
|
@ -55,6 +56,9 @@ class LyricsProvider : public QObject {
|
|||
|
||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||
|
||||
protected:
|
||||
QString ParseLyricsFromHTML(const QString &content, const QRegularExpression &start_tag, const QRegularExpression &end_tag, const QRegularExpression &lyrics_start);
|
||||
|
||||
signals:
|
||||
void AuthenticationComplete(bool, QStringList = QStringList());
|
||||
void AuthenticationSuccess();
|
||||
|
|
Loading…
Reference in New Issue