import requests

# SearXNG instance URL
searxng_url = "http://localhost:8080/search"

# Test query
query = "python programming"

# Parameters for the search - using HTML format instead of JSON
params = {"q": query, "format": "html"}

# Browser-like headers
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.9",
    "Referer": "http://localhost:8080/",
    "Connection": "keep-alive",
}

try:
    # First get cookies from the main page
    session = requests.Session()
    session.get("http://localhost:8080/")

    # Send the search request with cookies
    response = session.get(searxng_url, params=params, headers=headers)

    # Check status
    if response.status_code == 200:
        print(f"SearXNG is working! Got HTML response of {len(response.text)} bytes")
        print("Check your SearXNG web interface at http://localhost:8080")
    else:
        print(f"Error: Got status code {response.status_code}")
        print(response.text[:500])  # Show just the first 500 chars

except Exception as e:
    print(f"Error connecting to SearXNG: {e}")
    print("\nIs your SearXNG container running? Check with: docker ps | grep searxng")
