2010-09-26 16:21:23 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-09-26 16:21:23 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ultimatelyricsreader.h"
|
|
|
|
|
2010-10-10 18:09:20 +02:00
|
|
|
#include <QCoreApplication>
|
2010-09-26 16:21:23 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "ultimatelyricsprovider.h"
|
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
UltimateLyricsReader::UltimateLyricsReader(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent), thread_(qApp->thread()) {}
|
2010-09-26 16:21:23 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void UltimateLyricsReader::SetThread(QThread* thread) { thread_ = thread; }
|
2013-07-10 13:24:23 +02:00
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
QList<SongInfoProvider*> UltimateLyricsReader::Parse(
|
|
|
|
const QString& filename) const {
|
2010-09-26 16:21:23 +02:00
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Error opening" << filename;
|
2010-10-10 18:09:20 +02:00
|
|
|
return QList<SongInfoProvider*>();
|
2010-09-26 16:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParseDevice(&file);
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
QList<SongInfoProvider*> UltimateLyricsReader::ParseDevice(
|
|
|
|
QIODevice* device) const {
|
2010-10-10 18:09:20 +02:00
|
|
|
QList<SongInfoProvider*> ret;
|
2010-09-26 16:21:23 +02:00
|
|
|
|
|
|
|
QXmlStreamReader reader(device);
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
reader.readNext();
|
|
|
|
|
|
|
|
if (reader.name() == "provider") {
|
2010-10-10 18:09:20 +02:00
|
|
|
SongInfoProvider* provider = ParseProvider(&reader);
|
|
|
|
if (provider) {
|
2013-07-10 13:24:23 +02:00
|
|
|
provider->moveToThread(thread_);
|
2010-09-26 16:21:23 +02:00
|
|
|
ret << provider;
|
2010-10-10 18:09:20 +02:00
|
|
|
}
|
2010-09-26 16:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
SongInfoProvider* UltimateLyricsReader::ParseProvider(
|
|
|
|
QXmlStreamReader* reader) const {
|
2010-09-26 16:21:23 +02:00
|
|
|
QXmlStreamAttributes attributes = reader->attributes();
|
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
UltimateLyricsProvider* scraper = new UltimateLyricsProvider;
|
2010-09-26 16:21:23 +02:00
|
|
|
scraper->set_name(attributes.value("name").toString());
|
|
|
|
scraper->set_title(attributes.value("title").toString());
|
|
|
|
scraper->set_charset(attributes.value("charset").toString());
|
|
|
|
scraper->set_url(attributes.value("url").toString());
|
|
|
|
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
reader->readNext();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reader->tokenType() == QXmlStreamReader::EndElement) break;
|
2010-09-26 16:21:23 +02:00
|
|
|
|
|
|
|
if (reader->tokenType() == QXmlStreamReader::StartElement) {
|
|
|
|
if (reader->name() == "extract")
|
|
|
|
scraper->add_extract_rule(ParseRule(reader));
|
|
|
|
else if (reader->name() == "exclude")
|
|
|
|
scraper->add_exclude_rule(ParseRule(reader));
|
|
|
|
else if (reader->name() == "invalidIndicator")
|
|
|
|
scraper->add_invalid_indicator(ParseInvalidIndicator(reader));
|
|
|
|
else if (reader->name() == "urlFormat") {
|
2014-02-07 16:34:20 +01:00
|
|
|
scraper->add_url_format(
|
|
|
|
reader->attributes().value("replace").toString(),
|
|
|
|
reader->attributes().value("with").toString());
|
2010-09-26 16:21:23 +02:00
|
|
|
reader->skipCurrentElement();
|
2014-02-07 16:34:20 +01:00
|
|
|
} else
|
2010-09-26 16:21:23 +02:00
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scraper;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
UltimateLyricsProvider::Rule UltimateLyricsReader::ParseRule(
|
|
|
|
QXmlStreamReader* reader) const {
|
2010-10-10 18:09:20 +02:00
|
|
|
UltimateLyricsProvider::Rule ret;
|
2010-09-26 16:21:23 +02:00
|
|
|
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
reader->readNext();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reader->tokenType() == QXmlStreamReader::EndElement) break;
|
2010-09-26 16:21:23 +02:00
|
|
|
|
|
|
|
if (reader->tokenType() == QXmlStreamReader::StartElement) {
|
|
|
|
if (reader->name() == "item") {
|
|
|
|
QXmlStreamAttributes attr = reader->attributes();
|
|
|
|
if (attr.hasAttribute("tag"))
|
2014-02-07 16:34:20 +01:00
|
|
|
ret << UltimateLyricsProvider::RuleItem(attr.value("tag").toString(),
|
|
|
|
QString());
|
2013-12-03 10:21:52 +01:00
|
|
|
else if (attr.hasAttribute("url"))
|
2014-02-07 16:34:20 +01:00
|
|
|
ret << UltimateLyricsProvider::RuleItem(attr.value("url").toString(),
|
|
|
|
QString());
|
2010-09-26 16:21:23 +02:00
|
|
|
else if (attr.hasAttribute("begin"))
|
2014-02-07 16:34:20 +01:00
|
|
|
ret << UltimateLyricsProvider::RuleItem(
|
2020-09-18 16:15:19 +02:00
|
|
|
attr.value("begin").toString(), attr.value("end").toString());
|
2010-09-26 16:21:23 +02:00
|
|
|
}
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
QString UltimateLyricsReader::ParseInvalidIndicator(
|
|
|
|
QXmlStreamReader* reader) const {
|
2010-09-26 16:21:23 +02:00
|
|
|
QString ret = reader->attributes().value("value").toString();
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
return ret;
|
|
|
|
}
|