-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.rb
More file actions
executable file
·57 lines (44 loc) · 1.3 KB
/
script.rb
File metadata and controls
executable file
·57 lines (44 loc) · 1.3 KB
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
#!/usr/bin/ruby
# Usage:
# ruby script.rb email-to-notify@example.com
require 'wombat'
require 'digest'
require 'json'
WEB_PATH = "/mobil-og-telefoni/mobiltelefoner-og-tilbehoer/mobiltelefoner/maerke-google/" +
"?soeg=pixel&fra=privat&sort=listingdate-desc".freeze
class Item
include Wombat::Crawler
base_url "http://www.dba.dk/"
path WEB_PATH
items "css=table.searchResults tr.dbaListing", :iterator do |node|
name "css=td.mainContent script" do |payload|
JSON.parse(payload.gsub(/\n/, ''))['name']
end
url "css=td.mainContent script" do |payload|
JSON.parse(payload.gsub(/\n/, ''))['url']
end
price "css=td.mainContent script" do |payload|
JSON.parse(payload.gsub(/\n/, ''))['offers']['price']
end
end
end
def notify(item)
to = ARGV[0]
subject = "Something new on dba"
content = "#{item['name']}\n\n#{item['url']}\n\n#{item['price']}"
`mail -s "#{subject}" #{to}<<EOM
#{content}
EOM`
sleep(2) # Do not flood email server (I use a free Amazon SES)
end
items = Item.new.crawl["items"]
items.each do |item|
id = Digest::MD5.hexdigest(item['url'])
filename = File.join(Dir.pwd, "logs", "#{id}.log")
unless File.file?(filename)
File.open(filename, 'w') do |file|
file.write(item.to_json)
end
notify(item) if ARGV[0]
end
end