aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/General/lyrics/ultimatelyricsparser.cpp
blob: 07a89684bf586d1992d9dc5fe51ece0e0f946981 (plain) (blame)
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
#include <QXmlStreamReader>
#include <QFile>
#include <QtDebug>
#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
{
    for(LyricsProvider *provider : qAsConst(m_providers))
    {
        if(provider->name() == name)
            return provider;
    }
    return nullptr;
}

QStringList UltimateLyricsParser::defaultProviders()
{
    QStringList out = {
        "lyrics.wikia.com",
        "Encyclopaedia Metallum",
        "letras.mus.br",
        "darklyrics.com"
    };

    return out;
}