Add parsing functions

This commit is contained in:
Luca Beltrame 2018-08-22 19:45:09 +02:00
parent 9b37769c62
commit 65a35113f7
Signed by: einar
GPG key ID: 8DF631FD021DB0C5
2 changed files with 132 additions and 49 deletions

View file

@ -31,6 +31,22 @@
#include "libdanbooru_debug.h"
typedef QLatin1String QL1S;
static const QMap<QString, Danbooru::Rating> RATING_MAP = Danbooru::initRatingMap();
const QMap< QString, Danbooru::Rating > Danbooru::initRatingMap()
{
QMap<QString, Danbooru::Rating> 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<QString, Rating> RATING_MAP = initRatingMap();
static const QMap< QString, Danbooru::Rating > initRatingMap()
{
QMap<QString, Danbooru::Rating> 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<QString> 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<QString>::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<QString>::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<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::Request request, bool *result) {
QVariantMap map;
bool ok;
QVariant rawMapped = parseDanbooruResult(data, &ok);
QList<QVariantMap> postList;
if (!ok) {
*result = false;
return QList<QVariantMap>();
}
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<QVariantMap> parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) {
return QList<QVariantMap>();
}
} // namespace Danbooru

View file

@ -120,10 +120,13 @@ bool isPostBlacklisted(DanbooruPost *post, QSet<QString> 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<QVariantMap> parseResult(const QByteArray &data, Danbooru::ApiType type, Danbooru::Request request, bool *result);
QList<QVariantMap> 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();
}