# -*- coding: utf-8 -*-
"""ProcessCart 함수 추출"""

import requests
import asyncio
from playwright.async_api import async_playwright

async def extract():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        
        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')
        
        cookies = await page.context.cookies()
        await browser.close()
    
    session = requests.Session()
    for c in cookies:
        session.cookies.set(c['name'], c['value'])
    
    resp = session.get('https://gwn.geoweb.kr/bundles/order?v=PGhSOAjQ9z6ruAJgJUFuhW9tGQSiJeX6ek-ky3E-tOk1')
    content = resp.text
    
    # ProcessCart 함수 찾기
    start = content.find('function ProcessCart')
    if start > 0:
        depth = 0
        end = start
        in_func = False
        
        for i in range(start, min(start + 5000, len(content))):
            if content[i] == '{':
                depth += 1
                in_func = True
            elif content[i] == '}':
                depth -= 1
                if in_func and depth == 0:
                    end = i + 1
                    break
        
        func_content = content[start:end]
        print("="*60)
        print("ProcessCart 함수:")
        print("="*60)
        print(func_content)
    else:
        # 다른 패턴으로 찾기
        print("ProcessCart를 변수로 찾기...")
        start = content.find('ProcessCart=function')
        if start > 0:
            print(content[start:start+2000])
        else:
            # ajax 호출 찾기
            import re
            ajax_calls = re.findall(r'\$\.ajax\s*\(\s*\{[^}]{100,1000}(Cart|Order)[^}]{0,500}\}', content, re.IGNORECASE | re.DOTALL)
            print(f"\nAJAX 호출 {len(ajax_calls)}개 발견")
            
            # url 패턴 찾기
            urls = re.findall(r'url\s*:\s*["\']([^"\']+)["\']', content)
            print("\n모든 URL:")
            for url in set(urls):
                if 'Cart' in url or 'Order' in url or 'Add' in url or 'Insert' in url:
                    print(f"  {url}")

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