Skip to content

Instantly share code, notes, and snippets.

@i09158knct
Last active December 15, 2015 04:39
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 i09158knct/5203145 to your computer and use it in GitHub Desktop.
Save i09158knct/5203145 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'json'
def parse_rss_xml(file_name)
file = File.read file_name
doc = Nokogiri.XML file
to_info = -> rss {
title = rss.attr 'title'
html_url = rss.attr 'htmlUrl'
xml_url = rss.attr 'xmlUrl'
{title: title, html_url: html_url, xml_url: xml_url}
}
root_elements = doc.css 'body > outline'
groups = root_elements.group_by {|elm| elm.attr('type') == 'rss' }
categories = groups[false]
rss_table = categories.each_with_object({}) { |categorie_root, acc|
name = categorie_root.attr 'title'
rss_list = categorie_root.css('outline[type=rss]')
acc[name] = rss_list.map &to_info
}
uncategorized_rss_list = groups[true]
rss_table['__uncategorized-rss-list'] = uncategorized_rss_list.map &to_info
rss_table
end
file = File.read 'subscriptions.xml'
if __FILE__ == $0
file_name = ARGV[0] || 'subscriptions.xml'
rss_table = parse_rss_xml file_name
puts rss_table.to_json(
indent: ' ',
object_nl: "\n",
space: ' ',
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment