#!/usr/bin/env python3
"""
Simple test script to check Tokopedia access with requests
"""

import requests
from bs4 import BeautifulSoup
import time

def test_tokopedia_requests():
    """Test accessing Tokopedia with requests"""
    print("Testing Tokopedia access with requests...")
    
    # Set headers to mimic a real browser
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 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.5',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'keep-alive',
    }
    
    try:
        # Test homepage access
        print("Accessing Tokopedia homepage...")
        response = requests.get("https://www.tokopedia.com", headers=headers, timeout=30)
        print(f"Homepage status code: {response.status_code}")
        print(f"Homepage title: {response.text[:100]}...")
        
        if response.status_code == 200:
            print("Successfully accessed Tokopedia homepage!")
        else:
            print("Failed to access Tokopedia homepage")
            return False
            
        # Wait a moment
        time.sleep(2)
        
        # Test search page access
        print("\nAccessing search results...")
        search_url = "https://www.tokopedia.com/search?st=product&q=phone"
        response = requests.get(search_url, headers=headers, timeout=30)
        print(f"Search status code: {response.status_code}")
        
        if response.status_code == 200:
            print("Successfully accessed search results!")
            # Try to parse with BeautifulSoup
            soup = BeautifulSoup(response.text, 'html.parser')
            print(f"Page title: {soup.title.text if soup.title else 'No title found'}")
            return True
        else:
            print("Failed to access search results")
            return False
            
    except Exception as e:
        print(f"Error: {e}")
        return False

if __name__ == "__main__":
    success = test_tokopedia_requests()
    if success:
        print("\nTokopedia requests test completed successfully!")
    else:
        print("\nTokopedia requests test failed!")
