Refactor Fetcher

This commit is contained in:
Tobias Fella 2020-05-18 16:47:12 +02:00
parent fe7723416a
commit fc1f5ef378
4 changed files with 78 additions and 55 deletions

View File

@ -19,8 +19,8 @@
*/ */
#include <QDateTime> #include <QDateTime>
#include <QVector>
#include <QSqlQuery> #include <QSqlQuery>
#include <QVector>
#include "database.h" #include "database.h"
#include "entryListModel.h" #include "entryListModel.h"
@ -34,20 +34,18 @@ EntryListModel::EntryListModel(QObject *parent)
setEditStrategy(OnFieldChange); setEditStrategy(OnFieldChange);
select(); select();
connect(&Fetcher::instance(), &Fetcher::updated, this, [this] () { connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { select(); });
select();
});
} }
QVariant EntryListModel::data(const QModelIndex &index, int role) const QVariant EntryListModel::data(const QModelIndex &index, int role) const
{ {
if(role == Authors) { if (role == Authors) {
QSqlQuery query; QSqlQuery query;
query.prepare(QStringLiteral("SELECT name FROM Authors WHERE id=:id")); query.prepare(QStringLiteral("SELECT name FROM Authors WHERE id=:id"));
query.bindValue(QStringLiteral(":id"), data(index, Id)); query.bindValue(QStringLiteral(":id"), data(index, Id));
Database::instance().execute(query); Database::instance().execute(query);
QStringList authors; QStringList authors;
while(query.next()) { while (query.next()) {
authors += query.value(0).toString(); authors += query.value(0).toString();
} }
return authors; return authors;

View File

@ -51,58 +51,79 @@ void Fetcher::fetch(QUrl url)
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString()); Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString());
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom")); Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
if (feed.isNull()) processFeed(feed, url);
return;
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;"));
query.bindValue(QStringLiteral(":name"), feed->title());
query.bindValue(QStringLiteral(":url"), url.toString());
if (feed->image()->url().startsWith(QStringLiteral("/"))) {
QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url();
query.bindValue(QStringLiteral(":image"), absolute);
} else
query.bindValue(QStringLiteral(":image"), feed->image()->url());
Database::instance().execute(query);
qDebug() << "Updated feed title:" << feed->title();
for (const auto &entry : feed->items()) {
qDebug() << "Processing" << entry->title();
query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries WHERE id=:id;"));
query.bindValue(QStringLiteral(":id"), entry->id());
Database::instance().execute(query);
query.next();
if (query.value(0).toInt() != 0)
continue;
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link);"));
query.bindValue(QStringLiteral(":feed"), url.toString());
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText());
query.bindValue(QStringLiteral(":created"), static_cast<int>(entry->datePublished()));
query.bindValue(QStringLiteral(":updated"), static_cast<int>(entry->dateUpdated()));
query.bindValue(QStringLiteral(":link"), entry->link());
if (!entry->content().isEmpty())
query.bindValue(QStringLiteral(":content"), entry->content());
else
query.bindValue(QStringLiteral(":content"), entry->description());
Database::instance().execute(query);
for (const auto &author : entry->authors()) {
query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);"));
query.bindValue(QStringLiteral(":feed"), url.toString());
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":name"), author->name());
query.bindValue(QStringLiteral(":uri"), author->uri());
query.bindValue(QStringLiteral(":email"), author->email());
Database::instance().execute(query);
}
}
emit updated(); emit updated();
delete reply; delete reply;
}); });
} }
void Fetcher::processFeed(Syndication::FeedPtr feed, QUrl url)
{
if (feed.isNull())
return;
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;"));
query.bindValue(QStringLiteral(":name"), feed->title());
query.bindValue(QStringLiteral(":url"), url);
if (feed->image()->url().startsWith(QStringLiteral("/"))) {
QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url();
query.bindValue(QStringLiteral(":image"), absolute);
} else
query.bindValue(QStringLiteral(":image"), feed->image()->url());
Database::instance().execute(query);
qDebug() << "Updated feed title:" << feed->title();
for (const auto &entry : feed->items()) {
processEntry(entry, url);
}
}
void Fetcher::processEntry(Syndication::ItemPtr entry, QUrl url)
{
qDebug() << "Processing" << entry->title();
QSqlQuery query;
query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries WHERE id=:id;"));
query.bindValue(QStringLiteral(":id"), entry->id());
Database::instance().execute(query);
query.next();
if (query.value(0).toInt() != 0)
return;
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link);"));
query.bindValue(QStringLiteral(":feed"), url);
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText());
query.bindValue(QStringLiteral(":created"), static_cast<int>(entry->datePublished()));
query.bindValue(QStringLiteral(":updated"), static_cast<int>(entry->dateUpdated()));
query.bindValue(QStringLiteral(":link"), entry->link());
if (!entry->content().isEmpty())
query.bindValue(QStringLiteral(":content"), entry->content());
else
query.bindValue(QStringLiteral(":content"), entry->description());
Database::instance().execute(query);
for (const auto &author : entry->authors()) {
processAuthor(author, entry, url);
}
}
void Fetcher::processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QUrl url)
{
QSqlQuery query;
query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);"));
query.bindValue(QStringLiteral(":feed"), url.toString());
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":name"), author->name());
query.bindValue(QStringLiteral(":uri"), author->uri());
query.bindValue(QStringLiteral(":email"), author->email());
Database::instance().execute(query);
}
QString Fetcher::image(QString url) QString Fetcher::image(QString url)
{ {
QString path = imagePath(url); QString path = imagePath(url);

View File

@ -23,6 +23,7 @@
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QObject> #include <QObject>
#include <QUrl> #include <QUrl>
#include <Syndication/Syndication>
class Fetcher : public QObject class Fetcher : public QObject
{ {
@ -42,6 +43,9 @@ private:
Fetcher(const Fetcher &); Fetcher(const Fetcher &);
QString imagePath(QString); QString imagePath(QString);
void processFeed(Syndication::FeedPtr feed, QUrl url);
void processEntry(Syndication::ItemPtr entry, QUrl url);
void processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QUrl url);
QNetworkAccessManager *manager; QNetworkAccessManager *manager;

View File

@ -24,10 +24,10 @@
#include <QApplication> #include <QApplication>
#endif #endif
#include <QCommandLineParser>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext> #include <QQmlContext>
#include <QQuickView> #include <QQuickView>
#include <QCommandLineParser>
#include <KAboutData> #include <KAboutData>
#include <KLocalizedContext> #include <KLocalizedContext>
@ -68,10 +68,10 @@ int main(int argc, char *argv[])
parser.addPositionalArgument(QLatin1String("url"), i18n("Url to add to the subscriptions")); parser.addPositionalArgument(QLatin1String("url"), i18n("Url to add to the subscriptions"));
parser.process(app); parser.process(app);
if(parser.positionalArguments().size() == 1) { if (parser.positionalArguments().size() == 1) {
QString url = parser.positionalArguments().at(0); QString url = parser.positionalArguments().at(0);
qDebug() << url; qDebug() << url;
//TODO // TODO
} }
KAboutData about(QStringLiteral("alligator"), i18n("Alligator"), QStringLiteral("0.1"), i18n("Feed Reader"), KAboutLicense::GPL, i18n("© 2020 KDE Community")); KAboutData about(QStringLiteral("alligator"), i18n("Alligator"), QStringLiteral("0.1"), i18n("Feed Reader"), KAboutLicense::GPL, i18n("© 2020 KDE Community"));