#!/usr/bin/env ruby -W0
# Filter RSS feeds for only the first item
# Designed to be called as newsboat filter script, for example:
#    "filter:~/.config/newsboat/scripts/only-last:https://www.heise.de/rss/heise.rdf"
#
#
# Copyright (c) 2021, Timm Heuss
#
# Based on
# rss-tag-filter
# https://github.com/kouya/snownews/blob/master/contrib/untested/rss-tag-filter

require 'rss/2.0'

# parse the RSS
rss_source = STDIN.read
begin
  rss = RSS::Parser.parse(rss_source)
rescue RSS::InvalidRSSError
  rss = RSS::Parser.parse(rss_source, false)    # no validation
end

item = rss.channel.items[0]
rss.channel.items.clear()
rss.channel.items.push(item)

# write the modified rss
STDOUT.puts(rss)
