diff --git a/dataengines/potd/natgeoprovider.cpp b/dataengines/potd/natgeoprovider.cpp index d15644038..826206292 100644 --- a/dataengines/potd/natgeoprovider.cpp +++ b/dataengines/potd/natgeoprovider.cpp @@ -1,119 +1,115 @@ /* * Copyright 2007 Tobias Koenig * Copyright 2008 Anne-Marie Mahfouf * Copyright 2013 Aaron Seigo * * 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 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "natgeoprovider.h" #include #include -#include +#include #include #include class NatGeoProvider::Private { public: Private( NatGeoProvider *parent ) : mParent( parent ) { } void pageRequestFinished( KJob* ); void imageRequestFinished( KJob* ); NatGeoProvider *mParent; QImage mImage; - QXmlStreamReader mXmlReader; + QRegularExpression re; }; void NatGeoProvider::Private::pageRequestFinished( KJob* _job ) { KIO::StoredTransferJob *job = static_cast( _job ); if (job->error()) { emit mParent->error( mParent ); return; } const QString data = QString::fromUtf8( job->data() ); - - mXmlReader.clear(); - mXmlReader.addData(data); + const QStringList lines = data.split('\n'); QString url; - while (!mXmlReader.atEnd()) { - mXmlReader.readNext(); - - if (mXmlReader.isStartElement() && mXmlReader.name() == QLatin1String( "meta" )) { - const auto attrs = mXmlReader.attributes(); - if (attrs.value(QLatin1String("property")).toString() == QLatin1String("og:image")) { - url = attrs.value(QLatin1String("content")).toString(); - break; - } + + re.setPattern("^$"); + + for (int i = 0; i < lines.size(); i++) { + QRegularExpressionMatch match = re.match(lines.at(i).toLocal8Bit().constData()); + if (match.hasMatch()) { + url = match.captured(1); } } if (url.isEmpty()) { emit mParent->error( mParent ); return; } KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo ); mParent->connect( imageJob, &KIO::StoredTransferJob::finished, mParent, [this] (KJob *job) { imageRequestFinished(job); }); } void NatGeoProvider::Private::imageRequestFinished( KJob *_job ) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { emit mParent->error( mParent ); return; } mImage = QImage::fromData( job->data() ); emit mParent->finished( mParent ); } NatGeoProvider::NatGeoProvider( QObject *parent, const QVariantList &args ) : PotdProvider( parent, args ), d( new Private( this ) ) { - const QUrl url( QLatin1String( "https://www.nationalgeographic.com/photography/photo-of-the-day" ) ); + const QUrl url( QLatin1String( "http://www.nationalgeographic.com/photography/photo-of-the-day/" ) ); KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *job) { d->pageRequestFinished(job); }); } NatGeoProvider::~NatGeoProvider() { delete d; } QImage NatGeoProvider::image() const { return d->mImage; } K_PLUGIN_FACTORY_WITH_JSON(NatGeoProviderFactory, "natgeoprovider.json", registerPlugin();) #include "natgeoprovider.moc"