|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from base64 import b64encode |
| 5 | + |
| 6 | +import aiofiles |
| 7 | + |
| 8 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) |
| 9 | + |
| 10 | +from twocaptcha import AsyncTwoCaptcha |
| 11 | + |
| 12 | +# in this example we store the API key inside environment variables that can be set like: |
| 13 | +# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS |
| 14 | +# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows |
| 15 | +# you can just set the API key directly to it's value like: |
| 16 | +# api_key="1abc234de56fab7c89012d34e56fa7b8" |
| 17 | + |
| 18 | +api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY') |
| 19 | + |
| 20 | +solver = AsyncTwoCaptcha(api_key) |
| 21 | + |
| 22 | + |
| 23 | +async def solve_captcha(): |
| 24 | + async with aiofiles.open('../images/drag_drop_main.jpeg', 'rb') as f: |
| 25 | + body_b64 = b64encode(await f.read()).decode('utf-8') |
| 26 | + |
| 27 | + async with aiofiles.open('../images/drag_drop_image1.jpeg', 'rb') as f: |
| 28 | + image1_b64 = b64encode(await f.read()).decode('utf-8') |
| 29 | + |
| 30 | + async with aiofiles.open('../images/drag_drop_image2.jpeg', 'rb') as f: |
| 31 | + image2_b64 = b64encode(await f.read()).decode('utf-8') |
| 32 | + |
| 33 | + try: |
| 34 | + return await solver.drag_and_drop(body=body_b64, |
| 35 | + images=[image1_b64, image2_b64]) |
| 36 | + except Exception as e: |
| 37 | + sys.exit(e) |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + result = asyncio.run(solve_captcha()) |
| 42 | + sys.exit('result: ' + str(result)) |
0 commit comments