Example #1
0
def test_captcha_builder_auto(api_key):
    from PIL import Image
    from svglib.svglib import svg2rlg
    from reportlab.graphics import renderPM
    from anticaptchaofficial.imagecaptcha import imagecaptcha
    import re, time
    drawing = svg2rlg('captcha.svg')
    renderPM.drawToFile(drawing, "captcha.png", fmt="PNG")

    solver = imagecaptcha()
    solver.set_verbose(1)
    solver.set_key(api_key)

    print("Started solving captcha...")
    tic = time.perf_counter()
    captcha_text = solver.solve_and_return_solution("captcha.png")
    toc = time.perf_counter()

    if captcha_text == "SNNvu":
        print(f"Captcha solve success: {captcha_text}")
        print(f"It took {toc - tic:0.4f} seconds to solve captcha")
    else:
        print(
            f"Task finished with error: {solver.error_code} - {captcha_text}")

    return captcha_text
Example #2
0
def captcha_buider(resp, api_key):
    with tempfile.TemporaryDirectory() as tmpdir:
        captcha_file = f"{tmpdir}/captcha.svg"
        with open(captcha_file, "w") as f:
            f.write(resp["captcha"])

        imgfile = open(captcha_file, "r+")
        captcha_cleaned = re.sub('(<path d=)(.*?)(fill="none"/>)', "",
                                 imgfile.read())
        imgfile.seek(0)
        imgfile.write(captcha_cleaned)
        imgfile.truncate()
        imgfile.close()

        drawing = svg2rlg(captcha_file)
        png_file = f"{tmpdir}/captcha.png"
        renderPM.drawToFile(drawing, png_file, fmt="PNG")

        if api_key:
            renderPM.drawToFile(drawing, "captcha.png", fmt="PNG")
            solver = imagecaptcha()
            solver.set_verbose(1)
            solver.set_key(api_key)
            captcha_text = solver.solve_and_return_solution(png_file)

            if captcha_text != 0:
                print(f"Captcha text: {captcha_text}")
            else:
                print(f"Task finished with error: {solver.error_code}")

            return captcha_text
        else:
            layout = [
                [sg.Image(png_file)],
                [sg.Text("Enter Captcha Below")],
                [sg.Input(key="txtcaptcha")],
                [sg.Button("Submit", bind_return_key=True)],
            ]

            window = sg.Window("Enter Captcha", layout, finalize=True)
            window.TKroot.focus_force()  # focus on window
            window.Element("txtcaptcha").SetFocus()  # focus on field

            event, values = window.read()
            window.close()

            return values["txtcaptcha"]
Example #3
0
def captcha_builder_auto(resp, api_key, logger):
    with open('captcha.svg', 'w') as f:
        f.write(re.sub('(<path d=)(.*?)(fill=\"none\"/>)', '', resp['captcha']))

    drawing = svg2rlg('captcha.svg')
    renderPM.drawToFile(drawing, "captcha.png", fmt="PNG")

    solver = imagecaptcha()
    solver.set_verbose(1)
    solver.set_key(api_key)
    captcha_text = solver.solve_and_return_solution("captcha.png")

    if captcha_text != 0:
        logger.debug(f"Captcha text: {captcha_text}")
    else:
        logger.debug(f"Task finished with error: {solver.error_code}")

    return captcha_text