diff options
author | Alexander Batischev <eual.jp@gmail.com> | 2023-12-22 19:02:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-22 19:02:14 +0300 |
commit | e4975fe9fdafa38d466d50097f3a03985d8ef076 (patch) | |
tree | 7de8a76381853f8a9e36867ce09ce714e3835173 | |
parent | 3efef71bd2a3991595fcbeb590d5ab23e2d4f971 (diff) | |
parent | 028b7656bd75d8f09a665945da28e3bc4b613245 (diff) |
Merge pull request #2626 from dennisschagt/latest-publish-date-attribute
Add feed attribute for filtering on latest article publish date
-rw-r--r-- | doc/newsboat.asciidoc | 3 | ||||
-rw-r--r-- | src/rssfeed.cpp | 12 | ||||
-rw-r--r-- | test/rssfeed.cpp | 23 |
3 files changed, 37 insertions, 1 deletions
diff --git a/doc/newsboat.asciidoc b/doc/newsboat.asciidoc index b7f407de..927ecb8e 100644 --- a/doc/newsboat.asciidoc +++ b/doc/newsboat.asciidoc @@ -823,7 +823,7 @@ Attribute:Context:Meaning [[attr-enclosure_url]]<<attr-enclosure_url,+enclosure_url+>>:article:the URL of an enclosure (e.g. podcast file), empty if there is no enclosure [[attr-enclosure_type]]<<attr-enclosure_type,+enclosure_type+>>:article:the MIME type of the enclosure, empty if there is no enclosure [[attr-flags]]<<attr-flags,+flags+>>:article:The set of flags of the article -[[attr-age]]<<attr-age,+age+>>:article:Age of an article (in days) +[[attr-age]]<<attr-age,+age+>>:article:Number of days since the article was published [[attr-articleindex]]<<attr-articleindex,+articleindex+>>:article:Index of an article in an article list [[attr-feedtitle]]<<attr-feedtitle,+feedtitle+>>:feed, article:title of the feed [[attr-description]]<<attr-description,+description+>>:feed, article:feed description @@ -834,6 +834,7 @@ Attribute:Context:Meaning [[attr-total_count]]<<attr-total_count,+total_count+>>:feed, article:total number of articles in the feed [[attr-tags]]<<attr-tags,+tags+>>:feed, article:space-separated list of tags that are associated with the feed. Note that for tags that have spaces in them, it's impossible to tell where they start and end, so +#+ and +!#+ operators don't work for such tags. [[attr-feedindex]]<<attr-feedindex,+feedindex+>>:feed, article:Index of a feed in the feed list +[[attr-latest_article_age]]<<attr-latest_article_age,+latest_article_age+>>:feed, article:Number of days since the most recent article in a feed was published |========================================================================= Note that it's also possible to filter for feed attributes when you query for diff --git a/src/rssfeed.cpp b/src/rssfeed.cpp index 0d659612..873b6a3d 100644 --- a/src/rssfeed.cpp +++ b/src/rssfeed.cpp @@ -197,6 +197,18 @@ nonstd::optional<std::string> RssFeed::attribute_value(const std::string& return tags; } else if (attribname == "feedindex") { return std::to_string(idx); + } else if (attribname == "latest_article_age") { + using ItemType = std::shared_ptr<RssItem>; + const auto latest_article_iterator = std::max_element(items_.begin(), + items_.end(), [](const ItemType& a, const ItemType& b) { + return a->pubDate_timestamp() < b->pubDate_timestamp(); + }); + if (latest_article_iterator != items_.end()) { + const auto latest_article = *latest_article_iterator; + const auto timestamp = latest_article->pubDate_timestamp(); + return std::to_string((time(nullptr) - timestamp) / 86400); + } + return "0"; } return nonstd::nullopt; } diff --git a/test/rssfeed.cpp b/test/rssfeed.cpp index c4b6b5be..27d75714 100644 --- a/test/rssfeed.cpp +++ b/test/rssfeed.cpp @@ -570,4 +570,27 @@ TEST_CASE("RssFeed contains a number of matchable attributes", "[RssFeed]") check(65536); check(100500); } + + SECTION("latest_article_age, the number of days since the most recent articles publish date") { + const auto attr = "latest_article_age"; + const auto current_time = ::time(nullptr); + const auto seconds_per_day = 24 * 60 * 60; + + SECTION("empty feed => latest_article_age == 0") { + REQUIRE(f.attribute_value(attr) == "0"); + } + + SECTION("feed with two items => latest_article_age == <days since most recent publish date>") { + auto item1 = std::make_shared<RssItem>(&rsscache); + auto item2 = std::make_shared<RssItem>(&rsscache); + + item1->set_pubDate(current_time - 3 * seconds_per_day); + item2->set_pubDate(current_time - 5 * seconds_per_day); + + f.add_item(item1); + f.add_item(item2); + + REQUIRE(f.attribute_value(attr) == "3"); + } + } } |