-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (81 loc) · 3.22 KB
/
main.py
File metadata and controls
106 lines (81 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import time
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# CapSolver API configuration
CAPSOLVER_API_KEY = os.getenv("CAPSOLVER_API_KEY")
CREATE_TASK_URL = "https://api.capsolver.com/createTask"
GET_RESULT_URL = "https://api.capsolver.com/getTaskResult"
# Target website configuration
# Replace with your target site URL
SITE_URL = "https://example.com"
# IMPORTANT: Proxy is REQUIRED for Cloudflare Challenge
# Use Static or Sticky proxy only (rotating proxies not supported)
# Format: ip:port:user:pass
PROXY = os.getenv("PROXY", "ip:port:user:pass")
# User agent should match what you use in your requests
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
def create_task():
"""Create a Cloudflare Challenge solving task."""
payload = {
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "AntiCloudflareTask",
"websiteURL": SITE_URL,
"proxy": PROXY,
# Optional parameters:
# "userAgent": USER_AGENT,
# "html": "<!DOCTYPE html>..." # The "Just a moment..." page HTML
}
}
response = requests.post(CREATE_TASK_URL, json=payload)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"Failed to create task: {result.get('errorDescription')}")
return result.get("taskId")
def get_task_result(task_id):
"""Poll for the task result until it's ready."""
payload = {
"clientKey": CAPSOLVER_API_KEY,
"taskId": task_id
}
while True:
response = requests.post(GET_RESULT_URL, json=payload)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"Failed to get result: {result.get('errorDescription')}")
status = result.get("status")
if status == "ready":
return result.get("solution")
elif status == "failed":
raise Exception("Task failed")
elif status == "processing":
print("Task is still processing, waiting...")
time.sleep(2)
else:
raise Exception(f"Unknown status: {status}")
def main():
if not CAPSOLVER_API_KEY:
print("Error: CAPSOLVER_API_KEY not found in .env file")
print("Please create a .env file with your API key:")
print("CAPSOLVER_API_KEY=your_api_key_here")
return
print("Creating Cloudflare Challenge solving task...")
print(f"Site URL: {SITE_URL}")
print(f"Proxy: {PROXY}")
task_id = create_task()
print(f"Task created successfully! Task ID: {task_id}")
print("Waiting for solution...")
solution = get_task_result(task_id)
print("\nSolution received!")
print(f"cf_clearance: {solution.get('cookies', {}).get('cf_clearance')}")
print(f"Token: {solution.get('token')}")
print(f"User Agent: {solution.get('userAgent')}")
# Use these values in your requests:
# cookies = {"cf_clearance": solution["cookies"]["cf_clearance"]}
# headers = {"User-Agent": solution["userAgent"]}
# response = requests.get(SITE_URL, cookies=cookies, headers=headers)
if __name__ == "__main__":
main()