From 6af8a41d48d0f7dcb7a675d25014ff9ef40cc5ac Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Wed, 19 Aug 2020 16:32:10 +0200 Subject: [PATCH] Get rid of the different overloads for parsing results Default arguments do the job as well --- src/libdanbooru/utils.cpp | 160 +++++++++++++++++--------------------- src/libdanbooru/utils.h | 25 +----- 2 files changed, 75 insertions(+), 110 deletions(-) diff --git a/src/libdanbooru/utils.cpp b/src/libdanbooru/utils.cpp index 27bc714..76661ec 100644 --- a/src/libdanbooru/utils.cpp +++ b/src/libdanbooru/utils.cpp @@ -59,17 +59,17 @@ namespace { namespace Danbooru { - -QUrl requestUrl(QUrl &url, const QString &path, - const QString &username, const QString &password, - const dictMap ¶meters, const QStringList &tags) + +QUrl requestUrl(const QUrl &url, + const QString &username, + const QString &password, + const dictMap ¶meters, + const QStringList &tags) { QUrl danbooruUrl = QUrl(url); - danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash); - danbooruUrl.setPath(danbooruUrl.path() + '/' + path); // If we have parameters, add them @@ -89,7 +89,9 @@ QUrl requestUrl(QUrl &url, const QString &path, query.addQueryItem("tags", tags.join(" ")); } - danbooruUrl.setQuery(query); + if (!query.isEmpty()) { + danbooruUrl.setQuery(query); + } if (!username.isEmpty() && !password.isEmpty()) { danbooruUrl.setUserName(username); @@ -99,50 +101,7 @@ QUrl requestUrl(QUrl &url, const QString &path, return danbooruUrl; } -QUrl requestUrl(QUrl &url, const QString &path, const QString &username, - const QString &password, const dictMap ¶meters) -{ - QUrl danbooruUrl = QUrl(url); - danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash); - danbooruUrl.setPath(danbooruUrl.path() + '/' + path); - - // If we have parameters, add them - - QUrlQuery query; - - if (!parameters.isEmpty()) { - - for (auto it = parameters.cbegin(), e = parameters.cend(); it != e; ++it) { - query.addQueryItem(it.key(), it.value()); - } - - } - - danbooruUrl.setQuery(query); - - if (!username.isEmpty() && !password.isEmpty()) { - danbooruUrl.setUserName(username); - danbooruUrl.setPassword(password); - } - - return danbooruUrl; -} - -QUrl requestUrl(QUrl &url, const QString &path, const QString &username, - const QString &password) -{ - QUrl danbooruUrl = QUrl(url); - danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash); - danbooruUrl.setPath(danbooruUrl.path() + '/' + path); - - if (!username.isEmpty() && !password.isEmpty()) { - danbooruUrl.setUserName(username); - danbooruUrl.setPassword(password); - } - - return danbooruUrl; -} QList< QVariant > parseDanbooruResult(QByteArray data, QString xlmElement, bool *result, ApiType apiType) { @@ -197,7 +156,10 @@ QList< QVariant > parseDanbooruResult(QByteArray data, QString xlmElement, bool QVariantMap values; while(reader.readNextStartElement()) { - values.insert(reader.name().toString(), reader.readElementText()); + // XML tags in Danbooru use "-" instead of "_" to separate + // elements ("file-size" instead of "file_size" in JSON), + // hence replace "-" with "_" + values.insert(reader.name().toString().replace("-", "_"), reader.readElementText()); } QVariant converted = QVariant(values); postData.append(converted); @@ -224,7 +186,7 @@ QVariant parseDanbooruResult(QByteArray data, bool *result) } QVariant postData = parsed.toVariant(); - + // qCDebug(LIBDANBOORU) << "Raw JSON response" << parsed.toJson(QJsonDocument::Indented).constData(); *result = true; @@ -264,19 +226,19 @@ void fixPostUrl(DanbooruPost *post, QUrl boardUrl) { } QVariantMap extractPoolData(const QVariant &data, ApiType type) { - + auto mapped = data.toMap(); int id; QString name; int postCount; QString description; QVariantList rawData; - + 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(); @@ -291,19 +253,19 @@ QVariantMap extractPoolData(const QVariant &data, ApiType type) { {"description", description}, {"raw_post_data", rawData} }; - + return map; - + } QVariantMap extractTagData(const QVariant &data, ApiType type) { - + auto mapped = data.toMap(); int id = mapped.value("id").toInt(); QString name = mapped.value("name").toString(); int postCount; int category; - + switch(type) { case MoeBooru: postCount = mapped.value("count").toInt(); @@ -313,89 +275,106 @@ QVariantMap extractTagData(const QVariant &data, ApiType type) { postCount = mapped.value("post_count").toInt(); category = mapped.value("category").toInt(); }; - + QVariantMap map = { - {QL1S("id"), id}, + {QL1S("id"), id}, {QL1S("name"), name}, {QL1S("postCount"), postCount}, {QL1S("category"), category} }; - + return map; } QVariantMap extractPostData(const QVariant &data, ApiType type) { auto mapped = data.toMap(); - + int id; - QSet tags; int width; int height; int fileSize; + QSet tags; QUrl url; QUrl thumbnailUrl; QUrl sampleUrl; Danbooru::Rating rating; - + QStringList splitted; + switch(type) { case Danbooru::MoeBooru: - tags = QSet::fromList(mapped.value("tags").toString().split(' ')); + + splitted = mapped.value("tags").toString().split(' '); + id = mapped.value("id").toString().toInt(); + tags = QSet(splitted.begin(), splitted.end()); height = mapped.value("height").toString().toInt(); width = mapped.value("width").toString().toInt(); + url = QUrl::fromUserInput(mapped.value("file_url").toString()); thumbnailUrl = QUrl::fromUserInput(mapped.value("preview_url").toString()); sampleUrl = QUrl::fromUserInput(mapped.value("sample_url").toString()); + rating = RATING_MAP.value(mapped.value("rating").toString()); fileSize = mapped.value("file_size").toInt(); + break; + default: + + splitted = mapped.value("tag_string").toString().split(' '); + id = mapped.value("id").toString().toInt(); - tags = QSet::fromList(mapped.value("tag_string_general").toString().split(' ')); + tags = QSet(splitted.begin(), splitted.end()); + width = mapped.value("image_width").toString().toInt(); height = mapped.value("image_height").toString().toInt(); + url = mapped.value("file_url").toUrl(); thumbnailUrl = mapped.value("preview_file_url").toUrl(); - fileSize = mapped.value("file_size").toInt(); - rating = RATING_MAP.value(mapped.value("rating").toString()); sampleUrl = mapped.value("large_file_url").toUrl(); + + rating = RATING_MAP.value(mapped.value("rating").toString()); + fileSize = mapped.value("file_size").toInt(); + }; - - + QVariantMap map = { - {QL1S("width"), width}, {QL1S("height"), height}, {QL1S("id"), id}, + {QL1S("width"), width}, {QL1S("height"), height}, {QL1S("id"), id}, {QL1S("url"), url}, {QL1S("thumbnailUrl"), thumbnailUrl}, - {QL1S("sampleUrl"), sampleUrl}, {QL1S("rating"), rating}, + {QL1S("sampleUrl"), sampleUrl}, {QL1S("rating"), rating}, {QL1S("size"), fileSize}, {QL1S("tags"), QVariant::fromValue(tags)} }; - + return(map); - + } -QList parseResult(const QByteArray &data, ApiType type, Danbooru::Request request, bool *result) { - +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) { - + } + + for (const auto &element : mapped) { + QVariantMap mapped; - + switch(request) { case Pool: mapped = extractPoolData(element, type); @@ -409,16 +388,19 @@ QList parseResult(const QByteArray &data, ApiType type, Danbooru::R default: mapped = extractPostData(element, type); } - + postList.append(mapped); } - - *result = true; - + + *result = true; + return postList; } QList parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) { + Q_UNUSED(data) + Q_UNUSED(type) + Q_UNUSED(result) return QList(); } diff --git a/src/libdanbooru/utils.h b/src/libdanbooru/utils.h index 59134ca..ab8074a 100644 --- a/src/libdanbooru/utils.h +++ b/src/libdanbooru/utils.h @@ -59,9 +59,10 @@ typedef QMap dictMap; * * **/ -QUrl requestUrl(QUrl &url, const QString &path, const QString &username, - const QString &password, const dictMap ¶meters, - const QStringList &tags); +QUrl requestUrl(const QUrl &url, const QString &username = QString(), + const QString &password = QString(), + const dictMap ¶meters = dictMap(), + const QStringList &tags = QStringList()); /** @brief Generate a request URL for a Danbooru board. * @@ -80,25 +81,7 @@ QUrl requestUrl(QUrl &url, const QString &path, const QString &username, * * **/ -QUrl requestUrl(QUrl &url, const QString &path, const QString &username, - const QString &password, const dictMap ¶meters); -/** @brief Generate a request URL for a Danbooru board. - * - * This is an overloaded function provided for convenience. - * - * @param url The board URL. - * @param path The API path of the call to use - * @param username The username to supply (optional) - * @param password The password to use (optional) - * - * @return A constructed URL to be used for a Danbooru API call. - * @author Luca Beltrame (lbeltrame@kde.org) - * - * - **/ -QUrl requestUrl(QUrl &url, const QString &path, const QString &username, - const QString &password); QList parseDanbooruResult(QByteArray data, QString xlmElement, bool *result, ApiType=ApiType::MoeBooru);