R - while or repeat loop for solving ReCaptcha V2 until the solution is found

VRSnik

New member
I am trying to use a modified version of the R code from Submit a form using POST with g-recaptcha-response argument to use 2captcha to log into a Web site as a registered user.

Regarding the R code below, I have used both the repeat and the while loops with both success and failure with regards to getting a solution to the ReCaptcha V2.

I want to find a loop method (while or repeat) that will always work. By this I mean that the loop will keep repeating until the token is found. Thus, it will keep looping until CAPCHA_NOT_READY is not the value for hzzo_response.

Will you suggest changes to either the while or repeat loop as needed?

Thank you.
Code:
library("rvest")
library("roperators")

my.uri <- "https://mics.unicef.org/visitors/sign-in"

session <- html_session(my.uri)

email <- getPass::getPass("Please enter your MICS username:")
pass <- getPass::getPass("Please enter your MICS password:")

# parameters
api_key <- key1
api_url <- "https://2captcha.com/in.php"
site_key <- key2

# GET method
req_url <- paste0("https://2captcha.com/in.php?key=", api_key, "&method=userrecaptcha&googlekey=",
site_key, "&pageurl=", my.uri)
get_response <- POST(req_url)
hzzo_content <- content(get_response)
hzzo_content <- xml_text(hzzo_content)
captcha_id <- stringr::str_extract_all(hzzo_content[[1]], "\\d+")[[1]]

captcha2_solve <- function(apiKey, captcha_id){
  req_url <- paste0("https://2captcha.com/res.php?key=", api_key, "&action=get&id=", captcha_id)
  result <- GET(req_url, timeout(20))
  captcha_content <- content(result)
  hzzo_responser <- captcha_content
  hzzo_response <- strsplit(hzzo_responser, "\\|")
  return(hzzo_response)
}

repeat {
  Sys.sleep(20L)
  hzzo_response <- captcha2_solve(api_key, captcha_id)
  if ("CAPCHA_NOT_READY" %ni% hzzo_response) {
  break }
  return(hzzo_response)
}


while(hzzo_response[[1]] == "CAPCHA_NOT_READY"){
  Sys.sleep(20L)
  hzzo_response <- captcha2_solve(api_key, captcha_id)
  return(hzzo_response)
}