Created
January 25, 2022 16:05
-
-
Save svandragt/408f5fb4ff01a25a508e23757667052d to your computer and use it in GitHub Desktop.
Combine items from multiple feeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "byebug", "~> 11.1", :groups => [:development, :test] | |
gem "rss", "~> 0.2.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rss" | |
require "byebug" | |
require "open-uri" | |
all = [] | |
urls = [ | |
"https://www.ruby-lang.org/en/feeds/news.rss", | |
"https://amasan.co.uk/feed-digest/feed/", | |
].each do |url| | |
URI.open(url) do |rss| | |
feed = nil | |
# parse even invalid feeds, where possible | |
begin | |
feed = RSS::Parser.parse rss, validate: false | |
rescue RSS::InvalidRSSError => e | |
end | |
if feed | |
puts "Title: #{feed.channel.title}" | |
all += feed.items if feed.items.any? | |
end | |
end | |
end | |
# todo only items published within the last 24 hrs | |
all.sort_by! { |item| -item.pubDate.to_i } | |
all.first(10).each do |item| | |
puts " - #{item.title} (#{item.pubDate})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment