-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautomator.rb
96 lines (86 loc) · 3.26 KB
/
automator.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
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
require 'rubygems'
require 'logger'
require 'json'
require 'open-uri'
require 'mechanize'
require 'rbconfig'
require_relative './helpers/connection.rb'
require_relative './helpers/new_month.rb'
# only continue execution if form has not been submitted for this month
exit if File.exist?('logfile.log') && !new_month?
# create a logger to direct the data to logfile
logger = Logger.new('logfile.log')
# read the usrename and password from auth.json file
auth = File.read('auth.json')
auth_hash = JSON.parse(auth)
# check for internet connection
# quit if unable to establish connection after trying hard
# try accessing google ten times over the next three hours
unless can_be_reached? 10, 'http://www.google.com/', 1800
# exit after ten trials
logger.warn 'failed to stablish a network connection'
exit
end
# make a request for all the eligible months
# to improve the readibility and logging, the following code is flattened
agent = Mechanize.new
# There are issues with opening SSL URLs with Mechanize on Windows
# Workaround is to not verify URLs thereby bypassing certificates
# The following lines check if the host is windows and set the http mode to verify none
if RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
# fetch the upassbc website
login_page = agent.get('https://upassbc.translink.ca')
unless %r{https://upassbc.translink.ca}.match(login_page.uri.to_s)
logger.warn 'cannot reach the upassbc website'
exit
end
# select sfu from select school form
sfu_login_page = login_page.form_with(action: '/') do |select_school_form|
select_school_form.PsiId = 'sfu' # select sfu from the drop down menu
end.submit
# makes sure the submission redirects to cas for login
unless %r{https://cas.sfu.ca/cas/login}.match(sfu_login_page.uri.to_s)
logger.warn 'something about the upass bc select school form has changed'
exit
end
# login using your sfu computing id and password
hidden = sfu_login_page.form_with(name: 'login') do |login_form|
login_form.username = auth_hash['username']
login_form.password = auth_hash['password']
end.submit
# makes sure the submission is successful and doesn't redirects back to cas page
unless %r{https://idp.sfu.ca/idp/profile/SAML2}.match(hidden.uri.to_s)
logger.warn 'login failed! please check your username and password'
exit
end
# follows the redirects
hidden = hidden.forms.first.submit
# this redirects you to 'my upass bc' page
my_upassbc_page = hidden.forms.first.submit
# make sure you are logged into upassbc website
unless %r{https://upassbc.translink.ca}.match(my_upassbc_page.uri.to_s)
logger.warn 'something has changed in sfu authentication process'
exit
end
# requests the next month upass
next_month = 0
response = my_upassbc_page.form_with(action: '/fs/Eligibility/Request') do |request_form|
if request_form.checkbox_with(name: /^Eligibility\[.*\].Selected$/)
next_month = 1
request_form.checkbox_with(name: /^Eligibility\[.*\].Selected$/).check
end
end.submit
# log the result
if next_month > 0
response.form_with(action: '/fs/Eligibility/Request') do |request_form|
if request_form.checkbox_with(name: /^Eligibility\[.*\].Selected$/)
logger.warn 'request failed'
else
logger.info 'success'
end
end
else
logger.info 'not availibale yet'
end