-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapartment_search.rb
114 lines (96 loc) · 2.7 KB
/
apartment_search.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra/reloader' if development?
Capybara.register_driver :poltergeist do |app|
options = {
phantomjs_options: ["--disk-cache=true", "--load-images=false"],#, "--ignore-host='(google.com|google-analytics.com)'"],
js_errors: false
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.javascript_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.configure do |config|
config.ignore_hidden_elements = true
config.visible_text_only = true
config.page.driver.add_headers( "User-Agent" =>
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1" )
end
configure :development do
register Sinatra::Reloader
end
get "/" do
"try /yad2 , or /yad2.rss "
end
get "/yad2/rent.rss" do
get_rss('rent')
end
get "/yad2/sales.rss" do
get_rss('sales')
end
private
def get_rss(ad_type)
@apartments = load_apartments(ad_type, request.params)
headers 'Content-Type' => 'text/xml; charset=windows-1255'
builder :rss
end
def load_apartments(ad_type, request_params)
3.times.map do |page_number|
@@url = create_url(ad_type, request_params, page_number + 1)
Capybara.visit(@@url)
begin
table = Capybara.page.find '#main_table'
rescue Capybara::ElementNotFound
raise "Couldn't find the ads table"
end
trs = table.all "tr[id^='tr_Ad_']"
apartments = trs.map do |tr|
cells = tr.all "td"
Apartment.new(ad_type, cells)
end
end.flatten
end
def create_url(ad_type, params, page_number)
params["Page"] = page_number
uri = Addressable::URI.new
uri.host = 'www.yad2.co.il'
uri.path = "/Nadlan/#{ad_type}.php"
uri.scheme = 'http'
uri.query_values = params
@url = uri.to_s
@url
end
class Apartment
attr_accessor :address, :price,:room_count,:entry_date,:floor,:link
def initialize(ad_type, cells)
apartment_attributes = ad_type == 'rent' ? apartment_for_rent(cells) : apartment_for_sale(cells)
apartment_attributes.each do | key, value |
send("#{key}=", value)
end
end
def apartment_for_rent(cells)
link = "http://www.yad2.co.il/" +
cells[21].all("a").last['href'].to_s
puts link
{
address: cells[8].text,
price: cells[10].text,
room_count: cells[12].text,
entry_date: cells[14].text,
floor: cells[16].text,
link: link
}
end
def apartment_for_sale(cells)
link = "http://www.yad2.co.il" + cells[20].all("a").last['href'].to_s
{
address: cells[8].text,
price: cells[10].text,
room_count: cells[12].text,
entry_date: cells[18].text,
floor: cells[14].text,
link: link
}
end
end