Skip to content

Instantly share code, notes, and snippets.

@sandric
Last active December 17, 2015 00:29
Show Gist options
  • Save sandric/5520896 to your computer and use it in GitHub Desktop.
Save sandric/5520896 to your computer and use it in GitHub Desktop.
Disqus api parser to YAML format
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://disqus.com/api/docs/'))
api = {}
doc.css('ul#resources h4 a').each do |section_link|
section_name = section_link.content
api[section_name] = {}
section_doc = Nokogiri::HTML(open(section_link[:href]))
section_doc.css('.main ul a').each do |method_link|
method_name = method_link.content
api[section_name][method_name] = {
"params" => {
"required" => [],
"optional" => []
}
}
method_doc = Nokogiri::HTML(open(method_link[:href]))
if method_doc.css('.api-arg-list').size == 1
if method_doc.at_css('.api-arg-list h4').content == "Optional"
method_doc.css('.api-arg-list').css('h5').each do |optional_argument|
api[section_name][method_name]["params"]["optional"] << optional_argument.content.split(" ")[0]
end
else
method_doc.css('.api-arg-list').css('h5').each do |required_argument|
api[section_name][method_name]["params"]["required"] << required_argument.content.split(" ")[0]
end
end
else
method_doc.css('.api-arg-list').first.css('h5').each do |required_argument|
api[section_name][method_name]["params"]["required"] << required_argument.content.split(" ")[0]
end
method_doc.css('.api-arg-list').last.css('h5').each do |optional_argument|
api[section_name][method_name]["params"]["optional"] << optional_argument.content.split(" ")[0]
end
end
api[section_name][method_name]["requires_authentication"] = method_doc.css('.api-doc p')[4].content == "yes" ? true : false
api[section_name][method_name]["url"] = method_doc.at_css('.api-doc p a').content
api[section_name][method_name]["method"] = method_doc.css('.api-doc p')[2].content
end
end
File.open("disqus_api.yml", 'w') { |file| file.write(api.to_yaml) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment