HerzaJ commited on
Commit
9553ab4
Β·
verified Β·
1 Parent(s): 4c1dc8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -68,7 +68,7 @@ def download_chromedriver():
68
  driver_path = os.path.join(driver_dir, "chromedriver")
69
 
70
  if os.path.exists(driver_path):
71
- print(f"βœ… ChromeDriver already exists: {driver_path}")
72
  return driver_path
73
 
74
  os.makedirs(driver_dir, exist_ok=True)
@@ -80,7 +80,7 @@ def download_chromedriver():
80
  print(f"πŸ” ChromeDriver version: {driver_version}")
81
 
82
  download_url = f"https://storage.googleapis.com/chrome-for-testing-public/{driver_version}/linux64/chromedriver-linux64.zip"
83
- print(f"⬇️ Downloading from: {download_url}")
84
 
85
  zip_path = os.path.join(driver_dir, "chromedriver.zip")
86
  response = requests.get(download_url, timeout=60)
@@ -151,13 +151,24 @@ def create_driver():
151
  options.add_argument('--disable-gpu')
152
  options.add_argument('--window-size=1920,1080')
153
  options.add_argument('--disable-blink-features=AutomationControlled')
 
 
 
 
 
154
  options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
155
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
156
  options.add_experimental_option('useAutomationExtension', False)
 
157
 
158
  service = Service(driver_path)
 
 
159
  driver = webdriver.Chrome(service=service, options=options)
160
  driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
 
 
 
161
  print("βœ… Driver ready")
162
  return driver
163
 
@@ -275,17 +286,23 @@ def detect_challenge_type(driver) -> str:
275
  except:
276
  return "unknown"
277
 
278
- def screenshot_and_upload(driver, element) -> str:
279
  try:
280
  png = element.screenshot_as_png()
 
281
  temp_dir = tempfile.gettempdir()
282
- temp_path = os.path.join(temp_dir, f"hcaptcha_{int(time.time())}.png")
283
 
284
  with open(temp_path, 'wb') as f:
285
  f.write(png)
286
 
287
  url = upload_to_tmpfiles(temp_path)
288
- os.remove(temp_path)
 
 
 
 
 
289
  return url
290
  except Exception as e:
291
  print(f"❌ Screenshot error: {e}")
@@ -313,7 +330,7 @@ def solve_hcaptcha(sitekey: str, url: str) -> Dict:
313
  if not checkbox_iframe:
314
  return {'success': False, 'error': 'Checkbox not found'}
315
 
316
- screenshot_url = screenshot_and_upload(driver, checkbox_iframe)
317
  if screenshot_url:
318
  screenshot_urls.append({'type': 'checkbox', 'url': screenshot_url})
319
 
@@ -338,7 +355,7 @@ def solve_hcaptcha(sitekey: str, url: str) -> Dict:
338
  else:
339
  return {'success': False, 'error': 'No token', 'screenshots': screenshot_urls}
340
 
341
- screenshot_url = screenshot_and_upload(driver, challenge_iframe)
342
  if screenshot_url:
343
  screenshot_urls.append({'type': 'challenge', 'url': screenshot_url})
344
 
@@ -378,10 +395,16 @@ def solve_hcaptcha(sitekey: str, url: str) -> Dict:
378
  return {'success': False, 'error': 'No token', 'screenshots': screenshot_urls}
379
 
380
  except Exception as e:
381
- return {'success': False, 'error': str(e), 'screenshots': screenshot_urls}
 
 
 
382
  finally:
383
  if driver:
384
- driver.quit()
 
 
 
385
 
386
  def extract_token(driver):
387
  try:
@@ -428,7 +451,7 @@ def health():
428
  def root():
