Get rid of the different overloads for parsing results
Default arguments do the job as well
This commit is contained in:
parent
1a6fc34895
commit
6af8a41d48
2 changed files with 75 additions and 110 deletions
|
@ -59,17 +59,17 @@ namespace {
|
||||||
|
|
||||||
namespace Danbooru
|
namespace Danbooru
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
QUrl requestUrl(QUrl &url, const QString &path,
|
|
||||||
const QString &username, const QString &password,
|
QUrl requestUrl(const QUrl &url,
|
||||||
const dictMap ¶meters, const QStringList &tags)
|
const QString &username,
|
||||||
|
const QString &password,
|
||||||
|
const dictMap ¶meters,
|
||||||
|
const QStringList &tags)
|
||||||
{
|
{
|
||||||
|
|
||||||
QUrl danbooruUrl = QUrl(url);
|
QUrl danbooruUrl = QUrl(url);
|
||||||
danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash);
|
|
||||||
danbooruUrl.setPath(danbooruUrl.path() + '/' + path);
|
|
||||||
|
|
||||||
// If we have parameters, add them
|
// If we have parameters, add them
|
||||||
|
|
||||||
|
@ -89,7 +89,9 @@ QUrl requestUrl(QUrl &url, const QString &path,
|
||||||
query.addQueryItem("tags", tags.join(" "));
|
query.addQueryItem("tags", tags.join(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
danbooruUrl.setQuery(query);
|
if (!query.isEmpty()) {
|
||||||
|
danbooruUrl.setQuery(query);
|
||||||
|
}
|
||||||
|
|
||||||
if (!username.isEmpty() && !password.isEmpty()) {
|
if (!username.isEmpty() && !password.isEmpty()) {
|
||||||
danbooruUrl.setUserName(username);
|
danbooruUrl.setUserName(username);
|
||||||
|
@ -99,50 +101,7 @@ QUrl requestUrl(QUrl &url, const QString &path,
|
||||||
return danbooruUrl;
|
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)
|
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;
|
QVariantMap values;
|
||||||
|
|
||||||
while(reader.readNextStartElement()) {
|
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);
|
QVariant converted = QVariant(values);
|
||||||
postData.append(converted);
|
postData.append(converted);
|
||||||
|
@ -224,7 +186,7 @@ QVariant parseDanbooruResult(QByteArray data, bool *result)
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant postData = parsed.toVariant();
|
QVariant postData = parsed.toVariant();
|
||||||
|
|
||||||
// qCDebug(LIBDANBOORU) << "Raw JSON response" << parsed.toJson(QJsonDocument::Indented).constData();
|
// qCDebug(LIBDANBOORU) << "Raw JSON response" << parsed.toJson(QJsonDocument::Indented).constData();
|
||||||
|
|
||||||
*result = true;
|
*result = true;
|
||||||
|
@ -264,19 +226,19 @@ void fixPostUrl(DanbooruPost *post, QUrl boardUrl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap extractPoolData(const QVariant &data, ApiType type) {
|
QVariantMap extractPoolData(const QVariant &data, ApiType type) {
|
||||||
|
|
||||||
auto mapped = data.toMap();
|
auto mapped = data.toMap();
|
||||||
int id;
|
int id;
|
||||||
QString name;
|
QString name;
|
||||||
int postCount;
|
int postCount;
|
||||||
QString description;
|
QString description;
|
||||||
QVariantList rawData;
|
QVariantList rawData;
|
||||||
|
|
||||||
id = mapped.value("id").toInt();
|
id = mapped.value("id").toInt();
|
||||||
name = mapped.value("name").toString();
|
name = mapped.value("name").toString();
|
||||||
postCount = mapped.value("post_count").toInt();
|
postCount = mapped.value("post_count").toInt();
|
||||||
description = mapped.value("description").toString();
|
description = mapped.value("description").toString();
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case MoeBooru:
|
case MoeBooru:
|
||||||
rawData = mapped.value("posts").toList();
|
rawData = mapped.value("posts").toList();
|
||||||
|
@ -291,19 +253,19 @@ QVariantMap extractPoolData(const QVariant &data, ApiType type) {
|
||||||
{"description", description},
|
{"description", description},
|
||||||
{"raw_post_data", rawData}
|
{"raw_post_data", rawData}
|
||||||
};
|
};
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap extractTagData(const QVariant &data, ApiType type) {
|
QVariantMap extractTagData(const QVariant &data, ApiType type) {
|
||||||
|
|
||||||
auto mapped = data.toMap();
|
auto mapped = data.toMap();
|
||||||
int id = mapped.value("id").toInt();
|
int id = mapped.value("id").toInt();
|
||||||
QString name = mapped.value("name").toString();
|
QString name = mapped.value("name").toString();
|
||||||
int postCount;
|
int postCount;
|
||||||
int category;
|
int category;
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case MoeBooru:
|
case MoeBooru:
|
||||||
postCount = mapped.value("count").toInt();
|
postCount = mapped.value("count").toInt();
|
||||||
|
@ -313,89 +275,106 @@ QVariantMap extractTagData(const QVariant &data, ApiType type) {
|
||||||
postCount = mapped.value("post_count").toInt();
|
postCount = mapped.value("post_count").toInt();
|
||||||
category = mapped.value("category").toInt();
|
category = mapped.value("category").toInt();
|
||||||
};
|
};
|
||||||
|
|
||||||
QVariantMap map = {
|
QVariantMap map = {
|
||||||
{QL1S("id"), id},
|
{QL1S("id"), id},
|
||||||
{QL1S("name"), name}, {QL1S("postCount"), postCount},
|
{QL1S("name"), name}, {QL1S("postCount"), postCount},
|
||||||
{QL1S("category"), category}
|
{QL1S("category"), category}
|
||||||
};
|
};
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap extractPostData(const QVariant &data, ApiType type) {
|
QVariantMap extractPostData(const QVariant &data, ApiType type) {
|
||||||
|
|
||||||
auto mapped = data.toMap();
|
auto mapped = data.toMap();
|
||||||
|
|
||||||
int id;
|
int id;
|
||||||
QSet<QString> tags;
|
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
int fileSize;
|
int fileSize;
|
||||||
|
QSet<QString> tags;
|
||||||
QUrl url;
|
QUrl url;
|
||||||
QUrl thumbnailUrl;
|
QUrl thumbnailUrl;
|
||||||
QUrl sampleUrl;
|
QUrl sampleUrl;
|
||||||
Danbooru::Rating rating;
|
Danbooru::Rating rating;
|
||||||
|
QStringList splitted;
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case Danbooru::MoeBooru:
|
case Danbooru::MoeBooru:
|
||||||
tags = QSet<QString>::fromList(mapped.value("tags").toString().split(' '));
|
|
||||||
|
splitted = mapped.value("tags").toString().split(' ');
|
||||||
|
|
||||||
id = mapped.value("id").toString().toInt();
|
id = mapped.value("id").toString().toInt();
|
||||||
|
tags = QSet<QString>(splitted.begin(), splitted.end());
|
||||||
height = mapped.value("height").toString().toInt();
|
height = mapped.value("height").toString().toInt();
|
||||||
width = mapped.value("width").toString().toInt();
|
width = mapped.value("width").toString().toInt();
|
||||||
|
|
||||||
url = QUrl::fromUserInput(mapped.value("file_url").toString());
|
url = QUrl::fromUserInput(mapped.value("file_url").toString());
|
||||||
thumbnailUrl = QUrl::fromUserInput(mapped.value("preview_url").toString());
|
thumbnailUrl = QUrl::fromUserInput(mapped.value("preview_url").toString());
|
||||||
sampleUrl = QUrl::fromUserInput(mapped.value("sample_url").toString());
|
sampleUrl = QUrl::fromUserInput(mapped.value("sample_url").toString());
|
||||||
|
|
||||||
rating = RATING_MAP.value(mapped.value("rating").toString());
|
rating = RATING_MAP.value(mapped.value("rating").toString());
|
||||||
fileSize = mapped.value("file_size").toInt();
|
fileSize = mapped.value("file_size").toInt();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
|
splitted = mapped.value("tag_string").toString().split(' ');
|
||||||
|
|
||||||
id = mapped.value("id").toString().toInt();
|
id = mapped.value("id").toString().toInt();
|
||||||
tags = QSet<QString>::fromList(mapped.value("tag_string_general").toString().split(' '));
|
tags = QSet<QString>(splitted.begin(), splitted.end());
|
||||||
|
|
||||||
width = mapped.value("image_width").toString().toInt();
|
width = mapped.value("image_width").toString().toInt();
|
||||||
height = mapped.value("image_height").toString().toInt();
|
height = mapped.value("image_height").toString().toInt();
|
||||||
|
|
||||||
url = mapped.value("file_url").toUrl();
|
url = mapped.value("file_url").toUrl();
|
||||||
thumbnailUrl = mapped.value("preview_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();
|
sampleUrl = mapped.value("large_file_url").toUrl();
|
||||||
|
|
||||||
|
rating = RATING_MAP.value(mapped.value("rating").toString());
|
||||||
|
fileSize = mapped.value("file_size").toInt();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
QVariantMap map = {
|
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("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)}
|
{QL1S("size"), fileSize}, {QL1S("tags"), QVariant::fromValue(tags)}
|
||||||
};
|
};
|
||||||
|
|
||||||
return(map);
|
return(map);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::Request request, bool *result) {
|
QList<QVariantMap> parseResult(const QByteArray &data,
|
||||||
|
ApiType type,
|
||||||
|
Danbooru::Request request,
|
||||||
|
bool *result) {
|
||||||
|
|
||||||
QVariantMap map;
|
QVariantMap map;
|
||||||
bool ok;
|
bool ok;
|
||||||
QVariant rawMapped = parseDanbooruResult(data, &ok);
|
QVariant rawMapped = parseDanbooruResult(data, &ok);
|
||||||
QList<QVariantMap> postList;
|
QList<QVariantMap> postList;
|
||||||
|
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
*result = false;
|
*result = false;
|
||||||
return QList<QVariantMap>();
|
return QList<QVariantMap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantList mapped = rawMapped.toList();
|
QVariantList mapped = rawMapped.toList();
|
||||||
|
|
||||||
// For single queries like pools
|
// For single queries like pools
|
||||||
if (mapped.isEmpty()) {
|
if (mapped.isEmpty()) {
|
||||||
mapped.append(rawMapped.toMap());
|
mapped.append(rawMapped.toMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto element : mapped) {
|
for (const auto &element : mapped) {
|
||||||
|
|
||||||
QVariantMap mapped;
|
QVariantMap mapped;
|
||||||
|
|
||||||
switch(request) {
|
switch(request) {
|
||||||
case Pool:
|
case Pool:
|
||||||
mapped = extractPoolData(element, type);
|
mapped = extractPoolData(element, type);
|
||||||
|
@ -409,16 +388,19 @@ QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::R
|
||||||
default:
|
default:
|
||||||
mapped = extractPostData(element, type);
|
mapped = extractPostData(element, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
postList.append(mapped);
|
postList.append(mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = true;
|
*result = true;
|
||||||
|
|
||||||
return postList;
|
return postList;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QVariantMap> parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) {
|
QList<QVariantMap> parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) {
|
||||||
|
Q_UNUSED(data)
|
||||||
|
Q_UNUSED(type)
|
||||||
|
Q_UNUSED(result)
|
||||||
return QList<QVariantMap>();
|
return QList<QVariantMap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,9 +59,10 @@ typedef QMap<QString, QString> dictMap;
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
QUrl requestUrl(QUrl &url, const QString &path, const QString &username,
|
QUrl requestUrl(const QUrl &url, const QString &username = QString(),
|
||||||
const QString &password, const dictMap ¶meters,
|
const QString &password = QString(),
|
||||||
const QStringList &tags);
|
const dictMap ¶meters = dictMap(),
|
||||||
|
const QStringList &tags = QStringList());
|
||||||
|
|
||||||
/** @brief Generate a request URL for a Danbooru board.
|
/** @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<QVariant> parseDanbooruResult(QByteArray data, QString xlmElement,
|
QList<QVariant> parseDanbooruResult(QByteArray data, QString xlmElement,
|
||||||
bool *result, ApiType=ApiType::MoeBooru);
|
bool *result, ApiType=ApiType::MoeBooru);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue