Commit ff26a139 authored by Tagore Chandan Reddy's avatar Tagore Chandan Reddy Committed by Nate Graham

[KdePlasma-Addons/POTD/NOAA] Fixed the web address and fetched the picture from new address

Summary:
The web address for NOAA is no longer valid. So, the new address had to be provided.

Old address: http://www.nnvl.noaa.gov/imageoftheday.php
new address: https://www.nesdis.noaa.gov/content/imagery-and-data

'Image of the day' has a new landing page everyday. So the code had to slightly altered to fetch image from new address.

Test Plan: All plugins should work as usual.

Reviewers: #plasma, kossebau, davidedmundson

Reviewed By: #plasma, davidedmundson

Subscribers: ngraham, plasma-devel, #plasma

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D13868
parent 62c106fb
...@@ -26,14 +26,13 @@ ...@@ -26,14 +26,13 @@
#include <KPluginFactory> #include <KPluginFactory>
#include <KIO/Job> #include <KIO/Job>
NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args) NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args)
: PotdProvider(parent, args) : PotdProvider(parent, args)
{ {
const QUrl url(QStringLiteral("http://www.nnvl.noaa.gov/imageoftheday.php")); const QUrl url(QStringLiteral("http://www.nesdis.noaa.gov/content/imagery-and-data"));
KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::pageRequestFinished); connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::firstPageRequestFinished);
} }
NOAAProvider::~NOAAProvider() = default; NOAAProvider::~NOAAProvider() = default;
...@@ -43,7 +42,39 @@ QImage NOAAProvider::image() const ...@@ -43,7 +42,39 @@ QImage NOAAProvider::image() const
return mImage; return mImage;
} }
void NOAAProvider::pageRequestFinished(KJob* _job) void NOAAProvider::firstPageRequestFinished(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("href=\"(.*)\"><img alt=\"Latest Image of the Day"), QRegularExpression::MultilineOption);
auto result = re.match(data);
if (result.hasMatch())
{
url = 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 ); KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job );
if (job->error()) { if (job->error()) {
...@@ -59,10 +90,10 @@ void NOAAProvider::pageRequestFinished(KJob* _job) ...@@ -59,10 +90,10 @@ void NOAAProvider::pageRequestFinished(KJob* _job)
// to use heavy weight QtWebkit. So we use QRegularExpression to capture // to use heavy weight QtWebkit. So we use QRegularExpression to capture
// the wanted url here. // the wanted url here.
QString url; QString url;
QRegularExpression re(QStringLiteral("_curPic = (.*?)</script>")); QRegularExpression re(QStringLiteral("a href='(.*)'><img style"), QRegularExpression::MultilineOption);
auto result = re.match(data); auto result = re.match(data);
if (result.hasMatch()) { if (result.hasMatch()) {
url = QLatin1String("http://www.nnvl.noaa.gov/") + result.captured(1); url = result.captured(1);
} }
if (url.isEmpty()) { if (url.isEmpty()) {
emit error(this); emit error(this);
......
...@@ -30,8 +30,8 @@ class KJob; ...@@ -30,8 +30,8 @@ class KJob;
/** /**
* This class provides the image for NOAA Environmental Visualization Laboratory * This class provides the image for NOAA Environmental Visualization Laboratory
* Picture Of the Day * Image Of the Day
* located at http://www.nnvl.noaa.gov/imageoftheday.php. * located at http://www.nesdis.noaa.gov/content/imagery-and-data.
*/ */
class NOAAProvider : public PotdProvider class NOAAProvider : public PotdProvider
{ {
...@@ -60,7 +60,8 @@ class NOAAProvider : public PotdProvider ...@@ -60,7 +60,8 @@ class NOAAProvider : public PotdProvider
QImage image() const override; QImage image() const override;
private: private:
void pageRequestFinished(KJob *job); void firstPageRequestFinished(KJob *job);
void secondPageRequestFinished(KJob *job);
void imageRequestFinished(KJob *job); void imageRequestFinished(KJob *job);
private: 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