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 <QVector>
#include <QSqlQuery>
#include <QVector>
#include "database.h"
#include "entryListModel.h"
@ -34,9 +34,7 @@ EntryListModel::EntryListModel(QObject *parent)
setEditStrategy(OnFieldChange);
select();
connect(&Fetcher::instance(), &Fetcher::updated, this, [this] () {
select();
});
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { select(); });
}
QVariant EntryListModel::data(const QModelIndex &index, int role) const

View File

@ -51,14 +51,22 @@ void Fetcher::fetch(QUrl url)
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString());
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
processFeed(feed, url);
emit updated();
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.toString());
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);
@ -68,26 +76,45 @@ void Fetcher::fetch(QUrl url)
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)
continue;
return;
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link);"));
query.bindValue(QStringLiteral(":feed"), url.toString());
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());
@ -96,12 +123,6 @@ void Fetcher::fetch(QUrl url)
query.bindValue(QStringLiteral(":email"), author->email());
Database::instance().execute(query);
}
}
emit updated();
delete reply;
});
}
QString Fetcher::image(QString url)
{

View File

@ -23,6 +23,7 @@
#include <QNetworkAccessManager>
#include <QObject>
#include <QUrl>
#include <Syndication/Syndication>
class Fetcher : public QObject
{
@ -42,6 +43,9 @@ private:
Fetcher(const Fetcher &);
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;

View File

@ -24,10 +24,10 @@
#include <QApplication>
#endif
#include <QCommandLineParser>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickView>
#include <QCommandLineParser>
#include <KAboutData>
#include <KLocalizedContext>