File indexing completed on 2024-12-29 05:00:27

0001 /*
0002  *   SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0003  *   SPDX-FileCopyrightText: 2008 Anne-Marie Mahfouf <annma@kde.org>
0004  *   SPDX-FileCopyrightText: 2016 Weng Xuetian <wengxt@gmail.com>
0005  *
0006  *   SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "wcpotdprovider.h"
0010 
0011 #include <QJsonArray>
0012 #include <QJsonDocument>
0013 #include <QTextDocumentFragment>
0014 #include <QUrlQuery>
0015 
0016 #include <KIO/StoredTransferJob>
0017 #include <KPluginFactory>
0018 
0019 WcpotdProvider::WcpotdProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0020     : PotdProvider(parent, data, args)
0021 {
0022     QUrl url(QStringLiteral("https://commons.wikimedia.org/w/api.php"));
0023 
0024     QUrlQuery urlQuery(url);
0025     urlQuery.addQueryItem(QStringLiteral("action"), QStringLiteral("parse"));
0026     urlQuery.addQueryItem(QStringLiteral("text"), QStringLiteral("{{Potd}}"));
0027     urlQuery.addQueryItem(QStringLiteral("contentmodel"), QStringLiteral("wikitext"));
0028     urlQuery.addQueryItem(QStringLiteral("format"), QStringLiteral("json"));
0029     url.setQuery(urlQuery);
0030 
0031     KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
0032     connect(job, &KIO::StoredTransferJob::finished, this, &WcpotdProvider::pageRequestFinished);
0033 }
0034 
0035 void WcpotdProvider::pageRequestFinished(KJob *_job)
0036 {
0037     KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
0038     if (job->error()) {
0039         Q_EMIT error(this);
0040         return;
0041     }
0042 
0043     const QJsonObject jsonObject = QJsonDocument::fromJson(job->data()).object().value(QLatin1String("parse")).toObject();
0044     const QJsonArray jsonImageArray = jsonObject.value(QLatin1String("images")).toArray();
0045 
0046     if (jsonImageArray.size() == 0) {
0047         Q_EMIT error(this);
0048         return;
0049     }
0050 
0051     const QString imageFile = jsonImageArray.at(0).toString();
0052     if (!imageFile.isEmpty()) {
0053         m_remoteUrl = QUrl(QStringLiteral("https://commons.wikimedia.org/wiki/Special:FilePath/") + imageFile);
0054         KIO::StoredTransferJob *imageJob = KIO::storedGet(m_remoteUrl, KIO::NoReload, KIO::HideProgressInfo);
0055         connect(imageJob, &KIO::StoredTransferJob::finished, this, &WcpotdProvider::imageRequestFinished);
0056     } else {
0057         Q_EMIT error(this);
0058         return;
0059     }
0060 
0061     const QJsonObject jsonTextObject = jsonObject.value(QStringLiteral("text")).toObject();
0062     const QString text = jsonTextObject.value(QStringLiteral("*")).toString().trimmed();
0063 
0064     if (text.isEmpty()) {
0065         return;
0066     }
0067 
0068     /**
0069      * Match link and title.
0070      * Example:
0071      * <div lang="en" dir="ltr" class="description en" style="display:inline;"><a href="https://en.wikipedia.org/wiki/Petros_(Chornohora)" class="extiw"
0072      * title="w:Petros (Chornohora)">Mount Petros</a>
0073      */
0074     const QRegularExpression titleRegEx(QStringLiteral("<div.*?class=\"description.*?>.*?<a href=\"(.+?)\".*?>(.+?)</a>"));
0075     const QRegularExpressionMatch titleMatch = titleRegEx.match(text);
0076     if (titleMatch.hasMatch()) {
0077         m_infoUrl = QUrl(titleMatch.captured(1).trimmed());
0078         m_title = QTextDocumentFragment::fromHtml(titleMatch.captured(2).trimmed()).toPlainText();
0079     }
0080 }
0081 
0082 void WcpotdProvider::imageRequestFinished(KJob *_job)
0083 {
0084     KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
0085     if (job->error()) {
0086         Q_EMIT error(this);
0087         return;
0088     }
0089     QByteArray data = job->data();
0090     Q_EMIT finished(this, QImage::fromData(data));
0091 }
0092 
0093 K_PLUGIN_CLASS_WITH_JSON(WcpotdProvider, "wcpotdprovider.json")
0094 
0095 #include "wcpotdprovider.moc"