from flask import Flask, jsonify
import requests
import re
from urllib.parse import urlparse, parse_qs

app = Flask(__name__)

def recaptcha_bypass():
    site_key = "6LcGDr8UAAAAAE-XU-YHARKMIe7L5vz_TIfEZua5"
    base_url = "https://www.google.com/recaptcha/api2/"
    anchor_url = (f"{base_url}anchor?ar=1&k={site_key}&co="
                  "aHR0cHM6Ly93d3cudHVybmEuY29tOjQ0Mw..&hl=tr&v=GUGrl5YkSwqiWrzO3ShIKDlu&size=invisible&cb=wshbqvc87pwx")
    reload_url = f"{base_url}reload?k={site_key}"

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
    }

    response = requests.get(anchor_url, headers=headers)
    token_match = re.search(r'value="([^"]+)"', response.text)
    if not token_match:
        raise Exception("Anchor token bulunamadı!")
    token = token_match.group(1)

    parsed_url = urlparse(anchor_url)
    params = parse_qs(parsed_url.query)

    data = {
        'v': params['v'][0],
        'reason': 'q',
        'c': token,
        'k': params['k'][0],
        'co': params['co'][0],
        'hl': 'tr',
        'size': 'invisible'
    }

    headers.update({
        "Referer": response.url,
        "Content-Type": "application/x-www-form-urlencoded"
    })

    reload_response = requests.post(reload_url, headers=headers, data=data)
    rresp_match = re.search(r'\["rresp","([^"]+)"', reload_response.text)
    if not rresp_match:
        raise Exception("reCAPTCHA yanıtı parse edilemedi!")

    recaptcha_token = rresp_match.group(1)
    return recaptcha_token

@app.route('/get_recaptcha', methods=['GET'])
def get_recaptcha():
    try:
        token = recaptcha_bypass()
        return jsonify({"status": "success", "recaptcha_token": token})
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5050)