-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyamada.rb
39 lines (36 loc) · 1.01 KB
/
yamada.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding: utf-8
require 'open-uri'
require 'nokogiri'
class Yamada
URL = "http://www.yamada-denkiweb.com/category/108/001/009/"
def check(htmlfp=nil)
charset = nil
html = nil
begin
html = open(URL) do |f|
charset = "UTF-8"
f.read
end
rescue OpenURI::HTTPError => e
return nil if e.message == "404 Not Found"
throw e
end
htmlfp.write(html) if htmlfp
doc = Nokogiri::HTML.parse(html, nil, charset)
items = doc.css(".item-wrapper").map do |d|
name = d.css(".item-name").text.strip
price = d.css(".item-price-box p.subject-text:nth-of-type(1) span.highlight").text.strip
available = (d.css(".note").text !~ /好評につき売り切れました/)
if name.empty? || price.empty? || !available
nil
else
{name: name, price: price, available: available}
end
end.compact
return nil if items.empty?
result = [URL]
result + items.map do |t|
t[:name] + "\n" + t[:price] + "\n"
end
end
end