Commit b0ae7ffc authored by Fushan Wen's avatar Fushan Wen Committed by Nate Graham
Browse files

dataengines/potd: Parse title, author, infoUrl and remoteUrl in FlickrProvider

Parse the title from XML, the author from the XML data, and set the
info URL and the remote URL.

CCBUG: 422934
parent e8272963
......@@ -10,6 +10,7 @@
#include <QDebug>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <QTextDocumentFragment>
#include <QUrlQuery>
#include <KIO/Job>
......@@ -70,6 +71,7 @@ void FlickrProvider::xmlRequestFinished(KJob *_job)
// Clear the list
m_photoList.clear();
m_photoList.reserve(100);
xml.clear();
xml.addData(data);
......@@ -98,7 +100,20 @@ void FlickrProvider::xmlRequestFinished(KJob *_job)
// Get the best url.
QLatin1String urlAttrString(urlAttr);
if (attributes.hasAttribute(urlAttrString)) {
m_photoList.append(attributes.value(urlAttrString).toString());
QString title, userId, photoId;
if (attributes.hasAttribute(QStringLiteral("title"))) {
title = QTextDocumentFragment::fromHtml(attributes.value(QStringLiteral("title")).toString().trimmed()).toPlainText();
}
if (attributes.hasAttribute(QStringLiteral("owner")) && attributes.hasAttribute(QStringLiteral("id"))) {
userId = attributes.value(QStringLiteral("owner")).toString();
photoId = attributes.value(QStringLiteral("id")).toString();
}
m_photoList.emplace_back(PhotoEntry{
attributes.value(urlAttrString).toString(),
title,
userId,
photoId,
});
found = true;
break;
}
......@@ -110,7 +125,7 @@ void FlickrProvider::xmlRequestFinished(KJob *_job)
if (found) {
QLatin1String originAttr("url_o");
if (attributes.hasAttribute(originAttr)) {
m_photoList.back() = attributes.value(QLatin1String(originAttr)).toString();
m_photoList.back().urlString = attributes.value(QLatin1String(originAttr)).toString();
}
}
}
......@@ -122,8 +137,22 @@ void FlickrProvider::xmlRequestFinished(KJob *_job)
}
if (m_photoList.begin() != m_photoList.end()) {
QUrl url(m_photoList.at(QRandomGenerator::global()->bounded(m_photoList.size())));
KIO::StoredTransferJob *imageJob = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
const auto randomNumber = QRandomGenerator::global()->bounded(static_cast<int>(m_photoList.size()));
const PhotoEntry &randomPhotoEntry = m_photoList.at(randomNumber);
potdProviderData()->wallpaperRemoteUrl = QUrl(randomPhotoEntry.urlString);
potdProviderData()->wallpaperTitle = randomPhotoEntry.title;
/**
* Visit the photo page to get the author
* API document: https://www.flickr.com/services/api/misc.urls.html
* https://www.flickr.com/photos/{user-id}/{photo-id}
*/
if (!(randomPhotoEntry.userId.isEmpty() || randomPhotoEntry.photoId.isEmpty())) {
potdProviderData()->wallpaperInfoUrl =
QUrl(QStringLiteral("https://www.flickr.com/photos/%1/%2").arg(randomPhotoEntry.userId, randomPhotoEntry.photoId));
}
KIO::StoredTransferJob *imageJob = KIO::storedGet(potdProviderData()->wallpaperRemoteUrl, KIO::NoReload, KIO::HideProgressInfo);
connect(imageJob, &KIO::StoredTransferJob::finished, this, &FlickrProvider::imageRequestFinished);
} else {
qDebug() << "empty list";
......@@ -138,7 +167,34 @@ void FlickrProvider::imageRequestFinished(KJob *_job)
return;
}
// Visit the photo page to get the author
if (!potdProviderData()->wallpaperInfoUrl.isEmpty()) {
KIO::StoredTransferJob *pageJob = KIO::storedGet(potdProviderData()->wallpaperInfoUrl, KIO::NoReload, KIO::HideProgressInfo);
connect(pageJob, &KIO::StoredTransferJob::finished, this, &FlickrProvider::pageRequestFinished);
}
potdProviderData()->wallpaperImage = QImage::fromData(job->data());
}
void FlickrProvider::pageRequestFinished(KJob *_job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
if (job->error()) {
Q_EMIT finished(this); // No author is fine
return;
}
const QString data = QString::fromUtf8(job->data()).simplified();
// Example: <a href="/photos/jellybeanzgallery/" class="owner-name truncate" title="Go to Hammerchewer&#x27;s photostream"
// data-track="attributionNameClick">Hammerchewer</a>
QRegularExpression authorRegEx(QStringLiteral("<a.*?class=\"owner-name truncate\".*?>(.+?)</a>"));
QRegularExpressionMatch match = authorRegEx.match(data);
if (match.hasMatch()) {
potdProviderData()->wallpaperAuthor = QTextDocumentFragment::fromHtml(match.captured(1).trimmed()).toPlainText();
}
Q_EMIT finished(this);
}
......
......@@ -38,6 +38,11 @@ private:
void xmlRequestFinished(KJob *job);
void imageRequestFinished(KJob *job);
/**
* Parse the author from the HTML source
*/
void pageRequestFinished(KJob *job);
private:
QDate mActualDate;
QString mApiKey;
......@@ -46,7 +51,13 @@ private:
int mFailureNumber = 0;
QStringList m_photoList;
struct PhotoEntry {
QString urlString;
QString title;
QString userId;
QString photoId;
};
std::vector<PhotoEntry> m_photoList;
};
#endif
Supports Markdown
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