# -*- coding: utf-8 -*-
"""지오영 API 엔드포인트 분석 - 간단 버전"""

import asyncio
from playwright.async_api import async_playwright

async def analyze():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        
        # 모든 요청 로깅
        all_requests = []
        
        def log_request(request):
            all_requests.append({
                'url': request.url,
                'method': request.method,
                'data': request.post_data
            })
        
        page.on('request', log_request)
        
        # 로그인
        await page.goto('https://gwn.geoweb.kr/Member/Login')
        await page.fill('input[type="text"]', '7390')
        await page.fill('input[type="password"]', 'trajet6640')
        await page.click('button, input[type="submit"]')
        await page.wait_for_load_state('networkidle')
        
        # 메인 페이지 HTML 분석
        await page.goto('https://gwn.geoweb.kr/Home/Index')
        await page.wait_for_load_state('networkidle')
        
        # JavaScript에서 API 엔드포인트 찾기
        js_content = await page.content()
        
        await browser.close()
        
        # POST 요청만 필터
        print("="*60)
        print("POST 요청들:")
        print("="*60)
        for r in all_requests:
            if r['method'] == 'POST':
                print(f"URL: {r['url']}")
                if r['data']:
                    print(f"Data: {r['data'][:300]}")
                print()
        
        # HTML에서 API 힌트 찾기
        print("="*60)
        print("HTML에서 발견된 API 관련 패턴:")
        print("="*60)
        
        import re
        # ajax, fetch, url 패턴 찾기
        patterns = [
            r'url:\s*[\'"]([^"\']+)[\'"]',
            r'action=[\'"]([^"\']+)[\'"]',
            r'\.post\([\'"]([^"\']+)[\'"]',
            r'\.get\([\'"]([^"\']+)[\'"]',
            r'fetch\([\'"]([^"\']+)[\'"]',
        ]
        
        found_urls = set()
        for pattern in patterns:
            matches = re.findall(pattern, js_content)
            for m in matches:
                if 'Order' in m or 'Cart' in m or 'Add' in m or 'Product' in m:
                    found_urls.add(m)
        
        for url in sorted(found_urls):
            print(url)

if __name__ == "__main__":
    asyncio.run(analyze())
