#!/usr/bin/env python3
"""
Test script to check Tokopedia access with different approach
"""

import requests
import time

def test_tokopedia_access():
    """Test accessing Tokopedia with different settings"""
    print("Testing Tokopedia access with modified settings...")
    
    # Set headers to mimic a real browser more closely
    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',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Cache-Control': 'max-age=0'
    }
    
    try:
        # Test homepage access with shorter timeout
        print("Accessing Tokopedia homepage...")
        response = requests.get("https://www.tokopedia.com", headers=headers, timeout=15)
        print(f"Homepage status code: {response.status_code}")
        
        if response.status_code == 200:
            print("Successfully accessed Tokopedia homepage!")
            print(f"Content length: {len(response.text)} characters")
            return True
        elif response.status_code == 403:
            print("Access forbidden (403) - likely blocked by Tokopedia")
            return False
        elif response.status_code == 429:
            print("Too many requests (429) - rate limited")
            return False
        else:
            print(f"Unexpected status code: {response.status_code}")
            return False
            
    except requests.exceptions.Timeout:
        print("Request timed out - Tokopedia may be blocking requests from this IP")
        return False
    except requests.exceptions.ConnectionError:
        print("Connection error - network issue or site blocking requests")
        return False
    except Exception as e:
        print(f"Unexpected error: {e}")
        return False

if __name__ == "__main__":
    success = test_tokopedia_access()
    if success:
        print("\nTokopedia access test completed successfully!")
    else:
        print("\nTokopedia access test failed!")
        print("\nNote: This is likely because Tokopedia blocks requests from VPS/server IPs")
        print("to prevent scraping. This is a common anti-bot measure.")