Jump to content
Pilot123

Help needed: Improving Autoit script

Recommended Posts

Pilot123

I'm new to Autoit.

Could someone please be so kind to help me improve my script.

When I run the script it goes to a website and gets data and converts it to 24h time format (30 minutes apart) and displays it in an array.

The output of the array is exactly what I need, but I'm  lacking proper coding skills to improve it.

#Include <String.au3>
#Include <INET.au3>
#include <Array.au3>
#include <IE.au3>

$oIE = _IECreate("http://www.poweralert.co.za/poweralert5/slider_1.php", 0, 0, 0) ; loading url hidden, don't wait for page loading
$var = _IELoadWait ($oIE, 0, 20000) ; wait for page loading -> timeout 20 sec
_IEQuit ($oIE)

If $var = 1 Then
   $source = _INetGetSource("http://www.poweralert.co.za/poweralert5/slider_1.php")
Else
    MsgBox (0,"Timeout", "...loading page -> maybe bad connection!", 3)
    Exit
EndIf


$html = _StringBetween($source, '<td valign="top">', '</td>')   ; _StringBetween returns an array

If Not @error Then

    If $html[0] = "&nbsp;" then
       $html[0] = $html[1] -1 &":30"
    Else
       $html[0] = $html[0] &":00"
    Endif

    If $html[63] = "&nbsp;" then
       $html[63] = $html[62] &":30"
    Else
       $html[63] = $html[63] &":00"
    Endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

     For $i = 1 To 62
       If $html[$i] <> "&nbsp;" then
       $html[$i] = $html[$i] &":00"
     Else
       Endif
 Next

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

For $i = 1 To 62
       If $html[$i] = "&nbsp;" then
       $var1 = $html[$i-1]
       $var2 = StringReplace($var1, ":00", "")
       $html[$i] = $var2 &":30"
     Else
       Endif
 Next

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 $array = _ArrayDisplay($html,"Output")

Else
    MsgBox(0,"","Error not Found")
 EndIf

 

Share this post


Link to post
Share on other sites
jvds

Hi Pilot123, and welcome to the forums! =)

you could add this to your script to make it less vulnerable to errors if they ever happen, this is specially important if you are going to work with arrays

if isarray($html) then ;check if it is an array
    if ubound($html)<= 64 then ;check if it have atleast 64 items becouse you are using up to 64 items ($html[63] is the 64th item)

        ;your code here after you create the $html array, from line 20 of your example script
        



    Else
;~      MsgBox(0,"","less then 64 items found in $html array")
        filewrite('debug.txt',"$html array only had "&ubound($html)&" items"&@CRLF)
    EndIf
Else 
;~  MsgBox(0,"","$html was not an array!!")
    filewrite('debug.txt',"$html was not an array"&@CRLF)
EndIf

this will make sure the array you create have atleast 64 items becouse you use up to 64 items and also to make sure that _StringBetween did return an array, however 'If Not @error Then' is ok instead of if isarray($html) because _StringBetween  will always return an array or an error, so both methods are ok in this case
if your array $html have less than 64 arrays (for some reason out of your control, example the site change something in their code) and you try to do something like $html[63] = $html[63] &":00" then the script will crash because it does not have that amount of arrays, so ubound($html) <= 64  is just a healthy safety measure =)
Big mistake I made as a newbie, (and im still one =P) is to write scripts without taking the time to make them error proof, it's simple and does no take a lot of time, but will save you a lot of head aches, and its a healthy way to start out, because it becomes second nature to just do it always while you write, and avoid hours of figuring out latter What went wrong with your script

note that 'If Not @error Then' like in your script, and ubound($html) and also isarray($html) are the important ones for arrays, if you report the error or not like in my example is totally optional.

Edited by jvds
editing

Share this post


Link to post
Share on other sites
Pilot123

Thanks jvds,

I added your if isarray($html) to my script, but I had to change the Less-than operator <: 

if ubound($html)<= 64 then

to a Greater-than operator >:

if ubound($html)>= 64 then

otherwise the script exit with the debug.txt --> 

$html array only had 65 items

 

Ok, I did not mention that the source code of the html(.php) for that page changes every 30 minutes, as time progresses through the day.

On every half hour of the day(example: 14h30) an extra

<td valign="top">&nbsp;</td>

is added to the the html source code, that adds an extra item to the array that I don't need ( array now has 65 items). Then I get the debug.txt as mention above with the Less-than operator.

If the time of the day happens to be on the hour, then there's no problem.

 

 

 

Edited by Pilot123

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×