To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / import / ImportHoboken.cpp

History | View | Annotate | Download (5.57 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2

    
3
#include "ImportHoboken.h"
4

    
5
#include <dataquay/Debug.h>
6

    
7
#include <QFile>
8
#include <QFileInfo>
9
#include <QTextStream>
10
#include <QRegExp>
11
#include <QVariant>
12

    
13
#include <exception>
14

    
15
using namespace Dataquay;
16

    
17
namespace ClassicalData {
18

    
19
void
20
HobokenImporter::setSource(QUrl source)
21
{
22
    DEBUG << "HobokenImporter::setSource: " << source << endl;
23
    import(source);
24
}
25

    
26
QString
27
hobToForm(QString hob)
28
{
29
    QStringList bits = hob.split(':');
30
    QString group = bits[0];
31
    int num = bits[1].toInt();
32
    if (group == "I") return "symphony";
33
    if (group == "II" && (num <= 24 || !bits[1][0].isDigit())) return "divertimento";
34
    if (group == "III") return "string quartet";
35
    if (group == "IV") return "divertimento";
36
    if (group == "V") return "string trio;trio";
37
    if (group == "VI") return "string duo;duo;sonata";
38
    if (group == "VII") return "concerto";
39
    if (group == "VIII") return "march";
40
    if (group == "IX") return "dance";
41
    if (group == "X") return "divertimento";
42
    if (group == "XI") return "trio";
43
    if (group == "XII") return "duo";
44
    if (group == "XIII") return "concerto";
45
    if (group == "XIV") return "divertimento";
46
    if (group == "XV") return "piano trio;trio";
47
    if (group == "XVI") return "piano sonata;sonata";
48
    if (group == "XVII") return "work for piano";
49
    if (group == "XVIIa") return "work for piano";
50
    if (group == "XVIII") return "piano concerto;concerto";
51
    if (group == "XXII") return "mass";
52
    if (group == "XXIIa") return "requiem";
53
    //!!! choral works
54
    return "";
55
}
56

    
57
void
58
HobokenImporter::import(QUrl source)
59
{
60
    //!!! for now
61
    QString filename = source.toLocalFile();
62

    
63
    QFile file(filename);
64
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
65
        throw std::exception();
66
    }
67

    
68
    QTextStream stream(&file);
69
    stream.setCodec("UTF-8");
70
    
71
    QString composerName = "Joseph Haydn";
72

    
73
    DEBUG << "composerName = " << composerName << endl;
74

    
75
    QMap<QString, Work *> hobMap;
76
    QMap<int, Work *> opusMap;
77

    
78
    while (!stream.atEnd()) {
79

    
80
        QString line = stream.readLine();
81

    
82
        QString hob = "";
83

    
84
        QRegExp hobre("^([\\d][^ _]+_([A-Za-z]*)[^ ]+) ");
85

    
86
        if (hobre.indexIn(line) >= 0) {
87

    
88
            hob = hobre.cap(1);
89
            Work *w = 0;
90
            Composition *cn = 0;
91

    
92
            if (!hobMap.contains(hob)) {
93
                w = new Work();
94
                QString key = hobre.cap(2);
95
                if (key != "") {
96
                    if (key.length() > 1 && key[1] == 's') {
97
                        key = key[0] + "-flat";
98
                    }
99
                    if (key[0].isLower()) {
100
                        key[0] = key[0].toUpper();
101
                        key += " minor";
102
                    } else {
103
                        key += " major";
104
                    }
105
                    w->setKey(key);
106
                }
107
                cn = new Composition();
108
                cn->setComposerName(composerName);
109
                cn->addWork(w);
110
                w->setComposition(cn);
111
                hobMap[hob] = w;
112
            } else {
113
                w = hobMap[hob];
114
                cn = w->composition();
115
            }
116

    
117
            QRegExp hobre2("^[^ ]+ # (Hob [^ ]*)");
118
            if (hobre2.indexIn(line) >= 0) {
119
                QString hobtext = hobre2.cap(1);
120
                w->setCatalogue(hobtext);
121
                QStringList forms = hobToForm(hobtext).split(";");
122
                foreach (QString f, forms) {
123
                    if (f != "") {
124
                        w->addForm(Form::getFormByName(f));
125
                    }
126
                }
127
                continue;
128
            }
129

    
130
            QRegExp titlere("^[^ ]+ @([^ ]+) (.*)");
131
            if (titlere.indexIn(line) >= 0) {
132
                QString title = titlere.cap(2).trimmed();
133
                if (titlere.cap(1) == "en") {
134
                    if (w->name() != "") {
135
                        w->addAlias(w->name());
136
                    }
137
                    w->setName(title);
138
                } else {
139
                    if (w->name() == "") {
140
                        w->setName(title);
141
                    } else {
142
                        w->addAlias(title);
143
                    }
144
                }
145
                continue;
146
            }
147

    
148
            QRegExp httpre("^[^ ]+ (http:[^ ]*) *$");
149
            if (httpre.indexIn(line) >= 0) {
150
                QString url = httpre.cap(1).trimmed();
151
                Document *d = new Document;
152
                d->setUri(Uri(url));
153
                d->setTopic(w);
154
                if (url.contains("wikipedia")) d->setSiteName("Wikipedia");
155
                else if (url.contains("klassika.info")) {
156
                    d->setSiteName("Klassika - Die deutschsprachigen Klassikseiten");
157
                }
158
                w->addPage(d);
159
                continue;
160
            }
161
            
162
            QRegExp datere("^[^ ]+ \\[[^]]*(\\d{4})[^]]*\\]");
163
            if (datere.indexIn(line) >= 0) {
164
                cn->setYear(datere.cap(1).toInt());
165
                continue;
166
            }
167
            
168
            QRegExp opre("^[^ ]+ -> ([^ ]+)");
169
            if (opre.indexIn(line) >= 0) {
170
                QString optext = opre.cap(1);
171
                w->setOpus(optext);
172
                if (optext.contains('/')) {
173
                    QStringList ops = optext.split('/');
174
                    int opno = ops[0].toInt();
175
                    if (opno == 0) {
176
                        DEBUG << "Failed to convert " << optext << " to op no" << endl;
177
                    } else {
178
                        if (!opusMap.contains(opno)) {
179
                            opusMap[opno] = new Work();
180
                            opusMap[opno]->setOpus(ops[0]);
181
                            opusMap[opno]->setComposition(new Composition());
182
                            opusMap[opno]->composition()->setComposerName(composerName);
183
                        }
184
                        opusMap[opno]->addPart(w);
185
                        w->setPartOf(opusMap[opno]);
186
                        w->setOpus(ops[0]);
187
                        w->setNumber(ops[1]);
188
                    }
189
                }
190
                continue;
191
            }
192

    
193
            continue;
194
        }
195

    
196
        QRegExp opre("^Opus ([\\d][^ ]*): (.*)");
197
        if (opre.indexIn(line) >= 0) {
198
            QString optext = opre.cap(1);
199
            int opno = optext.toInt();
200
            if (!opusMap.contains(opno)) {
201
                opusMap[opno] = new Work();
202
                opusMap[opno]->setOpus(optext);
203
                opusMap[opno]->setComposition(new Composition());
204
                opusMap[opno]->composition()->setComposerName(composerName);
205
            }
206
            QString title = opre.cap(2);
207
            title.replace("<br>", " - ");
208
            opusMap[opno]->setName(title);
209
            continue;
210
        }
211

    
212
        DEBUG << "Failed to match line: " << line << endl;
213
    }
214

    
215
    foreach (Work *w, hobMap) m_objects.push_back(w);
216
    foreach (Work *w, opusMap) m_objects.push_back(w);
217

    
218

    
219
    DEBUG << "Found " << m_objects.size() << " things" << endl;
220
}
221

    
222

    
223
}
224

    
225