429
  return jsonify({
430
  'service': 'hCaptcha Solver',
431
- 'version': '2.2',
432
  'endpoints': {
433
  '/solve': 'GET ?sitekey=X&url=Y',
434
  '/health': 'GET'
@@ -436,6 +459,6 @@ def root():
436
  })
437
 
438
  if __name__ == '__main__':
439
- print("\nπŸ€– hCaptcha Solver API v2.2")
440
  print("🌐 http://0.0.0.0:7860\n")
441
  app.run(host='0.0.0.0', port=7860, debug=False)
 
68
  driver_path = os.path.join(driver_dir, "chromedriver")
69
 
70
  if os.path.exists(driver_path):
71
+ print(f"βœ… ChromeDriver exists: {driver_path}")
72
  return driver_path
73
 
74
  os.makedirs(driver_dir, exist_ok=True)
 
80
  print(f"πŸ” ChromeDriver version: {driver_version}")
81
 
82
  download_url = f"https://storage.googleapis.com/chrome-for-testing-public/{driver_version}/linux64/chromedriver-linux64.zip"
83
+ print(f"⬇️ Downloading...")
84
 
85
  zip_path = os.path.join(driver_dir, "chromedriver.zip")
86
  response = requests.get(download_url, timeout=60)
 
151
  options.add_argument('--disable-gpu')
152
  options.add_argument('--window-size=1920,1080')
153
  options.add_argument('--disable-blink-features=AutomationControlled')
154
+ options.add_argument('--disable-web-security')
155
+ options.add_argument('--disable-features=IsolateOrigins,site-per-process')
156
+ options.add_argument('--allow-running-insecure-content')
157
+ options.add_argument('--disable-setuid-sandbox')
158
+ options.add_argument('--disable-software-rasterizer')
159
  options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
160
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
161
  options.add_experimental_option('useAutomationExtension', False)
162
+ options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
163
 
164
  service = Service(driver_path)
165
+ service.log_path = '/dev/null'
166
+
167
  driver = webdriver.Chrome(service=service, options=options)
168
  driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
169
+
170
+ driver.set_window_size(1920, 1080)
171
+
172
  print("βœ… Driver ready")
173
  return driver
174
 
 
286
  except:
287
  return "unknown"
288
 
289
+ def screenshot_and_upload(element) -> str:
290
  try:
291
  png = element.screenshot_as_png()
292
+
293
  temp_dir = tempfile.gettempdir()
294
+ temp_path = os.path.join(temp_dir, f"hcaptcha_{int(time.time())}_{random.randint(1000,9999)}.png")
295
 
296
  with open(temp_path, 'wb') as f:
297
  f.write(png)
298
 
299
  url = upload_to_tmpfiles(temp_path)
300
+
301
+ try:
302
+ os.remove(temp_path)
303
+ except:
304
+ pass
305
+
306
  return url
307
  except Exception as e:
308
  print(f"❌ Screenshot error: {e}")
 
330
  if not checkbox_iframe:
331
  return {'success': False, 'error': 'Checkbox not found'}
332
 
333
+ screenshot_url = screenshot_and_upload(checkbox_iframe)
334
  if screenshot_url:
335
  screenshot_urls.append({'type': 'checkbox', 'url': screenshot_url})
336
 
 
355
  else:
356
  return {'success': False, 'error': 'No token', 'screenshots': screenshot_urls}
357
 
358
+ screenshot_url = screenshot_and_upload(challenge_iframe)
359
  if screenshot_url:
360
  screenshot_urls.append({'type': 'challenge', 'url': screenshot_url})
361
 
 
395
  return {'success': False, 'error': 'No token', 'screenshots': screenshot_urls}
396
 
397
  except Exception as e:
398
+ import traceback
399
+ error_detail = traceback.format_exc()
400
+ print(f"❌ Error: {error_detail}")
401
+ return {'success': False, 'error': str(e), 'error_detail': error_detail, 'screenshots': screenshot_urls}
402
  finally:
403
  if driver:
404
+ try:
405
+ driver.quit()
406
+ except:
407
+ pass
408
 
409
  def extract_token(driver):
410
  try:
 
451
  def root():
452
  return jsonify({
453
  'service': 'hCaptcha Solver',
454
+ 'version': '2.3',
455
  'endpoints': {
456
  '/solve': 'GET ?sitekey=X&url=Y',
457
  '/health': 'GET'
 
459
  })
460
 
461
  if __name__ == '__main__':
462
+ print("\nπŸ€– hCaptcha Solver API v2.3")
463
  print("🌐 http://0.0.0.0:7860\n")
464
  app.run(host='0.0.0.0', port=7860, debug=False)