Skip to content

Instantly share code, notes, and snippets.

@libbymiller
Created June 3, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libbymiller/ca4f8c651364a4588bbb28ddb38cf0d1 to your computer and use it in GitHub Desktop.
Save libbymiller/ca4f8c651364a4588bbb28ddb38cf0d1 to your computer and use it in GitHub Desktop.
A script to take an OPML file, hack-parse it, and find 10 random things to read
require 'rss'
require 'open-uri'
require 'open_uri_redirections'
require 'cgi'
links = []
# hackparse opml file
file = File.new("subscriptions.opml", "r")
while (line = file.gets)
if(line.match(/<outline type=\"rss\"/))
foo = line.gsub(/\s*<outline type=\"rss\" xmlUrl=\"/,"")
foo = foo.gsub("\"/>","")
links.push(foo.chomp!)
end
end
file.close
# get 10 random feeds
things = links.sample(10)
# now get the feeds and get the latest item
html = "<html><head><meta charset=\"utf-8\" /> </head>\n<body>\n"
things.each do |url|
puts url
begin
open(url,{:allow_redirections => :safe}) do |rss|
begin
# false here stops it trying to validate: https://www.ruby-forum.com/topic/4412182
feed = RSS::Parser.parse(rss, false)
feed_title = nil
feed_item_title = nil
feed_item_link = nil
feed_item_date = nil
feed_item_description = nil
begin
feed_title = feed.channel.title
rescue=> a
feed_title = feed.title
#puts a.backtrace
end
# shouldn't need to do this, can't figure out another way, docs are useless
feed_item = feed_item.to_s.gsub(/<[^>]*>/,"")
item = feed.items[0]
begin
feed_item_link = item.link.href
rescue => b
feed_item_link = item.link
#puts b.backtrace
end
begin
feed_item_title = item.title.to_s
rescue => c
feed_item_title = item.title
#puts c.backtrace
end
# remove tags
feed_item_title = feed_item_title.to_s.gsub(/<[^>]*>/,"")
begin
feed_item_date = item.updated
rescue => d
#puts d.backtrace
begin
feed_item_date = item.pubDate
rescue => e
feed_item_date = item.dc_date
#puts e.backtrace
end
end
begin
feed_item_description = item.description
rescue => f
#puts f.backtrace
if(item.content && item.content!="")
feed_item_description = item.content
else
feed_item_description = item.summary
end
end
# remove html tags
feed_item_description = CGI.unescapeHTML(feed_item_description)
feed_item_description = feed_item_description.gsub(/<[^>]*>/,"")
# shorten
feed_item_description = feed_item_description[0,200]+"..."
html = html + "<h2><a href='#{feed_item_link}'>#{feed_item_title}</a></h2>\n<p>#{feed_item_description}</p>\n<p><i>From <a href='#{url}'>#{feed_title}</a>, #{feed_item_date}</i></p>\n"
rescue => g
puts g.backtrace
end
end
rescue => f
puts f.backtrace # timeout
end
sleep(1)
end
html = html + "</body></html>\n"
File.open("#{Dir.pwd}/latest.html", 'w') { |file| file.write(html) }
@danbri
Copy link

danbri commented Jun 4, 2017

'cgi' :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment