Applescript login into website not quick enough.

Hi,


I am hoping that someone might be able to help me. I have a script to login to the YMCA to reserve a place in a class. The script works for 90% of all events except for the very popular ones - for example for certain classes (the popular ones) all 45 slots are taken within 2-3 seconds of the login window timeframe opening. You can sign into a class up to 36hours ahead of time.


I believe that my script is not fast enough. I have timers in the log file but I can’t record sub-second times - so I only know that I fail within the zero or one second after the hour.


If you would be so kind as to look at my script and see if there is a better way to do the signin that might be quicker.


Many thanks,


Terry


After the script I include the crontab data and the bash file.


Here’s the script:


the first part is missing as I exceeded the 5000 character limit.

set timestamp3 to (current date)

tell application "System Events"

tell process "Safari"

open location URL1 & URL2 & "-" & URL3 & URL4 & URL5 & URL6 & URL7 & URL8

delay 5

-- display dialog "the scpt script is still running."

end tell

-- tell application "System Events"

tell application "Safari"


activate

delay 5

if user is equal to "bf" then

do JavaScript "document.forms[0].login.value='fname_lastname@comcast.net'" in document 1

delay 2

do JavaScript "document.forms[0].password.value=‘badpswd’” in document 1

end if

if user is equal to "tf" then

do JavaScript "document.forms[0].login.value='fname_lastname1@comcast.net'" in document 1

delay 2

do JavaScript "document.forms[0].password.value=‘badpswd’” in document 1

end if


-- display dialog "Passed the if" & user -- (for debugging)

--

-- delay until the exact minute

--

repeat

set myDate to date string of (current date)

set myTime to time string of (current date)

set myMin to minutes of (current date)

if myMin is equal to 0 then

exit repeat

end if

if myMin is equal to 15 then -- I change this for testing.

exit repeat

end if

end repeat

set timestamp4 to (current date)

-- display dialog "the exit minute is " & myMin

-- delay 0.02 -- this is for testing with repeat section commented out

--

-- and then press the login button

--

do JavaScript "document.forms[0]['submit'].click()" in front document


delay 0.3

--

-- now click the reserve spot

--

do JavaScript "document.forms[0]['submit'].click()" in front document


set timestamp5 to (current date)

delay 2

set someText to "Y_sched.scpt has hopefully Reserved the Spot. "

do shell script "echo " & quoted form of someText & timestamp5 & " " & fnam & " " & user & " " & classnbr & " >> " & quoted form of logFile

tell application "System Events"

click at {250, 235}

end tell


--

-- now logout of session

--

delay 2

open location logoutURL

delay 2

--

-- now kill window

--

close document "GroupExPRO.com"

end tell







This is a portion of the crontab data


# The following statement points to a bash script that will invoke the AppleScript and pass the variables (Var1,2,3)

CRON_WRAPPER=/Users/ymca/tasks/Y_sched/Y_sched.sh

# 0900 on Sun for Tues classname - instructor

59 20 * * 0 $CRON_WRAPPER &> /Users/ymca/tasks/Y_sched/logs/Y_sched_$(date +\%a).log 0Sun4Tue_9A~bf~12345678





contents of Y_sched.sh:


#!/bin/bash

#

# This script is normally invoked by crontab ($CRON_WRAPPER)

#

# osascript /Users/ymca/tasks/Y_sched/Y_sched.scpt -ss - $1 $2 $3 $4 # this doesn't work

#

# Example of variables passed:- "0sun4tue_9A~tf~12345678” The ~ is to be used as a text field seperator

#

osascript /Users/ymca/tasks/Y_sched/Y_sched.scpt $1 $2 $3 $4

# osascript /Users/ymca/tasks/Y_sched/Y_sched.scpt "0sun4tue_9A~tf~11885688" #testing Manual


Posted on Apr 22, 2023 12:05 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 24, 2023 11:04 AM

Off hand, there is nothing inherently or obviously wrong with your script.


The fact that the script works most of the time indicates that your flow is valid. You seem to have the login part working, and your submit flow breaks down, essentially, to:


			repeat
				set myMin to minutes of (current date)
				if myMin is equal to 0 then
					exit repeat
				end if
			end repeat
			set timestamp4 to (current date)
			do JavaScript "document.forms[0]['submit'].click()" in front document


There is not a lot of chance to optimize this - as soon as myMin rolls to 0 you click 'Submit'.


So that leaves a couple of possibilities - some of which depend on the actual site, which obviously I can't see...


First off, if many people are signing up on the hour, the limit may be on the server-side and you're just unlucky (not much you can do if their server is inundated with requests, and it's pot-luck as to which ones get accepted).


Second is network latency - the form has to submit over the internet, so is prone to normal internet issues and delays. Not much you can do about this one, either since it's largely based on the network between you and their server.


The only other consideration is that you seem to be preparing the login:


			if user is equal to "bf" then
				do JavaScript "document.forms[0].login.value='fname_lastname@comcast.net'" in document 1
				delay 2
				do JavaScript "document.forms[0].password.value=‘badpswd’” in document 1
			end if


but you don't actually submit the login - this means that the server has to go through the login flow, then through the class signup flow, which could add delays that other users aren't subject to. I don't know the site, but there may be an optimization to sign into the site first (e.g. at 20:59, then the actual class signup submits at 21:00 without having to process the login.


Beyond that, I can't see much else that's going to make a difference.

Similar questions

2 replies
Question marked as Top-ranking reply

Apr 24, 2023 11:04 AM in response to hgtefla

Off hand, there is nothing inherently or obviously wrong with your script.


The fact that the script works most of the time indicates that your flow is valid. You seem to have the login part working, and your submit flow breaks down, essentially, to:


			repeat
				set myMin to minutes of (current date)
				if myMin is equal to 0 then
					exit repeat
				end if
			end repeat
			set timestamp4 to (current date)
			do JavaScript "document.forms[0]['submit'].click()" in front document


There is not a lot of chance to optimize this - as soon as myMin rolls to 0 you click 'Submit'.


So that leaves a couple of possibilities - some of which depend on the actual site, which obviously I can't see...


First off, if many people are signing up on the hour, the limit may be on the server-side and you're just unlucky (not much you can do if their server is inundated with requests, and it's pot-luck as to which ones get accepted).


Second is network latency - the form has to submit over the internet, so is prone to normal internet issues and delays. Not much you can do about this one, either since it's largely based on the network between you and their server.


The only other consideration is that you seem to be preparing the login:


			if user is equal to "bf" then
				do JavaScript "document.forms[0].login.value='fname_lastname@comcast.net'" in document 1
				delay 2
				do JavaScript "document.forms[0].password.value=‘badpswd’” in document 1
			end if


but you don't actually submit the login - this means that the server has to go through the login flow, then through the class signup flow, which could add delays that other users aren't subject to. I don't know the site, but there may be an optimization to sign into the site first (e.g. at 20:59, then the actual class signup submits at 21:00 without having to process the login.


Beyond that, I can't see much else that's going to make a difference.

Apr 24, 2023 11:30 AM in response to Camelot

Hi Camelot,

Many thanks for your response. I have tried a differnet script where I do signin earlier and then try to click on the reserve a spot button at the exact time - it rejects me saying I'm already signed in. Funnily enough on the iphone it works fine that way. We try it on the iphone as well and if the script works then we release the other reservation.

I think I'm as far as I am going to get with this thing.

Again many thanks for your advise. Terry

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript login into website not quick enough.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.