Commit b2d041fb authored by Yunhe Guo's avatar Yunhe Guo Committed by Nate Graham

Fix NOAA picture of the day provider

Summary:
The webpage structure of NOAA changed so the old regular expression doesn't work anymore. Updated it. But it can break in future again.

{F7614976}

BUG: 408580

Reviewers: #plasma, sitter

Reviewed By: sitter

Subscribers: sitter, ngraham, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D24727
parent bd228189
......@@ -29,10 +29,10 @@
NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args)
: PotdProvider(parent, args)
{
const QUrl url(QStringLiteral("http://www.nesdis.noaa.gov/content/imagery-and-data"));
const QUrl url(QStringLiteral("https://www.nesdis.noaa.gov/content/imagery-and-data"));
KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::firstPageRequestFinished);
connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::pageRequestFinished);
}
NOAAProvider::~NOAAProvider() = default;
......@@ -42,7 +42,7 @@ QImage NOAAProvider::image() const
return mImage;
}
void NOAAProvider::firstPageRequestFinished(KJob* _job)
void NOAAProvider::pageRequestFinished(KJob* _job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job );
if (job->error()) {
......@@ -57,50 +57,18 @@ void NOAAProvider::firstPageRequestFinished(KJob* _job)
// not be parsed successfully till the content we want. And we do not want
// to use heavy weight QtWebkit. So we use QRegularExpression to capture
// the wanted url here.
QString url;
QRegularExpression re(QStringLiteral("href=\".*\\/content\\/(.*)\"><img alt=\"Latest Image of the Day"), QRegularExpression::MultilineOption);
auto result = re.match(data);
if (result.hasMatch())
{
url = QStringLiteral("http://www.nesdis.noaa.gov/content/") + result.captured(1);
}
if (url.isEmpty())
{
emit error(this);
return;
}
job = KIO::storedGet(QUrl(url), KIO::NoReload, KIO::HideProgressInfo);
connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::secondPageRequestFinished);
}
void NOAAProvider::secondPageRequestFinished(KJob* _job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job );
if (job->error()) {
emit error(this);
return;
}
const QString data = QString::fromUtf8( job->data() );
// Using regular expression could be fragile in such case, but the HTML
// NOAA page itself is not a valid XML file and unfortunately it could
// not be parsed successfully till the content we want. And we do not want
// to use heavy weight QtWebkit. So we use QRegularExpression to capture
// the wanted url here.
QString url;
QRegularExpression re(QStringLiteral("a href='(.*)'><img style"), QRegularExpression::MultilineOption);
QUrl url;
QRegularExpression re(QStringLiteral("\"(/sites/default/files/.*\\.jpg)\""));
auto result = re.match(data);
if (result.hasMatch()) {
url = result.captured(1);
url = QUrl(QStringLiteral("https://www.nesdis.noaa.gov") + result.captured(1));
}
if (url.isEmpty()) {
if (!url.isValid()) {
emit error(this);
return;
}
KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo );
KIO::StoredTransferJob *imageJob = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo );
connect(imageJob, &KIO::StoredTransferJob::finished, this, &NOAAProvider::imageRequestFinished);
}
......
......@@ -3,10 +3,10 @@
* Copyright 2008 by Anne-Marie Mahfouf <annma@kde.org>
* Copyright 2016 Weng Xuetian <wengxt@gmail.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
......@@ -60,8 +60,7 @@ class NOAAProvider : public PotdProvider
QImage image() const override;
private:
void firstPageRequestFinished(KJob *job);
void secondPageRequestFinished(KJob *job);
void pageRequestFinished(KJob *job);
void imageRequestFinished(KJob *job);
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment