Get rid of the different overloads for parsing results

Default arguments do the job as well
This commit is contained in:
Luca Beltrame 2020-08-19 16:32:10 +02:00
parent 1a6fc34895
commit 6af8a41d48
Signed by: einar
GPG key ID: 4707F46E9EC72DEC
2 changed files with 75 additions and 110 deletions

View file

@ -62,14 +62,14 @@ namespace Danbooru
QUrl requestUrl(QUrl &url, const QString &path, QUrl requestUrl(const QUrl &url,
const QString &username, const QString &password, const QString &username,
const dictMap &parameters, const QStringList &tags) const QString &password,
const dictMap &parameters,
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 &parameters)
{
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);
@ -328,39 +290,53 @@ 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},
@ -373,7 +349,10 @@ QVariantMap extractPostData(const QVariant &data, ApiType type) {
} }
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;
@ -392,7 +371,7 @@ QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::R
mapped.append(rawMapped.toMap()); mapped.append(rawMapped.toMap());
} }
for (const auto element : mapped) { for (const auto &element : mapped) {
QVariantMap mapped; QVariantMap mapped;
@ -419,6 +398,9 @@ QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::R
} }
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>();
} }

View file

@ -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 &parameters, const QString &password = QString(),
const QStringList &tags); const dictMap &parameters = 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 &parameters);
/** @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);