Update handlers/common.py

This commit is contained in:
2026-03-19 12:52:45 +09:00
parent d547bcf50d
commit 609740baff

View File

@@ -2,7 +2,8 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
공통 핸들러 유틸리티 공통 핸들러 함수
- Claude API 호출
- JSON/HTML 추출 - JSON/HTML 추출
""" """
@@ -12,7 +13,7 @@ import re
import json import json
import anthropic import anthropic
# Claude API 클라이언트 # Claude API 클라이언트 설정
client = anthropic.Anthropic( client = anthropic.Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY") api_key=os.environ.get("ANTHROPIC_API_KEY")
) )
@@ -39,7 +40,7 @@ def extract_json(text: str) -> dict:
text = text.strip() text = text.strip()
# JSON 데이터 파싱 # JSON 파싱 시도
try: try:
return json.loads(text) return json.loads(text)
except json.JSONDecodeError: except json.JSONDecodeError:
@@ -65,8 +66,9 @@ def extract_html(text: str) -> str:
text = text.strip() text = text.strip()
# <!DOCTYPE 나 <html로 시작하는 부분만 추출 # <!DOCTYPE 나 <html로 시작하지 않는 경우 확인
if '<html' in text.lower(): if not text.startswith('<!DOCTYPE') and not text.startswith('<html'):
# HTML 부분만 추출
match = re.search(r'(<!DOCTYPE html[\s\S]*</html>)', text, re.IGNORECASE) match = re.search(r'(<!DOCTYPE html[\s\S]*</html>)', text, re.IGNORECASE)
if match: if match:
text = match.group(1) text = match.group(1)