Check Data and Alert

From iMacros

Jump to: navigation, search

This project is designed to be run automatically via the Windows Scheduler. It checks for new property listing on a website and sends updates to the user's telephone with details of new listings or else it will send a message to say that no new properties were found.

Visual Basic Script

 Dim extractedPropInfo, existingPropInfo
 
 ' These variable control which part of the extracted Information
 ' is included in the message to the Cell phone. If the variable 
 ' is set to True, the value is included, if set to False, not.
 incl_price = True 
 incl_bd = True 
 incl_baths = False 
 incl_house_size = True 
 incl_lot_size = True 
 incl_address = True 
 incl_agent = True 
 incl_phone = True 
 incl_remarks = True 
 
 'open log file
 Set objFileSystem = CreateObject("Scripting.fileSystemObject")
 Set logFile = objFileSystem.OpenTextFile("./tarmls_logs.txt", 8, TRUE)
 logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": Log file opened.")
 
 'read existing data from "results.csv" in the same directory as this script
 existingPropInfo = readInfo()
 
 ' start iMacros
 Set imacros = CreateObject("InternetMacros.iim")
 iret = imacros.iimInit()
 logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": iimInit went "+CStr(iret))
 
 'play macro which navigates to the results page
 iret = imacros.iimPlay("tarmls_goto")
 logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": tarmls_goto went "+CStr(iret))
 If iret < 0 Then
   logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": error was " + imacros.iimGetLastError())
 End If
 
 'play results number extract macro
 iret = imacros.iimPlay("tarmls_extract_maxnum")
 logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": tarmls_extract_maxnum went "+CStr(iret))
 
 maxNumber_tmp = Split(imacros.iimGetLastExtract(), "[EXTRACT]")
 maxNumber = Trim(maxNumber_tmp(0))
 logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": extract_maxnum is "+CStr(maxNumber))
 
 ' check if result number was found
 If InStr(maxNumber, "#EANF#") > 0 Then
 	' if the maximum number of results could not be extracted, end with error written in log file.
   logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": Navigation failed. Exiting.")
   iret = imacros.iimExit()
   logFile.Close()
 End If
 
 ReDim extractedPropInfo(maxNumber-1)
 
 ' extract prop ids from web site
 For i=0 To maxNumber-1
   iret = imacros.iimSet("-var_MY_POS", CStr(i*8+2))
   iret = imacros.iimPlay("tarmls_extract")
   tmp = Split(imacros.iimGetLastExtract(), "[EXTRACT]")
   actNumber = Split(tmp(0), "#")
   extractedPropInfo(i) = Trim(actNumber(1))
 Next
 
 ' extractedPropInfo now contains all the property ids from the currently display result page.
 
 'write new data
 writeInfo()
 
 'compare newly extracted prop ids with the formerly written ones
 newPropCount = 0
 newProps = ""
 If isArray(existingPropInfo) Then
   logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": Comparing...")
   For i=0 To maxNumber-1
     matchFound = False
     For k=0 To UBound(existingPropInfo)
       If extractedPropInfo(i) = existingPropInfo(k) Then
         matchFound = True
         Exit For
       End If
     Next
     ' if the newly extracted property id is not found in the old ones, it is added to a list.
     If Not matchFound Then
       logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": New propNumber is " + CStr(i))
       If newPropCount = 0 Then
         newProps = Cstr(i)
       Else
         newProps = newProps + "," + Cstr(i)
       End If
       newPropCount = newPropCount + 1
     End If
   Next
 Else
   logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": existingPropInfo is apparently empty. ")
   newProps = ""
 End If
 
 ' for each new properties extract the infos and send to phone
 If StrComp(newProps, "") <> 0 Then
   logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": newProps is " + newProps)
   newPropsArr = Split(newProps, ",")
   ' extract info for new props
   For i=0 To UBound(newPropsArr)
     logFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": tarmls_extract_info pos is "+CStr(newPropsArr(i)*3+1))
     ' extract the information for the new property
     iret = imacros.iimSet("-var_MY_POS", CStr(newPropsArr(i)*3+1))
     iret = imacros.iimPlay("tarmls_extract_info")
     extractedInfo = imacros.iimGetLastExtract()
     ' parse info for new prop
     msgText = getMessageText(extractedInfo)
     Set logMsgFile = objFileSystem.OpenTextFile("./tarmls_msg_logs.txt", 8, TRUE)
     logMsgFile.WriteLine(CStr(Date()) + " " + CStr(Time()) + ": "+msgText)
     logMsgFile.Close()
     ' send parsed info to phone
     iret = imacros.iimSet("-var_MY_TEXT", msgText)
     iret = imacros.iimPlay("tarmls_phone")
     If iret < 0 Then
       logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Sending Message failed.")
     End If
   Next
 Else
   'send message that no new props were found
   msgText = "No new properties found."
   Set logMsgFile = objFileSystem.OpenTextFile("./tarmls_msg_logs.txt", 8, TRUE)
   logMsgFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": "+msgText)
   logMsgFile.Close()
   iret = imacros.iimSet("-var_MY_TEXT", msgText)
   iret = imacros.iimPlay("tarmls_phone")
   If iret < 0 Then
     logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Sending Message failed.")
   End If
   logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": newProps is empty.")
   logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": No new properties found.")
 End If
 
 ' make a clean ending
 iret = imacros.iimExit()
 logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": All done.")
 logFile.Close()
 
 '=================
 ' parse the extracted information and make nice text out of it
 Function getMessageText(extrInfo)
 '=================
   tmp1 = Split(extrInfo, "[EXTRACT]")
   tmp2 = Split(tmp1(0), "#NEXT#")
   price = Trim(tmp2(0))
   bd = Trim(tmp2(1))
   baths = Trim(tmp2(2))
   house_size = Trim(tmp2(3))
   lot_size = Trim(tmp2(4))
   tmp3 = Split(tmp1(1), "#NEXT##NEWLINE#")
   add_tmp = Split(tmp3(1), "#NEXT#")
   address = Trim(add_tmp(0))+","+Trim(add_tmp(1))+","+Trim(add_tmp(2))
   name_tmp = Split(tmp3(4), "#NEXT#")
   name_tmp1 = Split(name_tmp(0), ":")
   agent = Trim(name_tmp1(1)) 
   phone_tmp = Split(tmp3(5), "#NEXT#")
   phone_tmp1 = Split(phone_tmp(0), ":")
   If UBound(phone_tmp1) = 0 Then
     phone_tmp1 = Split(phone_tmp(1), ":")
   End If
   phone = Trim(phone_tmp1(1))
   remarks_tmp = Split(tmp3(6), ":")
   remarks = Trim(remarks_tmp(1))
   msgText_tmp = ""
   If incl_price Then msgText_tmp = msgText_tmp + price + ","
   If incl_bd Then msgText_tmp = msgText_tmp + bd + ","
   If incl_baths Then msgText_tmp = msgText_tmp + baths + ","
   If incl_house_size Then msgText_tmp = msgText_tmp + house_size + ","
   If incl_lot_size Then msgText_tmp = msgText_tmp + lot_size + ","
   If incl_address Then msgText_tmp = msgText_tmp + address + ","
   If incl_agent Then msgText_tmp = msgText_tmp + agent + ","
   If incl_phone Then msgText_tmp = msgText_tmp + phone + ","
   If incl_remarks Then msgText_tmp = msgText_tmp + remarks + ","
   If Len(msgText_tmp) > 160 Then 
     getMessageText = Replace(Left(msgText_tmp, 160), vbNewLine, "")
   Else 
     getMessageText = Replace(Left(msgText_tmp, Len(msgText_tmp)-1), vbNewLine, "")
   End If
 End Function
 
 '=================
 ' write prop ids to file for future comparisson
 Sub writeInfo
 '=================
   'open result file (if it is opened the first time, write header)
   logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Rewriting result file with length "+CStr(UBound(extractedPropInfo)+1)+".")
   Set outputFile = objFileSystem.OpenTextFile("./results.csv", 2, TRUE)
   For i=0 To UBound(extractedPropInfo)-1
     outputFile.WriteLine(CStr(extractedPropInfo(i))) 
   Next
   outputFile.Write(CStr(extractedPropInfo(i)))
   outputFile.Close()
   logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Result written.")
 End Sub
 
 '=================
 ' read existing prop ids for comparison
 Function readInfo 
 '================= 
   'open result file (if it is opened the first time, write header)
   logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Probing result file...")
   If objFileSystem.FileExists("./results.csv") Then
     Set inputFile = objFileSystem.OpenTextFile("./results.csv", 1, TRUE)
     retstring = inputFile.ReadAll()
     inputFile.Close()
     readInfo = Split(retString, vbNewline)
     logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": " + CStr(UBound(readInfo)+1) + " data entries found.")
   Else
     logFile.WriteLine(cstr(date()) + " " + cstr(time()) + ": Result file NOT found, starting from scratch...")
     readInfo = ""
   End If
 End Function

Macro code for tarmls_goto

 VERSION BUILD=5200531
 'SET !REPLAYSPEED FAST     
 TAB T=1     
 TAB CLOSEALLOTHERS     
 URL GOTO=http://www.tarmls.com/     
 SIZE X=876 Y=593    
 TAG POS=1 TYPE=IMG ATTR=TXT:<IMG<SP>height=27<SP>src="images/publicsearch.gif"<SP>width=121<SP>border=0>   
 TAG POS=1 TYPE=INPUT:CHECKBOX FORM=NAME:InputForm ATTR=NAME:Prop_Type_RESI&&VALUE:on CONTENT=YES 
 TAG POS=1 TYPE=IMG ATTR=HREF:javascript://#LookUp   
 TAB T=2     
 FRAME F=2     
 TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:InputForm ATTR=NAME:City_Criteria CONTENT=Tucson 
 TAG POS=1 TYPE=INPUT:SUBMIT FORM=NAME:InputForm ATTR=NAME:Submit&&VALUE:<SP>Search<SP>
 TAG POS=1 TYPE=INPUT:CHECKBOX FORM=NAME:InputForm ATTR=NAME:City_1&&VALUE:on CONTENT=YES
 TAB T=2 
 FRAME F=1     
 TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:InputForm ATTR=NAME:btn1Ok&&VALUE:*OK*
 TAB T=1     
 TAB CLOSEALLOTHERS     
 FRAME F=0
 'WINCLICK X=514 Y=411 CONTENT=EVENT:MOUSEOVER   
 'WINCLICK X=514 Y=411 CONTENT=   
 TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:InputForm ATTR=NAME:Price_Thru_M1 CONTENT=120 
 TAG POS=1 TYPE=SELECT FORM=NAME:InputForm ATTR=NAME:Bathrooms_From_F CONTENT=2 
 TAG POS=1 TYPE=SELECT FORM=NAME:InputForm ATTR=NAME:Bathrooms_From_T CONTENT=2    
 TAG POS=1 TYPE=INPUT:SUBMIT FORM=NAME:InputForm ATTR=NAME:submit&&VALUE:Submit 

Macro code for tarmls_extract_maxnum

 VERSION BUILD=5200531     
 SET !REPLAYSPEED FAST   
 TAB T=1
 EXTRACT POS=1 TYPE=TXT ATTR=<FONT<SP>class=mRedTextB<SP>id=RecordsFound>*  

Macro code for tarmls_extract

 VERSION BUILD=5200531     
 SET !REPLAYSPEED FAST     
 TAB T=1   
 EXTRACT POS={{MY_POS}} TYPE=TXT ATTR=<TD<SP>noWrap<SP>width="1%">*

Macro code for tarmls_extract_info

 VERSION BUILD=5200531     
 SET !REPLAYSPEED FAST     
 TAB T=1
 SET !VAR1 {{MY_POS}}
 EXTRACT POS={{MY_POS}} TYPE=TXT ATTR=<TR<SP>c*bg*i*BarsBlackText*>*
 ADD !VAR1 1
 EXTRACT POS={{!VAR1}} TYPE=TXT ATTR=<TR<SP>c*bg*i*BarsBlackText*>*   

Macro code for tarmls_phone

 VERSION BUILD=5200531     
 TAB T=1
 TAB OPEN
 TAB T=2     
 URL GOTO=http://messaging.sprintpcs.com/textmessaging/     
 SIZE X=805 Y=484  
 'Please note the bogus phone number  
 TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:composeForm ATTR=NAME:phoneNumber CONTENT=555-123-4567
 TAG POS=1 TYPE=TEXTAREA FORM=NAME:composeForm ATTR=NAME:message CONTENT={{MY_TEXT}}   
 TAG POS=1 TYPE=INPUT:IMAGE FORM=NAME:composeForm ATTR=NAME:&&VALUE:Send
 'Comment: New page loaded
 TAB CLOSE  
 TAB T=1