KOSMIndoorMap

wikidataquery.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "wikidataquery.h"
8
9#include <QDebug>
10#include <QJsonDocument>
11#include <QJsonObject>
12#include <QNetworkRequest>
13#include <QNetworkReply>
14#include <QUrlQuery>
15
16using namespace Qt::Literals::StringLiterals;
17using namespace Wikidata;
18
19inline constexpr const auto WikidataGetEntitiesMaxCount = 50;
20inline constexpr const auto WikidataQueryMaxCount = 50;
21
22Query::Query(QObject *parent) :
23 QObject(parent)
24{
25}
26
27Query::~Query() = default;
28
29Query::Error Query::error() const
30{
31 return m_error;
32}
33
34QUrlQuery Query::commonUrlQuery()
35{
37 query.addQueryItem(u"format"_s, u"json"_s);
38 return query;
39}
40
41
42EntitiesQuery::EntitiesQuery(QObject* parent)
43 : Query(parent)
44{
45}
46
47EntitiesQuery::~EntitiesQuery() = default;
48
49void EntitiesQuery::setItems(std::vector<Wikidata::Q> &&items)
50{
51 m_items = std::move(items);
52}
53
54QNetworkRequest EntitiesQuery::nextRequest()
55{
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);
60
61 QString ids;
62 for (auto i = m_nextBatch; i < std::min(m_items.size(), m_nextBatch + WikidataGetEntitiesMaxCount); ++i) {
63 if (i != m_nextBatch) {
64 ids += '|'_L1;
65 }
66 ids += m_items[i].toString();
67 }
68 m_nextBatch += WikidataGetEntitiesMaxCount;
69 query.addQueryItem(u"ids"_s, ids);
70 url.setQuery(query);
71
72 QNetworkRequest req(url);
74 return req;
75}
76
77std::vector<Wikidata::Item>&& EntitiesQuery::takeResult()
78{
79 return std::move(m_result);
80}
81
82bool EntitiesQuery::processReply(QNetworkReply *reply)
83{
84 const auto doc = QJsonDocument::fromJson(reply->readAll());
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());
89 }
90 Q_EMIT partialResult(this);
91
92 if (m_nextBatch < m_items.size()) {
93 return false;
94 }
95
96 Q_EMIT finished();
97 return true;
98}
99
100
101ImageMetadataQuery::ImageMetadataQuery(QObject* parent)
102 : Query(parent)
103{
104}
105
106ImageMetadataQuery::~ImageMetadataQuery() = default;
107
108void ImageMetadataQuery::setImages(std::vector<QString> &&images)
109{
110 m_images = std::move(images);
111}
112
113QNetworkRequest ImageMetadataQuery::nextRequest()
114{
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);
120 QString ids;
121 for (auto i = m_nextBatch; i < std::min(m_images.size(), m_nextBatch + WikidataQueryMaxCount); ++i) {
122 if (i != m_nextBatch) {
123 ids += '|'_L1;
124 }
125 ids += "File:"_L1 + m_images[i];
126 }
127 m_nextBatch += WikidataQueryMaxCount;
128 query.addQueryItem(u"titles"_s, ids);
129 url.setQuery(query);
130
131 QNetworkRequest req(url);
133 return req;
134}
135
136std::vector<Wikidata::Image>&& ImageMetadataQuery::takeResult()
137{
138 return std::move(m_result);
139}
140
141bool ImageMetadataQuery::processReply(QNetworkReply *reply)
142{
143 const auto doc = QJsonDocument::fromJson(reply->readAll());
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());
148 }
149 Q_EMIT partialResult(this);
150
151 if (m_nextBatch < m_images.size()) {
152 return false;
153 }
154
155 Q_EMIT finished();
156 return true;
157}
158
159#include "moc_wikidataquery.cpp"
Wikidata item identifier.
Definition entities.h:75
Base class for Wikidata queries.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.