Setting an expiration on a function for a retry

Mansur

New member
I am harvesting captcha, and captcha responses are only good for 2 minutes. I am trying to figure out how to set an "Expired, fetching again" function
Code:
def captchaToken():
    global captoken
    s = requests.Session()
    # here we post site key to 2captcha to get captcha ID (and we parse it here too)
    captcha_id = s.post("http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(APItoken, sitekey, url)).text.split('|')[1]
    #time.sleep(5)
    print("Waiting for captcha...")
    # then we parse gresponse from 2captcha response
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(APItoken, captcha_id)).text
    print("Solving captcha...")
    while 'CAPCHA_NOT_READY' in recaptcha_answer:
        print("Waiting for a response . . .")
        time.sleep(2.5)
        recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(APItoken, captcha_id)).text
    recaptcha_answer = recaptcha_answer.split('|')[1]
    captoken = recaptcha_answer
    print(Fore.MAGENTA + "Captcha Response: "+recaptcha_answer)
 

Artem

New member
I'd suggest:
Code:
while 1:
   captcha = get_captcha()
   request = post_captcha(captcha)
   start = time.time()
   while 1:
      response = poll_for_result(request)
      if start + 120 < time.time():
         time_is_up()
         break
      if response:
         return deal_with_response(response)