Created
May 27, 2014 10:33
-
-
Save Liooo/c6948824537b0dbcc999 to your computer and use it in GitHub Desktop.
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
require 'open-uri' | |
require 'nokogiri' | |
class CookpadInspector | |
def initialize(recipe_id) | |
@doc = Nokogiri::HTML(open("http://cookpad.com/recipe/#{recipe_id.to_i}")) | |
end | |
def get_title | |
@doc.css("h1")[0].inner_html.strip | |
end | |
def get_ingredients | |
ingredient_names = @doc.css(".ingredient_name span").map do |elem| | |
next elem.inner_html if elem.child.is_a? Nokogiri::XML::Text | |
elem.child.inner_html if elem.child.is_a? Nokogiri::XML::Element | |
end | |
ingredient_quantities = @doc.css(".ingredient_quantity").map do |elem| | |
elem.inner_html | |
end | |
ary = [ingredient_names, ingredient_quantities].transpose | |
Hash[*ary.flatten] | |
end | |
def get_process | |
@doc.css(".step_text").map {|elem| elem.inner_html } | |
end | |
end | |
begin | |
c=CookpadInspector.new(ARGV[0].to_i) | |
rescue OpenURI::HTTPError | |
p 'not found!' | |
exit | |
end | |
p '=' * 20 | |
p 'TITLE' | |
p c.get_title | |
p '=' * 20 | |
p 'INGREDIENTS' | |
c.get_ingredients.each{|i| p i} | |
p '=' * 20 | |
p 'PROCESSES' | |
c.get_process.each{|pr| p pr} | |
p '=' * 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment