mirror of
https://github.com/KDE/kasts.git
synced 2024-12-24 15:40:44 +01:00
Refactor Fetcher
This commit is contained in:
parent
fe7723416a
commit
fc1f5ef378
@ -19,8 +19,8 @@
|
||||
*/
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QVector>
|
||||
#include <QSqlQuery>
|
||||
#include <QVector>
|
||||
|
||||
#include "database.h"
|
||||
#include "entryListModel.h"
|
||||
@ -34,20 +34,18 @@ 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
|
||||
{
|
||||
if(role == Authors) {
|
||||
if (role == Authors) {
|
||||
QSqlQuery query;
|
||||
query.prepare(QStringLiteral("SELECT name FROM Authors WHERE id=:id"));
|
||||
query.bindValue(QStringLiteral(":id"), data(index, Id));
|
||||
Database::instance().execute(query);
|
||||
QStringList authors;
|
||||
while(query.next()) {
|
||||
while (query.next()) {
|
||||
authors += query.value(0).toString();
|
||||
}
|
||||
return authors;
|
||||
|
113
src/fetcher.cpp
113
src/fetcher.cpp
@ -51,58 +51,79 @@ void Fetcher::fetch(QUrl url)
|
||||
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString());
|
||||
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
|
||||
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
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 path = imagePath(url);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -24,10 +24,10 @@
|
||||
#include <QApplication>
|
||||
#endif
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
#include <QQuickView>
|
||||
#include <QCommandLineParser>
|
||||
|
||||
#include <KAboutData>
|
||||
#include <KLocalizedContext>
|
||||
@ -68,10 +68,10 @@ int main(int argc, char *argv[])
|
||||
parser.addPositionalArgument(QLatin1String("url"), i18n("Url to add to the subscriptions"));
|
||||
parser.process(app);
|
||||
|
||||
if(parser.positionalArguments().size() == 1) {
|
||||
if (parser.positionalArguments().size() == 1) {
|
||||
QString url = parser.positionalArguments().at(0);
|
||||
qDebug() << url;
|
||||
//TODO
|
||||
// TODO
|
||||
}
|
||||
|
||||
KAboutData about(QStringLiteral("alligator"), i18n("Alligator"), QStringLiteral("0.1"), i18n("Feed Reader"), KAboutLicense::GPL, i18n("© 2020 KDE Community"));
|
||||
|
Loading…
Reference in New Issue
Block a user