Skip to content

Instantly share code, notes, and snippets.

@Coro365
Created July 19, 2021 03:59
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 Coro365/2f5d13b4ed4c6fce278be446e0a489cb to your computer and use it in GitHub Desktop.
Save Coro365/2f5d13b4ed4c6fce278be446e0a489cb to your computer and use it in GitHub Desktop.
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