Last active
November 24, 2015 16:07
-
-
Save kejadlen/156ea8f3af796fcc02c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require 'time' | |
require 'dotenv' | |
require 'pry' | |
require 'watir-webdriver' | |
Dotenv.load | |
class Rescheduler | |
attr_reader :username, :password, :logger | |
def initialize(username, password) | |
@username, @password = username, password | |
@logger = Logger.new(STDOUT) | |
logger.level = Logger::INFO | |
end | |
def reschedule! | |
browser = Watir::Browser.new :phantomjs | |
# Sign in and go to the Nexus application | |
browser.goto('https://goes-app.cbp.dhs.gov/main/goes') | |
browser.text_field(id: 'user').set(username) | |
browser.text_field(id: 'password').set(password) | |
browser.button(id: 'SignIn').click | |
browser.link(text: 'Enter').click | |
browser.button(value: 'Manage Interview Appointment').click | |
# Get the current interview location and time | |
date = browser.text[/Interview Date: (.+)/, 1] | |
time = browser.text[/Interview Time: (.+)/, 1] | |
location = browser.text[/Enrollment Center: (.+)/, 1] | |
current_interview = Time.parse("#{date} #{time}") | |
logger.info "Current interview: #{current_interview.strftime('%D %H:%M')} at #{location}" | |
browser.button(value: 'Reschedule Appointment').click | |
browser.button(value: 'Next').click | |
# Get the interview options | |
links = browser.links(href: /javascript:scheduleInterview\('\d+'\)/) | |
times = browser.text.scan(/Date:(.+),\s+Start Time:(\d{2})(\d{2}),/) | |
.map {|d,h,m| Time.parse("#{d} #{h}:#{m}") } | |
# Drop the time for Blaine, since there's a time for it but no link. | |
times.shift | |
interview_options = links.zip(times) | |
logger.info "Interview options:\n#{interview_options.map {|l,d| "#{d.strftime('%D %H:%M')} at #{l.text}" }.join("\n")}" | |
# Really, we only care about Seattle | |
seattle = interview_options.find {|l,_| l.text.include?('Seattle') } || interview_options.first | |
return if current_interview < seattle.last || !yield(seattle.last) | |
logger.info "Found a better time! Switching interview to #{seattle.last.strftime('%D %H:%M')}" | |
seattle.first.click | |
browser.link(class: 'entry').click | |
browser.text_field(name: 'comments').set('Better time') | |
browser.button(name: 'Confirm').click | |
end | |
end | |
rescheduler = Rescheduler.new(ENV['GOES_USERNAME'], ENV['GOES_PASSWORD']) | |
while true | |
rescheduler.reschedule! do |time| | |
(time > Time.now + 24*60*60) && # Require more than a day of warning | |
(time.saturday? || time.sunday?) # Only a weekend | |
end | |
sleep 10*60 # 10 minutes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment