171 lines
4.9 KiB
C++
171 lines
4.9 KiB
C++
/*
|
|
* <one line to give the library's name and an idea of what it does.>
|
|
* Copyright 2013 Luca Beltrame <lbeltrame@kde.org>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
#include "utils.h"
|
|
#include <qjson/parser.h>
|
|
#include <QtXml/QXmlStreamReader>
|
|
#include <QDebug>
|
|
|
|
namespace Danbooru {
|
|
|
|
KUrl requestUrl(KUrl& url, const QString& path,
|
|
const QString& username, const QString& password,
|
|
const dictMap& parameters, const QStringList& tags)
|
|
{
|
|
|
|
KUrl danbooruUrl = KUrl(url);
|
|
danbooruUrl.addPath(path);
|
|
|
|
// If we have parameters, add them
|
|
|
|
if (!parameters.isEmpty()) {
|
|
|
|
QMap<QString, QString>::const_iterator iter;
|
|
|
|
for (iter = parameters.constBegin(); iter!= parameters.constEnd();
|
|
++iter) {
|
|
danbooruUrl.addQueryItem(iter.key(), iter.value());
|
|
}
|
|
|
|
}
|
|
// 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;
|
|
}
|
|
|
|
KUrl requestUrl(KUrl& url, const QString& path, const QString& username,
|
|
const QString& password, const dictMap& parameters)
|
|
{
|
|
|
|
KUrl danbooruUrl = KUrl(url);
|
|
danbooruUrl.addPath(path);
|
|
|
|
// If we have parameters, add them
|
|
|
|
if (!parameters.isEmpty()) {
|
|
|
|
QMap<QString, QString>::const_iterator iter;
|
|
|
|
for (iter = parameters.constBegin(); iter!= parameters.constEnd();
|
|
++iter) {
|
|
danbooruUrl.addQueryItem(iter.key(), iter.value());
|
|
}
|
|
|
|
}
|
|
|
|
if (!username.isEmpty() && !password.isEmpty()) {
|
|
danbooruUrl.setUserName(username);
|
|
danbooruUrl.setPassword(password);
|
|
}
|
|
|
|
return danbooruUrl;
|
|
}
|
|
|
|
KUrl requestUrl(KUrl& url, const QString& path, const QString& username,
|
|
const QString& password)
|
|
{
|
|
KUrl danbooruUrl = KUrl(url);
|
|
danbooruUrl.addPath(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<QVariant> 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;
|
|
|
|
Q_FOREACH(const QXmlStreamAttribute& attribute, attributes) {
|
|
values.insert(attribute.name().toString(),
|
|
attribute.value().toString());
|
|
}
|
|
|
|
if (values.isEmpty()) {
|
|
*result = false;
|
|
return QList<QVariant>();
|
|
}
|
|
|
|
QVariant converted = QVariant(values);
|
|
|
|
postData.append(converted);
|
|
}
|
|
}
|
|
*result = true;
|
|
return postData;
|
|
}
|
|
|
|
QList< QVariant > parseDanbooruResult(QByteArray data, bool* result)
|
|
{
|
|
QJson::Parser parser;
|
|
|
|
bool ok;
|
|
|
|
QVariant postData = parser.parse(data, &ok);
|
|
|
|
if (!ok) {
|
|
*result = ok;
|
|
return QList<QVariant>();
|
|
}
|
|
|
|
*result = true;
|
|
|
|
return postData.toList();
|
|
}
|
|
|
|
|
|
} // namespace Danbooru
|