Implement limited number of new entries for new feed

This commit is contained in:
Bart De Vries 2021-04-09 17:03:02 +02:00
parent 58b5719f71
commit 3a4cdbfec0
4 changed files with 38 additions and 8 deletions

View File

@ -47,7 +47,7 @@ bool Database::migrate()
bool Database::migrateTo1() bool Database::migrateTo1()
{ {
qDebug() << "Migrating database to version 1"; qDebug() << "Migrating database to version 1";
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT, image TEXT, link TEXT, description TEXT, deleteAfterCount INTEGER, deleteAfterType INTEGER, subscribed INTEGER, lastUpdated INTEGER, notify BOOL);"))); TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT, image TEXT, link TEXT, description TEXT, deleteAfterCount INTEGER, deleteAfterType INTEGER, subscribed INTEGER, lastUpdated INTEGER, new BOOL, notify BOOL);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT, created INTEGER, updated INTEGER, link TEXT, read bool, new bool, hasEnclosure BOOL, image TEXT);"))); TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT, created INTEGER, updated INTEGER, link TEXT, read bool, new bool, hasEnclosure BOOL, image TEXT);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Authors (feed TEXT, id TEXT, name TEXT, uri TEXT, email TEXT);"))); TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Authors (feed TEXT, id TEXT, name TEXT, uri TEXT, email TEXT);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Enclosures (feed TEXT, id TEXT, duration INTEGER, size INTEGER, title TEXT, type TEXT, url TEXT, playposition INTEGER);"))); //, filename TEXT);"))); TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Enclosures (feed TEXT, id TEXT, duration INTEGER, size INTEGER, title TEXT, type TEXT, url TEXT, playposition INTEGER);"))); //, filename TEXT);")));

View File

@ -226,7 +226,7 @@ void DataManager::addFeed(const QString &url)
QUrl urlFromInput = QUrl::fromUserInput(url); QUrl urlFromInput = QUrl::fromUserInput(url);
QSqlQuery query; QSqlQuery query;
query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image, :link, :description, :deleteAfterCount, :deleteAfterType, :subscribed, :lastUpdated, :notify);")); query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image, :link, :description, :deleteAfterCount, :deleteAfterType, :subscribed, :lastUpdated, :new, :notify);"));
query.bindValue(QStringLiteral(":name"), urlFromInput.toString()); query.bindValue(QStringLiteral(":name"), urlFromInput.toString());
query.bindValue(QStringLiteral(":url"), urlFromInput.toString()); query.bindValue(QStringLiteral(":url"), urlFromInput.toString());
query.bindValue(QStringLiteral(":image"), QLatin1String("")); query.bindValue(QStringLiteral(":image"), QLatin1String(""));
@ -236,6 +236,7 @@ void DataManager::addFeed(const QString &url)
query.bindValue(QStringLiteral(":deleteAfterType"), 0); query.bindValue(QStringLiteral(":deleteAfterType"), 0);
query.bindValue(QStringLiteral(":subscribed"), QDateTime::currentDateTime().toSecsSinceEpoch()); query.bindValue(QStringLiteral(":subscribed"), QDateTime::currentDateTime().toSecsSinceEpoch());
query.bindValue(QStringLiteral(":lastUpdated"), 0); query.bindValue(QStringLiteral(":lastUpdated"), 0);
query.bindValue(QStringLiteral(":new"), true);
query.bindValue(QStringLiteral(":notify"), false); query.bindValue(QStringLiteral(":notify"), false);
Database::instance().execute(query); Database::instance().execute(query);

View File

