-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-switch-shop.rb
executable file
·99 lines (91 loc) · 2.33 KB
/
check-switch-shop.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env ruby
# coding: utf-8
$LOAD_PATH.push File.dirname(File.expand_path($0))
require 'optparse'
require 'omni7'
require 'toysrus'
require 'amazon'
require 'rakuten_books'
require 'yamada'
require 'ito_yokado'
require 'nojima'
require 'joshin'
require 'yodobashi'
require 'hmv'
require 'amiami'
require 'edion'
exfile=nil
htmloutdir=nil
OptionParser.new do |opt|
opt.banner = "Usage: check-switch-shop.rb [options]"
opt.separator ""
opt.separator "Specific options:"
opt.on("-e", "--exception-file=FILE") {|v|
exfile = v
}
opt.on("-H", "--html-output-dir=DIR") {|v|
htmloutdir = v
}
opt.parse!(ARGV)
end
shops = [
{crawler: Omni7.new, name: "オムニ7"},
{crawler: Toysrus.new, name: "トイザラス"},
{crawler: Amazon.new, name: "Amazon"},
{crawler: RakutenBooks.new, name: "楽天ブックス"},
{crawler: Yamada.new, name: "ヤマダ"},
{crawler: ItoYokado.new, name: "イトーヨーカドー"},
{crawler: Nojima.new, name: "ノジマ"},
{crawler: Joshin.new, name: "ジョーシン"},
{crawler: Yodobashi.new, name: "ヨドバシ"},
#{crawler: Hmv.new, name: "ローチケHMV"},
{crawler: Amiami.new, name: "あみあみ"},
{crawler: Edion.new, name: "Edion"},
]
if htmloutdir
Dir.mkdir(htmloutdir) unless Dir.exists?(htmloutdir)
end
errors = []
availables = shops.map do |s|
begin
if htmloutdir
fname = File.join(htmloutdir, "#{s[:crawler].class.name.downcase}-#{Time.now.strftime("%Y%m%d-%H%M%S")}.html")
fp = File.open(fname, "w")
end
r = s[:crawler].check(fp)
if htmloutdir
fp.close
system("gzip #{fname}")
end
r ? {detail: r, name: s[:name]} : nil
rescue => ex
errors << {name: s[:name], ex: ex}
nil
end
end.compact
#errors.delete_if do |h|
# h[:name] == "Amazon" &&
# h[:ex].class == OpenURI::HTTPError &&
# h[:ex].to_s == "503 Service Unavailable"
#end
exf = exfile ? File.open(exfile, "a") : STDERR
errors.each do |h|
name = h[:name]
ex = h[:ex]
exf.puts Time.now.to_s
exf.puts name
exf.puts ex
exf.puts ex.backtrace
exf.puts ""
end
exf.close
if availables.length > 0
text = ["販売中!急げ!", ""]
text << availables.map do |a|
"- #{a[:name]}\n" + a[:detail].join("\n")
end.join("\n")
puts text.join("\n")
exit 0
end
puts "在庫なし"
exit 1