428 lines
11 KiB
C++
428 lines
11 KiB
C++
/*
|
|
* Copyright 2015 Luca Beltrame <lbeltrame@kde.org>
|
|
*
|
|
* This file is part of Danbooru Client.
|
|
*
|
|
* Danbooru Client 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 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Danbooru Client 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 Danbooru Client. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "danbooru_donmai.h"
|
|
#include "danboorupost.h"
|
|
#include "danboorupool.h"
|
|
#include "danboorutag.h"
|
|
#include "utils.h"
|
|
#include "libdanbooru_debug.h"
|
|
|
|
// KF5
|
|
|
|
#include <KIO/Job>
|
|
#include <KIO/Scheduler>
|
|
#include <KImageCache>
|
|
|
|
using KIO::StoredTransferJob;
|
|
|
|
namespace Danbooru {
|
|
|
|
///////
|
|
// URIs
|
|
///////
|
|
|
|
const QUrl DanbooruService::postUri() const {
|
|
auto url = QUrl(m_url);
|
|
url.setPath("/posts.json");
|
|
return url;
|
|
}
|
|
|
|
const QUrl DanbooruService::poolUri() const {
|
|
auto url = QUrl(m_url);
|
|
url.setPath("/pools.json");
|
|
return url;
|
|
|
|
}
|
|
|
|
const QUrl DanbooruService::artistUri() const {
|
|
auto url = QUrl(m_url);
|
|
url.setPath("/artists.json");
|
|
return url;
|
|
}
|
|
|
|
const QUrl DanbooruService::tagUri() const {
|
|
auto url = QUrl(m_url);
|
|
url.setPath("/tags.json");
|
|
return url;
|
|
}
|
|
|
|
const QUrl DanbooruService::poolDataUri() const {
|
|
return QUrl();
|
|
}
|
|
|
|
const QUrl DanbooruService::relatedTagUri() const {
|
|
auto url = QUrl(m_url);
|
|
url.setPath("/related_tag.json");
|
|
return url;
|
|
}
|
|
|
|
////////////////
|
|
// Other methods
|
|
////////////////
|
|
|
|
|
|
Danbooru::ApiType DanbooruService::apiType() const {
|
|
return Danbooru::ApiType::Danbooru;
|
|
}
|
|
|
|
//////////////
|
|
// API methods
|
|
//////////////
|
|
|
|
void DanbooruService::getPostList() {
|
|
// We can't fetch more than 100 items, API limitation
|
|
|
|
QMap<QString, QString> parameters;
|
|
|
|
parameters.insert("limit", QString::number(m_maxPosts));
|
|
parameters.insert("page", QString::number(m_currentPage));
|
|
|
|
QUrl danbooruUrl = requestUrl(postUri(), m_username,
|
|
m_password, parameters, m_tags);
|
|
|
|
qCDebug(LIBDANBOORU) << "Final constructed post URL" << danbooruUrl;
|
|
|
|
KIO::StoredTransferJob *job = KIO::storedGet(danbooruUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo);
|
|
|
|
connect(job, &KIO::StoredTransferJob::result, this, &DanbooruServiceBase::processPostList);
|
|
|
|
}
|
|
|
|
void DanbooruService::getPoolList(int limit)
|
|
{
|
|
|
|
QUrl danbooruUrl;
|
|
|
|
if (m_currentPage == 1) {
|
|
danbooruUrl = requestUrl(poolUri(), m_username, m_password);
|
|
} else {
|
|
QMap<QString, QString> map;
|
|
map.insert("page", QString::number(m_currentPage));
|
|
|
|
danbooruUrl = requestUrl(poolUri(), m_username,
|
|
m_password, map);
|
|
}
|
|
|
|
qCDebug(LIBDANBOORU) << "Final constructed pool list URL" << danbooruUrl.url();
|
|
|
|
KIO::StoredTransferJob *job = KIO::storedGet(danbooruUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo);
|
|
|
|
connect(job, &KIO::StoredTransferJob::result, [this, limit](KJob * job) {
|
|
|
|
if (job->error()) {
|
|
Q_EMIT(downloadError(job->errorString()));
|
|
return;
|
|
}
|
|
|
|
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
|
|
QByteArray data = jobResult->data();
|
|
|
|
bool ok;
|
|
QList<QVariant> poolList = parseDanbooruResult(data, &ok).toList();
|
|
|
|
if (!ok) {
|
|
Q_EMIT(downloadError(QString("Unable to decode data")));
|
|
return;
|
|
}
|
|
|
|
if (limit > 0 && poolList.length() > limit) {
|
|
poolList = poolList.mid(0, limit);
|
|
}
|
|
|
|
for (auto element : qAsConst(poolList)) {
|
|
QVariantMap map = element.toMap();
|
|
|
|
DanbooruPool *pool = new DanbooruPool(map);
|
|
Q_EMIT(poolDownloaded(pool));
|
|
}
|
|
|
|
qCDebug(LIBDANBOORU) << "Pool download finished";
|
|
Q_EMIT(poolDownloadFinished());
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
void DanbooruService::getPool(int poolId, int page)
|
|
{
|
|
|
|
QMap<QString, QString> parameters;
|
|
|
|
if (page > 1) {
|
|
parameters.insert("page", QString::number(page));
|
|
}
|
|
|
|
QUrl dataUri;
|
|
dataUri = QUrl(m_url);
|
|
dataUri.setPath(QString("pools/%1.json").arg(poolId));
|
|
|
|
QUrl danbooruUrl = requestUrl(dataUri, m_username,
|
|
m_password, parameters);
|
|
|
|
qCDebug(LIBDANBOORU) << "Final constructed URL pool" << danbooruUrl;
|
|
|
|
KIO::StoredTransferJob *job = KIO::storedGet(danbooruUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo);
|
|
|
|
connect(job, &StoredTransferJob::result, [this](KJob * job) {
|
|
|
|
if (job->error()) {
|
|
Q_EMIT(downloadError(job->errorString()));
|
|
return;
|
|
}
|
|
|
|
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
|
|
QByteArray data = jobResult->data();
|
|
bool ok;
|
|
const QList<QVariantMap> postList = parseResult(data, apiType(), Pool, &ok);
|
|
|
|
if (!ok) {
|
|
Q_EMIT(downloadError(QString("Unable to decode data")));
|
|
return;
|
|
}
|
|
|
|
const auto postIds = postList.at(0).value("raw_post_data").toList();
|
|
|
|
m_postsToFetch = postIds.length();
|
|
|
|
for (const auto &postId: postIds) {
|
|
auto postUrl = QUrl(m_url);
|
|
postUrl.setPath(QString("/posts/%1.json").arg(postId.toString()));
|
|
postUrl = requestUrl(postUrl, m_username,
|
|
m_password);
|
|
StoredTransferJob* postJob = KIO::storedGet(postUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo);
|
|
|
|
connect(postJob, &StoredTransferJob::result, this, &DanbooruService::processSinglePost);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DanbooruService::getTagList(int limit, QString name)
|
|
{
|
|
QMap<QString, QString> parameters;
|
|
parameters.insert("limit", QString::number(limit));
|
|
|
|
if (!name.isEmpty()) {
|
|
parameters.insert("search[name]", name);
|
|
}
|
|
parameters.insert("search[order]", "date");
|
|
|
|
QUrl danbooruUrl = requestUrl(tagUri(), m_username, m_password,
|
|
parameters);
|
|
// qCDebug(LIBDANBOORU) << "Final constructed tag URL" << danbooruUrl.url();
|
|
|
|
KIO::StoredTransferJob *job = KIO::storedGet(danbooruUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo);
|
|
connect(job, &KIO::StoredTransferJob::result, this, &DanbooruService::processTagList);
|
|
|
|
}
|
|
|
|
void DanbooruService::getRelatedTags(const QStringList &tags,
|
|
DanbooruTag::TagType tagType)
|
|
{
|
|
|
|
QString type;
|
|
switch (tagType) {
|
|
case DanbooruTag::General:
|
|
type = "general";
|
|
break;
|
|
case DanbooruTag::Artist:
|
|
type = "artist";
|
|
break;
|
|
case DanbooruTag::Copyright:
|
|
type = "copyright";
|
|
break;
|
|
case DanbooruTag::Character:
|
|
type = "character";
|
|
break;
|
|
case DanbooruTag::Unknown:
|
|
type = "unknown";
|
|
break;
|
|
}
|
|
|
|
QMap<QString, QString> parameters;
|
|
parameters.insert("type", type);
|
|
parameters.insert("query", tags.join(" "));
|
|
|
|
QUrl danbooruUrl = requestUrl(relatedTagUri(), m_username,
|
|
m_password, parameters);
|
|
|
|
// qCDebug(LIBDANBOORU) << "Final constructed related tag URL" << danbooruUrl;
|
|
|
|
StoredTransferJob *job = KIO::storedGet(
|
|
danbooruUrl, KIO::NoReload,
|
|
KIO::HideProgressInfo
|
|
);
|
|
|
|
connect(job, &StoredTransferJob::result, [this](KJob * job) {
|
|
|
|
if (job->error()) {
|
|
Q_EMIT(downloadError(job->errorString()));
|
|
return;
|
|
}
|
|
|
|
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
|
|
QByteArray data = jobResult->data();
|
|
bool ok;
|
|
|
|
QVariantMap tagList = parseDanbooruResult(data, &ok).toMap();
|
|
|
|
if (!ok) {
|
|
Q_EMIT(downloadError(QString("Unable to decode data")));
|
|
return;
|
|
}
|
|
|
|
QVariantMap::const_iterator iter;
|
|
|
|
// The service returns a list of key-related tag list pair,
|
|
// we iterate through them and remove the empty (not found) ones, then
|
|
// we call getTagList. Unfortunately Danbooru doesn't have a method to
|
|
// fetch all tags in batch, so this is done one by one.
|
|
|
|
for (iter = tagList.constBegin(); iter != tagList.constEnd(); ++iter) {
|
|
|
|
QList<QVariant> tags = iter.value().toList();
|
|
|
|
if (tags.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
for (auto tag : tags) {
|
|
// We get the first element in the list, the second is
|
|
// the ID which is useless (no API methods in Danbooru)
|
|
QString tagName = tag.toList().at(0).toString();
|
|
getTagList(1, tagName);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
////////
|
|
// Slots
|
|
////////
|
|
|
|
void DanbooruService::processTagList(KJob *job)
|
|
{
|
|
|
|
if (job->error()) {
|
|
Q_EMIT(downloadError(job->errorString()));
|
|
return;
|
|
}
|
|
|
|
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
|
|
QByteArray data = jobResult->data();
|
|
|
|
bool ok;
|
|
|
|
// Most Danbooru implementations return tags in wrong order when
|
|
// using JSON, so we have to fall back to XML
|
|
const QList<QVariant> tagList = parseDanbooruResult(data, "tag", &ok);
|
|
|
|
if (!ok) {
|
|
Q_EMIT(downloadError(QString("Unable to decode data")));
|
|
return;
|
|
}
|
|
|
|
for (auto element : tagList) {
|
|
QVariantMap map = element.toMap();
|
|
DanbooruTag *tag = new DanbooruTag(map);
|
|
|
|
if (!tag) {
|
|
continue;
|
|
}
|
|
|
|
Q_EMIT(tagDownloaded(tag));
|
|
}
|
|
}
|
|
|
|
void DanbooruService::processSinglePost(KJob* job) {
|
|
|
|
if (job->error())
|
|
{
|
|
Q_EMIT(downloadError(job->errorString()));
|
|
return;
|
|
}
|
|
|
|
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
|
|
|
|
if (jobResult == 0) {
|
|
Q_EMIT(downloadError(QString("Internal error")));
|
|
return;
|
|
|
|
}
|
|
|
|
QByteArray data = jobResult->data();
|
|
bool ok;
|
|
|
|
QList<QVariantMap> postList;
|
|
postList = parseResult(data, apiType(), Danbooru::Post, &ok);
|
|
if (!ok) {
|
|
Q_EMIT(downloadError(QString("Unable to decode data")));
|
|
return;
|
|
}
|
|
|
|
// How many posts do we have to fetch?
|
|
|
|
if (postList.isEmpty()) {
|
|
qCDebug(LIBDANBOORU) << "No post found";
|
|
Q_EMIT(postDownloadFinished());
|
|
return;
|
|
}
|
|
|
|
DanbooruPost* post = new DanbooruPost(postList.at(0));
|
|
if (isPostBlacklisted(post, m_blacklist, m_maxRating)) {
|
|
m_postsToFetch--;
|
|
delete post;
|
|
return;
|
|
}
|
|
|
|
QPixmap pix;
|
|
qCDebug(LIBDANBOORU) << "About to download image";
|
|
|
|
StoredTransferJob *pixmapJob = KIO::storedGet(post->thumbnailUrl(),
|
|
KIO::NoReload, KIO::HideProgressInfo);
|
|
|
|
QVariant variant;
|
|
variant.setValue(post);
|
|
|
|
pixmapJob->setProperty("post", variant);
|
|
pixmapJob->setProperty("pixmap", pix);
|
|
connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruServiceBase::processPixmap);
|
|
|
|
}
|
|
|
|
|
|
void DanbooruService::downloadAllTags(KJob* job) {
|
|
Q_UNUSED(job);
|
|
}
|
|
|
|
} //namespace Danbooru
|
|
|
|
|
|
|