Anti Captcha avatar
Anti Captcha
Try for free

No credit card required

View all Actors
Anti Captcha

Anti Captcha

petr_cermak/anti-captcha
Try for free

No credit card required

Dockerfile

1# Dockerfile contains instructions how to build a Docker image that
2# will contain all the code and configuration needed to run your actor.
3# For a full Dockerfile reference,
4# see https://docs.docker.com/engine/reference/builder/
5
6# First, specify the base Docker image. Apify provides the following
7# base images for your convenience:
8#  apify/actor-node-basic (Node.js on Alpine Linux, small and fast)
9#  apify/actor-node-chrome (Node.js + Chrome on Debian)
10#  apify/actor-node-chrome-xvfb (Node.js + Chrome + Xvfb on Debian)
11# For more information, see https://docs.apify.com/actor/build#base-images
12# Note that you can use any other image from Docker Hub.
13FROM apify/actor-node-basic
14
15# Second, copy just package.json since it should be the only file
16# that affects "npm install" in the next step, to speed up the build
17COPY package.json ./
18
19# Install NPM packages, skip optional and development dependencies to
20# keep the image small. Avoid logging too much and print the dependency
21# tree for debugging
22RUN npm --quiet set progress=false \
23 && npm install --only=prod --no-optional \
24 && echo "Installed NPM packages:" \
25 && npm list || true \
26 && echo "Node.js version:" \
27 && node --version \
28 && echo "NPM version:" \
29 && npm --version
30
31# Next, copy the remaining files and directories with the source code.
32# Since we do this after NPM install, quick build will be really fast
33# for most source file changes.
34COPY . ./
35
36# Optionally, specify how to launch the source code of your actor.
37# By default, Apify's base Docker images define the CMD instruction
38# that runs the Node.js source code using the command specified
39# in the "scripts.start" section of the package.json file.
40# In short, the instruction looks something like this:
41#
42# CMD npm start

main.js

1const Apify = require('apify');
2const fetch = require('node-fetch');
3
4const postJson = async (url, body) => {
5    const opt = {
6        method: 'POST',
7        body: JSON.stringify(body),
8        headers: {'Content-Type': 'application/json'}
9    };
10    const response = await fetch(url, opt);
11    return await response.json();
12};
13
14class AntiCaptchaTask {
15    
16    constructor(input){
17        this.input = input;
18        this.taskId = null;
19        this.clientKey = input.clientKey;
20    }
21    
22    async create(){
23        const data = await postJson('http://api.anti-captcha.com/createTask', this.input);
24        if(data.errorId > 0){throw new Error(data.errorDescription);}
25        this.taskId = data.taskId;
26        return data.taskId;
27    }
28    
29    async getResult(){
30        const body = {
31            taskId: this.taskId,
32            clientKey: this.clientKey,
33        };
34        const data = await postJson('http://api.anti-captcha.com/getTaskResult', body);
35        if(data.errorId > 0){throw new Error(data.errorDescription);}
36        return data;
37    }
38
39    async waitForResult(timeout = 600000){
40        return new Promise((resolve, reject) => {
41            const startedAt = new Date();
42            const waitLoop = () => {
43                if((new Date() - startedAt) > timeout){
44                    reject(new Error("Waiting for result timed out."));
45                }
46                this.getResult().then((response) => {
47                    if(response.errorId !== 0){
48                        reject(new Error(response.errorDescription));
49                    } 
50                    else{
51                        console.log(response);
52                        if(response.status === 'ready'){resolve(response);}
53                        else{
54                            console.log('...');
55                            setTimeout(waitLoop, 1000);
56                        }
57                    }
58                }).catch((e) => reject(e));
59            };
60            waitLoop();
61        });
62    }
63    
64}
65
66Apify.main(async () => {
67    
68    const input = await Apify.getValue('INPUT');
69    if(!input.clientKey){
70        throw new Error('ERROR: missing "clientKey" attribute in INPUT');
71    }
72    if(!input.task){
73        throw new Error('ERROR: missing "task" attribute in INPUT');
74    }
75    if(!input.task.type){
76        throw new Error('ERROR: missing "task.type" attribute in INPUT');
77    }
78    
79    console.log('Solving captcha, type: ' + input.task.type);
80
81    const task = new AntiCaptchaTask(input);
82    console.log('Creating AntiCaptcha task...');
83    await task.create();
84    console.log('Waiting for result...');
85    const result = await task.waitForResult();
86    console.log('Captcha solving succesful.');
87    await Apify.setValue('OUTPUT', result.solution);
88    console.log('Solution: ');
89    console.dir(result.solution);
90});

package.json

1{
2    "name": "anti-captcha",
3    "version": "0.0.1",
4    "dependencies": {
5        "apify": "^1.0.0",
6        "node-fetch": "^2.6.1"
7    },
8    "scripts": {
9        "start": "node main.js"
10    },
11    "author": "Petr Cermak"
12}
Developer
Maintained by Community
Actor metrics
  • 1 monthly users
  • 0.0% runs succeeded
  • 0.0 days response time
  • Created in Feb 2021
  • Modified about 3 years ago
Categories