/* * * Copyright 2013 Luca Beltrame * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ // Own #include "utils.h" // Qt #include #include #include // KDE #include namespace Danbooru { QUrl requestUrl(QUrl& url, const QString& path, const QString& username, const QString& password, const dictMap& parameters, const QStringList& tags) { QUrl danbooruUrl = QUrl(url); danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash); danbooruUrl.setPath(danbooruUrl.path() + '/' + path); // If we have parameters, add them if (!parameters.isEmpty()) { for (auto key: parameters.keys()) { danbooruUrl.addQueryItem(key, parameters.value(key)); } } // Now, let's add tags should we have them if (!tags.isEmpty()) { QByteArray encoded_tags = QUrl::toPercentEncoding(tags.join(" ")); danbooruUrl.addEncodedQueryItem("tags", encoded_tags); } 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, const dictMap& parameters) { QUrl danbooruUrl = QUrl(url); danbooruUrl = danbooruUrl.adjusted(QUrl::StripTrailingSlash); danbooruUrl.setPath(danbooruUrl.path() + '/' + path); // If we have parameters, add them if (!parameters.isEmpty()) { for (auto key: parameters.keys()) { danbooruUrl.addQueryItem(key, parameters.value(key)); } } 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 elementName, bool* result) { QXmlStreamReader reader; reader.addData(data); QList postData; while(!reader.atEnd() && !reader.hasError()) { QXmlStreamReader::TokenType token = reader.readNext(); if (token == QXmlStreamReader::StartDocument) { continue; } if(token == QXmlStreamReader::StartElement && reader.name() == elementName) { QVariantMap values; QXmlStreamAttributes attributes = reader.attributes(); // qDebug() << attributes; for (auto attribute: attributes) { values.insert(attribute.name().toString(), attribute.value().toString()); } if (values.isEmpty()) { *result = false; qWarning() << "No results found when parsing XML"; return QList(); } QVariant converted = QVariant(values); postData.append(converted); } } *result = true; return postData; } QVariant parseDanbooruResult(QByteArray data, bool* result) { QJsonDocument parsed = QJsonDocument::fromJson(data); if (parsed.isNull()) { return QList(); } QVariant postData = parsed.toVariant(); *result = true; return postData; } } // namespace Danbooru