Sunday, August 11, 2013

“No Items to Review - Stack Overflow” Macro

I will going to show you the constructions of an intermediate automation script using an automation software called AutoIt v3. This tutorial slightly explains how to automate the opening of Internet Explorer, automatically visit “Review First Posts” in a condition, and “Review Late Answers” in new tab and whatever review page(s) of Stack Overflow (all in new tab,) that you want to inject, and reload each one of those web browser's tab and Go to next tab if There are no items for you to review. or An error has occurred. and Go to next tab if the active IE's tab have the review page that you've already finished reviewing its 20 items, and more to make the reviewing of the review items of SO less repetitive!

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         Mkt on Net (Marketing on the Internet)
 Link:           http://mktonnet.blogspot.com/

 Title:          “No Review Items - Stack Overflow” Automation
 Script Function:
 Stack Overflow refreshing automation of no items on Review First Posts and Late Answers.

#ce ----------------------------------------------------------------------------

#include <IE.au3>

Local $oIE = _IECreate("http://stackoverflow.com/users/login", 0, 0, 0, 1) ; IE OBJECT

Local $oLinks, $oAction = 0, $oString

While 1
   If WinExists("Log In - Stack Overflow") Then
   While 1
   _IELoadWait($oIE)
   $oIE.Visible = 1
   
   $oLinks = _IELinkGetCollection($oIE)
   $oAction = "Logged"
   
   For $oLink In $oLinks
   _IEAttach($oIE, "instance", 1)
   If @Error = 7 Then
      MsgBox(0, "Macro Stopped", "The macro is automatically exit from running.")
      Exit
   EndIf
   
   $oString = StringRegExp($oLink.href, "http://stackoverflow.com/users/login")
   If $oString = 1 AND NOT WinExists("Log In - Stack Overflow") Then
      $oAction = "Redirect"
   EndIf
   Next
   If $oAction = "Redirect" Then
   _IENavigate($oIE, "http://stackoverflow.com/users/login")
   MsgBox(0, "Plese Login", "Please, click the logo to log in with it.")
   ElseIf $oAction = "Logged" And WinExists("Stack Overflow") Then
   _IENavigate($oIE, "http://stackoverflow.com/review/first-posts")
   ExitLoop 2
   EndIf
   WEnd
   ElseIf WinExists("Add Login - Stack Overflow") Then
   $oIE.Visible = 1
   MsgBox(0, "Already Logged In", "You are already logged in.")
   _IENavigate($oIE, "http://stackoverflow.com/review/first-posts")
   If @Error = $_IEStatus_InvalidObjectType Then
   MsgBox(0, "Macro Exited", "The macro is automatically stop running.")
   Exit
   EndIf
   ExitLoop
   ElseIf WinExists("This page can't be displayed") Then
   $oIE.Visible = 1
   MsgBox(0, "The page can't be displayed", "The page with the address of http://stackoverflow.com/users/login can't be displayed.")
   MsgBox(0, "Script Stopped", "The macro will stop running.")
   _IEQuit($oIE)
   Exit
   EndIf
WEnd

While 1
   If WinExists("Review First Posts - Stack Overflow") Then
   ExitLoop
   ElseIf WinExists("This page can't be displayed") Then
   MsgBox(0, "The page can't be displayed", "The page with the address of http://stackoverflow.com/review/first-posts can't be displayed.")
   MsgBox(0, "Macro Stopped", "The script will stop running.")
   _IEQuit($oIE)
   Exit
   EndIf
WEnd

Local $aTitle, $location
$aTitle = WinGetTitle("[active]")

__IENavigate($oIE, "http://stackoverflow.com/review/late-answers", 0, 0x800)
WinWait("Review Late Answers - Stack Overflow")

$location = StringInStr($aTitle, "Review First Posts - Stack Overflow")
If $location == 0 Then
   WinActivate($aTitle)
EndIf

Local $i, $oTabs[1] ; IE GET TABS variables
Local $aTab, $aText, $wActivate, $o = 0

While 1
   Sleep(12000)
   
   _IEAttach($oIE, "instance", 1)
   If @Error = 7 Then
   MsgBox(0, "Script Exited", "The automation script is automatically stop running.")
   Exit
   EndIf
   
; #IE GET TABS# =================================================================================================================
   $i = 1
   While 1
   $oTabs[$i - 1] = _IEAttach($oIE, "instance", $i)
   If @Error = $_IEStatus_NoMatch Then
   $i -= 1
   If $i < 1 Then
   MsgBox(0, "Script Stopped", "The automation script is automatically exit from running.")
   ExitLoop 2
   EndIf
   ReDim $oTabs[$i]
   ExitLoop
   Else 
   $i += 1
   ReDim $oTabs[$i]
   EndIf
   WEnd
; ===============================================================================================================================
   
   $aTab = _IEGetActiveTab()
   $aText = _IEBodyReadText($aTab)
   If @Error = 3 Then
   $location = 0
   Else 
   $location = StringInStr($aText, "There are no items for you to review.")
   $location += StringInStr($aText, "Please try again.")
   $location += StringInStr($aText, "Thank you for reviewing")
   EndIf
   
   If $location > 0 AND UBound($oTabs) > 1 Then
   $aTitle = WinGetTitle("[active]")
   $location = StringInStr($aTitle, "Review")
   
   ControlSend(_IEGetActiveTitle(), "", "", "^{TAB}")
   
   If $location == 0 Then
   $wActivate = WinActivate($aTitle)
   EndIf
   EndIf
   
   If $o = $i OR $o > $i Then
   $o = 0
   EndIf
   
   $aText = _IEBodyReadText($oTabs[$o])
   If @Error = 3 Then
   Exit
   Else 
   $location = StringInStr($aText, "There are no items for you to review.")
   $location += StringInStr($aText, "Please try again.")
   EndIf
   
   If $location > 0 Then
   $oTabs[$o].Refresh
   $o += 1
   Else 
   $o += 1
   EndIf
WEnd

; #FUNCTIONS# ===================================================================================================================
Func _IEGetActiveTitle()
   Local $iIE, $hwnd, $title
   
   _IELoadWait($oTabs[0]) ; IE GET TABS' 1st tab object included
   
   $iIE = _IEAttach($oIE, "instance", 1) ; IE OBJECT included
   
   If @Error = $_IEStatus_Success Then
   $hwnd = _IEPropertyGet($iIE, "hwnd")
   $title = WinGetTitle($hwnd) ; get window title
   EndIf
   
   Return $title
EndFunc

Func _IEGetActiveTab()
   Local $title, $tIE
   
   $title = _IEGetActiveTitle() ; includes _IEGetActiveTitle()
   $tIE = _IEAttach($title, "windowtitle")
   
   Return $tIE
EndFunc
; ===============================================================================================================================