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  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "epodprovider.h"
0009 
0010 #include <QRegularExpression>
0011 #include <QTextDocumentFragment>
0012 
0013 #include <KIO/StoredTransferJob>
0014 #include <KPluginFactory>
0015 
0016 EpodProvider::EpodProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0017     : PotdProvider(parent, data, args)
0018 {
0019     const QUrl url(QStringLiteral("https://epod.usra.edu/blog/"));
0020 
0021     KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
0022     connect(job, &KIO::StoredTransferJob::finished, this, &EpodProvider::pageRequestFinished);
0023 }
0024 
0025 void EpodProvider::pageRequestFinished(KJob *_job)
0026 {
0027     KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
0028     if (job->error()) {
0029         Q_EMIT error(this);
0030         return;
0031     }
0032 
0033     const QString data = QString::fromUtf8(job->data()).simplified();
0034 
0035     const QString pattern = QStringLiteral("://epod.usra.edu/.a/*-pi");
0036     const auto exp = QRegularExpression(QRegularExpression::wildcardToRegularExpression(pattern));
0037     const auto expMatch = exp.match(data);
0038 
0039     if (expMatch.hasMatch()) {
0040         /**
0041          * Match link and title
0042          * Example:
0043          * <h3 class="entry-header">
0044          * <a href="https://epod.usra.edu/blog/2022/01/archive-panamint-delta-.html">Archive - Panamint Delta </a>
0045          * </h3><!-- .entry-header -->
0046          */
0047         const QRegularExpression titleRegEx(QStringLiteral("<h3 class=\"entry-header\">.*?<a href=\"(.+?)\">(.+?)</a>.*?</h3>"));
0048         const QRegularExpressionMatch titleMatch = titleRegEx.match(data);
0049         if (titleMatch.hasMatch()) {
0050             m_infoUrl = QUrl(titleMatch.captured(1).trimmed());
0051             m_title = QTextDocumentFragment::fromHtml(titleMatch.captured(2).trimmed()).toPlainText();
0052         }
0053 
0054         /**
0055          * Match author
0056          * Example:
0057          * <strong>Photographer</strong>: <a href="...">Wendy Van Norden</a>
0058          */
0059         const QRegularExpression authorRegEx(QStringLiteral("<strong>Photographer.*?</strong>.*?<a.+?>(.+?)</a>"));
0060         const QRegularExpressionMatch authorMatch = authorRegEx.match(data);
0061         if (authorMatch.hasMatch()) {
0062             m_author = QTextDocumentFragment::fromHtml(authorMatch.captured(1).trimmed()).toPlainText();
0063         }
0064     } else {
0065         Q_EMIT error(this);
0066         return;
0067     }
0068 
0069     int pos = expMatch.capturedStart() + pattern.length();
0070     const QString sub = data.mid(pos - 4, pattern.length() + 10);
0071     m_remoteUrl = QUrl(QStringLiteral("https://epod.usra.edu/.a/%1-pi").arg(sub));
0072 
0073     KIO::StoredTransferJob *imageJob = KIO::storedGet(m_remoteUrl, KIO::NoReload, KIO::HideProgressInfo);
0074     connect(imageJob, &KIO::StoredTransferJob::finished, this, &EpodProvider::imageRequestFinished);
0075 }
0076 
0077 void EpodProvider::imageRequestFinished(KJob *_job)
0078 {
0079     KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
0080     if (job->error()) {
0081         Q_EMIT error(this);
0082         return;
0083     }
0084 
0085     // FIXME: this really should be done in a thread as this can block
0086     Q_EMIT finished(this, QImage::fromData(job->data()));
0087 }
0088 
0089 K_PLUGIN_CLASS_WITH_JSON(EpodProvider, "epodprovider.json")
0090 
0091 #include "epodprovider.moc"