Skip to content

Instantly share code, notes, and snippets.

@JesseHerrick
Last active August 29, 2015 14:15
Show Gist options
  • Save JesseHerrick/406464c89d5f45042bf6 to your computer and use it in GitHub Desktop.
Save JesseHerrick/406464c89d5f45042bf6 to your computer and use it in GitHub Desktop.
A little Ruby script to quickly get my teacher's emails.
# Kettering Staff "API"
#
# kettering-staff.rb <LAST NAME>
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Jesse Herrick
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'mechanize'
# initialize scraper
mech = Mechanize.new
page = mech.get('http://www.ketteringschools.org/district_staff.aspx')
# query for teacher last name
name_query = if ARGV.first
ARGV.shift
else
print "Last Name: "
gets.chomp
end
form = page.forms.first
form['ctl00_ctl00_MasterContent_ContentColumnRight_uc_staff_dd_schools'] = '1'
data = form.submit form.button_with(name: 'ctl00$ctl00$MasterContent$ContentColumnRight$uc_staff$ib_submit')
emails_raw = data.search('.emailstaff')
emails = {}
emails_raw.each do |email|
email = email.attributes['href'].value.gsub('mailto:', '')
name_data = /(\w*)\.(\w*)/.match(email)
name = "#{name_data[1].capitalize} #{name_data[2].capitalize}"
emails["#{name}"] = email
end
selected_emails = emails.select { |k, v| k.include? name_query }
abort "No emails found." if selected_emails.empty?
selected_emails.each do |n, e|
puts "#{n}: #{e}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment