Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Randomly select a cut cake from Ginza Cozy.
# 銀座コージーのカットケーキをランダムに選ぶ
require 'open-uri'
require 'nokogiri'
def cakes
domain = 'https://www.cozycorner.co.jp/'
url = 'https://www.cozycorner.co.jp/product/cutcake/'
charset = nil
html = URI.open(url) do |f|
charset = f.charset
f.read
end
doc = Nokogiri::HTML.parse(html, nil, charset)
cakes = doc.xpath('//li').map do |node|
description = node.css('span[@class="description"]').inner_text.gsub(/\s|\r|\n/,'')
if m = description.match(/(^.*?)(\d*?)円.*?(\d*?)円/)
cake_name = m[1]
cake_price = m[2]
tax_price = m[3]
else
next
end
cake_img = 'https:' + node.css('img').attribute('src').to_s
cake_url = File.join(domain, node.css('a').attribute('href').to_s)
{name: cake_name, price: cake_price, tax_price: tax_price, image: cake_img, url: cake_url}
end
cakes.compact
end
def display(cake)
system(File.join(__dir__, 'imgcat'), '-u', cake[:image])
puts("#{cake[:name]} ¥#{cake[:tax_price]}")
puts(cake[:url])
end
display(cakes.sample)
# cakes.sample(3).each { |c| display(c) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment