Skip to content

Commit 812021c

Browse files
committed
Add examples and fix FunCaptchaTask
1 parent 22b729c commit 812021c

8 files changed

Lines changed: 111 additions & 23 deletions

File tree

.github/workflows/e2e.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ jobs:
2727
run: make test
2828
env:
2929
KEY: ${{ secrets.anticaptcha_key }}
30+
PROXY_URL: "${{ secrets.proxy_url }}

CHANGELOG.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Changelog
22
=========
33

4+
0.7.1 - 2020-07-17
5+
------------------
6+
7+
Added
8+
#####
9+
10+
- Added examples for proxy mode including `hcaptcha_request_proxy`
11+
12+
Changed
13+
#######
14+
15+
- Fix inheritance of `FunCaptchaTask`
16+
- Added `FunCaptchaTask` to e2e tests
17+
418
0.7.0 - 2020-06-08
519
------------------
620

examples/funcaptcha_request.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from urllib.parse import urlparse
2-
1+
from six.moves.urllib import parse
32
import requests
43
from os import environ
54
import re
@@ -8,8 +7,8 @@
87
from python_anticaptcha import AnticaptchaClient, FunCaptchaTask
98

109
api_key = environ["KEY"]
11-
site_key_pattern = 'data-pkey="(.+?)"'
12-
url = "https://www.funcaptcha.com/demo/"
10+
site_key_pattern = 'public_key: "(.+?)",'
11+
url = "https://client-demo.arkoselabs.com/solo-animals"
1312
client = AnticaptchaClient(api_key)
1413
session = requests.Session()
1514

@@ -18,11 +17,11 @@
1817
"(KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
1918
)
2019
session.headers = {"User-Agent": UA}
21-
proxy_urls = environ["PROXY_URL"].split(",")
20+
proxy_url = environ["PROXY_URL"]
2221

2322

2423
def parse_url(url):
25-
parsed = urlparse(url)
24+
parsed = parse.urlparse(url)
2625
return dict(
2726
proxy_type=parsed.scheme,
2827
proxy_address=parsed.hostname,
@@ -37,11 +36,10 @@ def get_form_html():
3736

3837

3938
def get_token(form_html):
40-
proxy_url = choice(proxy_urls)
4139
proxy = parse_url(proxy_url)
4240

4341
site_key = re.search(site_key_pattern, form_html).group(1)
44-
task = FunCaptchaTask(url, site_key, proxy=proxy, user_agent=UA)
42+
task = FunCaptchaTask(url, site_key, user_agent=UA, **proxy)
4543
job = client.createTask(task)
4644
job.join(maximum_time=10 ** 4)
4745
return job.get_token_response()
@@ -53,4 +51,4 @@ def process():
5351

5452

5553
if __name__ == "__main__":
56-
print(process())
54+
print('Solved!' in process())

examples/funcaptcha_selenium.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from urllib.parse import urlparse, quote
1+
from six.moves.urllib.parse import quote
2+
from six.moves.urllib import parse
23

34
import requests
45
from os import environ
@@ -19,7 +20,7 @@
1920

2021

2122
def parse_url(url):
22-
parsed = urlparse(url)
23+
parsed = parse.urlparse(url)
2324
return dict(
2425
proxy_type=parsed.scheme,
2526
proxy_address=parsed.hostname,

examples/funcaptcha_selenium_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
import time
3-
from urllib.parse import urlparse, quote
3+
from six.moves.urllib.parse import quote
44
import os
55
from os import environ
66
import gzip

examples/hcaptcha_request_proxy.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from six.moves.urllib import parse
2+
3+
import re
4+
import requests
5+
from os import environ
6+
7+
from python_anticaptcha import AnticaptchaClient, HCaptchaTask
8+
9+
api_key = environ["KEY"]
10+
proxy_url = environ["PROXY_URL"] # eg. socks5://user:password/123.123.123.123:8888/
11+
site_key_pattern = 'data-sitekey="(.+?)"'
12+
url = "http://hcaptcha.jawne.info.pl/"
13+
client = AnticaptchaClient(api_key)
14+
session = requests.Session()
15+
EXPECTED_RESULT = "Your request have submitted successfully."
16+
17+
UA = (
18+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 "
19+
"(KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
20+
)
21+
22+
23+
def parse_url(url):
24+
parsed = parse.urlparse(url)
25+
return dict(
26+
proxy_type=parsed.scheme,
27+
proxy_address=parsed.hostname,
28+
proxy_port=parsed.port,
29+
proxy_login=parsed.username,
30+
proxy_password=parsed.password,
31+
)
32+
33+
34+
def get_form_html():
35+
return session.get(url).text
36+
37+
38+
def get_token(form_html):
39+
site_key = re.search(site_key_pattern, form_html).group(1)
40+
proxy = parse_url(proxy_url)
41+
task = HCaptchaTask(
42+
website_url=url,
43+
website_key=site_key,
44+
user_agent=UA,
45+
cookies="test=test",
46+
**proxy
47+
)
48+
job = client.createTask(task)
49+
job.join()
50+
return job.get_solution_response()
51+
52+
53+
def form_submit(token):
54+
return requests.post(url, data={"g-recaptcha-response": token}).text
55+
56+
57+
def process():
58+
html = get_form_html()
59+
token = get_token(html)
60+
return form_submit(token)
61+
62+
63+
if __name__ == "__main__":
64+
assert EXPECTED_RESULT in process()

python_anticaptcha/tasks.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def serialize(self, **result):
9595
return result
9696

9797

98-
class FunCaptchaTask(ProxyMixin, NoCaptchaTaskProxylessTask):
98+
class FunCaptchaTask(ProxyMixin, FunCaptchaProxylessTask):
9999
type = "FunCaptchaTask"
100100

101101

@@ -197,12 +197,13 @@ class HCaptchaTaskProxyless(BaseTask):
197197
websiteURL = None
198198
websiteKey = None
199199

200-
def __init__(self, website_url, website_key):
200+
def __init__(self, website_url, website_key, *args, **kwargs):
201201
self.websiteURL = website_url
202202
self.websiteKey = website_key
203+
super(HCaptchaTaskProxyless, self).__init__(*args, **kwargs)
203204

204-
def serialize(self):
205-
data = super(HCaptchaTaskProxyless, self).serialize()
205+
def serialize(self, **result):
206+
data = super(HCaptchaTaskProxyless, self).serialize(**result)
206207
data["type"] = self.type
207208
data["websiteURL"] = self.websiteURL
208209
data["websiteKey"] = self.websiteKey

tests/test_examples.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def missing_key(*args, **kwargs):
1616
)(*args, **kwargs)
1717

1818

19+
def missing_proxy(*args, **kwargs):
20+
return skipIf(
21+
"PROXY_URL" not in os.environ, "Missing PROXY_URL environment variable"
22+
)(*args, **kwargs)
23+
24+
1925
@missing_key
2026
class CustomDotTestCase(TestCase):
2127
# For unknown reasons, workers are not always
@@ -51,15 +57,15 @@ def test_process_bulk(self):
5157

5258

5359
@missing_key
54-
@skipIf("PROXY_URL" not in os.environ, "Missing PROXY_URL environment variable")
60+
@missing_proxy
5561
class FuncaptchaTestCase(TestCase):
5662
# CI Proxy is unstable.
5763
# Occasionally fails, so I repeat my attempt to have others selected.
5864
@retry(tries=3)
5965
def test_funcaptcha(self):
60-
from examples import funcaptcha
66+
from examples import funcaptcha_request
6167

62-
self.assertTrue(funcaptcha.process())
68+
self.assertIn('Solved!', funcaptcha_request.process())
6369

6470

6571
@missing_key
@@ -73,7 +79,8 @@ def test_process(self):
7379

7480

7581
@missing_key
76-
class RecaptchaV3TestCase(TestCase):
82+
@skipIf(True, "Anti-captcha unable to provide required score, but we tests via proxy")
83+
class RecaptchaV3ProxylessTestCase(TestCase):
7784
# Anticaptcha responds is not fully reliable.
7885
@retry(tries=3)
7986
def test_process(self):
@@ -114,6 +121,7 @@ def test_process(self):
114121

115122

116123
@missing_key
124+
@skipIf(True, 'We testing via proxy for performance reason.')
117125
class HCaptchaTaskProxylessTestCase(TestCase):
118126
@retry(tries=3)
119127
def test_process(self):
@@ -123,12 +131,13 @@ def test_process(self):
123131

124132

125133
@missing_key
126-
class HCaptchaTaskProxylessTestCase(TestCase):
134+
class HCaptchaTaskTestCase(TestCase):
127135
def test_process(self):
128-
from examples import hcaptcha_request
136+
from examples import hcaptcha_request_proxy
129137

130138
self.assertIn(
131-
"Your request have submitted successfully.", hcaptcha_request.process()
139+
"Your request have submitted successfully.",
140+
hcaptcha_request_proxy.process(),
132141
)
133142

134143

0 commit comments

Comments
 (0)