Created
April 11, 2023 01:05
-
-
Save vicovictor/60925b5d33864810eb78d75435b4cc9e to your computer and use it in GitHub Desktop.
SQ
This file contains 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 'mechanize' | |
# Set up the Mechanize agent with Safari user agent | |
agent = Mechanize.new | |
agent.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' | |
# Set custom headers | |
headers = { | |
"authority" => "www.singaporeair.com", | |
"accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", | |
"accept-language" => "en-GB,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7,zh-TW;q=0.6,zh;q=0.5,ms;q=0.4", | |
"cache-control" => "max-age=0", | |
"content-type" => "application/x-www-form-urlencoded", | |
"origin" => "https://www.singaporeair.com", | |
"referer" => "https://www.singaporeair.com/kfLogin.form", | |
"sec-ch-ua" => "\"Chromium\";v=\"112\", \"Brave\";v=\"112\", \"Not:A-Brand\";v=\"99\"", | |
"sec-ch-ua-mobile" => "?0", | |
"sec-ch-ua-platform" => "\"macOS\"", | |
"sec-fetch-dest" => "document", | |
"sec-fetch-mode" => "navigate", | |
"sec-fetch-site" => "same-origin", | |
"sec-fetch-user" => "?1", | |
"sec-gpc" => "1", | |
"upgrade-insecure-requests" => "1" | |
} | |
agent.request_headers = headers | |
# Set logger level to DEBUG | |
agent.log = Logger.new(STDOUT) | |
agent.log.level = Logger::DEBUG | |
# Load the login page | |
page = agent.get('https://www.singaporeair.com/kfLogin.form') | |
# Fill in the login form | |
form = page.form_with(id: 'kfLoginForm') | |
form.field_with(name: 'kfNumber').value = 'username' | |
form.field_with(name: 'pin').value = 'password' | |
# Submit the form and get the response | |
response = form.submit | |
# Print the response body | |
puts response.body | |
# Add a delay of 5 seconds | |
sleep(10) | |
# Go to the homepage | |
agent.get('https://www.singaporeair.com/krisflyer/account-summary/elite') | |
puts agent.page.body | |
puts agent.page.title | |
# Check if login is successful by looking for the user's name on the page | |
if agent.page.title.include?('Account Summary') | |
puts 'Login successful!' | |
else | |
puts 'Login failed.' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment