-
Notifications
You must be signed in to change notification settings - Fork 8
/
scraper.rb
52 lines (43 loc) · 1.56 KB
/
scraper.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
require 'scraperwiki'
require 'date'
require "mechanize"
class String
def squish
string = strip
string.gsub!(/\s+/, ' ')
string
end
end
def convert_date(s)
Date.strptime(s, "%d/%m/%Y").to_s
rescue ArgumentError
nil
end
agent = Mechanize.new
site = "https://eplanning.cityofsydney.nsw.gov.au"
url = "https://eplanning.cityofsydney.nsw.gov.au/Pages/XC.Track/SearchApplication.aspx?e=y"
page = agent.get(url)
puts "#{url} loaded"
csvTable = page.at(".csvTable")
count = 0
csvTable.element_children.map.each do |application|
next if application['class'] == "headerRow"
count = count + 1
info_url = application.at("#applicationReference a")['href'].squish.sub("../..",site)
# split bad concat addresses by strings that look like postcodes, but include the string we split on in the result
addresses = application.at("#applicationAddress").inner_text.squish.split(/(NSW \d{4})/).each_slice(2).map(&:join)
record = {
"address" => addresses.first,
"description" => application.at("#applicationDetails").inner_text.squish,
"date_received" => convert_date(application.at("#lodgementDate").inner_text.squish),
"on_notice_to" => convert_date(application.at("#exhibitionCloseDate").inner_text.squish),
"council_reference" => application.at("#applicationReference a").inner_text.squish,
"info_url" => info_url,
"comment_url" => "mailto:dasubmissions@cityofsydney.nsw.gov.au",
"date_scraped" => Date.today.to_s
}
puts record
ScraperWiki.save_sqlite(['council_reference'], record)
end
# Return number of applications found
puts count