-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdatasynchronizer.cpp
575 lines (477 loc) · 19.3 KB
/
webdatasynchronizer.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "webdatasynchronizer.h"
WebDataSynchronizer::WebDataSynchronizer(QObject *parent) : QObject(parent)
{
QString dataPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
imagePath = QString("%1%2%3").arg(dataPath, directoryHelper.separator(), "images");
}
void WebDataSynchronizer::setData(QMutex *mutex, QMap<QString, Invertebrate> *invertebrates, QMap<QString, Stream> *streams)
{
this->mutex = mutex;
this->invertebratesFromLocal = invertebrates;
this->streamsFromLocal = streams;
}
void WebDataSynchronizer::run()
{
emit started();
network = QSharedPointer<QNetworkAccessManager>(new QNetworkAccessManager(), [](QNetworkAccessManager *obj){ obj->deleteLater(); });
if(network->networkAccessible() == QNetworkAccessManager::Accessible) {
// qDebug() << "Begin";
syncStreams();
// qDebug() << "Streams done";
syncInvertebrates();
// qDebug() << "Inverts done";
syncImages();
// qDebug() << "Images done";
syncAbout();
// qDebug() << "About done";
finalize();
// qDebug() << "Finalize done";
} else {
emit finished(WebDataSynchonizerExitStatus::FAILED_NETWORK_ACCESS);
}
}
void WebDataSynchronizer::syncStreams()
{
if(!syncingShouldContinue) {
// qDebug() << "We're not OK";
return;
}
QUrl url("https://wikieducator.org/api.php");
QUrlQuery query;
query.addQueryItem("action", "query");
query.addQueryItem("list", "categorymembers");
query.addQueryItem("cmtitle", "Category:Stream"); // Only difference with Invertebrate Query...
query.addQueryItem("cmlimit", "500");
query.addQueryItem("format", "json");
query.addQueryItem("action", "query");
query.addQueryItem("cmprop", "ids|title");
url.setQuery(query);
bool ok;
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(synchronouslyGetUrl(url, &ok));
if(ok) {
handleNetworkReplyForStreamList(reply.data());
} else {
syncingShouldContinue = false;
}
}
void WebDataSynchronizer::handleNetworkReplyForStreamList(QNetworkReply *reply)
{
if(!syncingShouldContinue) {
// qDebug() << "A network error occurred, or we're not OK: " << reply->errorString() << " syncingShouldContinue: " << syncingShouldContinue;
return;
}
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
if(doc.isNull()) {
// qDebug() << "Doc is null/invalid.";
syncingShouldContinue = false;
return;
}
QJsonArray streamValues = doc.object().value("query").toObject().value("categorymembers").toArray();
QStringList streamTitles;
for(const QJsonValue &value: streamValues) {
streamTitles.append(value.toObject().value("title").toString());
}
// MediaWiki API recommends sorting so caching is more likely to occur, and of course uniqueing
std::sort(streamTitles.begin(), streamTitles.end());
auto end = std::unique(streamTitles.begin(), streamTitles.end());
streamTitles.erase(end, streamTitles.end());
QUrl url("https://wikieducator.org/api.php");
QUrlQuery query;
query.addQueryItem("action", "query");
query.addQueryItem("export", "");
query.addQueryItem("exportnowrap", "");
int i = 0;
bool ok;
do {
int batchStart = i * batchSize;
int batchEnd = batchStart + batchSize;
QStringList streamTitlesBatch = streamTitles.mid(batchStart, batchEnd);
query.removeAllQueryItems("titles");
query.addQueryItem("titles", streamTitlesBatch.join("|"));
url.setQuery(query);
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nextReply(synchronouslyGetUrl(url, &ok));
if(ok && syncingShouldContinue) {
handleNetworkReplyForStreamData(nextReply.data());
emit statusUpdateMessage(QString("Syncing stream batch %0").arg(i));
} else {
syncingShouldContinue = false;
return;
}
i++;
} while((i * batchSize) <= streamTitles.count());
}
void WebDataSynchronizer::handleNetworkReplyForStreamData(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError || !syncingShouldContinue) {
// qDebug() << "A network error occurred, or we're not OK: " << reply->errorString() << " syncingShouldContinue: " << syncingShouldContinue;
syncingShouldContinue = false;
return;
}
QXmlStreamReader xmlReader(reply->readAll());
StreamHandler handler;
while(!xmlReader.atEnd()) {
xmlReader.readNextStartElement();
if(xmlReader.name() == "text") {
Stream stream = handler.parse(xmlReader.readElementText());
if(!stream.title.isEmpty()) {
QMutexLocker locker(mutex);
streamsFromLocal->insert(stream.title, stream);
}
}
}
}
void WebDataSynchronizer::syncInvertebrates()
{
if(!syncingShouldContinue) {
// qDebug() << "We're not OK";
return;
}
QUrl url("https://wikieducator.org/api.php");
QUrlQuery query;
query.addQueryItem("action", "query");
query.addQueryItem("list", "categorymembers");
query.addQueryItem("cmtitle", "Category:Aquatic Invertebrate"); // Only difference with Stream Query...
query.addQueryItem("cmlimit", "500");
query.addQueryItem("format", "json");
query.addQueryItem("action", "query");
query.addQueryItem("cmprop", "ids|title");
url.setQuery(query);
bool ok;
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nextReply(synchronouslyGetUrl(url, &ok));
if(ok) {
handleNetworkReplyForInvertebrateList(nextReply.data());
} else {
syncingShouldContinue = false;
return;
}
}
void WebDataSynchronizer::handleNetworkReplyForInvertebrateList(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError || !syncingShouldContinue) {
// qDebug() << "A network error occurred, or we're not OK: " << reply->errorString() << " syncingShouldContinue: " << syncingShouldContinue;
syncingShouldContinue = false;
return;
}
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
QStringList invertebrateTitles;
if(doc.isNull()) {
// qDebug() << "Doc is null/invalid in inverts.";
syncingShouldContinue = false;
return;
}
QJsonArray invertebrateValues = doc.object().value("query").toObject().value("categorymembers").toArray();
for(const QJsonValue &value: invertebrateValues) {
QString streamName = value.toObject().value("title").toString().trimmed();
if(!streamName.isEmpty()) {
invertebrateTitles.append(streamName);
}
}
if(!syncingShouldContinue) {
// qDebug() << "Not ok";
return;
}
std::sort(invertebrateTitles.begin(), invertebrateTitles.end());
auto end = std::unique(invertebrateTitles.begin(), invertebrateTitles.end());
invertebrateTitles.erase(end, invertebrateTitles.end());
QUrl url("https://wikieducator.org/api.php");
QUrlQuery query;
query.addQueryItem("action", "query");
query.addQueryItem("export", "");
query.addQueryItem("exportnowrap", "");
int i = 0;
bool ok;
do {
emit statusUpdateMessage(QString("Syncing invertebrate batch %0").arg(i));
int batchStart = i * batchSize;
int batchEnd = batchStart + batchSize;
QStringList invertebrateTitlesBatch = invertebrateTitles.mid(batchStart, batchEnd);
query.removeAllQueryItems("titles");
query.addQueryItem("titles", invertebrateTitlesBatch.join("|"));
url.setQuery(query);
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nextReply(synchronouslyGetUrl(url, &ok));
if(ok && syncingShouldContinue) {
handleNetworkReplyForInvertebrateData(nextReply.data());
} else {
// qDebug() << "get FAILED for " << url;
syncingShouldContinue = false;
return;
}
i++;
} while((i * batchSize) <= invertebrateTitles.count());
}
void WebDataSynchronizer::handleNetworkReplyForInvertebrateData(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError || !syncingShouldContinue) {
// qDebug() << "A network error occurred, or we're not OK: " << reply->errorString() << " syncingShouldContinue: " << syncingShouldContinue;
syncingShouldContinue = false;
return;
}
QXmlStreamReader xmlReader(reply->readAll());
InvertebrateHandler handler;
while(!xmlReader.atEnd()) {
xmlReader.readNextStartElement();
if(xmlReader.name() == "text") {
Invertebrate invertebrate = handler.parse(xmlReader.readElementText());
QMutexLocker locker(mutex);
if(invertebrate.name != "Baetis") { // Baetis is the demo invertebrate and is not useful.
invertebratesFromLocal->insert(invertebrate.name, invertebrate);
} else {
// pass
}
}
}
}
void WebDataSynchronizer::syncImages()
{
if(!syncingShouldContinue) {
// qDebug() << "Exiting sync images";
return;
}
// if this method is only ever called from the end of the invertebrate sync there's no need to check for syncingShouldContinue
QStringList titles;
for(Invertebrate &invertebrate: *invertebratesFromLocal) {
titles.append(invertebrate.imageFileRemote);
}
std::sort(titles.begin(), titles.end());
auto end = std::unique(titles.begin(), titles.end());
titles.erase(end, titles.end());
QUrl url("https://wikieducator.org/api.php");
QUrlQuery query;
query.addQueryItem("action", "query");
query.addQueryItem("iiurlwidth", "400");
query.addQueryItem("prop", "imageinfo");
query.addQueryItem("iiprop", "url|timestamp|sha1");
query.addQueryItem("format", "json");
int i = 0;
bool ok;
QMap<QString, QList<Invertebrate*>> invertebrateImages;
for(Invertebrate& invertebrate: *invertebratesFromLocal) {
QString storedFileName = invertebrate.imageFileRemote;
QString spacesToUnderscores = invertebrate.imageFileRemote.replace(" ", "_");
QString underscoresToSpaces = invertebrate.imageFileRemote.replace("_", " ");
if(!invertebrateImages.contains(invertebrate.imageFileRemote)) {
invertebrateImages.insert(storedFileName, QList<Invertebrate *>());
// It bothers me that this is necessary, but there is some bad data here
invertebrateImages.insert(spacesToUnderscores, QList<Invertebrate *>());
invertebrateImages.insert(underscoresToSpaces, QList<Invertebrate *>());
}
invertebrateImages[storedFileName].append(&invertebrate);
invertebrateImages[spacesToUnderscores].append(&invertebrate);
invertebrateImages[underscoresToSpaces].append(&invertebrate);
}
do {
emit statusUpdateMessage(QString("Fetching image batch %0").arg(i));
if(!syncingShouldContinue) {
// qDebug() << "Exiting sync images";
return;
}
int batchStart = i * batchSize;
int batchEnd = batchStart + batchSize;
QStringList imageTitlesBatch = titles.mid(batchStart, batchEnd);
query.removeAllQueryItems("titles");
query.addQueryItem("titles", imageTitlesBatch.join("|"));
//sje 1-14-2020 needed to add &* to end of query string to make compatible with changes with wikieducator.org.
query.addQueryItem("&", "*");
url.setQuery(query);
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(synchronouslyGetUrl(url, &ok));
if(ok) {
handleNetworkReplyForImageList(reply.data(), &invertebrateImages);
} else {
syncingShouldContinue = false;
}
i++;
} while((i * batchSize) <= titles.count());
}
void WebDataSynchronizer::handleNetworkReplyForImageList(QNetworkReply *reply, QMap<QString, QList<Invertebrate*>> *invertebrateImages)
{
if(reply->error() != QNetworkReply::NoError || !syncingShouldContinue) {
// qDebug() << "A network error occurred, or we're not OK: " << reply->errorString() << " syncingShouldContinue: " << syncingShouldContinue;
syncingShouldContinue = false;
return;
}
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
if(doc.isNull()) {
// qDebug() << "Doc is null/invalid in ImageList.";
syncingShouldContinue = false;
return;
}
QJsonObject images = doc.object().value("query").toObject().value("pages").toObject();
QCryptographicHash hasher(QCryptographicHash::Sha256);
bool ok;
for(const QJsonValue &value: images) {
if(!syncingShouldContinue) {
// qDebug() << "Exiting from handleNetworkReplyForImageList";
return;
}
QJsonObject obj = value.toObject();
QString title = obj.value("title").toString();
const QList<Invertebrate*> invertebrates = invertebrateImages->value(title);
for(Invertebrate *invertebrate: invertebrates) {
QString extension = title.split(".").last().toLower();
QJsonObject imageInfo = obj.value("imageinfo").toArray().at(0).toObject();
QString urlString = imageInfo.value("thumburl").toString();
if(urlString.isEmpty()) {
continue;
}
QUrl thumbUrl(urlString);
QString sha = imageInfo.value("sha1").toString();
hasher.reset();
hasher.addData(title.toUtf8());
hasher.addData(sha.toUtf8());
QString localFileName = QString("%1%2%3.%4").arg(imagePath, directoryHelper.separator(), hasher.result().toHex(), extension);
QMutexLocker locker(mutex);
if(!directoryHelper.exists(localFileName)) {
emit statusUpdateMessage(QString("Fetching image for %0").arg(invertebrate->name));
QNetworkReply *nextReply = synchronouslyGetUrl(thumbUrl, &ok);
if(ok) {
if(handleNetworkReplyForImageData(nextReply, localFileName)) {
invertebrate->imageIsReady = ImageStatus::READY;
invertebrate->imageFileLocal = localFileName;
} else {
invertebrate->imageIsReady = ImageStatus::UNAVAILABLE;
}
} else {
// qDebug() << "Request was not ok: " << thumbUrl << " " << nextReply->errorString();
invertebrate->imageIsReady = ImageStatus::UNAVAILABLE;
}
} else {
invertebrate->imageIsReady = ImageStatus::READY;
invertebrate->imageFileLocal = localFileName;
}
}
}
}
bool WebDataSynchronizer::handleNetworkReplyForImageData(QNetworkReply *reply, QString localFileName)
{
if(!syncingShouldContinue) {
return false;
}
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> replyPtr(reply);
if(reply->error() != QNetworkReply::NoError || reply->bytesAvailable() == 0) {
// qDebug() << "Something went wrong in image request: " << reply->url().toString();
return false;
}
QFile imageFile(localFileName);
if(!imageFile.open(QFile::WriteOnly)) {
// qDebug() << "Unable to open image file: " << localFileName << " | " << imageFile.errorString();
return false;
}
imageFile.write(reply->readAll());
imageFile.close();
return true;
}
void WebDataSynchronizer::finalize()
{
if(!syncingShouldContinue) {
emit statusUpdateMessage("Sync stopped; Incomplete.");
emit finished(WebDataSynchonizerExitStatus::FAILED_RUNTIME);
return;
}
saveData();
QSettings settings;
settings.setValue("lastUpdate", QDateTime::currentDateTime().toString(Qt::SystemLocaleShortDate));
emit statusUpdateMessage("Syncing completed successfully.");
emit finished(WebDataSynchonizerExitStatus::SUCCEEDED);
}
WebDataSynchronizer::~WebDataSynchronizer()
{
}
QNetworkReply *WebDataSynchronizer::synchronouslyGetUrl(const QUrl &url, bool *ok)
{
QNetworkReply *theReply;
QNetworkRequest request(url);
// Use of a waiter isn't the most reliable method, but it does work...
QEventLoop waiter;
auto conn = std::make_shared<QMetaObject::Connection>();
*conn = connect(network.data(), &QNetworkAccessManager::finished, [&](QNetworkReply *reply) {
if(reply->error() == QNetworkReply::NoError) {
*ok = true;
} else {
*ok = false;
}
QObject::disconnect(*conn);
});
connect(network.data(), &QNetworkAccessManager::finished, &waiter, &QEventLoop::quit);
theReply = network->get(request);
connect(this, &WebDataSynchronizer::shouldStop, theReply, &QNetworkReply::abort);
waiter.exec();
return theReply;
}
void WebDataSynchronizer::stop()
{
syncingShouldContinue = false;
emit shouldStop();
}
void WebDataSynchronizer::handleNetworkReplyForAbout(QNetworkReply* reply)
{
if(reply->error() != QNetworkReply::NoError || !syncingShouldContinue) {
return;
}
QXmlStreamReader xmlReader(reply->readAll());
QString htmlStub;
while(!xmlReader.atEnd()) {
if(!syncingShouldContinue) {
return;
}
xmlReader.readNextStartElement();
if(xmlReader.name() == "text") {
htmlStub = xmlReader.readElementText();
}
}
QGumboDocument doc = QGumboDocument::parse(htmlStub);
QGumboNodes paragraphs = doc.rootNode().getElementsByTagName(HtmlTag::P);
QStringList paragraphList;
for(QGumboNode node: paragraphs) {
if(!syncingShouldContinue) {
return;
}
QString trimmed = node.innerText().trimmed().replace(QRegularExpression("\\s{2, }"), " ");
if(!trimmed.isEmpty()) {
paragraphList << trimmed;
}
}
if(!paragraphList.isEmpty()) {
if(!syncingShouldContinue) {
return;
}
QSettings settings;
settings.setValue("aboutText", paragraphList.join("\n\n"));
emit aboutStringParsed(paragraphList.join("\n\n"));
}
}
void WebDataSynchronizer::syncAbout()
{
if(!syncingShouldContinue) {
return;
}
bool ok;
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(synchronouslyGetUrl(QUrl("https://wikieducator.org/api.php?action=parse&page=AboutStreamsApp&format=xml"), &ok));
if(ok) {
handleNetworkReplyForAbout(reply.data());
}
}
void WebDataSynchronizer::saveData()
{
QDir directoryHelper;
QString dataPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
QString streamDataPath = QString("%1%2%3").arg(dataPath, directoryHelper.separator(), "stream.data");
QFile streamDataFile(streamDataPath);
if(!streamDataFile.open(QFile::WriteOnly)) {
#ifndef MOBILE_DEPLOYMENT
qDebug() << "Unable to open local invertebrate data";
#endif
return;
}
QDataStream streamSaver(&streamDataFile);
streamSaver << *streamsFromLocal;
QString invertebrateDataPath = QString("%1%2%3").arg(dataPath, directoryHelper.separator(), "invertebrate.data");
QFile invertebrateDataFile(invertebrateDataPath);
if(!invertebrateDataFile.open(QFile::WriteOnly)) {
#ifndef MOBILE_DEPLOYMENT
qDebug() << "Unable to open local invertebrate data";
#endif
return;
}
QDataStream invertebrateSaver(&invertebrateDataFile);
invertebrateSaver << *invertebratesFromLocal;
}