7#include "wikidataquery.h"
10#include <QJsonDocument>
12#include <QNetworkRequest>
13#include <QNetworkReply>
17using namespace Wikidata;
19inline constexpr const auto WikidataGetEntitiesMaxCount = 50;
20inline constexpr const auto WikidataQueryMaxCount = 50;
27Query::~Query() =
default;
29Query::Error Query::error()
const
37 query.addQueryItem(u
"format"_s, u
"json"_s);
42EntitiesQuery::EntitiesQuery(
QObject* parent)
47EntitiesQuery::~EntitiesQuery() =
default;
49void EntitiesQuery::setItems(std::vector<Wikidata::Q> &&items)
51 m_items = std::move(items);
56 QUrl url(u
"https://www.wikidata.org/w/api.php"_s);
57 auto query = commonUrlQuery();
58 query.addQueryItem(u
"action"_s, u
"wbgetentities"_s);
59 query.addQueryItem(u
"props"_s, u
"claims"_s);
62 for (
auto i = m_nextBatch; i < std::min(m_items.size(), m_nextBatch + WikidataGetEntitiesMaxCount); ++i) {
63 if (i != m_nextBatch) {
66 ids += m_items[i].toString();
68 m_nextBatch += WikidataGetEntitiesMaxCount;
69 query.addQueryItem(u
"ids"_s, ids);
77std::vector<Wikidata::Item>&& EntitiesQuery::takeResult()
79 return std::move(m_result);
85 const auto entities = doc.object().value(
"entities"_L1).toObject();
86 m_result.reserve(entities.size());
87 for (
auto it = entities.begin(); it != entities.end(); ++it) {
88 m_result.emplace_back(
Q(it.key()), it.value().toObject());
90 Q_EMIT partialResult(
this);
92 if (m_nextBatch < m_items.size()) {
101ImageMetadataQuery::ImageMetadataQuery(
QObject* parent)
106ImageMetadataQuery::~ImageMetadataQuery() =
default;
108void ImageMetadataQuery::setImages(std::vector<QString> &&images)
110 m_images = std::move(images);
115 QUrl url(u
"https://commons.wikimedia.org/w/api.php"_s);
116 auto query = commonUrlQuery();
117 query.addQueryItem(u
"action"_s, u
"query"_s);
118 query.addQueryItem(u
"prop"_s, u
"imageinfo"_s);
119 query.addQueryItem(u
"iiprop"_s, u
"extmetadata|mime|size"_s);
121 for (
auto i = m_nextBatch; i < std::min(m_images.size(), m_nextBatch + WikidataQueryMaxCount); ++i) {
122 if (i != m_nextBatch) {
125 ids +=
"File:"_L1 + m_images[i];
127 m_nextBatch += WikidataQueryMaxCount;
128 query.addQueryItem(u
"titles"_s, ids);
136std::vector<Wikidata::Image>&& ImageMetadataQuery::takeResult()
138 return std::move(m_result);
144 const auto images = doc.object().value(
"query"_L1).toObject().value(
"pages"_L1).toObject();
145 m_result.reserve(images.size());
146 for (
const auto &img : images) {
147 m_result.emplace_back(img.toObject());
149 Q_EMIT partialResult(
this);
151 if (m_nextBatch < m_images.size()) {
159#include "moc_wikidataquery.cpp"
Wikidata item identifier.
Base class for Wikidata queries.
std::optional< QSqlQuery > query(const QString &queryStatement)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
CacheLoadControlAttribute