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
; ===============================================================================================================================

These are the meanings of the macros' sections therein the automation script:

  1. #cs is the abbreviation for #comments-start, and #ce is for #comments-end. The entire section of those keywords is commented out.
  2. We #include <IE.au3> a pre-written file with many user-functions, in our script.
  3. _IECreate a hidden Internet Explorer browser window that's navigate to http://stackoverflow.com/users/login which returns immediately.
  4. Create or declare variables that will going to use by our script.
  5. Write a repetitive loop While our IE web page is still loading:
    • If the title of the page is Log In - Stack Overflow, you will be in another loop, and set the hidden IE browser window visible.
      • If you're not logged in and have linked to the other page of Stack Overflow, you will be redirected back to http://stackoverflow.com/users/login again and leaving you a message.
      • ElseIf you've logged in, you the user will be redirected to http://stackoverflow.com/review/first-posts and you will ExitLoop 2.
    • ElseIf the page have a title of Add Login - Stack Overflow, means that you're already logged. Set the hidden IE to be visible, and redirected the user to http://stackoverflow.com/review/first-posts
      1. If $_IEStatus_InvalidObjectType has occurred, display some informal message and Exit the script. (Note: Our script has a lot of filters if an error has occured. In fact: There's a previous error filter that I haven't mentioned.)
    • ElseIf the title become This page can't be displayed, make the hidden IE visible and output some details, and Exit the script.
  6. Another While 1 loop:
    • If the title Review First Posts - Stack Overflow exist, Then ExitLoop.
    • ElseIf the title This page can't be displayed exist, _IEQuit($oIE) our specific IE object and Exit.
  7. Create and declare another variables, and get the title of the active window.
  8. Create a new IE tab that's navigate to http://stackoverflow.com/review/late-answers and WinWait the title Review Late Answers - Stack Overflow before the script continue running.
  9. If the title of the previous active window didn't have the words Review First Posts - Stack Overflow, then WinActivate that window.
  10. Create or declare another variables for the following macro lines.
  11. Create a While 1 loop, and pause the script execution in 12 seconds.
    1. If the _IEAttach to the specified instance fail, display some info, and Exit the script.
    2. IE GET TABS or store each object variable of every IE tab on an array.
    3. Get the object of the active tab of our IE, and get the text inside the <body> tag of the document of this object.
      • If Invalid Data Type, assign 0 to the variable $location.
      • Or Else , check if the variable $location contains There are no items for you to review. or Please try again. or Thank you for reviewing substring.
    4. If the string contains a given substring and if the size of the $oTabs array dimensions is greater than 1, checks the title of our active window if it contains Review word, and sends Ctrl + Tab to the active IE tab whether it's focused or not.
      1. If the title doesn't contains a given substring, gives focus to the window with this title after sending keystrokes directly to our IE tab's window.
    5. If the variable $o is equal or grater than to the variable $i then assign 0 to the variable $o.
    6. Read the body text of the IE tab's document of the specific dimension of the dynamic array $oTabs.
      • If Invalid Data Type, then Exit the script.
      • Or Else , check if the body text contains There are no items for you to review. or Please try again. sentence.
      • If the body text contains a given substring, refresh the IE tab's document of the specific dimension of the dynamic array $oTabs and add 1 to the variable $o.
      • Or Else , just add 1 to the variable $o.
  12. Create a function _IEGetActiveTitle() that will get the title of the active IE tab whether it's activated or not.
  13. Create a function called _IEGetActiveTab() that will get the object of the active IE tab.

This macro is too advance for a beginner. I spend days by writing and testing it. But of course, you can use an automation software just like what I did, and create an easy script that is much easier than this macro.

This intermediate script can sell worth 8$ USD, but Mkt on Net do not sell it for your sake, nor permit you to sell it to anyone. If you want to help some reviewers of Stack Overflow (like what I did,) to make their task less repetitive, just refer this blog. Thank you!

No comments:

Post a Comment

You can use some HTML tags, such as <b>, <i> and <a>.