From 65a35113f77b11bc407c78dcc8dcee45e24213ee Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Wed, 22 Aug 2018 19:45:09 +0200 Subject: [PATCH] Add parsing functions --- src/libdanbooru/utils.cpp | 172 ++++++++++++++++++++++++++++---------- src/libdanbooru/utils.h | 9 +- 2 files changed, 132 insertions(+), 49 deletions(-) diff --git a/src/libdanbooru/utils.cpp b/src/libdanbooru/utils.cpp index b3d8aa9..0af873c 100644 --- a/src/libdanbooru/utils.cpp +++ b/src/libdanbooru/utils.cpp @@ -31,6 +31,22 @@ #include "libdanbooru_debug.h" +typedef QLatin1String QL1S; + +static const QMap RATING_MAP = Danbooru::initRatingMap(); + +const QMap< QString, Danbooru::Rating > Danbooru::initRatingMap() +{ + + QMap map; + map.insert("s", Safe); + map.insert("q", Questionable); + map.insert("e", Explicit); + + return map; + +} + namespace { QUrl adjustUrl(QUrl baseUrl, QUrl boardUrl) { if(baseUrl.host().isEmpty()) { @@ -44,19 +60,7 @@ namespace { namespace Danbooru { -static const QMap RATING_MAP = initRatingMap(); -static const QMap< QString, Danbooru::Rating > initRatingMap() -{ - - QMap map; - map.insert("s", Safe); - map.insert("q", Questionable); - map.insert("e", Explicit); - - return map; - -} QUrl requestUrl(QUrl &url, const QString &path, const QString &username, const QString &password, @@ -220,6 +224,8 @@ QVariant parseDanbooruResult(QByteArray data, bool *result) } QVariant postData = parsed.toVariant(); + +// qCDebug(LIBDANBOORU) << "Raw JSON response" << parsed.toJson(QJsonDocument::Indented).constData(); *result = true; @@ -257,15 +263,42 @@ void fixPostUrl(DanbooruPost *post, QUrl boardUrl) { } -QVariantMap parseResult(const QByteArray &data, ApiType type) { +QVariantMap extractPoolData(const QVariant &data, ApiType type) { - QVariantMap map; - bool result; - QVariantMap mapped = parseDanbooruResult(data, &result).toMap(); + auto mapped = data.toMap(); + int id; + QString name; + int postCount; + QString description; + QVariantList rawData; - if (!result) { - return QVariantMap(); - } + id = mapped.value("id").toInt(); + name = mapped.value("name").toString(); + postCount = mapped.value("post_count").toInt(); + description = mapped.value("description").toString(); + + switch(type) { + case MoeBooru: + rawData = mapped.value("posts").toList(); + break; + default: + rawData = mapped.value("post_ids").toList(); + }; + QVariantMap map = { + {"id", id}, + {"post_count", postCount}, + {"name", name}, + {"description", description}, + {"raw_post_data", rawData} + }; + + return map; + +} + +QVariantMap extractPostData(const QVariant &data, ApiType type) { + + auto mapped = data.toMap(); int id; QSet tags; @@ -301,38 +334,85 @@ QVariantMap parseResult(const QByteArray &data, ApiType type) { rating = Danbooru::Safe; break; default: - auto tagKey = mapped.contains("tag_string") ? QLatin1String("tag_string"): QLatin1String("tag-string"); - auto widthKey = mapped.contains("image_width") ? QLatin1String("image_width"): QLatin1String("image-width"); - auto heightKey = mapped.contains("image_height") ? QLatin1String("image_height"): QLatin1String("image-height"); - auto fileSizeKey = mapped.contains("file_size") ? QLatin1String("file_size"): QLatin1String("file-size"); - auto fileUrlKey = mapped.contains("file_url") ? QLatin1String("file_url"): QLatin1String("file-url"); - auto thumbnailUrlKey = mapped.contains("preview_file_url") ? QLatin1String("preview_file_url"): QLatin1String("preview-file-url"); - auto sampleUrlKey = mapped.contains("large_file_url") ? QLatin1String("large_file_url"): QLatin1String("large-file-url"); + auto tagKey = mapped.contains("tag_string") ? QL1S("tag_string"): QL1S("tag-string"); + auto widthKey = mapped.contains("image_width") ? QL1S("image_width"): QL1S("image-width"); + auto heightKey = mapped.contains("image_height") ? QL1S("image_height"): QL1S("image-height"); + auto fileSizeKey = mapped.contains("file_size") ? QL1S("file_size"): QL1S("file-size"); + auto fileUrlKey = mapped.contains("file_url") ? QL1S("file_url"): QL1S("file-url"); + auto thumbnailUrlKey = mapped.contains("preview_file_url") ? QL1S("preview_file_url"): QL1S("preview-file-url"); + auto sampleUrlKey = mapped.contains("large_file_url") ? QL1S("large_file_url"): QL1S("large-file-url"); - id = mapped.value("id").toString().toInt(); - tags = QSet::fromList(mapped.value(tagKey).toString().split(' ')); - width = mapped.value(widthKey).toString().toInt(); - height = mapped.value(heightKey).toString().toInt(); - url = mapped.value(fileUrlKey).toUrl(); - thumbnailUrl = mapped.value(thumbnailUrlKey).toUrl(); - fileSize = mapped.value(fileSizeKey).toInt(); - rating = RATING_MAP.value(mapped.value("rating").toString()); - sampleUrl = mapped.value(sampleUrlKey).toUrl(); - break; + id = mapped.value("id").toString().toInt(); + tags = QSet::fromList(mapped.value(tagKey).toString().split(' ')); + width = mapped.value(widthKey).toString().toInt(); + height = mapped.value(heightKey).toString().toInt(); + url = mapped.value(fileUrlKey).toUrl(); + thumbnailUrl = mapped.value(thumbnailUrlKey).toUrl(); + fileSize = mapped.value(fileSizeKey).toInt(); + rating = RATING_MAP.value(mapped.value("rating").toString()); + sampleUrl = mapped.value(sampleUrlKey).toUrl(); + break; + }; + + + QVariantMap map = { + {QL1S("width"), width}, {QL1S("height"), height}, {QL1S("id"), id}, + {QL1S("url"), url}, {QL1S("thumbnailUrl"), thumbnailUrl}, + {QL1S("sampleUrl"), sampleUrl}, {QL1S("rating"), rating}, + {QL1S("size"), fileSize}, {QL1S("tags"), QVariant::fromValue(tags)} }; - map.insert(QLatin1String("width"), width); - map.insert(QLatin1String("height"), height); - map.insert(QLatin1String("id"), id); - map.insert(QLatin1String("url"), url); - map.insert(QLatin1String("thumbnailUrl"), url); - map.insert(QLatin1String("sampleUrl"), sampleUrl); - map.insert(QLatin1String("rating"), rating); - map.insert(QLatin1String("size"), fileSize); - map.insert(QLatin1String("tags"), QVariant::fromValue(tags)); - return(map); } +QList parseResult(const QByteArray &data, ApiType type, Danbooru::Request request, bool *result) { + + QVariantMap map; + bool ok; + QVariant rawMapped = parseDanbooruResult(data, &ok); + QList postList; + + if (!ok) { + *result = false; + return QList(); + } + + QVariantList mapped = rawMapped.toList(); + + // For single queries like pools + if (mapped.isEmpty()) { + mapped.append(rawMapped.toMap()); + } + + for (const auto element : mapped) { + + QVariantMap mapped; + + switch(request) { + case Pool: + mapped = extractPoolData(element, type); + break; + case Artist: + qDebug() << "Reimplement"; + break; + case Tag: + qDebug() << "Reimplement"; + break; + default: + mapped = extractPostData(element, type); + } + + postList.append(mapped); + } + + *result = true; + + return postList; +} + +QList parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) { + return QList(); +} + } // namespace Danbooru diff --git a/src/libdanbooru/utils.h b/src/libdanbooru/utils.h index 695969a..a618317 100644 --- a/src/libdanbooru/utils.h +++ b/src/libdanbooru/utils.h @@ -120,10 +120,13 @@ bool isPostBlacklisted(DanbooruPost *post, QSet blacklist, Danbooru::Ra void fixPostUrl(DanbooruPost *post, QUrl boardUrl); -QVariantMap parseResult(const QByteArray &data, Danbooru::ApiType type); -QVariantMap parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type); +QList parseResult(const QByteArray &data, Danbooru::ApiType type, Danbooru::Request request, bool *result); +QList parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result); -static const QMap< QString, Danbooru::Rating > initRatingMap(); +QVariantMap extractPostData(const QVariant &data, ApiType type); +QVariantMap extractPoolData(const QVariant &data, ApiType type); + +const QMap< QString, Danbooru::Rating > initRatingMap(); }