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
|
@ -62,14 +62,14 @@ 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(" "));
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -328,39 +290,53 @@ QVariantMap extractPostData(const QVariant &data, ApiType type) {
|
|||
auto mapped = data.toMap();
|
||||
|
||||
int id;
|
||||
QSet<QString> tags;
|
||||
int width;
|
||||
int height;
|
||||
int fileSize;
|
||||
QSet<QString> tags;
|
||||
QUrl url;
|
||||
QUrl thumbnailUrl;
|
||||
QUrl sampleUrl;
|
||||
Danbooru::Rating rating;
|
||||
QStringList splitted;
|
||||
|
||||
switch(type) {
|
||||
case Danbooru::MoeBooru:
|
||||
tags = QSet<QString>::fromList(mapped.value("tags").toString().split(' '));
|
||||
|
||||
splitted = mapped.value("tags").toString().split(' ');
|
||||
|
||||
id = mapped.value("id").toString().toInt();
|
||||
tags = QSet<QString>(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<QString>::fromList(mapped.value("tag_string_general").toString().split(' '));
|
||||
tags = QSet<QString>(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},
|
||||
|
@ -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;
|
||||
bool ok;
|
||||
|
@ -392,7 +371,7 @@ QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::R
|
|||
mapped.append(rawMapped.toMap());
|
||||
}
|
||||
|
||||
for (const auto element : mapped) {
|
||||
for (const auto &element : 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) {
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(type)
|
||||
Q_UNUSED(result)
|
||||
return QList<QVariantMap>();
|
||||
}
|
||||
|
||||
|
|
|
@ -59,9 +59,10 @@ typedef QMap<QString, QString> 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<QVariant> parseDanbooruResult(QByteArray data, QString xlmElement,
|
||||
bool *result, ApiType=ApiType::MoeBooru);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue