Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/natgeoprovider.cpp
Show All 17 Lines | |||||
18 | * Free Software Foundation, Inc., | 18 | * Free Software Foundation, Inc., | ||
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "natgeoprovider.h" | 22 | #include "natgeoprovider.h" | ||
23 | 23 | | |||
24 | #include <QtCore/QRegExp> | 24 | #include <QtCore/QRegExp> | ||
25 | #include <QtGui/QImage> | 25 | #include <QtGui/QImage> | ||
26 | #include <QXmlStreamReader> | 26 | #include <QRegularExpression> | ||
27 | 27 | | |||
28 | #include <QDebug> | 28 | #include <QDebug> | ||
29 | #include <KIO/Job> | 29 | #include <KIO/Job> | ||
30 | 30 | | |||
31 | class NatGeoProvider::Private | 31 | class NatGeoProvider::Private | ||
32 | { | 32 | { | ||
33 | public: | 33 | public: | ||
34 | Private( NatGeoProvider *parent ) | 34 | Private( NatGeoProvider *parent ) | ||
35 | : mParent( parent ) | 35 | : mParent( parent ) | ||
36 | { | 36 | { | ||
37 | } | 37 | } | ||
38 | 38 | | |||
39 | void pageRequestFinished( KJob* ); | 39 | void pageRequestFinished( KJob* ); | ||
40 | void imageRequestFinished( KJob* ); | 40 | void imageRequestFinished( KJob* ); | ||
41 | 41 | | |||
42 | NatGeoProvider *mParent; | 42 | NatGeoProvider *mParent; | ||
43 | QImage mImage; | 43 | QImage mImage; | ||
44 | 44 | | |||
45 | QXmlStreamReader mXmlReader; | 45 | QRegularExpression re; | ||
46 | }; | 46 | }; | ||
47 | 47 | | |||
48 | void NatGeoProvider::Private::pageRequestFinished( KJob* _job ) | 48 | void NatGeoProvider::Private::pageRequestFinished( KJob* _job ) | ||
49 | { | 49 | { | ||
50 | KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job ); | 50 | KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job ); | ||
51 | if (job->error()) { | 51 | if (job->error()) { | ||
52 | emit mParent->error( mParent ); | 52 | emit mParent->error( mParent ); | ||
53 | return; | 53 | return; | ||
54 | } | 54 | } | ||
55 | 55 | | |||
56 | const QString data = QString::fromUtf8( job->data() ); | 56 | const QString data = QString::fromUtf8( job->data() ); | ||
57 | 57 | const QStringList lines = data.split('\n'); | |||
58 | mXmlReader.clear(); | | |||
59 | mXmlReader.addData(data); | | |||
60 | 58 | | |||
61 | QString url; | 59 | QString url; | ||
62 | while (!mXmlReader.atEnd()) { | | |||
63 | mXmlReader.readNext(); | | |||
64 | 60 | | |||
65 | if (mXmlReader.isStartElement() && mXmlReader.name() == QLatin1String( "meta" )) { | 61 | re.setPattern("^<meta\\s+property=\"og:image\"\\s+content=\"(.*)\".*/>$"); | ||
vitali: An alternative regex may be `"^<meta\\s.*property=\"og:image\"\\s.*content=\"(.*)\".*/>$"`, so… | |||||
66 | const auto attrs = mXmlReader.attributes(); | 62 | | ||
67 | if (attrs.value(QLatin1String("property")).toString() == QLatin1String("og:image")) { | 63 | for (int i = 0; i < lines.size(); i++) { | ||
68 | url = attrs.value(QLatin1String("content")).toString(); | 64 | QRegularExpressionMatch match = re.match(lines.at(i).toLocal8Bit().constData()); | ||
69 | break; | 65 | if (match.hasMatch()) { | ||
70 | } | 66 | url = match.captured(1); | ||
71 | } | 67 | } | ||
72 | } | 68 | } | ||
73 | 69 | | |||
74 | if (url.isEmpty()) { | 70 | if (url.isEmpty()) { | ||
75 | emit mParent->error( mParent ); | 71 | emit mParent->error( mParent ); | ||
76 | return; | 72 | return; | ||
77 | } | 73 | } | ||
78 | 74 | | |||
Show All 13 Lines | 82 | { | |||
92 | 88 | | |||
93 | mImage = QImage::fromData( job->data() ); | 89 | mImage = QImage::fromData( job->data() ); | ||
94 | emit mParent->finished( mParent ); | 90 | emit mParent->finished( mParent ); | ||
95 | } | 91 | } | ||
96 | 92 | | |||
97 | NatGeoProvider::NatGeoProvider( QObject *parent, const QVariantList &args ) | 93 | NatGeoProvider::NatGeoProvider( QObject *parent, const QVariantList &args ) | ||
98 | : PotdProvider( parent, args ), d( new Private( this ) ) | 94 | : PotdProvider( parent, args ), d( new Private( this ) ) | ||
99 | { | 95 | { | ||
100 | const QUrl url( QLatin1String( "https://www.nationalgeographic.com/photography/photo-of-the-day" ) ); | 96 | const QUrl url( QLatin1String( "http://www.nationalgeographic.com/photography/photo-of-the-day/" ) ); | ||
sebas: Why this change? Moving away from https to http seems backward... | |||||
The versions with https or without the trailing slash were not working for me, so I changed it to that one, but it could as well be just be my mistake. vitali: The versions with https or without the trailing slash were not working for me, so I changed it… | |||||
Zren: The https redirects to http (HTTP 302 temp move), then redirects to add the trailing slash… | |||||
101 | KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); | 97 | KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); | ||
102 | connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *job) { | 98 | connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *job) { | ||
103 | d->pageRequestFinished(job); | 99 | d->pageRequestFinished(job); | ||
104 | }); | 100 | }); | ||
105 | } | 101 | } | ||
106 | 102 | | |||
107 | NatGeoProvider::~NatGeoProvider() | 103 | NatGeoProvider::~NatGeoProvider() | ||
108 | { | 104 | { | ||
Show All 11 Lines |
An alternative regex may be "^<meta\\s.*property=\"og:image\"\\s.*content=\"(.*)\".*/>$", so to match any possible future fields before property, and between property and content.