Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/noaaprovider.cpp
Show All 20 Lines | |||||
21 | #include "noaaprovider.h" | 21 | #include "noaaprovider.h" | ||
22 | 22 | | |||
23 | #include <QRegularExpression> | 23 | #include <QRegularExpression> | ||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | 25 | | |||
26 | #include <KPluginFactory> | 26 | #include <KPluginFactory> | ||
27 | #include <KIO/Job> | 27 | #include <KIO/Job> | ||
28 | 28 | | |||
29 | | ||||
30 | NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args) | 29 | NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args) | ||
31 | : PotdProvider(parent, args) | 30 | : PotdProvider(parent, args) | ||
32 | { | 31 | { | ||
33 | const QUrl url(QStringLiteral("http://www.nnvl.noaa.gov/imageoftheday.php")); | 32 | const QUrl url(QStringLiteral("http://www.nesdis.noaa.gov/content/imagery-and-data")); | ||
34 | 33 | | |||
35 | KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | 34 | KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | ||
36 | connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::pageRequestFinished); | 35 | connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::firstPageRequestFinished); | ||
37 | } | 36 | } | ||
38 | 37 | | |||
39 | NOAAProvider::~NOAAProvider() = default; | 38 | NOAAProvider::~NOAAProvider() = default; | ||
40 | 39 | | |||
41 | QImage NOAAProvider::image() const | 40 | QImage NOAAProvider::image() const | ||
42 | { | 41 | { | ||
43 | return mImage; | 42 | return mImage; | ||
44 | } | 43 | } | ||
45 | 44 | | |||
46 | void NOAAProvider::pageRequestFinished(KJob* _job) | 45 | void NOAAProvider::firstPageRequestFinished(KJob* _job) | ||
46 | { | ||||
47 | KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job ); | ||||
48 | if (job->error()) { | ||||
49 | emit error(this); | ||||
50 | return; | ||||
51 | } | ||||
52 | | ||||
53 | const QString data = QString::fromUtf8( job->data() ); | ||||
54 | | ||||
55 | // Using regular expression could be fragile in such case, but the HTML | ||||
56 | // NOAA page itself is not a valid XML file and unfortunately it could | ||||
57 | // not be parsed successfully till the content we want. And we do not want | ||||
58 | // to use heavy weight QtWebkit. So we use QRegularExpression to capture | ||||
59 | // the wanted url here. | ||||
60 | QString url; | ||||
61 | QRegularExpression re(QStringLiteral("href=\"(.*)\"><img alt=\"Latest Image of the Day"), QRegularExpression::MultilineOption); | ||||
62 | auto result = re.match(data); | ||||
63 | if (result.hasMatch()) | ||||
64 | { | ||||
65 | url = result.captured(1); | ||||
66 | } | ||||
67 | if (url.isEmpty()) | ||||
68 | { | ||||
69 | emit error(this); | ||||
70 | return; | ||||
71 | } | ||||
72 | | ||||
73 | job = KIO::storedGet(QUrl(url), KIO::NoReload, KIO::HideProgressInfo); | ||||
74 | connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::secondPageRequestFinished); | ||||
75 | } | ||||
76 | | ||||
77 | void NOAAProvider::secondPageRequestFinished(KJob* _job) | ||||
47 | { | 78 | { | ||
48 | KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job ); | 79 | KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job ); | ||
49 | if (job->error()) { | 80 | if (job->error()) { | ||
50 | emit error(this); | 81 | emit error(this); | ||
51 | return; | 82 | return; | ||
52 | } | 83 | } | ||
53 | 84 | | |||
54 | const QString data = QString::fromUtf8( job->data() ); | 85 | const QString data = QString::fromUtf8( job->data() ); | ||
55 | 86 | | |||
56 | // Using regular expression could be fragile in such case, but the HTML | 87 | // Using regular expression could be fragile in such case, but the HTML | ||
57 | // NOAA page itself is not a valid XML file and unfortunately it could | 88 | // NOAA page itself is not a valid XML file and unfortunately it could | ||
58 | // not be parsed successfully till the content we want. And we do not want | 89 | // not be parsed successfully till the content we want. And we do not want | ||
59 | // to use heavy weight QtWebkit. So we use QRegularExpression to capture | 90 | // to use heavy weight QtWebkit. So we use QRegularExpression to capture | ||
60 | // the wanted url here. | 91 | // the wanted url here. | ||
61 | QString url; | 92 | QString url; | ||
62 | QRegularExpression re(QStringLiteral("_curPic = (.*?)</script>")); | 93 | QRegularExpression re(QStringLiteral("a href='(.*)'><img style"), QRegularExpression::MultilineOption); | ||
63 | auto result = re.match(data); | 94 | auto result = re.match(data); | ||
64 | if (result.hasMatch()) { | 95 | if (result.hasMatch()) { | ||
65 | url = QLatin1String("http://www.nnvl.noaa.gov/") + result.captured(1); | 96 | url = result.captured(1); | ||
66 | } | 97 | } | ||
67 | if (url.isEmpty()) { | 98 | if (url.isEmpty()) { | ||
68 | emit error(this); | 99 | emit error(this); | ||
69 | return; | 100 | return; | ||
70 | } | 101 | } | ||
71 | 102 | | |||
72 | KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo ); | 103 | KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo ); | ||
73 | connect(imageJob, &KIO::StoredTransferJob::finished, this, &NOAAProvider::imageRequestFinished); | 104 | connect(imageJob, &KIO::StoredTransferJob::finished, this, &NOAAProvider::imageRequestFinished); | ||
Show All 17 Lines |