Skip to content

Instantly share code, notes, and snippets.

@jpmckinney
Last active November 25, 2021 17:14
Show Gist options
  • Save jpmckinney/835264 to your computer and use it in GitHub Desktop.
Save jpmckinney/835264 to your computer and use it in GitHub Desktop.
Given an array of URLs, outputs an OPML file to STDOUT
# blog post: http://blog.slashpoundbang.com/post/3385815540/how-to-generate-an-opml-file-with-ruby
require 'open-uri'
require 'builder'
require 'truffle-hog'
require 'nokogiri'
# CHANGE ME
urls = %w(http://example.com/ http://example.org/)
xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
xml.instruct!
xml.opml(:version => 1.1) do
xml.head do
# CHANGE ME
xml.title 'OPML TITLE'
xml.dateCreated 'CREATION DATE'
xml.dateModified Time.now.httpdate
xml.ownerName 'YOUR NAME'
xml.ownerEmail 'example@example.com'
# ... and you're done!
end
xml.body do
urls.each do |url|
html = open(url).read
title = Nokogiri::HTML(html).at_css('title').text.strip
xml.outline({
:type => 'rss',
:version => 'RSS',
:description => '',
:title => title,
:text => title,
:htmlUrl => url,
:xmlUrl => TruffleHog.parse_feed_urls(html, :atom).uniq.reject{|x| x =~ %r{/comments/}}.first,
})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment