# -*- coding: utf-8 -*-
"""주문 확정 API 찾기 - 전체 검색"""

import requests
import asyncio
from playwright.async_api import async_playwright
import re

async def analyze():
    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'])
    
    # 모든 JS 번들 다운로드
    js_urls = [
        'https://gwn.geoweb.kr/bundles/order_product_cart?v=JPwFQ8DWaNMW1VmbtWYKTJqxT-5255z351W5iZE1qew1',
        'https://gwn.geoweb.kr/bundles/order?v=PGhSOAjQ9z6ruAJgJUFuhW9tGQSiJeX6ek-ky3E-tOk1',
        'https://gwn.geoweb.kr/bundles/javascript?v=Tn_AqbA-PX_uu3d0zjfQOYS6NPSDLtOVqjW95a949Ow1'
    ]
    
    all_content = ""
    for url in js_urls:
        resp = session.get(url)
        all_content += resp.text + "\n"
    
    print(f"총 JS 길이: {len(all_content)}")
    
    # 모든 ajax POST URL 찾기
    print("\n" + "="*60)
    print("모든 POST URL:")
    print("="*60)
    
    # $.ajax 패턴
    ajax_patterns = re.findall(r'\$\.ajax\s*\(\s*\{[^}]*url\s*:\s*["\']([^"\']+)["\'][^}]*type\s*:\s*["\']POST["\']', all_content, re.IGNORECASE | re.DOTALL)
    ajax_patterns += re.findall(r'\$\.ajax\s*\(\s*\{[^}]*type\s*:\s*["\']POST["\'][^}]*url\s*:\s*["\']([^"\']+)["\']', all_content, re.IGNORECASE | re.DOTALL)
    
    for url in set(ajax_patterns):
        print(url)
    
    # 주문저장, 저장 관련
    print("\n" + "="*60)
    print("저장/주문 관련 키워드:")
    print("="*60)
    
    keywords = ['주문저장', '저장', 'save', 'submit', 'confirm', 'order', 'insert']
    for kw in keywords:
        matches = re.findall(rf'.{{50}}{kw}.{{50}}', all_content, re.IGNORECASE)
        if matches:
            print(f"\n--- {kw} ---")
            for m in matches[:3]:
                print(m.replace('\n', ' ')[:100])
    
    # 버튼 onclick 찾기
    print("\n" + "="*60)
    print("주문저장 버튼:")
    print("="*60)
    
    save_btn = re.findall(r'주문저장.{0,200}', all_content)
    for s in save_btn[:5]:
        print(s[:150])

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