1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/***************************************************************************
* Copyright (C) 2019-2021 by Ilya Kotov *
* forkotov02@ya.ru *
* *
* Based on Amarok 2 Ultimate Lyrics script *
* Copyright (C) 2009-2010 Vladimir Brkic <vladimir_brkic@yahoo.com> *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QXmlStreamReader>
#include <QFile>
#include <QtDebug>
#include <algorithm>
#if (QT_VERSION < QT_VERSION_CHECK(5, 7, 0)) //qAsConst template
#include <qmmp/qmmp.h>
#endif
#include "ultimatelyricsparser.h"
UltimateLyricsParser::UltimateLyricsParser()
{}
UltimateLyricsParser::~UltimateLyricsParser()
{
qDeleteAll(m_providers);
m_providers.clear();
}
bool UltimateLyricsParser::load(const QString &path)
{
qDeleteAll(m_providers);
m_providers.clear();
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
m_errorString = file.errorString();
return false;
}
QXmlStreamReader reader(&file);
QString parentElement;
QList<QPair<QString, QString> > args;
while(!reader.atEnd())
{
reader.readNext();
if(reader.isStartElement())
{
if(reader.name() == "provider")
{
LyricsProvider *provider = new LyricsProvider;
QXmlStreamAttributes attrs = reader.attributes();
provider->setName(attrs.value("name").toString());
provider->setTitle(attrs.value("title").toString());
provider->setUrl(attrs.value("url").toString());
provider->setCharset(attrs.value("charser").toString());
m_providers << provider;
}
else if(reader.name() == "urlFormat" && !m_providers.isEmpty())
{
m_providers.last()->addUrlFormat(reader.attributes().value("replace").toString(),
reader.attributes().value("with").toString());
}
else if(reader.name() == "extract" || reader.name() == "exclude")
{
parentElement = reader.name().toString();
}
else if(reader.name() == "invalidIndicator" && !m_providers.isEmpty())
{
m_providers.last()->addInvalidIndicator(reader.attributes().value("value").toString());
}
else if(reader.name() == "item")
{
QXmlStreamAttributes attrs = reader.attributes();
QString arg1, arg2;
if(attrs.hasAttribute("begin") && attrs.hasAttribute("end"))
{
arg1 = attrs.value("begin").toString();
arg2 = attrs.value("end").toString();
}
else if(attrs.hasAttribute("tag"))
{
arg1 = attrs.value("tag").toString();
}
else if(attrs.hasAttribute("url"))
{
arg1 = attrs.value("url").toString();
}
args << qMakePair(arg1, arg2);
}
}
else if(reader.isEndElement())
{
if(reader.name() == "extract" || reader.name() == "exclude")
{
parentElement.clear();
m_providers.last()->addRule(args, reader.name() == "exclude");
args.clear();
}
}
if(reader.hasError())
{
m_errorString = tr("%1 (line: %2)").arg(reader.errorString()).arg(reader.lineNumber());
return false;
}
}
return true;
}
const QString &UltimateLyricsParser::errorString() const
{
return m_errorString;
}
const QList<LyricsProvider *> &UltimateLyricsParser::providers()
{
return m_providers;
}
LyricsProvider *UltimateLyricsParser::provider(const QString &name) const
{
auto it = std::find_if(m_providers.cbegin(), m_providers.cend(), [name](LyricsProvider *provider){ return provider->name() == name; });
return it == m_providers.cend() ? nullptr : *it;
}
const QStringList &UltimateLyricsParser::defaultProviders()
{
static const QStringList out = {
"lyrics.wikia.com",
"Encyclopaedia Metallum",
"letras.mus.br",
"darklyrics.com"
};
return out;
}
|