From 1f8f2acf24832d4c484310094e407b0c0f2da992 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Mon, 23 Jan 2017 19:37:01 -0600 Subject: [PATCH 1/2] create the api for chicago --- chicago_api.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 chicago_api.rb diff --git a/chicago_api.rb b/chicago_api.rb new file mode 100644 index 0000000..bfe49ef --- /dev/null +++ b/chicago_api.rb @@ -0,0 +1,47 @@ +require 'faraday' +require 'nokogiri' + +require 'models' + +class ChicagoApiError < StandardError; end + +class ChicagoApi # Chicago + CHICAGO_ENDPOINT = 'http://www.transitchicago.com/travel_information/accessibility_status.aspx'.freeze + + def self.get_data + begin + response = Faraday.get(CHICAGO_ENDPOINT).body + rescue StandardError => e + raise ChicagoApiError, "ERROR: #{e.class}: #{e.message}" + end + + html = Nokogiri::HTML.parse(response) do |config| + config.strict.noblanks + end + + messages = html.xpath('//a[@class="bold padrt"]').to_a.map(&:text) + # ["Roosevelt Street-to-Mezzanine Elevator Out of Service", "Western Kimball-bound Platform Elevator Out of Service", "Elevator at 18th Temporarily Out-of-Service"] + long_desc = html.xpath('//div[@class="col500"]/text()[preceding::div[@class="planned"] and following::div[@class="alertdate"]]').to_a.map{|e| e.text.strip}.reject(&:empty?) + # ["Red Line - The street-to-mezzanine elevator at the Roosevelt subway station will be temporarily out of service.", "Brown Line - The Kimball-bound platform elevator at the Western station will be temporarily out of service.", "The elevator to the 54th/Cermak-bound platform at 18th (Pink Line) is temporarily out-of-service."] + + elevator_names = [] + cta_lines = %w(Red Blue Brown Green Orange Purple Pink Yellow) + + messages.zip(long_desc).each do |msg, desc| + e = msg.sub(/(?:Temporarily )?Out.of.Service/i, '') + .sub(/Elevator at/i, '') + .sub(/Street.to.Mezzanine/i, '') + .sub(/Elevator/i, '') + .strip + cta_line = (desc.downcase.split(/\W+/) & cta_lines.map(&:downcase))[0] + cta_line = " #{cta_line.capitalize}" if cta_line + if e && e != '' + elevator_names << "Chicago#{cta_line}: #{e}".gsub(/\s+/, ' ') + else + Models::Unparseable.first_or_create(:data => msg) + end + end + + elevator_names + end +end From 1e2bb3420dbdf8e8c0eee6cc12b403c4834aa635 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Mon, 23 Jan 2017 19:37:08 -0600 Subject: [PATCH 2/2] update worker with chicago api --- worker.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/worker.rb b/worker.rb index c48d6b8..030b2af 100755 --- a/worker.rb +++ b/worker.rb @@ -5,6 +5,7 @@ require 'models' require 'bart_api' require 'muni_api' +require 'chicago_api' require 'notifier' require 'my_rollbar' @@ -38,10 +39,16 @@ def self.run! Models::Elevator.first_or_create(:name => name) end + # Chicago + chicago_data = ChicagoApi.get_data + out_elevators += chicago_data.map do |name| + Models::Elevator.first_or_create(:name => name) + end + total_count = Models::Elevator.count puts "New elevators: #{total_count - existing_count}." - puts "Data: #{bart_data}, #{muni_data}" + puts "Data: #{bart_data}, #{muni_data}", "#{chicago_data}" outages_to_notify = []