-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib
executable file
·166 lines (134 loc) · 5.43 KB
/
lib
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require "bundler"
Bundler.setup :default
require 'nokogiri'
require 'open-uri'
require 'pry'
require 'json'
require 'geocoder'
Geocoder.configure(:timeout => 10)
def swim_time_finder(week, lane_swim_row_index)
week.at_css("tbody").css('tr')[lane_swim_row_index].children
.map do |el|
nodes = el.children.find_all(&:text?)
if nodes.length == 1
nodes = [el.children.text]
else
nodes.map!(&:text)
end
end
end
def build_pool_schedule_array_from_html(doc)
weeks = {}
for i in 0..1 #eventually poll more weeks, possibly 4 of available 7
week = doc.at_css("#dropin_Swimming_#{i}")
!week.nil?? week_dates = week.at_css('tr').children.map(&:text) : next
!week_dates.nil?? lane_swim_row_index = week.at_css("tbody").css('tr').find_index { |el| el.text=~ /Lane Swim/ } : next
if !lane_swim_row_index.nil?
week_lane_swim_times = swim_time_finder(week, lane_swim_row_index)
weeks.merge!(week_dates.zip(week_lane_swim_times).to_h)
end
end
# remove days with no swim times
weeks.delete_if { |day, time| time == [" "] || time == [] }
end
# Gather the pools
def gather_pool_urls()
@pool_urls, @pool_names, @pool_addresses, @pool_links = [],[],[],[]
# Gather Pool Data
urls = ["http://www1.toronto.ca/parks/prd/facilities/indoor-pools/index.htm",
"http://www1.toronto.ca/parks/prd/facilities/indoor-pools/2-indoor_pool.htm",
"http://www1.toronto.ca/parks/prd/facilities/outdoor-pools/index.htm",
"http://www1.toronto.ca/parks/prd/facilities/outdoor-pools/2-outdoor_pool.htm"]
urls.each do |url|
doc = Nokogiri::HTML(open(url))
pools = doc.at_css("#pfrBody > div.pfrListing > table > tbody")
@pool_names += pools.css('a').map { |link| link.children.text }
@pool_links += pools.css('a').map { |link| link['href'] }
address_index_incrementer = pools.css('td').length / pools.css('tr').length
pools.css('td').each_with_index do |node, index|
# Address is always second column, table width varies for indoor vs. outdoor
if index % address_index_incrementer == 1
@pool_addresses << node.text
end
end
end
# Geotag pools
pool_coordinates ||= []
@pool_addresses.each do |address|
coordinates_arr = Geocoder.coordinates("#{address}, Toronto")
pool_coordinates << { latitude: coordinates_arr[0], longitude: coordinates_arr[1] }
# pool_coordinates << { latitude: 50.123, longitude: 50.12 }
puts "Geocoding... #{address}"
# To avoid triggering google API limit of 10 queries per second
sleep(0.15)
end
# Convert Pool Data to Hash
@pool_names.each_with_index do |pool, index|
current_pool = {}
current_pool[:name] = @pool_names[index]
current_pool[:url] = @pool_links[index]
current_pool[:address] = @pool_addresses[index]
current_pool[:coordinates] = pool_coordinates[index]
@pool_urls << current_pool
end
# Write Hash
File.open("pool_urls.json","w") do |f|
f.write(@pool_urls.to_json)
end
end
#####Parse Weekly Leisure Swim Data#####
def gather_pool_swim_times
if @pool_urls.nil?
@pool_urls = JSON.parse(File.read('pool_urls.json'), symbolize_names: true)
end
@pool_urls.each do |pool|
puts "Attempting to scrape: " + pool[:name]
url = "http://www1.toronto.ca" + pool[:url]
doc = Nokogiri::HTML(open(url))
pool[:times] = build_pool_schedule_array_from_html(doc)
end
File.open("pools_data.json","w") do |f|
f.write(@pool_urls.to_json)
puts "Writing pools_data.json complete"
end
end
def gather_pool_program_cost_status
@pools = JSON.parse(File.read('pools_data.json'), symbolize_names: true)
page = "http://www1.toronto.ca/wps/portal/contentonly?vgnextoid=aaafdada600f0410VgnVCM10000071d60f89RCRD&vgnextchannel=a96adada600f0410VgnVCM10000071d60f89RCRD"
doc = Nokogiri::HTML(open(page))
free_facility_article = doc.at_css("#maincontent")
links = free_facility_article.css('a')
all_hrefs = links.map { |link| link.attribute('href').to_s }.uniq.sort.delete_if { |href| href.empty? }
free_facility_urls_regexed = all_hrefs.keep_if{ |href| href.match("\/parks/prd/facilities/complex\w*") }
.map{ |url| url.match(/\/parks\/prd\/facilities\/complex\/\d*/).to_s }
@pools.each do |pool|
pool_url_regex = pool[:url].match(/\/parks\/prd\/facilities\/complex\/\d*/).to_s
match = free_facility_urls_regexed.find{ |e| pool_url_regex == e }
pool[:free_swim] = match ? true : false
end
File.open("pools_data.json","w") do |f|
f.write(@pools.to_json)
puts "Writing program cost status to pools_data.json complete"
end
end
# gather_pool_urls()
# gather_pool_swim_times()
gather_pool_program_cost_status()
# Todo
# add a test suite, break geotagging into separate method
# break rejecting days into separate method?
# remind self how to log name of vars while blown up (smaller method with info passed in probably!)
#start displaying, filtering?
#maybe transform date save
#Indoor pools list: http://www1.toronto.ca/parks/prd/facilities/outdoor-pools/index.htm
#Outdoor pools list: http://www1.toronto.ca/parks/prd/facilities/outdoor-pools/index.htm
#Bugs
#Done
#figure out how to split multiple swim times into array
#loop for multiple weeks
#build hashes
#capture indoor pool lists
#determine output, save as json object or YAML?
# Pool geocode data - http://www.rubygeocoder.com/
# Symbolize keys for JSON retrieval as JSON doesn't natively support keys, rather uses strings
# latitude / longitude, display proper