@ -22,6 +22,7 @@
#include "database.h" #include "database.h"
#include "fetcher.h" #include "fetcher.h"
#include "alligatorsettings.h"
Fetcher::Fetcher() Fetcher::Fetcher()
{ {
@ -69,15 +70,25 @@ void Fetcher::processFeed(Syndication::FeedPtr feed, const QString &url)
if (feed.isNull()) if (feed.isNull())
return; return;
// First check if this is a newly added feed
bool isNewFeed = false;
QSqlQuery query;
query.prepare(QStringLiteral("SELECT new FROM Feeds WHERE url=:url;"));
query.bindValue(QStringLiteral(":url"), url);
Database::instance().execute(query);
if (query.next())
isNewFeed = query.value(QStringLiteral("new")).toBool();
qDebug() << "New feed" << feed->title() << ":" << isNewFeed;
// Retrieve "other" fields; this will include the "itunes" tags // Retrieve "other" fields; this will include the "itunes" tags
QMultiMap<QString, QDomElement> otherItems = feed->additionalProperties(); QMultiMap<QString, QDomElement> otherItems = feed->additionalProperties();
QSqlQuery query; query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image, link=:link, description=:description, lastUpdated=:lastUpdated, new=:new WHERE url=:url;"));
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image, link=:link, description=:description, lastUpdated=:lastUpdated WHERE url=:url;"));
query.bindValue(QStringLiteral(":name"), feed->title()); query.bindValue(QStringLiteral(":name"), feed->title());
query.bindValue(QStringLiteral(":url"), url); query.bindValue(QStringLiteral(":url"), url);
query.bindValue(QStringLiteral(":link"), feed->link()); query.bindValue(QStringLiteral(":link"), feed->link());
query.bindValue(QStringLiteral(":description"), feed->description()); query.bindValue(QStringLiteral(":description"), feed->description());
query.bindValue(QStringLiteral(":new"), false); // set "new" to false now that new feed is being processed
QDateTime current = QDateTime::currentDateTime(); QDateTime current = QDateTime::currentDateTime();
query.bindValue(QStringLiteral(":lastUpdated"), current.toSecsSinceEpoch()); query.bindValue(QStringLiteral(":lastUpdated"), current.toSecsSinceEpoch());
@ -126,13 +137,29 @@ void Fetcher::processFeed(Syndication::FeedPtr feed, const QString &url)
Q_EMIT feedDetailsUpdated(url, feed->title(), image, feed->link(), feed->description(), current); Q_EMIT feedDetailsUpdated(url, feed->title(), image, feed->link(), feed->description(), current);
for (const auto &entry : feed->items()) { for (const auto &entry : feed->items()) {
processEntry(entry, url); processEntry(entry, url, isNewFeed);
}
// Now mark the appropriate number of recent entries "new" and "read" for new feeds
query.prepare(QStringLiteral("SELECT * FROM Entries WHERE feed=:feed ORDER BY updated DESC LIMIT :recentNew;"));
query.bindValue(QStringLiteral(":feed"), url);
query.bindValue(QStringLiteral(":recentNew"), AlligatorSettings::self()->numberNewEpisodes());
Database::instance().execute(query);
QSqlQuery updateQuery;
while (query.next()) {
qDebug() << "new" << query.value(QStringLiteral("id")).toString();
updateQuery.prepare(QStringLiteral("UPDATE Entries SET read=:read, new=:new WHERE id=:id AND feed=:feed;"));
updateQuery.bindValue(QStringLiteral(":read"), false);
updateQuery.bindValue(QStringLiteral(":new"), true);
updateQuery.bindValue(QStringLiteral(":feed"), url);
updateQuery.bindValue(QStringLiteral(":id"), query.value(QStringLiteral("id")).toString());
Database::instance().execute(updateQuery);
} }
Q_EMIT feedUpdated(url); Q_EMIT feedUpdated(url);
} }
void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url) void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url, const bool &isNewFeed)
{ {
qDebug() << "Processing" << entry->title(); qDebug() << "Processing" << entry->title();
@ -148,7 +175,7 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url)
if (query.value(0).toInt() != 0) if (query.value(0).toInt() != 0)
return; return;
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link, 0, 1, :hasEnclosure, :image);")); query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link, :read, :new, :hasEnclosure, :image);"));
query.bindValue(QStringLiteral(":feed"), url); query.bindValue(QStringLiteral(":feed"), url);
query.bindValue(QStringLiteral(":id"), entry->id()); query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText()); query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText());
@ -156,6 +183,8 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url)
query.bindValue(QStringLiteral(":updated"), static_cast<int>(entry->dateUpdated())); query.bindValue(QStringLiteral(":updated"), static_cast<int>(entry->dateUpdated()));
query.bindValue(QStringLiteral(":link"), entry->link()); query.bindValue(QStringLiteral(":link"), entry->link());
query.bindValue(QStringLiteral(":hasEnclosure"), entry->enclosures().length() == 0 ? 0 : 1); query.bindValue(QStringLiteral(":hasEnclosure"), entry->enclosures().length() == 0 ? 0 : 1);
query.bindValue(QStringLiteral(":read"), isNewFeed); // if new feed, then mark all as read
query.bindValue(QStringLiteral(":new"), !isNewFeed); // if new feed, then mark none as new
if (!entry->content().isEmpty()) if (!entry->content().isEmpty())
query.bindValue(QStringLiteral(":content"), entry->content()); query.bindValue(QStringLiteral(":content"), entry->content());

View File

@ -34,7 +34,7 @@ private:
Fetcher(); Fetcher();
void processFeed(Syndication::FeedPtr feed, const QString &url); void processFeed(Syndication::FeedPtr feed, const QString &url);
void processEntry(Syndication::ItemPtr entry, const QString &url); void processEntry(Syndication::ItemPtr entry, const QString &url, const bool &isNewFeed);
void processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail); void processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail);
void processEnclosure(Syndication::EnclosurePtr enclosure, Syndication::ItemPtr entry, const QString &feedUrl); void processEnclosure(Syndication::EnclosurePtr enclosure, Syndication::ItemPtr entry, const QString &feedUrl);