Upload 15 files
Browse files- api_utils.py +253 -0
- app.py +722 -555
- category_analysis.py +1029 -0
- export_utils.py +510 -0
- keyword_analysis.py +1687 -0
- keyword_analysis_report.css +422 -0
- keyword_diversity_fix.py +918 -0
- keyword_processor.py +388 -0
- keyword_search.py +194 -0
- product_search.py +430 -0
- requirements.txt +9 -5
- style.css +1 -6
- text_utils.py +227 -0
- trend_analysis.py +323 -0
- trend_analysis_v2.py +1128 -0
api_utils.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import time
|
| 11 |
+
import hmac
|
| 12 |
+
import hashlib
|
| 13 |
+
import base64
|
| 14 |
+
import requests
|
| 15 |
+
import threading
|
| 16 |
+
import random
|
| 17 |
+
import google.generativeai as genai
|
| 18 |
+
import logging
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
# ํ๊ฒฝ๋ณ์์์ API ์ค์ ๋ก๋
|
| 23 |
+
def get_api_configs():
|
| 24 |
+
# ํ๊ฒฝ๋ณ์ 'API_CONFIGS'์์ ์ ์ฒด ์ค์ ์ ๊ฐ์ ธ์ด
|
| 25 |
+
api_configs_str = os.getenv('API_CONFIGS', '')
|
| 26 |
+
|
| 27 |
+
if not api_configs_str:
|
| 28 |
+
logger.error("API_CONFIGS ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 29 |
+
return [], [], [], []
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# ํ๊ฒฝ๋ณ์ ๊ฐ์ exec๋ก ์คํํ์ฌ ์ค์ ๋ก๋
|
| 33 |
+
local_vars = {}
|
| 34 |
+
exec(api_configs_str, {}, local_vars)
|
| 35 |
+
|
| 36 |
+
return (
|
| 37 |
+
local_vars.get('NAVER_API_CONFIGS', []),
|
| 38 |
+
local_vars.get('NAVER_SHOPPING_CONFIGS', []),
|
| 39 |
+
local_vars.get('NAVER_DATALAB_CONFIGS', []),
|
| 40 |
+
local_vars.get('GEMINI_API_CONFIGS', [])
|
| 41 |
+
)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"ํ๊ฒฝ๋ณ์ ํ์ฑ ์ค๋ฅ: {e}")
|
| 44 |
+
return [], [], [], []
|
| 45 |
+
|
| 46 |
+
# API ์ค์ ๋ก๋
|
| 47 |
+
NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS = get_api_configs()
|
| 48 |
+
|
| 49 |
+
# ์์ฐจ ์ฌ์ฉ์ ์ํ ์ธ๋ฑ์ค์ ๋ฝ
|
| 50 |
+
current_api_index = 0
|
| 51 |
+
current_shopping_api_index = 0
|
| 52 |
+
current_datalab_api_index = 0
|
| 53 |
+
current_gemini_api_index = 0
|
| 54 |
+
api_lock = threading.Lock()
|
| 55 |
+
shopping_lock = threading.Lock()
|
| 56 |
+
datalab_lock = threading.Lock()
|
| 57 |
+
gemini_lock = threading.Lock()
|
| 58 |
+
|
| 59 |
+
# Gemini ๋ชจ๋ธ ์บ์
|
| 60 |
+
_gemini_models = {}
|
| 61 |
+
|
| 62 |
+
# API ์ค์ ์ด๊ธฐํ ํจ์ ์ถ๊ฐ
|
| 63 |
+
def initialize_api_configs():
|
| 64 |
+
"""API ์ค์ ์ ์ด๊ธฐํํ๊ณ ๋๋คํ๊ฒ ์ ๋ ฌ"""
|
| 65 |
+
global NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS
|
| 66 |
+
|
| 67 |
+
# API ์ค์ ์ ๋ค์ ๋ก๋
|
| 68 |
+
NAVER_API_CONFIGS, NAVER_SHOPPING_CONFIGS, NAVER_DATALAB_CONFIGS, GEMINI_API_CONFIGS = get_api_configs()
|
| 69 |
+
|
| 70 |
+
# API ์ค์ ์ ๋๋คํ๊ฒ ์๊ธฐ
|
| 71 |
+
if NAVER_API_CONFIGS:
|
| 72 |
+
random.shuffle(NAVER_API_CONFIGS)
|
| 73 |
+
if NAVER_SHOPPING_CONFIGS:
|
| 74 |
+
random.shuffle(NAVER_SHOPPING_CONFIGS)
|
| 75 |
+
if NAVER_DATALAB_CONFIGS:
|
| 76 |
+
random.shuffle(NAVER_DATALAB_CONFIGS)
|
| 77 |
+
if GEMINI_API_CONFIGS:
|
| 78 |
+
random.shuffle(GEMINI_API_CONFIGS)
|
| 79 |
+
|
| 80 |
+
print(f"API ์ค์ ์ด๊ธฐํ ์๋ฃ:")
|
| 81 |
+
print(f" - ๊ฒ์๊ด๊ณ API: {len(NAVER_API_CONFIGS)}๊ฐ")
|
| 82 |
+
print(f" - ์ผํ API: {len(NAVER_SHOPPING_CONFIGS)}๊ฐ")
|
| 83 |
+
print(f" - ๋ฐ์ดํฐ๋ฉ API: {len(NAVER_DATALAB_CONFIGS)}๊ฐ")
|
| 84 |
+
print(f" - Gemini API: {len(GEMINI_API_CONFIGS)}๊ฐ")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def generate_signature(timestamp, method, uri, secret_key):
|
| 88 |
+
"""์๊ทธ๋์ฒ ์์ฑ ํจ์"""
|
| 89 |
+
message = f"{timestamp}.{method}.{uri}"
|
| 90 |
+
digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest()
|
| 91 |
+
return base64.b64encode(digest).decode()
|
| 92 |
+
|
| 93 |
+
def get_header(method, uri, api_key, secret_key, customer_id):
|
| 94 |
+
"""API ํค๋ ์์ฑ ํจ์"""
|
| 95 |
+
timestamp = str(round(time.time() * 1000))
|
| 96 |
+
signature = generate_signature(timestamp, method, uri, secret_key)
|
| 97 |
+
return {
|
| 98 |
+
"Content-Type": "application/json; charset=UTF-8",
|
| 99 |
+
"X-Timestamp": timestamp,
|
| 100 |
+
"X-API-KEY": api_key,
|
| 101 |
+
"X-Customer": str(customer_id),
|
| 102 |
+
"X-Signature": signature
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
def get_next_api_config():
|
| 106 |
+
"""์์ฐจ์ ์ผ๋ก ๋ค์ API ์ค์ ์ ๋ฐํ (์ค๋ ๋ ์์ )"""
|
| 107 |
+
global current_api_index
|
| 108 |
+
|
| 109 |
+
if not NAVER_API_CONFIGS:
|
| 110 |
+
logger.error("๋ค์ด๋ฒ ๊ฒ์๊ด๊ณ API ์ค์ ์ด ์์ต๋๋ค.")
|
| 111 |
+
return None
|
| 112 |
+
|
| 113 |
+
with api_lock:
|
| 114 |
+
config = NAVER_API_CONFIGS[current_api_index]
|
| 115 |
+
current_api_index = (current_api_index + 1) % len(NAVER_API_CONFIGS)
|
| 116 |
+
return config
|
| 117 |
+
|
| 118 |
+
def get_next_shopping_api_config():
|
| 119 |
+
"""์์ฐจ์ ์ผ๋ก ๋ค์ ์ผํ API ์ค์ ์ ๋ฐํ (์ค๋ฅ ํค ๊ฑด๋๋ฐ๊ธฐ ์ถ๊ฐ)"""
|
| 120 |
+
global current_shopping_api_index
|
| 121 |
+
|
| 122 |
+
if not NAVER_SHOPPING_CONFIGS:
|
| 123 |
+
logger.error("๋ค์ด๋ฒ ์ผํ API ์ค์ ์ด ์์ต๋๋ค.")
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
with shopping_lock:
|
| 127 |
+
# ์ต๋ ์ ์ฒด ํค ์๋งํผ ์๋ (๋ฌดํ ๋ฃจํ ๋ฐฉ์ง)
|
| 128 |
+
for _ in range(len(NAVER_SHOPPING_CONFIGS)):
|
| 129 |
+
config = NAVER_SHOPPING_CONFIGS[current_shopping_api_index]
|
| 130 |
+
current_shopping_api_index = (current_shopping_api_index + 1) % len(NAVER_SHOPPING_CONFIGS)
|
| 131 |
+
|
| 132 |
+
# ๊ธฐ๋ณธ๊ฐ ์ฒดํฌ
|
| 133 |
+
if config["CLIENT_ID"] and not config["CLIENT_ID"].startswith("YOUR_"):
|
| 134 |
+
return config
|
| 135 |
+
|
| 136 |
+
# ๋ชจ๋ ํค๊ฐ ๊ธฐ๋ณธ๊ฐ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ํค ๋ฐํ
|
| 137 |
+
return NAVER_SHOPPING_CONFIGS[0] if NAVER_SHOPPING_CONFIGS else None
|
| 138 |
+
|
| 139 |
+
def get_next_datalab_api_config():
|
| 140 |
+
"""์์ฐจ์ ์ผ๋ก ๋ค์ ๋ฐ์ดํฐ๋ฉ API ์ค์ ์ ๋ฐํ (์ค๋ ๋ ์์ )"""
|
| 141 |
+
global current_datalab_api_index
|
| 142 |
+
|
| 143 |
+
if not NAVER_DATALAB_CONFIGS:
|
| 144 |
+
logger.error("๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ์ค์ ์ด ์์ต๋๋ค.")
|
| 145 |
+
return None
|
| 146 |
+
|
| 147 |
+
with datalab_lock:
|
| 148 |
+
# API ํค๊ฐ ์ค์ ๋์ง ์์์ผ๋ฉด None ๋ฐํ
|
| 149 |
+
if not NAVER_DATALAB_CONFIGS[0]["CLIENT_ID"] or NAVER_DATALAB_CONFIGS[0]["CLIENT_ID"].startswith("YOUR_"):
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
config = NAVER_DATALAB_CONFIGS[current_datalab_api_index]
|
| 153 |
+
current_datalab_api_index = (current_datalab_api_index + 1) % len(NAVER_DATALAB_CONFIGS)
|
| 154 |
+
return config
|
| 155 |
+
|
| 156 |
+
def get_next_gemini_api_key():
|
| 157 |
+
"""์์ฐจ์ ์ผ๋ก ๋ค์ Gemini API ํค๋ฅผ ๋ฐํ (์ค๋ ๋ ์์ )"""
|
| 158 |
+
global current_gemini_api_index
|
| 159 |
+
|
| 160 |
+
if not GEMINI_API_CONFIGS:
|
| 161 |
+
logger.warning("์ฌ์ฉ ๊ฐ๋ฅํ Gemini API ํค๊ฐ ์์ต๋๋ค.")
|
| 162 |
+
return None
|
| 163 |
+
|
| 164 |
+
with gemini_lock:
|
| 165 |
+
# ์ต๋ ์ ์ฒด ํค ์๋งํผ ์๋ (๋ฌดํ ๋ฃจํ ๋ฐฉ์ง)
|
| 166 |
+
for _ in range(len(GEMINI_API_CONFIGS)):
|
| 167 |
+
api_key = GEMINI_API_CONFIGS[current_gemini_api_index]
|
| 168 |
+
current_gemini_api_index = (current_gemini_api_index + 1) % len(GEMINI_API_CONFIGS)
|
| 169 |
+
|
| 170 |
+
# ๊ธฐ๋ณธ๊ฐ์ด ์๋ ํค๋ง ๋ฐํ
|
| 171 |
+
if api_key and not api_key.startswith("YOUR_") and api_key.strip():
|
| 172 |
+
return api_key
|
| 173 |
+
|
| 174 |
+
# ๋ชจ๋ ํค๊ฐ ๊ธฐ๋ณธ๊ฐ์ธ ๊ฒฝ์ฐ None ๋ฐํ
|
| 175 |
+
logger.warning("์ฌ์ฉ ๊ฐ๋ฅํ Gemini API ํค๊ฐ ์์ต๋๋ค.")
|
| 176 |
+
return None
|
| 177 |
+
|
| 178 |
+
def get_gemini_model():
|
| 179 |
+
"""์บ์๋ Gemini ๋ชจ๋ธ์ ๋ฐํํ๊ฑฐ๋ ์๋ก ์์ฑ"""
|
| 180 |
+
api_key = get_next_gemini_api_key()
|
| 181 |
+
|
| 182 |
+
if not api_key:
|
| 183 |
+
logger.error("Gemini API ํค๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.")
|
| 184 |
+
return None
|
| 185 |
+
|
| 186 |
+
# ์บ์์์ ๋ชจ๋ธ ํ์ธ
|
| 187 |
+
if api_key in _gemini_models:
|
| 188 |
+
return _gemini_models[api_key]
|
| 189 |
+
|
| 190 |
+
try:
|
| 191 |
+
# ์ ๋ชจ๋ธ ์์ฑ
|
| 192 |
+
genai.configure(api_key=api_key)
|
| 193 |
+
model = genai.GenerativeModel("gemini-2.0-flash-exp")
|
| 194 |
+
|
| 195 |
+
# ์บ์์ ์ ์ฅ
|
| 196 |
+
_gemini_models[api_key] = model
|
| 197 |
+
|
| 198 |
+
logger.info(f"Gemini ๋ชจ๋ธ ์์ฑ ์ฑ๊ณต: {api_key[:8]}***{api_key[-4:]}")
|
| 199 |
+
return model
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
logger.error(f"Gemini ๋ชจ๋ธ ์์ฑ ์คํจ ({api_key[:8]}***): {e}")
|
| 203 |
+
return None
|
| 204 |
+
|
| 205 |
+
def validate_api_config(api_config):
|
| 206 |
+
"""API ์ค์ ์ ํจ์ฑ ๊ฒ์ฌ"""
|
| 207 |
+
if not api_config:
|
| 208 |
+
return False, "API ์ค์ ์ด ์์ต๋๋ค."
|
| 209 |
+
|
| 210 |
+
API_KEY = api_config.get("API_KEY", "")
|
| 211 |
+
SECRET_KEY = api_config.get("SECRET_KEY", "")
|
| 212 |
+
CUSTOMER_ID_STR = api_config.get("CUSTOMER_ID", "")
|
| 213 |
+
|
| 214 |
+
if not all([API_KEY, SECRET_KEY, CUSTOMER_ID_STR]):
|
| 215 |
+
return False, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 216 |
+
|
| 217 |
+
if CUSTOMER_ID_STR.startswith("YOUR_") or API_KEY.startswith("YOUR_"):
|
| 218 |
+
return False, "API ํค๊ฐ ํ๋ ์ด์คํ๋์
๋๋ค."
|
| 219 |
+
|
| 220 |
+
try:
|
| 221 |
+
CUSTOMER_ID = int(CUSTOMER_ID_STR)
|
| 222 |
+
except ValueError:
|
| 223 |
+
return False, f"CUSTOMER_ID ๋ณํ ์ค๋ฅ: '{CUSTOMER_ID_STR}'๋ ์ ํจํ ์ซ์๊ฐ ์๋๋๋ค."
|
| 224 |
+
|
| 225 |
+
return True, "์ ํจํ API ์ค์ ์
๋๋ค."
|
| 226 |
+
|
| 227 |
+
def validate_datalab_config(datalab_config):
|
| 228 |
+
"""๋ฐ์ดํฐ๋ฉ API ์ค์ ์ ํจ์ฑ ๊ฒ์ฌ"""
|
| 229 |
+
if not datalab_config:
|
| 230 |
+
return False, "๋ฐ์ดํฐ๋ฉ API ์ค์ ์ด ์์ต๋๋ค."
|
| 231 |
+
|
| 232 |
+
CLIENT_ID = datalab_config.get("CLIENT_ID", "")
|
| 233 |
+
CLIENT_SECRET = datalab_config.get("CLIENT_SECRET", "")
|
| 234 |
+
|
| 235 |
+
if not all([CLIENT_ID, CLIENT_SECRET]):
|
| 236 |
+
return False, "๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 237 |
+
|
| 238 |
+
if CLIENT_ID.startswith("YOUR_") or CLIENT_SECRET.startswith("YOUR_"):
|
| 239 |
+
return False, "๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ํ๋ ์ด์คํ๋์
๋๋ค."
|
| 240 |
+
|
| 241 |
+
return True, "์ ํจํ ๋ฐ์ดํฐ๋ฉ API ์ค์ ์
๋๋ค."
|
| 242 |
+
|
| 243 |
+
def validate_gemini_config():
|
| 244 |
+
"""Gemini API ์ค์ ์ ํจ์ฑ ๊ฒ์ฌ"""
|
| 245 |
+
valid_keys = 0
|
| 246 |
+
for api_key in GEMINI_API_CONFIGS:
|
| 247 |
+
if api_key and not api_key.startswith("YOUR_") and api_key.strip():
|
| 248 |
+
valid_keys += 1
|
| 249 |
+
|
| 250 |
+
if valid_keys == 0:
|
| 251 |
+
return False, "์ฌ์ฉ ๊ฐ๋ฅํ Gemini API ํค๊ฐ ์์ต๋๋ค."
|
| 252 |
+
|
| 253 |
+
return True, f"{valid_keys}๊ฐ์ ์ ํจํ Gemini API ํค๊ฐ ์ค์ ๋์ด ์์ต๋๋ค."
|
app.py
CHANGED
|
@@ -1,46 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
-
|
| 6 |
-
import
|
|
|
|
| 7 |
import time
|
| 8 |
-
import tempfile
|
| 9 |
-
import zipfile
|
| 10 |
import re
|
| 11 |
-
import
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ๋ก๊น
์ค์
|
| 14 |
-
logging.basicConfig(level=logging.
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# ===== API
|
| 24 |
-
def
|
| 25 |
-
"""
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# ํ๊ฒฝ๋ณ์์์ API ์๋ํฌ์ธํธ ๊ฐ์ ธ์ค๊ธฐ
|
| 30 |
-
api_endpoint = os.getenv('API_ENDPOINT')
|
| 31 |
-
|
| 32 |
-
if not api_endpoint:
|
| 33 |
-
logger.error("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 34 |
-
raise ValueError("API_ENDPOINT ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 35 |
-
|
| 36 |
-
client = Client(api_endpoint)
|
| 37 |
-
logger.info("์๊ฒฉ API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ฑ๊ณต")
|
| 38 |
-
return client
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
logger.error(f"
|
| 42 |
return None
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
# ===== ํ๊ตญ์๊ฐ ๊ด๋ จ ํจ์ =====
|
| 45 |
def get_korean_time():
|
| 46 |
"""ํ๊ตญ์๊ฐ ๋ฐํ"""
|
|
@@ -61,108 +77,427 @@ def format_korean_datetime(dt=None, format_type="filename"):
|
|
| 61 |
else:
|
| 62 |
return dt.strftime("%y%m%d_%H%M")
|
| 63 |
|
| 64 |
-
# =====
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
if not
|
| 119 |
-
logger.warning("โ ๏ธ Export ๋ฐ์ดํฐ๊ฐ ์๊ฑฐ๋ ๋์
๋๋ฆฌ๊ฐ ์๋ - ๊ธฐ๋ณธ ๊ตฌ์กฐ ์์ฑ")
|
| 120 |
return {
|
| 121 |
-
"
|
| 122 |
-
"
|
| 123 |
-
"
|
| 124 |
-
"
|
| 125 |
-
"related_keywords_df": None, # ๋๋ฏธ ๋ฐ์ดํฐ ๋์ None
|
| 126 |
-
"analysis_completed": True
|
| 127 |
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
"main_keyword": "๋ฉ์ธํค์๋",
|
| 133 |
-
"analysis_html": "<div>๋ถ์ ์๋ฃ</div>",
|
| 134 |
-
"analysis_completed": True
|
| 135 |
-
}
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
export_data[key] = default_value
|
| 140 |
-
logger.info(f"๐ง {key} ํค ๋ณต๊ตฌ: {default_value}")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
export_data[df_key] = None
|
| 160 |
-
elif not hasattr(df_data, 'shape'):
|
| 161 |
-
logger.warning(f"โ ๏ธ {df_key}๊ฐ DataFrame์ด ์๋ - None์ผ๋ก ์ค์ ")
|
| 162 |
-
export_data[df_key] = None
|
| 163 |
-
|
| 164 |
-
logger.info("โ
Export ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ๋ฐ ๋ณต๊ตฌ ์๋ฃ (๋๋ฏธ ๋ฐ์ดํฐ ์์)")
|
| 165 |
-
return export_data
|
| 166 |
|
| 167 |
# ===== ํ์ผ ์ถ๋ ฅ ํจ์๋ค =====
|
| 168 |
def create_timestamp_filename(analysis_keyword):
|
|
@@ -173,16 +508,8 @@ def create_timestamp_filename(analysis_keyword):
|
|
| 173 |
return f"{safe_keyword}_{timestamp}_๋ถ์๊ฒฐ๊ณผ"
|
| 174 |
|
| 175 |
def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_keywords_df, filename_base):
|
| 176 |
-
"""์์
ํ์ผ๋ก ์ถ๋ ฅ
|
| 177 |
try:
|
| 178 |
-
# ์ค์ ๋ฐ๏ฟฝ๏ฟฝํฐ๊ฐ ์๋์ง ํ์ธ
|
| 179 |
-
has_main_data = main_keywords_df is not None and not main_keywords_df.empty
|
| 180 |
-
has_related_data = related_keywords_df is not None and not related_keywords_df.empty
|
| 181 |
-
|
| 182 |
-
if not has_main_data and not has_related_data:
|
| 183 |
-
logger.info("๐ ์์ฑํ ๋ฐ์ดํฐ๊ฐ ์์ด ์์
ํ์ผ ์์ฑ ๊ฑด๋๋")
|
| 184 |
-
return None
|
| 185 |
-
|
| 186 |
excel_filename = f"{filename_base}.xlsx"
|
| 187 |
excel_path = os.path.join(tempfile.gettempdir(), excel_filename)
|
| 188 |
|
|
@@ -214,8 +541,8 @@ def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_ke
|
|
| 214 |
'border': 1
|
| 215 |
})
|
| 216 |
|
| 217 |
-
# ์ฒซ ๋ฒ์งธ ์ํธ: ๋ฉ์ธํค์๋ ์กฐํฉํค์๋
|
| 218 |
-
if
|
| 219 |
main_keywords_df.to_excel(writer, sheet_name=f'{main_keyword}_์กฐํฉํค์๋', index=False)
|
| 220 |
worksheet1 = writer.sheets[f'{main_keyword}_์กฐํฉํค์๋']
|
| 221 |
|
|
@@ -226,7 +553,7 @@ def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_ke
|
|
| 226 |
# ๋ฐ์ดํฐ ์คํ์ผ ์ ์ฉ
|
| 227 |
for row_num in range(1, len(main_keywords_df) + 1):
|
| 228 |
for col_num, value in enumerate(main_keywords_df.iloc[row_num-1]):
|
| 229 |
-
if
|
| 230 |
worksheet1.write(row_num, col_num, value, number_format)
|
| 231 |
else:
|
| 232 |
worksheet1.write(row_num, col_num, value, data_format)
|
|
@@ -238,11 +565,9 @@ def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_ke
|
|
| 238 |
len(str(col))
|
| 239 |
)
|
| 240 |
worksheet1.set_column(i, i, min(max_len + 2, 50))
|
| 241 |
-
|
| 242 |
-
logger.info(f"โ
๋ฉ์ธํค์๋ ์ํธ ์์ฑ: {main_keywords_df.shape}")
|
| 243 |
|
| 244 |
-
# ๋ ๋ฒ์งธ ์ํธ: ๋ถ์ํค์๋ ์ฐ๊ด๊ฒ์์ด
|
| 245 |
-
if
|
| 246 |
related_keywords_df.to_excel(writer, sheet_name=f'{analysis_keyword}_์ฐ๊ด๊ฒ์์ด', index=False)
|
| 247 |
worksheet2 = writer.sheets[f'{analysis_keyword}_์ฐ๊ด๊ฒ์์ด']
|
| 248 |
|
|
@@ -253,7 +578,7 @@ def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_ke
|
|
| 253 |
# ๋ฐ์ดํฐ ์คํ์ผ ์ ์ฉ
|
| 254 |
for row_num in range(1, len(related_keywords_df) + 1):
|
| 255 |
for col_num, value in enumerate(related_keywords_df.iloc[row_num-1]):
|
| 256 |
-
if
|
| 257 |
worksheet2.write(row_num, col_num, value, number_format)
|
| 258 |
else:
|
| 259 |
worksheet2.write(row_num, col_num, value, data_format)
|
|
@@ -265,8 +590,6 @@ def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_ke
|
|
| 265 |
len(str(col))
|
| 266 |
)
|
| 267 |
worksheet2.set_column(i, i, min(max_len + 2, 50))
|
| 268 |
-
|
| 269 |
-
logger.info(f"โ
์ฐ๊ด๊ฒ์์ด ์ํธ ์์ฑ: {related_keywords_df.shape}")
|
| 270 |
|
| 271 |
logger.info(f"์์
ํ์ผ ์์ฑ ์๋ฃ: {excel_path}")
|
| 272 |
return excel_path
|
|
@@ -396,7 +719,7 @@ def export_to_html(analysis_html, filename_base):
|
|
| 396 |
<div class="container">
|
| 397 |
<div class="header">
|
| 398 |
<h1><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ถฉ๋ถ์ ๊ฒฐ๊ณผ</h1>
|
| 399 |
-
<p>AI ์ํ ์์ฑ ๋ถ์ ์์คํ
|
| 400 |
</div>
|
| 401 |
<div class="content">
|
| 402 |
{analysis_html}
|
|
@@ -425,71 +748,48 @@ def create_zip_file(excel_path, html_path, filename_base):
|
|
| 425 |
zip_filename = f"{filename_base}.zip"
|
| 426 |
zip_path = os.path.join(tempfile.gettempdir(), zip_filename)
|
| 427 |
|
| 428 |
-
files_added = 0
|
| 429 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 430 |
if excel_path and os.path.exists(excel_path):
|
| 431 |
zipf.write(excel_path, f"{filename_base}.xlsx")
|
| 432 |
logger.info(f"์์
ํ์ผ ์์ถ ์ถ๊ฐ: {filename_base}.xlsx")
|
| 433 |
-
files_added += 1
|
| 434 |
|
| 435 |
if html_path and os.path.exists(html_path):
|
| 436 |
zipf.write(html_path, f"{filename_base}.html")
|
| 437 |
logger.info(f"HTML ํ์ผ ์์ถ ์ถ๊ฐ: {filename_base}.html")
|
| 438 |
-
files_added += 1
|
| 439 |
|
| 440 |
-
|
| 441 |
-
logger.warning("์์ถํ ํ์ผ์ด ์์")
|
| 442 |
-
return None
|
| 443 |
-
|
| 444 |
-
logger.info(f"์์ถ ํ์ผ ์์ฑ ์๋ฃ: {zip_path} ({files_added}๊ฐ ํ์ผ)")
|
| 445 |
return zip_path
|
| 446 |
|
| 447 |
except Exception as e:
|
| 448 |
logger.error(f"์์ถ ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
|
| 449 |
return None
|
| 450 |
|
| 451 |
-
def
|
| 452 |
-
"""
|
| 453 |
try:
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
export_data = validate_and_repair_export_data(export_data)
|
| 458 |
|
| 459 |
-
analysis_keyword = export_data.get("analysis_keyword", "
|
| 460 |
-
analysis_html = export_data.get("analysis_html", "
|
| 461 |
-
main_keyword = export_data.get("main_keyword",
|
| 462 |
main_keywords_df = export_data.get("main_keywords_df")
|
| 463 |
related_keywords_df = export_data.get("related_keywords_df")
|
| 464 |
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
logger.info(f" - related_keywords_df: {related_keywords_df.shape if related_keywords_df is not None else 'None'}")
|
| 471 |
|
| 472 |
# ํ์ผ๋ช
์์ฑ (ํ๊ตญ์๊ฐ ์ ์ฉ)
|
| 473 |
filename_base = create_timestamp_filename(analysis_keyword)
|
| 474 |
-
logger.info(f"
|
| 475 |
-
|
| 476 |
-
# HTML ํ์ผ์ ๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด ์์ฑ
|
| 477 |
-
html_path = None
|
| 478 |
-
if analysis_html and len(str(analysis_html).strip()) > 20: # ์๋ฏธ์๋ HTML์ธ์ง ํ์ธ
|
| 479 |
-
logger.info("๐ HTML ํ์ผ ์์ฑ ์์...")
|
| 480 |
-
html_path = export_to_html(analysis_html, filename_base)
|
| 481 |
-
if html_path:
|
| 482 |
-
logger.info(f"โ
HTML ํ์ผ ์์ฑ ์ฑ๊ณต: {html_path}")
|
| 483 |
-
else:
|
| 484 |
-
logger.error("โ HTML ํ์ผ ์์ฑ ์คํจ")
|
| 485 |
-
else:
|
| 486 |
-
logger.info("๐ ๋ถ์ HTML์ด ์์ด HTML ํ์ผ ์์ฑ ๊ฑด๋๋")
|
| 487 |
|
| 488 |
-
# ์์
ํ์ผ ์์ฑ
|
| 489 |
excel_path = None
|
| 490 |
-
if
|
| 491 |
-
(related_keywords_df is not None and not related_keywords_df.empty):
|
| 492 |
-
logger.info("๐ ์์
ํ์ผ ์์ฑ ์์...")
|
| 493 |
excel_path = export_to_excel(
|
| 494 |
main_keyword,
|
| 495 |
main_keywords_df,
|
|
@@ -497,340 +797,53 @@ def export_analysis_results_enhanced(export_data):
|
|
| 497 |
related_keywords_df,
|
| 498 |
filename_base
|
| 499 |
)
|
| 500 |
-
if excel_path:
|
| 501 |
-
logger.info(f"โ
์์
ํ์ผ ์์ฑ ์ฑ๊ณต: {excel_path}")
|
| 502 |
-
else:
|
| 503 |
-
logger.warning("โ ๏ธ ์์
ํ์ผ ์์ฑ ์คํจ")
|
| 504 |
-
else:
|
| 505 |
-
logger.info("๐ ์ค์ DataFrame ๋ฐ์ดํฐ๊ฐ ์์ด ์์
ํ์ผ ์์ฑ ์๋ต")
|
| 506 |
|
| 507 |
-
#
|
| 508 |
-
|
| 509 |
-
logger.warning("โ ๏ธ ์์ฑ๋ ํ์ผ์ด ์์")
|
| 510 |
-
return None, "โ ๏ธ ์์ฑํ ์ ์๋ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค. ๋ถ์์ ๋จผ์ ์๋ฃํด์ฃผ์ธ์."
|
| 511 |
|
| 512 |
# ์์ถ ํ์ผ ์์ฑ
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
if excel_path:
|
| 520 |
-
file_types.append("์์
")
|
| 521 |
-
|
| 522 |
-
file_list = " + ".join(file_types)
|
| 523 |
-
logger.info(f"โ
์์ถ ํ์ผ ์์ฑ ์ฑ๊ณต: {zip_path} ({file_list})")
|
| 524 |
-
return zip_path, f"โ
๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ถ๋ ฅ๋์์ต๋๋ค!\nํ์ผ๋ช
: {filename_base}.zip\nํฌํจ ํ์ผ: {file_list}\n\n๐ก ๋๋ฏธ ๋ฐ์ดํฐ ์ ๊ฑฐ ๋ฒ์ - ์ค์ ๋ถ์ ๋ฐ์ดํฐ๋ง ํฌํจ๋ฉ๋๋ค."
|
| 525 |
else:
|
| 526 |
-
|
| 527 |
-
return None, "์์ถ ํ์ผ ์์ฑ์ ์คํจํ์ต๋๋ค."
|
| 528 |
|
| 529 |
except Exception as e:
|
| 530 |
-
logger.error(f"
|
| 531 |
-
import traceback
|
| 532 |
-
logger.error(f"์คํ ํธ๋ ์ด์ค:\n{traceback.format_exc()}")
|
| 533 |
return None, f"์ถ๋ ฅ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 534 |
|
| 535 |
-
# ===== ๋ก๋ฉ ์ ๋๋ฉ์ด์
=====
|
| 536 |
-
def create_loading_animation():
|
| 537 |
-
"""๋ก๋ฉ ์ ๋๋ฉ์ด์
HTML"""
|
| 538 |
-
return """
|
| 539 |
-
<div style="display: flex; flex-direction: column; align-items: center; padding: 40px; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
|
| 540 |
-
<div style="width: 60px; height: 60px; border: 4px solid #f3f3f3; border-top: 4px solid #FB7F0D; border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 20px;"></div>
|
| 541 |
-
<h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">๋ถ์ ์ค์
๋๋ค...</h3>
|
| 542 |
-
<p style="color: #666; margin: 5px 0; text-align: center;">์๊ฒฉ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ AI๊ฐ ๋ถ์ํ๊ณ ์์ต๋๋ค.<br>์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์.</p>
|
| 543 |
-
<div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
|
| 544 |
-
<div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
|
| 545 |
-
</div>
|
| 546 |
-
</div>
|
| 547 |
-
|
| 548 |
-
<style>
|
| 549 |
-
@keyframes spin {
|
| 550 |
-
0% { transform: rotate(0deg); }
|
| 551 |
-
100% { transform: rotate(360deg); }
|
| 552 |
-
}
|
| 553 |
-
|
| 554 |
-
@keyframes progress {
|
| 555 |
-
0% { transform: translateX(-100%); }
|
| 556 |
-
100% { transform: translateX(100%); }
|
| 557 |
-
}
|
| 558 |
-
</style>
|
| 559 |
-
"""
|
| 560 |
-
|
| 561 |
-
# ===== ์๋ฌ ์ฒ๋ฆฌ ํจ์ =====
|
| 562 |
-
def generate_error_response(error_message):
|
| 563 |
-
"""์๋ฌ ์๋ต ์์ฑ"""
|
| 564 |
-
return f'''
|
| 565 |
-
<div style="color: red; padding: 30px; text-align: center; width: 100%;
|
| 566 |
-
background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
|
| 567 |
-
<h3 style="margin-bottom: 15px;">โ ์ฐ๊ฒฐ ์ค๋ฅ</h3>
|
| 568 |
-
<p style="margin-bottom: 20px;">{error_message}</p>
|
| 569 |
-
<div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
|
| 570 |
-
<h4>ํด๊ฒฐ ๋ฐฉ๋ฒ:</h4>
|
| 571 |
-
<ul style="text-align: left; padding-left: 20px;">
|
| 572 |
-
<li>๋คํธ์ํฌ ์ฐ๊ฒฐ์ ํ์ธํด์ฃผ์ธ์</li>
|
| 573 |
-
<li>์๊ฒฉ ์๋ฒ ์ํ๋ฅผ ํ์ธํด์ฃผ์ธ์</li>
|
| 574 |
-
<li>์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์</li>
|
| 575 |
-
<li>๋ฌธ์ ๊ฐ ์ง์๋๋ฉด ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์</li>
|
| 576 |
-
</ul>
|
| 577 |
-
</div>
|
| 578 |
-
</div>
|
| 579 |
-
'''
|
| 580 |
-
|
| 581 |
-
# ===== ์๊ฒฉ API ํธ์ถ ํจ์๋ค =====
|
| 582 |
-
def call_collect_data_api(keyword):
|
| 583 |
-
"""1๋จ๊ณ: ์ํ ๋ฐ์ดํฐ ์์ง API ํธ์ถ"""
|
| 584 |
-
try:
|
| 585 |
-
client = get_api_client()
|
| 586 |
-
if not client:
|
| 587 |
-
return generate_error_response("API ํด๋ผ์ด์ธํธ๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค."), {}
|
| 588 |
-
|
| 589 |
-
logger.info("์๊ฒฉ API ํธ์ถ: ์ํ ๋ฐ์ดํฐ ์์ง")
|
| 590 |
-
result = client.predict(
|
| 591 |
-
keyword=keyword,
|
| 592 |
-
api_name="/on_collect_data"
|
| 593 |
-
)
|
| 594 |
-
|
| 595 |
-
logger.info(f"๋ฐ์ดํฐ ์์ง API ๊ฒฐ๊ณผ ํ์
: {type(result)}")
|
| 596 |
-
|
| 597 |
-
# ๊ฒฐ๊ณผ๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์๋ HTML, ๋ ๋ฒ์งธ๋ ์ธ์
๋ฐ์ดํฐ
|
| 598 |
-
if isinstance(result, tuple) and len(result) == 2:
|
| 599 |
-
html_result, session_data = result
|
| 600 |
-
|
| 601 |
-
# ์ธ์
๋ฐ์ดํฐ๊ฐ ์ ๋๋ก ์๋์ง ํ์ธ
|
| 602 |
-
if isinstance(session_data, dict):
|
| 603 |
-
logger.info(f"๋ฐ์ดํฐ ์์ง ์ธ์
๋ฐ์ดํฐ ์์ : {list(session_data.keys()) if session_data else '๋น ๋์
๋๋ฆฌ'}")
|
| 604 |
-
return html_result, session_data
|
| 605 |
-
else:
|
| 606 |
-
logger.warning("์ธ์
๋ฐ์ดํฐ๊ฐ ๋์
๋๋ฆฌ๊ฐ ์๋๋๋ค.")
|
| 607 |
-
return html_result, {}
|
| 608 |
-
else:
|
| 609 |
-
logger.warning("์์๊ณผ ๋ค๋ฅธ ๋ฐ์ดํฐ ์์ง ๊ฒฐ๊ณผ ํํ")
|
| 610 |
-
return str(result), {"keywords_collected": True}
|
| 611 |
-
|
| 612 |
-
except Exception as e:
|
| 613 |
-
logger.error(f"์ํ ๋ฐ์ดํฐ ์์ง API ํธ์ถ ์ค๋ฅ: {e}")
|
| 614 |
-
return generate_error_response(f"์๊ฒฉ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {str(e)}"), {}
|
| 615 |
-
|
| 616 |
-
def call_analyze_keyword_api_enhanced(analysis_keyword, base_keyword, keywords_data):
|
| 617 |
-
"""3๋จ๊ณ: ๊ฐํ๋ ํค์๋ ์ฌ์ถฉ๋ถ์ API ํธ์ถ (๋๋ฏธ ๋ฐ์ดํฐ ์ ๊ฑฐ)"""
|
| 618 |
-
try:
|
| 619 |
-
client = get_api_client()
|
| 620 |
-
if not client:
|
| 621 |
-
return generate_error_response("API ํด๋ผ์ด์ธํธ๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค."), {}
|
| 622 |
-
|
| 623 |
-
logger.info("=== ๐ ๊ฐํ๋ ํค์๋ ์ฌ์ถฉ๋ถ์ API ํธ์ถ (๋๋ฏธ ๋ฐ์ดํฐ ์ ๊ฑฐ) ===")
|
| 624 |
-
logger.info(f"ํ๋ผ๋ฏธํฐ - analysis_keyword: '{analysis_keyword}'")
|
| 625 |
-
logger.info(f"ํ๋ผ๋ฏธํฐ - base_keyword: '{base_keyword}'")
|
| 626 |
-
logger.info(f"ํ๋ผ๋ฏธํฐ - keywords_data ํ์
: {type(keywords_data)}")
|
| 627 |
-
|
| 628 |
-
# ์๊ฒฉ API ํธ์ถ
|
| 629 |
-
result = client.predict(
|
| 630 |
-
analysis_keyword,
|
| 631 |
-
base_keyword,
|
| 632 |
-
keywords_data,
|
| 633 |
-
api_name="/on_analyze_keyword"
|
| 634 |
-
)
|
| 635 |
-
|
| 636 |
-
logger.info(f"๐ก ์๊ฒฉ API ์๋ต ์์ :")
|
| 637 |
-
logger.info(f" - ์๋ต ํ์
: {type(result)}")
|
| 638 |
-
logger.info(f" - ์๋ต ๊ธธ์ด: {len(result) if hasattr(result, '__len__') else 'N/A'}")
|
| 639 |
-
|
| 640 |
-
# ์๋ต ์ฒ๋ฆฌ ๋ฐ Export ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์ฑ
|
| 641 |
-
if isinstance(result, tuple) and len(result) == 2:
|
| 642 |
-
html_result, remote_export_data = result
|
| 643 |
-
|
| 644 |
-
logger.info(f"๐ ์๊ฒฉ export ๋ฐ์ดํฐ:")
|
| 645 |
-
logger.info(f" - ํ์
: {type(remote_export_data)}")
|
| 646 |
-
logger.info(f" - ํค๋ค: {list(remote_export_data.keys()) if isinstance(remote_export_data, dict) else 'None'}")
|
| 647 |
-
|
| 648 |
-
# HTML ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด Export ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์ฑ (๋๋ฏธ ๋ฐ์ดํฐ ์์ด)
|
| 649 |
-
if html_result:
|
| 650 |
-
logger.info("๐ง Export ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์ฑ ์์ (๋๋ฏธ ๋ฐ์ดํฐ ์ ๊ฑฐ)")
|
| 651 |
-
enhanced_export_data = create_export_data_from_html(
|
| 652 |
-
analysis_keyword=analysis_keyword,
|
| 653 |
-
main_keyword=base_keyword,
|
| 654 |
-
analysis_html=html_result,
|
| 655 |
-
step1_data=keywords_data
|
| 656 |
-
)
|
| 657 |
-
|
| 658 |
-
# ์๊ฒฉ์์ ์จ ์ค์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ๋ณํฉ
|
| 659 |
-
if isinstance(remote_export_data, dict) and remote_export_data:
|
| 660 |
-
logger.info("๐ ์๊ฒฉ ์ค์ ๋ฐ์ดํฐ์ ๋ก์ปฌ ๋ฐ์ดํฐ ๋ณํฉ")
|
| 661 |
-
for key, value in remote_export_data.items():
|
| 662 |
-
if value is not None and key in ["main_keywords_df", "related_keywords_df"]:
|
| 663 |
-
# DataFrame ๋ฐ์ดํฐ๋ง ๊ฒ์ฆํ์ฌ ๋ณํฉ
|
| 664 |
-
if isinstance(value, dict) and value: # ๋น ๋์
๋๋ฆฌ๊ฐ ์๋ ๊ฒฝ์ฐ๋ง
|
| 665 |
-
enhanced_export_data[key] = value
|
| 666 |
-
logger.info(f" - {key} ์๊ฒฉ ์ค์ ๋ฐ์ดํฐ๋ก ์
๋ฐ์ดํธ")
|
| 667 |
-
elif hasattr(value, 'shape') and not value.empty: # DataFrame์ด๊ณ ๋น์ด์์ง ์์ ๊ฒฝ์ฐ
|
| 668 |
-
enhanced_export_data[key] = value
|
| 669 |
-
logger.info(f" - {key} ์๊ฒฉ DataFrame ๋ฐ์ดํฐ๋ก ์
๋ฐ์ดํธ")
|
| 670 |
-
elif value is not None and key not in ["main_keywords_df", "related_keywords_df"]:
|
| 671 |
-
enhanced_export_data[key] = value
|
| 672 |
-
logger.info(f" - {key} ์๊ฒฉ ๋ฐ์ดํฐ๋ก ์
๋ฐ์ดํธ")
|
| 673 |
-
|
| 674 |
-
logger.info(f"โ
์ต์ข
Export ๋ฐ์ดํฐ ๊ตฌ์กฐ (๋๋ฏธ ๋ฐ์ดํฐ ์์):")
|
| 675 |
-
logger.info(f" - ํค ๊ฐ์: {len(enhanced_export_data)}")
|
| 676 |
-
logger.info(f" - ํค ๋ชฉ๋ก: {list(enhanced_export_data.keys())}")
|
| 677 |
-
|
| 678 |
-
return html_result, enhanced_export_data
|
| 679 |
-
else:
|
| 680 |
-
logger.warning("โ ๏ธ HTML ๊ฒฐ๊ณผ๊ฐ ๋น์ด์์")
|
| 681 |
-
return str(result), {}
|
| 682 |
-
else:
|
| 683 |
-
logger.warning("โ ๏ธ ์์๊ณผ ๋ค๋ฅธ API ์๋ต ํํ")
|
| 684 |
-
# HTML๋ง ๋ฐํ๋ ๊ฒฝ์ฐ๋ ์ฒ๋ฆฌ
|
| 685 |
-
if isinstance(result, str) and len(result) > 100: # HTML์ผ ๊ฐ๋ฅ์ฑ์ด ๋์
|
| 686 |
-
logger.info("๐ HTML ๋ฌธ์์ด๋ก ์ถ์ ๋๋ ์๋ต - Export ๋ฐ์ดํฐ ์์ฑ (๋๋ฏธ ๋ฐ์ดํฐ ์์ด)")
|
| 687 |
-
enhanced_export_data = create_export_data_from_html(
|
| 688 |
-
analysis_keyword=analysis_keyword,
|
| 689 |
-
main_keyword=base_keyword,
|
| 690 |
-
analysis_html=result,
|
| 691 |
-
step1_data=keywords_data
|
| 692 |
-
)
|
| 693 |
-
return result, enhanced_export_data
|
| 694 |
-
else:
|
| 695 |
-
return str(result), {}
|
| 696 |
-
|
| 697 |
-
except Exception as e:
|
| 698 |
-
logger.error(f"โ ํค์๋ ์ฌ์ถฉ๋ถ์ API ํธ์ถ ์ค๋ฅ: {e}")
|
| 699 |
-
import traceback
|
| 700 |
-
logger.error(f"๏ฟฝ๏ฟฝํ ํธ๋ ์ด์ค:\n{traceback.format_exc()}")
|
| 701 |
-
return generate_error_response(f"์๊ฒฉ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {str(e)}"), {}
|
| 702 |
-
|
| 703 |
# ===== ๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค =====
|
| 704 |
def create_interface():
|
| 705 |
-
# CSS
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
--
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
--table-hover-bg: #f0f0f0;
|
| 721 |
-
--shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
|
| 722 |
-
--shadow-light: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 723 |
-
--border-radius: 18px;
|
| 724 |
-
}
|
| 725 |
-
@media (prefers-color-scheme: dark) {
|
| 726 |
-
:root {
|
| 727 |
-
--background-color: #1a1a1a;
|
| 728 |
-
--card-bg: #2d2d2d;
|
| 729 |
-
--input-bg: #2d2d2d;
|
| 730 |
-
--text-color: #e5e5e5;
|
| 731 |
-
--text-secondary: #a1a1aa;
|
| 732 |
-
--border-color: #404040;
|
| 733 |
-
--border-light: #525252;
|
| 734 |
-
--table-even-bg: #333333;
|
| 735 |
-
--table-hover-bg: #404040;
|
| 736 |
-
--shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
|
| 737 |
-
--shadow-light: 0 2px 4px rgba(0, 0, 0, 0.2);
|
| 738 |
}
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
padding: 0;
|
| 747 |
-
transition: background-color 0.3s ease, color 0.3s ease;
|
| 748 |
-
}
|
| 749 |
-
.gradio-container {
|
| 750 |
-
width: 100%;
|
| 751 |
-
margin: 0 auto;
|
| 752 |
-
padding: 20px;
|
| 753 |
-
background-color: var(--background-color) !important;
|
| 754 |
-
}
|
| 755 |
-
.custom-frame {
|
| 756 |
-
background-color: var(--card-bg) !important;
|
| 757 |
-
border: 1px solid var(--border-light) !important;
|
| 758 |
-
border-radius: var(--border-radius);
|
| 759 |
-
padding: 20px;
|
| 760 |
-
margin: 10px 0;
|
| 761 |
-
box-shadow: var(--shadow) !important;
|
| 762 |
-
color: var(--text-color) !important;
|
| 763 |
-
}
|
| 764 |
-
.custom-button {
|
| 765 |
-
border-radius: 30px !important;
|
| 766 |
-
background: var(--primary-color) !important;
|
| 767 |
-
color: white !important;
|
| 768 |
-
font-size: 18px !important;
|
| 769 |
-
padding: 10px 20px !important;
|
| 770 |
-
border: none;
|
| 771 |
-
box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25);
|
| 772 |
-
transition: transform 0.3s ease;
|
| 773 |
-
height: 45px !important;
|
| 774 |
-
width: 100% !important;
|
| 775 |
-
}
|
| 776 |
-
.custom-button:hover {
|
| 777 |
-
transform: translateY(-2px);
|
| 778 |
-
box-shadow: 0 6px 12px rgba(251, 127, 13, 0.3);
|
| 779 |
-
}
|
| 780 |
-
.export-button {
|
| 781 |
-
background: linear-gradient(135deg, #28a745, #20c997) !important;
|
| 782 |
-
color: white !important;
|
| 783 |
-
border-radius: 25px !important;
|
| 784 |
-
height: 50px !important;
|
| 785 |
-
font-size: 17px !important;
|
| 786 |
-
font-weight: bold !important;
|
| 787 |
-
width: 100% !important;
|
| 788 |
-
margin-top: 20px !important;
|
| 789 |
-
}
|
| 790 |
-
.section-title {
|
| 791 |
-
display: flex;
|
| 792 |
-
align-items: center;
|
| 793 |
-
font-size: 20px;
|
| 794 |
-
font-weight: 700;
|
| 795 |
-
color: var(--text-color) !important;
|
| 796 |
-
margin-bottom: 10px;
|
| 797 |
-
padding-bottom: 5px;
|
| 798 |
-
border-bottom: 2px solid var(--primary-color);
|
| 799 |
-
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 800 |
-
}
|
| 801 |
-
.section-title img, .section-title i {
|
| 802 |
-
margin-right: 10px;
|
| 803 |
-
font-size: 20px;
|
| 804 |
-
color: var(--primary-color);
|
| 805 |
-
}
|
| 806 |
-
.gr-input, .gr-text-input, .gr-sample-inputs,
|
| 807 |
-
input[type="text"], input[type="number"], textarea, select {
|
| 808 |
-
border-radius: var(--border-radius) !important;
|
| 809 |
-
border: 1px solid var(--border-color) !important;
|
| 810 |
-
padding: 12px !important;
|
| 811 |
-
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important;
|
| 812 |
-
transition: all 0.3s ease !important;
|
| 813 |
-
background-color: var(--input-bg) !important;
|
| 814 |
-
color: var(--text-color) !important;
|
| 815 |
-
}
|
| 816 |
-
.gr-input:focus, .gr-text-input:focus,
|
| 817 |
-
input[type="text"]:focus, textarea:focus, select:focus {
|
| 818 |
-
border-color: var(--primary-color) !important;
|
| 819 |
-
outline: none !important;
|
| 820 |
-
box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important;
|
| 821 |
-
}
|
| 822 |
-
.fade-in {
|
| 823 |
-
animation: fadeIn 0.5s ease-out;
|
| 824 |
-
}
|
| 825 |
-
@keyframes fadeIn {
|
| 826 |
-
from { opacity: 0; transform: translateY(10px); }
|
| 827 |
-
to { opacity: 1; transform: translateY(0); }
|
| 828 |
-
}
|
| 829 |
-
"""
|
| 830 |
|
| 831 |
with gr.Blocks(
|
| 832 |
css=custom_css,
|
| 833 |
-
title="๐ AI ์ํ ์์ฑ ๋ถ์๊ธฐ
|
| 834 |
theme=gr.themes.Default(primary_hue="orange", secondary_hue="orange")
|
| 835 |
) as interface:
|
| 836 |
|
|
@@ -840,11 +853,11 @@ def create_interface():
|
|
| 840 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
|
| 841 |
""")
|
| 842 |
|
| 843 |
-
# ์ธ์
๋ณ ์ํ ๋ณ์
|
| 844 |
keywords_data_state = gr.State()
|
| 845 |
export_data_state = gr.State({})
|
| 846 |
|
| 847 |
-
# ===
|
| 848 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 849 |
gr.HTML('<div class="section-title"><i class="fas fa-search"></i> 1๋จ๊ณ: ๋ฉ์ธ ํค์๋ ์
๋ ฅ</div>')
|
| 850 |
|
|
@@ -857,10 +870,12 @@ def create_interface():
|
|
| 857 |
|
| 858 |
collect_data_btn = gr.Button("1๋จ๊ณ: ์ํ ๋ฐ์ดํฐ ์์งํ๊ธฐ", elem_classes="custom-button", size="lg")
|
| 859 |
|
|
|
|
| 860 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 861 |
gr.HTML('<div class="section-title"><i class="fas fa-database"></i> 2๋จ๊ณ: ์์ง๋ ํค์๋ ๋ชฉ๋ก</div>')
|
| 862 |
keywords_result = gr.HTML()
|
| 863 |
|
|
|
|
| 864 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 865 |
gr.HTML('<div class="section-title"><i class="fas fa-bullseye"></i> 3๋จ๊ณ: ๋ถ์ํ ํค์๋ ์ ํ</div>')
|
| 866 |
|
|
@@ -873,21 +888,14 @@ def create_interface():
|
|
| 873 |
|
| 874 |
analyze_keyword_btn = gr.Button("ํค์๋ ์ฌ์ถฉ๋ถ์ ํ๊ธฐ", elem_classes="custom-button", size="lg")
|
| 875 |
|
|
|
|
| 876 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 877 |
gr.HTML('<div class="section-title"><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ถฉ๋ถ์</div>')
|
| 878 |
analysis_result = gr.HTML(label="ํค์๋ ์ฌ์ถฉ๋ถ์")
|
| 879 |
|
|
|
|
| 880 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 881 |
gr.HTML('<div class="section-title"><i class="fas fa-download"></i> ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ</div>')
|
| 882 |
-
|
| 883 |
-
gr.HTML("""
|
| 884 |
-
<div style="background: #e3f2fd; border-left: 4px solid #2196f3; padding: 15px; margin: 10px 0; border-radius: 5px;">
|
| 885 |
-
<h4 style="margin: 0 0 10px 0; color: #1976d2;"><i class="fas fa-info-circle"></i> ์ค์ ๋ฐ์ดํฐ ์ถ๋ ฅ ๋ฒ์ </h4>
|
| 886 |
-
<p style="margin: 0; color: #1976d2; font-size: 14px;">
|
| 887 |
-
โข ๋ถ์๋ ๋ฐ์ดํฐ๋ฅผ ํ์ผ๋ก ์ถ๋ ฅ๋ฉ๋๋ค<br>
|
| 888 |
-
</p>
|
| 889 |
-
</div>
|
| 890 |
-
""")
|
| 891 |
|
| 892 |
export_btn = gr.Button("๐ ๋ถ์๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ", elem_classes="export-button", size="lg")
|
| 893 |
export_result = gr.HTML()
|
|
@@ -901,10 +909,30 @@ def create_interface():
|
|
| 901 |
# ๋ก๋ฉ ์ํ ํ์
|
| 902 |
yield (create_loading_animation(), None)
|
| 903 |
|
| 904 |
-
|
| 905 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 906 |
|
| 907 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 908 |
|
| 909 |
def on_analyze_keyword(analysis_keyword, base_keyword, keywords_data):
|
| 910 |
if not analysis_keyword.strip():
|
|
@@ -913,30 +941,78 @@ def create_interface():
|
|
| 913 |
# ๋ก๋ฉ ์ํ ํ์
|
| 914 |
yield create_loading_animation(), {}
|
| 915 |
|
| 916 |
-
#
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
)
|
|
|
|
| 920 |
|
| 921 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 922 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 923 |
def on_export_results(export_data):
|
| 924 |
-
"""
|
| 925 |
try:
|
| 926 |
-
|
| 927 |
-
if isinstance(export_data, dict):
|
| 928 |
-
logger.info(f"๐ export_data ํค๋ค: {list(export_data.keys())}")
|
| 929 |
-
|
| 930 |
-
# ๊ฐํ๋ ์ถ๋ ฅ ํจ์ ํธ์ถ (๋๋ฏธ ๋ฐ์ดํฐ ์ ๊ฑฐ)
|
| 931 |
-
zip_path, message = export_analysis_results_enhanced(export_data)
|
| 932 |
|
| 933 |
if zip_path:
|
|
|
|
| 934 |
success_html = f"""
|
| 935 |
<div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 936 |
<h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> ์ถ๋ ฅ ์๋ฃ!</h4>
|
| 937 |
<p style="color: #155724; margin: 0; line-height: 1.6;">
|
| 938 |
{message}<br>
|
| 939 |
-
<strong
|
|
|
|
|
|
|
| 940 |
<br>
|
| 941 |
<i class="fas fa-download"></i> ์๋ ๋ค์ด๋ก๋ ๋ฒํผ์ ํด๋ฆญํ์ฌ ํ์ผ์ ์ ์ฅํ์ธ์.<br>
|
| 942 |
<small style="color: #666;">โฐ ํ๊ตญ์๊ฐ ๊ธฐ์ค์ผ๋ก ํ์ผ๋ช
์ด ์์ฑ๋ฉ๋๋ค.</small>
|
|
@@ -945,40 +1021,21 @@ def create_interface():
|
|
| 945 |
"""
|
| 946 |
return success_html, gr.update(value=zip_path, visible=True)
|
| 947 |
else:
|
|
|
|
| 948 |
error_html = f"""
|
| 949 |
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 950 |
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถ๋ ฅ ์คํจ</h4>
|
| 951 |
<p style="color: #721c24; margin: 0;">{message}</p>
|
| 952 |
-
<div style="margin-top: 15px; padding: 15px; background: white; border-radius: 5px;">
|
| 953 |
-
<h5 style="color: #721c24; margin: 0 0 10px 0;">๐ ๋๋ฒ๊น
์ ๋ณด:</h5>
|
| 954 |
-
<ul style="color: #721c24; margin: 0; padding-left: 20px;">
|
| 955 |
-
<li>Export ๋ฐ์ดํฐ ํ์
: {type(export_data)}</li>
|
| 956 |
-
<li>Export ๋ฐ์ดํฐ ์ ํจ์ฑ: {'์ ํจ' if export_data else '๋ฌดํจ'}</li>
|
| 957 |
-
<li>ํค์๋ ์ฌ์ถฉ๋ถ์ ์ํ: {'์๋ฃ' if export_data.get('analysis_completed') else '๋ฏธ์๋ฃ'}</li>
|
| 958 |
-
</ul>
|
| 959 |
-
</div>
|
| 960 |
</div>
|
| 961 |
"""
|
| 962 |
-
logger.error("โ ๊ฐํ๋ ์ถ๋ ฅ ์คํจ")
|
| 963 |
return error_html, gr.update(visible=False)
|
| 964 |
|
| 965 |
except Exception as e:
|
| 966 |
-
logger.error(f"
|
| 967 |
-
import traceback
|
| 968 |
-
logger.error(f"์คํ ํธ๋ ์ด์ค:\n{traceback.format_exc()}")
|
| 969 |
-
|
| 970 |
error_html = f"""
|
| 971 |
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 972 |
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์์คํ
์ค๋ฅ</h4>
|
| 973 |
-
<p style="color: #721c24; margin: 0;"
|
| 974 |
-
<code style="display: block; margin: 10px 0; padding: 10px; background: #f8f9fa; border-radius: 3px; color: #721c24;">
|
| 975 |
-
{type(e).__name__}: {str(e)}
|
| 976 |
-
</code>
|
| 977 |
-
<div style="margin-top: 15px; padding: 10px; background: #fff3cd; border-radius: 5px;">
|
| 978 |
-
<p style="margin: 0; color: #856404; font-size: 14px;">
|
| 979 |
-
๐ก ์ค์ ๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์์ด์ผ๋ง ํ์ผ์ด ์์ฑ๋ฉ๋๋ค.
|
| 980 |
-
</p>
|
| 981 |
-
</div>
|
| 982 |
</div>
|
| 983 |
"""
|
| 984 |
return error_html, gr.update(visible=False)
|
|
@@ -987,26 +1044,80 @@ def create_interface():
|
|
| 987 |
collect_data_btn.click(
|
| 988 |
fn=on_collect_data,
|
| 989 |
inputs=[keyword_input],
|
| 990 |
-
outputs=[keywords_result, keywords_data_state]
|
| 991 |
-
api_name="on_collect_data"
|
| 992 |
)
|
| 993 |
|
| 994 |
analyze_keyword_btn.click(
|
| 995 |
fn=on_analyze_keyword,
|
| 996 |
-
inputs=[analysis_keyword_input, keyword_input, keywords_data_state],
|
| 997 |
-
outputs=[analysis_result, export_data_state]
|
| 998 |
-
api_name="on_analyze_keyword"
|
| 999 |
)
|
| 1000 |
|
| 1001 |
export_btn.click(
|
| 1002 |
fn=on_export_results,
|
| 1003 |
inputs=[export_data_state],
|
| 1004 |
-
outputs=[export_result, download_file]
|
| 1005 |
-
api_name="on_export_results"
|
| 1006 |
)
|
| 1007 |
|
| 1008 |
return interface
|
| 1009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1010 |
# ===== ๋ฉ์ธ ์คํ =====
|
| 1011 |
if __name__ == "__main__":
|
| 1012 |
# pytz ๋ชจ๋ ์ค์น ํ์ธ
|
|
@@ -1014,8 +1125,64 @@ if __name__ == "__main__":
|
|
| 1014 |
import pytz
|
| 1015 |
logger.info("โ
pytz ๋ชจ๋ ๋ก๋ ์ฑ๊ณต - ํ๊ตญ์๊ฐ ์ง์")
|
| 1016 |
except ImportError:
|
|
|
|
| 1017 |
logger.info("์์คํ
์๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
| 1018 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
# ์ฑ ์คํ
|
| 1020 |
app = create_interface()
|
| 1021 |
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
AI ์ํ ์์ฑ ๋ถ์ ์์คํ
v2.9 - ์ถ๋ ฅ ๊ธฐ๋ฅ ์ถ๊ฐ + ๋ฉํฐ์ฌ์ฉ์ ์์
|
| 4 |
+
- ์ฐ๊ด๊ฒ์์ด ์์
์ถ๋ ฅ
|
| 5 |
+
- ํค์๋ ์ฌ์ถฉ๋ถ์ HTML ์ถ๋ ฅ
|
| 6 |
+
- ์์ถํ์ผ๋ก ๊ฒฐ๊ณผ ๋ค์ด๋ก๋
|
| 7 |
+
- Gemini API ํค ํตํฉ ๊ด๋ฆฌ
|
| 8 |
+
- ํ๊ตญ์๊ฐ ์ ์ฉ
|
| 9 |
+
- ๋ฉํฐ ์ฌ์ฉ์ ์์ : gr.State ์ฌ์ฉ์ผ๋ก ์ธ์
๋ณ ๋ฐ์ดํฐ ๊ด๋ฆฌ
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
import gradio as gr
|
| 13 |
import pandas as pd
|
| 14 |
import os
|
| 15 |
import logging
|
| 16 |
+
import google.generativeai as genai
|
| 17 |
+
from datetime import datetime, timedelta
|
| 18 |
+
import pytz # ํ๊ตญ์๊ฐ ์ ์ฉ์ ์ํ ์ถ๊ฐ
|
| 19 |
import time
|
|
|
|
|
|
|
| 20 |
import re
|
| 21 |
+
from collections import Counter
|
| 22 |
+
import zipfile
|
| 23 |
+
import tempfile
|
| 24 |
|
| 25 |
# ๋ก๊น
์ค์
|
| 26 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
+
# ๋ชจ๋ ์ํฌํธ
|
| 30 |
+
import api_utils
|
| 31 |
+
import text_utils
|
| 32 |
+
import keyword_search
|
| 33 |
+
import product_search
|
| 34 |
+
import keyword_processor
|
| 35 |
+
import export_utils
|
| 36 |
+
import keyword_analysis
|
| 37 |
+
import trend_analysis_v2
|
| 38 |
|
| 39 |
+
# ===== Gemini API ์ค์ =====
|
| 40 |
+
def setup_gemini_model():
|
| 41 |
+
"""Gemini ๋ชจ๋ธ ์ด๊ธฐํ - api_utils์์ ๊ด๋ฆฌ"""
|
| 42 |
try:
|
| 43 |
+
# api_utils์์ Gemini ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ
|
| 44 |
+
model = api_utils.get_gemini_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if model:
|
| 47 |
+
logger.info("Gemini ๋ชจ๋ธ ์ด๊ธฐํ ์ฑ๊ณต (api_utils ํตํฉ ๊ด๋ฆฌ)")
|
| 48 |
+
return model
|
| 49 |
+
else:
|
| 50 |
+
logger.warning("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
+
logger.error(f"Gemini ๋ชจ๋ธ ์ด๊ธฐํ ์คํจ: {e}")
|
| 55 |
return None
|
| 56 |
|
| 57 |
+
# Gemini ๋ชจ๋ธ ์ด๊ธฐํ
|
| 58 |
+
gemini_model = setup_gemini_model()
|
| 59 |
+
|
| 60 |
# ===== ํ๊ตญ์๊ฐ ๊ด๋ จ ํจ์ =====
|
| 61 |
def get_korean_time():
|
| 62 |
"""ํ๊ตญ์๊ฐ ๋ฐํ"""
|
|
|
|
| 77 |
else:
|
| 78 |
return dt.strftime("%y%m%d_%H%M")
|
| 79 |
|
| 80 |
+
# ===== ์ถ๋ ฅ ์ ์ฉ ์ํ ๋ณ์ ์ ๊ฑฐ (๋ฉํฐ ์ฌ์ฉ์ ์์ ์ ์ํด gr.State ์ฌ์ฉ) =====
|
| 81 |
+
# export_state ์ ์ญ ๋ณ์ ์ ๊ฑฐ - ๋ฉํฐ ์ฌ์ฉ์ ํ๊ฒฝ์์ ๋ฐ์ดํฐ ํผํฉ ๋ฌธ์ ํด๊ฒฐ
|
| 82 |
+
|
| 83 |
+
# ===== ์ฐ๊ด๊ฒ์์ด ๋ถ์ ๊ธฐ๋ฅ =====
|
| 84 |
+
def analyze_related_keywords(keyword):
|
| 85 |
+
"""์ฐ๊ด๊ฒ์์ด ๋ถ์ - ๋ค์ด๋ฒ ์ํ 40๊ฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ณตํฉํค์๋ ์ถ์ถ"""
|
| 86 |
+
logger.info(f"์ฐ๊ด๊ฒ์์ด ๋ถ์ ์์: '{keyword}'")
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
# 1๋จ๊ณ: ๋ค์ด๋ฒ ์ํ 40๊ฐ ์ถ์ถ
|
| 90 |
+
api_keyword = keyword.replace(" ", "")
|
| 91 |
+
products_data = []
|
| 92 |
+
|
| 93 |
+
# 40๊ฐ ์ํ์ ๊ฐ์ ธ์ค๊ธฐ ์ํด ์ฌ๋ฌ ํ์ด์ง ํธ์ถ
|
| 94 |
+
for page in range(1, 5): # 4ํ์ด์ง * 10๊ฐ = 40๊ฐ
|
| 95 |
+
result = product_search.fetch_products_by_keyword(api_keyword, page=page, display=10)
|
| 96 |
+
if result["status"] == "success" and result["products"]:
|
| 97 |
+
products_data.extend(result["products"])
|
| 98 |
+
else:
|
| 99 |
+
break
|
| 100 |
+
time.sleep(0.3) # API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง
|
| 101 |
+
|
| 102 |
+
if not products_data:
|
| 103 |
+
return {
|
| 104 |
+
"status": "error",
|
| 105 |
+
"message": f"'{keyword}' ํค์๋๋ก ์ํ์ ์ฐพ์ ์ ์์ต๋๋ค.",
|
| 106 |
+
"keywords_df": pd.DataFrame()
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# ์ค์ ๊ฐ์ ธ์จ ์ํ ์ ์ ํ
|
| 110 |
+
products_data = products_data[:40]
|
| 111 |
+
logger.info(f"์ํ ์ถ์ถ ์๋ฃ: {len(products_data)}๊ฐ")
|
| 112 |
+
|
| 113 |
+
# 2๋จ๊ณ: ์ํ๋ช
์์ ํค์๋ ์ถ์ถ (์คํ์ด์ค๋ฐ๋ก ๋ถ๋ฅ)
|
| 114 |
+
all_words = []
|
| 115 |
+
for product in products_data:
|
| 116 |
+
title = product.get("์ํ๋ช
", "")
|
| 117 |
+
# ๊ณต๋ฐฑ๊ณผ ์ผํ๋ก ๋ถ๋ฆฌ
|
| 118 |
+
words = re.split(r'[,\s]+', title)
|
| 119 |
+
all_words.extend([word.strip() for word in words if word.strip() and len(word.strip()) >= 1])
|
| 120 |
+
|
| 121 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
| 122 |
+
unique_words = list(set(all_words))
|
| 123 |
+
logger.info(f"์ถ์ถ๋ ๋จ์ด ์: {len(unique_words)}๊ฐ")
|
| 124 |
+
|
| 125 |
+
# 3๋จ๊ณ: ๋ถ์ํ ํค์๋๋ฅผ ์๋ค๋ก ๋ถ์ฌ์ ๋ณตํฉํค์๋ ์์ฑ
|
| 126 |
+
compound_keywords = []
|
| 127 |
+
main_keyword = keyword.strip()
|
| 128 |
+
|
| 129 |
+
for word in unique_words:
|
| 130 |
+
if word != main_keyword and len(word) >= 2: # ๋จ์ผ ๊ธ์ ์ ์ธ
|
| 131 |
+
# ์์ ๋ถ์ด๊ธฐ
|
| 132 |
+
front_compound = f"{word} {main_keyword}"
|
| 133 |
+
compound_keywords.append(front_compound)
|
| 134 |
+
|
| 135 |
+
# ๋ค์ ๋ถ์ด๊ธฐ
|
| 136 |
+
back_compound = f"{main_keyword} {word}"
|
| 137 |
+
compound_keywords.append(back_compound)
|
| 138 |
+
|
| 139 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
| 140 |
+
compound_keywords = list(set(compound_keywords))
|
| 141 |
+
logger.info(f"์์ฑ๋ ๋ณตํฉํค์๋ ์: {len(compound_keywords)}๊ฐ")
|
| 142 |
+
|
| 143 |
+
# 4๋จ๊ณ: ๊ฒ์๋ ์ถ์ถ
|
| 144 |
+
api_keywords = [kw.replace(" ", "") for kw in compound_keywords]
|
| 145 |
+
search_volumes = keyword_search.fetch_all_search_volumes(api_keywords)
|
| 146 |
+
|
| 147 |
+
# 5๋จ๊ณ: ์๋ค ํค์๋ ์ค ๋์ ๊ฒ ์ ํ, ๋ฎ์ ๊ฒ ์ ๊ฑฐ
|
| 148 |
+
keyword_pairs = {} # {base_word: {"front": front_kw, "back": back_kw, "front_vol": vol, "back_vol": vol}}
|
| 149 |
+
|
| 150 |
+
for word in unique_words:
|
| 151 |
+
if word != main_keyword and len(word) >= 2:
|
| 152 |
+
front_kw = f"{word} {main_keyword}"
|
| 153 |
+
back_kw = f"{main_keyword} {word}"
|
| 154 |
+
|
| 155 |
+
front_api = front_kw.replace(" ", "")
|
| 156 |
+
back_api = back_kw.replace(" ", "")
|
| 157 |
+
|
| 158 |
+
front_vol = search_volumes.get(front_api, {}).get("์ด๊ฒ์๋", 0)
|
| 159 |
+
back_vol = search_volumes.get(back_api, {}).get("์ด๊ฒ์๋", 0)
|
| 160 |
+
|
| 161 |
+
keyword_pairs[word] = {
|
| 162 |
+
"front": front_kw,
|
| 163 |
+
"back": back_kw,
|
| 164 |
+
"front_vol": front_vol,
|
| 165 |
+
"back_vol": back_vol
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
# 6๋จ๊ณ: ๋์ ๊ฒ์๋์ ํค์๋๋ง ์ ํ
|
| 169 |
+
final_keywords = []
|
| 170 |
+
for word, data in keyword_pairs.items():
|
| 171 |
+
if data["front_vol"] > data["back_vol"]:
|
| 172 |
+
selected_kw = data["front"]
|
| 173 |
+
selected_vol = data["front_vol"]
|
| 174 |
+
selected_api = selected_kw.replace(" ", "")
|
| 175 |
+
elif data["back_vol"] > data["front_vol"]:
|
| 176 |
+
selected_kw = data["back"]
|
| 177 |
+
selected_vol = data["back_vol"]
|
| 178 |
+
selected_api = selected_kw.replace(" ", "")
|
| 179 |
+
elif data["front_vol"] == data["back_vol"] and data["front_vol"] > 0:
|
| 180 |
+
# ๊ฐ์ ๊ฒ์๋์ด๋ฉด ์์ฐ์ค๋ฌ์ด ์์ ์ ํ (์ผ๋ฐ์ ์ผ๋ก ๋ค์ ๋ถ์ด๋ ๊ฒ์ด ์์ฐ์ค๋ฌ์)
|
| 181 |
+
selected_kw = data["back"]
|
| 182 |
+
selected_vol = data["back_vol"]
|
| 183 |
+
selected_api = selected_kw.replace(" ", "")
|
| 184 |
+
else:
|
| 185 |
+
# ๋ ๋ค 0์ด๋ฉด ์ ์ธ
|
| 186 |
+
continue
|
| 187 |
+
|
| 188 |
+
if selected_vol > 0: # ๊ฒ์๋์ด ์๋ ๊ฒ๋ง ํฌํจ
|
| 189 |
+
vol_data = search_volumes.get(selected_api, {})
|
| 190 |
+
final_keywords.append({
|
| 191 |
+
"์ฐ๊ด ํค์๋": selected_kw,
|
| 192 |
+
"PC๊ฒ์๋": vol_data.get("PC๊ฒ์๋", 0),
|
| 193 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": vol_data.get("๋ชจ๋ฐ์ผ๊ฒ์๋", 0),
|
| 194 |
+
"์ด๊ฒ์๋": selected_vol,
|
| 195 |
+
"๊ฒ์๋๊ตฌ๊ฐ": text_utils.get_search_volume_range(selected_vol)
|
| 196 |
+
})
|
| 197 |
+
|
| 198 |
+
# ๊ฒ์๋ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
|
| 199 |
+
final_keywords = sorted(final_keywords, key=lambda x: x["์ด๊ฒ์๋"], reverse=True)
|
| 200 |
+
|
| 201 |
+
# DataFrame ์์ฑ
|
| 202 |
+
df_keywords = pd.DataFrame(final_keywords)
|
| 203 |
+
|
| 204 |
+
logger.info(f"์ฐ๊ด๊ฒ์์ด ๋ถ์ ์๋ฃ: {len(final_keywords)}๊ฐ ํค์๋")
|
| 205 |
+
|
| 206 |
+
return {
|
| 207 |
+
"status": "success",
|
| 208 |
+
"message": f"'{keyword}' ์ฐ๊ด๊ฒ์์ด {len(final_keywords)}๊ฐ๋ฅผ ์ฐพ์์ต๋๋ค.",
|
| 209 |
+
"keywords_df": df_keywords,
|
| 210 |
+
"total_products": len(products_data)
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
except Exception as e:
|
| 214 |
+
logger.error(f"์ฐ๊ด๊ฒ์์ด ๋ถ์ ์ค๋ฅ: {e}")
|
| 215 |
+
return {
|
| 216 |
+
"status": "error",
|
| 217 |
+
"message": f"์ฐ๊ด๊ฒ์์ด ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}",
|
| 218 |
+
"keywords_df": pd.DataFrame()
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
# ===== ๋ก๋ฉ ์ ๋๋ฉ์ด์
=====
|
| 222 |
+
def create_loading_animation():
|
| 223 |
+
"""๋ก๋ฉ ์ ๋๋ฉ์ด์
HTML"""
|
| 224 |
+
return """
|
| 225 |
+
<div style="display: flex; flex-direction: column; align-items: center; padding: 40px; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
|
| 226 |
+
<div style="width: 60px; height: 60px; border: 4px solid #f3f3f3; border-top: 4px solid #FB7F0D; border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 20px;"></div>
|
| 227 |
+
<h3 style="color: #FB7F0D; margin: 10px 0; font-size: 18px;">๋ถ์ ์ค์
๋๋ค...</h3>
|
| 228 |
+
<p style="color: #666; margin: 5px 0; text-align: center;">๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ AI๊ฐ ๋ถ์ํ๊ณ ์์ต๋๋ค.<br>์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์.</p>
|
| 229 |
+
<div style="width: 200px; height: 4px; background: #f0f0f0; border-radius: 2px; margin-top: 15px; overflow: hidden;">
|
| 230 |
+
<div style="width: 100%; height: 100%; background: linear-gradient(90deg, #FB7F0D, #ff9a8b); border-radius: 2px; animation: progress 2s ease-in-out infinite;"></div>
|
| 231 |
+
</div>
|
| 232 |
+
</div>
|
| 233 |
+
|
| 234 |
+
<style>
|
| 235 |
+
@keyframes spin {
|
| 236 |
+
0% { transform: rotate(0deg); }
|
| 237 |
+
100% { transform: rotate(360deg); }
|
| 238 |
}
|
| 239 |
|
| 240 |
+
@keyframes progress {
|
| 241 |
+
0% { transform: translateX(-100%); }
|
| 242 |
+
100% { transform: translateX(100%); }
|
| 243 |
+
}
|
| 244 |
+
</style>
|
| 245 |
+
"""
|
| 246 |
+
|
| 247 |
+
# ===== ์๋ฌ ์ฒ๋ฆฌ ํจ์ =====
|
| 248 |
+
def generate_error_response(error_message):
|
| 249 |
+
"""์๋ฌ ์๋ต ์์ฑ"""
|
| 250 |
+
return f'''
|
| 251 |
+
<div style="color: red; padding: 30px; text-align: center; width: 100%;
|
| 252 |
+
background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb;">
|
| 253 |
+
<h3 style="margin-bottom: 15px;">โ ๋ถ์ ์ค๋ฅ</h3>
|
| 254 |
+
<p style="margin-bottom: 20px;">{error_message}</p>
|
| 255 |
+
<div style="background: white; padding: 15px; border-radius: 8px; color: #333;">
|
| 256 |
+
<h4>ํด๊ฒฐ ๋ฐฉ๋ฒ:</h4>
|
| 257 |
+
<ul style="text-align: left; padding-left: 20px;">
|
| 258 |
+
<li>ํค์๋ ์ฒ ์๋ฅผ ํ์ธํด์ฃผ์ธ์</li>
|
| 259 |
+
<li>๋ ๊ฐ๋จํ ํค์๋๋ฅผ ์ฌ์ฉํด๋ณด์ธ์</li>
|
| 260 |
+
<li>๋คํธ์ํฌ ์ฐ๊ฒฐ์ ํ์ธํด์ฃผ์ธ์</li>
|
| 261 |
+
<li>์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์</li>
|
| 262 |
+
</ul>
|
| 263 |
+
</div>
|
| 264 |
+
</div>
|
| 265 |
+
'''
|
| 266 |
+
|
| 267 |
+
# ===== ๋ฉ์ธ ํค์๋ ๋ถ์ ํจ์ =====
|
| 268 |
+
def safe_keyword_analysis(analysis_keyword, base_keyword, keywords_data):
|
| 269 |
+
"""์๋ฌ ๋ฐฉ์ง๋ฅผ ์ํ ์์ ํ ํค์๋ ๋ถ์ - ์ธ์
๋ณ ๋ฐ์ดํฐ ๋ฐํ"""
|
| 270 |
+
|
| 271 |
+
# ์
๋ ฅ๊ฐ ๊ฒ์ฆ
|
| 272 |
+
if not analysis_keyword or not analysis_keyword.strip():
|
| 273 |
+
return generate_error_response("๋ถ์ํ ํค์๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."), {}
|
| 274 |
+
|
| 275 |
+
analysis_keyword = analysis_keyword.strip()
|
| 276 |
+
|
| 277 |
+
try:
|
| 278 |
+
# ๊ฒ์๋ ์กฐํ - ์๋ฌ ๋ฐฉ์ง
|
| 279 |
+
api_keyword = keyword_analysis.normalize_keyword_for_api(analysis_keyword)
|
| 280 |
+
search_volumes = keyword_search.fetch_all_search_volumes([api_keyword])
|
| 281 |
+
volume_data = search_volumes.get(api_keyword, {"PC๊ฒ์๋": 0, "๋ชจ๋ฐ์ผ๊ฒ์๋": 0, "์ด๊ฒ์๋": 0})
|
| 282 |
+
|
| 283 |
+
# ๊ฒ์๋์ด 0์ด๊ฑฐ๋ ํค์๋๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 284 |
+
if volume_data['์ด๊ฒ์๋'] == 0:
|
| 285 |
+
logger.warning(f"'{analysis_keyword}' ํค์๋์ ๊ฒ์๋์ด 0์ด๊ฑฐ๋ ์กด์ฌํ์ง ์์ต๋๋ค.")
|
| 286 |
+
error_result = f"""
|
| 287 |
+
<div style="padding: 30px; text-align: center; background: #fff3cd; border-radius: 12px; border: 1px solid #ffeaa7;">
|
| 288 |
+
<h3 style="color: #856404; margin-bottom: 15px;">โ ๏ธ ํค์๋ ๋ถ์ ๋ถ๊ฐ</h3>
|
| 289 |
+
<p style="color: #856404; margin-bottom: 10px;"><strong>'{analysis_keyword}'</strong> ํค์๋๋ ๊ฒ์๋์ด ์๊ฑฐ๋ ์ฌ๋ฐ๋ฅด์ง ์์ ํค์๋์
๋๋ค.</p>
|
| 290 |
+
<div style="background: white; padding: 15px; border-radius: 8px; margin-top: 15px;">
|
| 291 |
+
<h4 style="color: #333; margin-bottom: 10px;">๐ก ๊ถ์ฅ์ฌํญ</h4>
|
| 292 |
+
<ul style="text-align: left; color: #666; padding-left: 20px;">
|
| 293 |
+
<li>ํค์๋ ์ฒ ์๋ฅผ ํ์ธํด์ฃผ์ธ์</li>
|
| 294 |
+
<li>๋ ์ผ๋ฐ์ ์ธ ํค์๋๋ฅผ ์ฌ์ฉํด๋ณด์ธ์</li>
|
| 295 |
+
<li>2๋จ๊ณ์์ ์ ์ํ ํค์๋ ๋ชฉ๋ก์ ์ฐธ๊ณ ํด์ฃผ์ธ์</li>
|
| 296 |
+
<li>ํค์๋๋ฅผ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถํด๋ณด์ธ์ (์: '์ฌ์ฑ ์ฌ๋ฆฌํผ')</li>
|
| 297 |
+
</ul>
|
| 298 |
+
</div>
|
| 299 |
+
</div>
|
| 300 |
+
"""
|
| 301 |
+
return error_result, {}
|
| 302 |
+
|
| 303 |
+
logger.info(f"'{analysis_keyword}' ํ์ฌ ๊ฒ์๋: {volume_data['์ด๊ฒ์๋']:,}")
|
| 304 |
+
|
| 305 |
+
# ํธ๋ ๋ ๋ถ์ ์๋
|
| 306 |
+
monthly_data_1year = {}
|
| 307 |
+
monthly_data_3year = {}
|
| 308 |
+
trend_available = False
|
| 309 |
+
|
| 310 |
+
try:
|
| 311 |
+
# ๋ฐ์ดํฐ๋ฉ API ํค ํ์ธ
|
| 312 |
+
datalab_config = api_utils.get_next_datalab_api_config()
|
| 313 |
+
if datalab_config and not datalab_config["CLIENT_ID"].startswith("YOUR_"):
|
| 314 |
+
logger.info("๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ด ์์ด 1๋
, 3๋
ํธ๋ ๋ ๋ถ์์ ์๋ํฉ๋๋ค.")
|
| 315 |
+
|
| 316 |
+
# ์ต์ ํ๋ API ํจ์ ์ฌ์ฉ
|
| 317 |
+
# 1๋
ํธ๋ ๋ ๋ฐ์ดํฐ
|
| 318 |
+
trend_data_1year = trend_analysis_v2.get_naver_trend_data_v5([analysis_keyword], "1year", max_retries=3)
|
| 319 |
+
if trend_data_1year:
|
| 320 |
+
current_volumes = {api_keyword: volume_data}
|
| 321 |
+
monthly_data_1year = trend_analysis_v2.calculate_monthly_volumes_v7([analysis_keyword], current_volumes, trend_data_1year, "1year")
|
| 322 |
+
|
| 323 |
+
# 3๋
ํธ๋ ๋ ๋ฐ์ดํฐ
|
| 324 |
+
trend_data_3year = trend_analysis_v2.get_naver_trend_data_v5([analysis_keyword], "3year", max_retries=3)
|
| 325 |
+
if trend_data_3year:
|
| 326 |
+
current_volumes = {api_keyword: volume_data}
|
| 327 |
+
monthly_data_3year = trend_analysis_v2.calculate_monthly_volumes_v7([analysis_keyword], current_volumes, trend_data_3year, "3year")
|
| 328 |
+
|
| 329 |
+
# 3๋
๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ 1๋
๋ฐ์ดํฐ๋ก ํ์ฅ
|
| 330 |
+
if not monthly_data_3year and monthly_data_1year:
|
| 331 |
+
logger.info("3๋
๋ฐ์ดํฐ๊ฐ ์์ด 1๋
๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก 3๋
์ฐจํธ ์์ฑ")
|
| 332 |
+
keyword = analysis_keyword
|
| 333 |
+
if keyword in monthly_data_1year:
|
| 334 |
+
data_1y = monthly_data_1year[keyword]
|
| 335 |
+
|
| 336 |
+
# 3๋
๋ถ๋์ ๋ ์ง ์์ฑ (24๊ฐ์ ์ถ๊ฐ)
|
| 337 |
+
extended_dates = []
|
| 338 |
+
extended_volumes = []
|
| 339 |
+
|
| 340 |
+
# ๊ธฐ์กด 1๋
๋ฐ์ดํฐ ์ด์ ์ 24๊ฐ์ ์ถ๊ฐ (๋ชจ๋ 0์ผ๋ก)
|
| 341 |
+
start_date = datetime.strptime(data_1y["dates"][0], "%Y-%m-%d")
|
| 342 |
+
for i in range(24, 0, -1):
|
| 343 |
+
prev_date = start_date - timedelta(days=30 * i)
|
| 344 |
+
extended_dates.append(prev_date.strftime("%Y-%m-%d"))
|
| 345 |
+
extended_volumes.append(0)
|
| 346 |
+
|
| 347 |
+
# ๊ธฐ์กด 1๋
๋ฐ์ดํฐ ์ถ๊ฐ (์์ ๋ฐ์ดํฐ ์ ์ธ)
|
| 348 |
+
actual_count = data_1y.get("actual_count", len(data_1y["dates"]))
|
| 349 |
+
extended_dates.extend(data_1y["dates"][:actual_count])
|
| 350 |
+
extended_volumes.extend(data_1y["monthly_volumes"][:actual_count])
|
| 351 |
+
|
| 352 |
+
monthly_data_3year = {
|
| 353 |
+
keyword: {
|
| 354 |
+
"monthly_volumes": extended_volumes,
|
| 355 |
+
"dates": extended_dates,
|
| 356 |
+
"current_volume": data_1y["current_volume"],
|
| 357 |
+
"growth_rate": trend_analysis_v2.calculate_3year_growth_rate_improved(extended_volumes),
|
| 358 |
+
"volume_per_percent": data_1y["volume_per_percent"],
|
| 359 |
+
"current_ratio": data_1y["current_ratio"],
|
| 360 |
+
"actual_count": len(extended_volumes),
|
| 361 |
+
"predicted_count": 0
|
| 362 |
+
}
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
if monthly_data_1year or monthly_data_3year:
|
| 366 |
+
trend_available = True
|
| 367 |
+
logger.info("ํธ๋ ๋ ๋ถ์ ์ฑ๊ณต")
|
| 368 |
+
else:
|
| 369 |
+
logger.info("ํธ๋ ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์คํจ")
|
| 370 |
else:
|
| 371 |
+
logger.info("๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ง ์์")
|
| 372 |
+
except Exception as e:
|
| 373 |
+
logger.info(f"ํธ๋ ๋ ๋ถ์ ๊ฑด๋๋: {str(e)[:100]}")
|
| 374 |
+
|
| 375 |
+
# ํค์๋ ๋ฐ์ดํฐ ์ค๋น
|
| 376 |
+
step2_keywords_df = keywords_data.get("keywords_df") if keywords_data else None
|
| 377 |
+
filtered_keywords_df = step2_keywords_df # ๋จ์ํ ์๋ณธ ๋ฐ์ดํฐ ์ฌ์ฉ
|
| 378 |
+
target_categories = [] # ๋น ๋ฆฌ์คํธ
|
| 379 |
+
|
| 380 |
+
# === ๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์ ์น์
===
|
| 381 |
+
if trend_available and (monthly_data_1year or monthly_data_3year):
|
| 382 |
+
try:
|
| 383 |
+
trend_chart = trend_analysis_v2.create_trend_chart_v7(monthly_data_1year, monthly_data_3year)
|
| 384 |
+
except Exception as e:
|
| 385 |
+
logger.warning(f"ํธ๋ ๋ ์ฐจํธ ์์ฑ ์คํจ, ๊ธฐ๋ณธ ์ฐจํธ ์ฌ์ฉ: {e}")
|
| 386 |
+
trend_chart = trend_analysis_v2.create_enhanced_current_chart(volume_data, analysis_keyword)
|
| 387 |
+
else:
|
| 388 |
+
trend_chart = trend_analysis_v2.create_enhanced_current_chart(volume_data, analysis_keyword)
|
| 389 |
+
|
| 390 |
+
# ํธ๋ ๋ ์น์
|
| 391 |
+
trend_section = f"""
|
| 392 |
+
<div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
|
| 393 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
|
| 394 |
+
<h3 style="margin: 0; font-size: 18px; color: white;">๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์</h3>
|
| 395 |
+
</div>
|
| 396 |
+
<div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">
|
| 397 |
+
{trend_chart}
|
| 398 |
+
</div>
|
| 399 |
+
</div>
|
| 400 |
+
"""
|
| 401 |
+
|
| 402 |
+
# === ๐ฏ ํค์๋ ๋ถ์ ์น์
(AI ๋ถ์) ===
|
| 403 |
+
# api_utils์์ Gemini ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ
|
| 404 |
+
current_gemini_model = api_utils.get_gemini_model()
|
| 405 |
+
|
| 406 |
+
keyword_analysis_html = keyword_analysis.analyze_keyword_for_sourcing(
|
| 407 |
+
analysis_keyword, volume_data, monthly_data_1year, monthly_data_3year,
|
| 408 |
+
filtered_keywords_df, target_categories, current_gemini_model
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
keyword_analysis_section = f"""
|
| 412 |
+
<div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
|
| 413 |
+
<div style="background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
|
| 414 |
+
<h3 style="margin: 0; font-size: 18px; color: white;">๐ฏ ํค์๋ ๋ถ์</h3>
|
| 415 |
+
</div>
|
| 416 |
+
<div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden;">
|
| 417 |
+
{keyword_analysis_html}
|
| 418 |
+
</div>
|
| 419 |
+
</div>
|
| 420 |
+
"""
|
| 421 |
+
|
| 422 |
+
# ๊ฒฝ๊ณ ์น์
(ํ์ํ ๊ฒฝ์ฐ)
|
| 423 |
+
warning_section = ""
|
| 424 |
+
if not trend_available:
|
| 425 |
+
warning_section = f"""
|
| 426 |
+
<div style="width: 100%; margin: 20px auto; padding: 15px; background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; font-family: 'Pretendard', sans-serif;">
|
| 427 |
+
<div style="display: flex; align-items: center;">
|
| 428 |
+
<span style="font-size: 20px; margin-right: 10px;">โ ๏ธ</span>
|
| 429 |
+
<div>
|
| 430 |
+
<strong style="color: #856404;">์ผ๋ถ ๊ธฐ๋ฅ ์ ํ</strong>
|
| 431 |
+
<div style="font-size: 14px; color: #856404; margin-top: 5px;">
|
| 432 |
+
ํธ๋ ๋ ๋ถ์์ ์ ํ์ด ์์ต๋๋ค. ํ์ฌ ๊ฒ์๋ ๋ถ์๊ณผ AI ์ถ์ฒ์ ์ ์ ์ ๊ณต๋ฉ๋๋ค.<br>
|
| 433 |
+
<small>์์ ํ ์ ๋ฐ์ดํฐ ๊ธฐ์ค์ผ๋ก ๋ถ์ํ๊ธฐ ์ํด ์ต์ ์๋ฃ๋ ์๊น์ง๋ง ํ์๋ฉ๋๋ค.</small>
|
| 434 |
+
</div>
|
| 435 |
+
</div>
|
| 436 |
+
</div>
|
| 437 |
+
</div>
|
| 438 |
+
"""
|
| 439 |
+
|
| 440 |
+
# ์ต์ข
๊ฒฐ๊ณผ ์กฐํฉ
|
| 441 |
+
final_result = warning_section + trend_section + keyword_analysis_section
|
| 442 |
+
|
| 443 |
+
# ์ธ์
๋ณ ์ถ๋ ฅ์ฉ ์ํ ๋ฐ์ดํฐ ๋ฐํ (๋ฉํฐ ์ฌ์ฉ์ ์์ )
|
| 444 |
+
session_export_data = {
|
| 445 |
+
"main_keyword": base_keyword,
|
| 446 |
+
"analysis_keyword": analysis_keyword,
|
| 447 |
+
"main_keywords_df": keywords_data.get("keywords_df") if keywords_data else None,
|
| 448 |
+
"related_keywords_df": None, # ์ฌ๊ธฐ์๋ ์ฐ๊ด๊ฒ์์ด ๋ถ์ํ์ง ์์
|
| 449 |
+
"analysis_html": final_result
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
return final_result, session_export_data
|
| 453 |
+
|
| 454 |
+
except Exception as e:
|
| 455 |
+
logger.error(f"ํค์๋ ๋ถ์ ์ค ์ ์ฒด ์ค๋ฅ: {e}")
|
| 456 |
+
error_result = generate_error_response(f"ํค์๋ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 457 |
+
return error_result, {}
|
| 458 |
|
| 459 |
+
# ===== 2๋จ๊ณ: ์ํ ๋ฐ์ดํฐ ๊ธฐ๋ฐ ํค์๋ ์ถ์ถ =====
|
| 460 |
+
def extract_keywords_from_products(keyword):
|
| 461 |
+
"""๋ค์ด๋ฒ ์ผํ์์ ์ค์ ์ํ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ ๋ชจ๋ ํค์๋ ํ์"""
|
| 462 |
+
logger.info(f"์ํ ํค์๋ ์ถ์ถ ์์: ํค์๋='{keyword}'")
|
| 463 |
+
|
| 464 |
+
api_keyword = keyword_analysis.normalize_keyword_for_api(keyword)
|
| 465 |
+
search_results = product_search.fetch_naver_shopping_data(
|
| 466 |
+
keyword, korean_only=True, apply_main_keyword=True, exclude_zero_volume=True
|
| 467 |
+
)
|
| 468 |
|
| 469 |
+
if not search_results.get("product_list"):
|
|
|
|
| 470 |
return {
|
| 471 |
+
"status": "error",
|
| 472 |
+
"message": "์ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.",
|
| 473 |
+
"products": [],
|
| 474 |
+
"keywords": []
|
|
|
|
|
|
|
| 475 |
}
|
| 476 |
|
| 477 |
+
processed_results = keyword_processor.process_search_results(
|
| 478 |
+
search_results, keyword, exclude_zero_volume=True
|
| 479 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
df_keywords = processed_results["keywords_df"]
|
| 482 |
+
df_products = processed_results["products_df"]
|
|
|
|
|
|
|
| 483 |
|
| 484 |
+
if df_keywords.empty:
|
| 485 |
+
return {
|
| 486 |
+
"status": "error",
|
| 487 |
+
"message": "์ถ์ถ๋ ํค์๋๊ฐ ์์ต๋๋ค.",
|
| 488 |
+
"products": [],
|
| 489 |
+
"keywords": []
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
logger.info(f"ํค์๋ ์ถ์ถ ์๋ฃ: ์ด {len(df_keywords)}๊ฐ ํค์๋")
|
| 493 |
+
|
| 494 |
+
return {
|
| 495 |
+
"status": "success",
|
| 496 |
+
"message": "ํค์๋ ์ถ์ถ ์๋ฃ",
|
| 497 |
+
"products": df_products,
|
| 498 |
+
"keywords_df": df_keywords,
|
| 499 |
+
"categories": processed_results["categories"]
|
| 500 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
# ===== ํ์ผ ์ถ๋ ฅ ํจ์๋ค =====
|
| 503 |
def create_timestamp_filename(analysis_keyword):
|
|
|
|
| 508 |
return f"{safe_keyword}_{timestamp}_๋ถ์๊ฒฐ๊ณผ"
|
| 509 |
|
| 510 |
def export_to_excel(main_keyword, main_keywords_df, analysis_keyword, related_keywords_df, filename_base):
|
| 511 |
+
"""์์
ํ์ผ๋ก ์ถ๋ ฅ"""
|
| 512 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
excel_filename = f"{filename_base}.xlsx"
|
| 514 |
excel_path = os.path.join(tempfile.gettempdir(), excel_filename)
|
| 515 |
|
|
|
|
| 541 |
'border': 1
|
| 542 |
})
|
| 543 |
|
| 544 |
+
# ์ฒซ ๋ฒ์งธ ์ํธ: ๋ฉ์ธํค์๋ ์กฐํฉํค์๋
|
| 545 |
+
if main_keywords_df is not None and not main_keywords_df.empty:
|
| 546 |
main_keywords_df.to_excel(writer, sheet_name=f'{main_keyword}_์กฐํฉํค์๋', index=False)
|
| 547 |
worksheet1 = writer.sheets[f'{main_keyword}_์กฐํฉํค์๋']
|
| 548 |
|
|
|
|
| 553 |
# ๋ฐ์ดํฐ ์คํ์ผ ์ ์ฉ
|
| 554 |
for row_num in range(1, len(main_keywords_df) + 1):
|
| 555 |
for col_num, value in enumerate(main_keywords_df.iloc[row_num-1]):
|
| 556 |
+
if col_num in [1, 2, 3]: # PC๊ฒ์๋, ๋ชจ๋ฐ์ผ๊ฒ์๋, ์ด๊ฒ์๋ ์ปฌ๋ผ
|
| 557 |
worksheet1.write(row_num, col_num, value, number_format)
|
| 558 |
else:
|
| 559 |
worksheet1.write(row_num, col_num, value, data_format)
|
|
|
|
| 565 |
len(str(col))
|
| 566 |
)
|
| 567 |
worksheet1.set_column(i, i, min(max_len + 2, 50))
|
|
|
|
|
|
|
| 568 |
|
| 569 |
+
# ๋ ๋ฒ์งธ ์ํธ: ๋ถ์ํค์๋ ์ฐ๊ด๊ฒ์์ด
|
| 570 |
+
if related_keywords_df is not None and not related_keywords_df.empty:
|
| 571 |
related_keywords_df.to_excel(writer, sheet_name=f'{analysis_keyword}_์ฐ๊ด๊ฒ์์ด', index=False)
|
| 572 |
worksheet2 = writer.sheets[f'{analysis_keyword}_์ฐ๊ด๊ฒ์์ด']
|
| 573 |
|
|
|
|
| 578 |
# ๋ฐ์ดํฐ ์คํ์ผ ์ ์ฉ
|
| 579 |
for row_num in range(1, len(related_keywords_df) + 1):
|
| 580 |
for col_num, value in enumerate(related_keywords_df.iloc[row_num-1]):
|
| 581 |
+
if col_num in [1, 2, 3]: # PC๊ฒ์๋, ๋ชจ๋ฐ์ผ๊ฒ์๋, ์ด๊ฒ์๋ ์ปฌ๋ผ
|
| 582 |
worksheet2.write(row_num, col_num, value, number_format)
|
| 583 |
else:
|
| 584 |
worksheet2.write(row_num, col_num, value, data_format)
|
|
|
|
| 590 |
len(str(col))
|
| 591 |
)
|
| 592 |
worksheet2.set_column(i, i, min(max_len + 2, 50))
|
|
|
|
|
|
|
| 593 |
|
| 594 |
logger.info(f"์์
ํ์ผ ์์ฑ ์๋ฃ: {excel_path}")
|
| 595 |
return excel_path
|
|
|
|
| 719 |
<div class="container">
|
| 720 |
<div class="header">
|
| 721 |
<h1><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ถฉ๋ถ์ ๊ฒฐ๊ณผ</h1>
|
| 722 |
+
<p>AI ์ํ ์์ฑ ๋ถ์ ์์คํ
v2.9</p>
|
| 723 |
</div>
|
| 724 |
<div class="content">
|
| 725 |
{analysis_html}
|
|
|
|
| 748 |
zip_filename = f"{filename_base}.zip"
|
| 749 |
zip_path = os.path.join(tempfile.gettempdir(), zip_filename)
|
| 750 |
|
|
|
|
| 751 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 752 |
if excel_path and os.path.exists(excel_path):
|
| 753 |
zipf.write(excel_path, f"{filename_base}.xlsx")
|
| 754 |
logger.info(f"์์
ํ์ผ ์์ถ ์ถ๊ฐ: {filename_base}.xlsx")
|
|
|
|
| 755 |
|
| 756 |
if html_path and os.path.exists(html_path):
|
| 757 |
zipf.write(html_path, f"{filename_base}.html")
|
| 758 |
logger.info(f"HTML ํ์ผ ์์ถ ์ถ๊ฐ: {filename_base}.html")
|
|
|
|
| 759 |
|
| 760 |
+
logger.info(f"์์ถ ํ์ผ ์์ฑ ์๋ฃ: {zip_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 761 |
return zip_path
|
| 762 |
|
| 763 |
except Exception as e:
|
| 764 |
logger.error(f"์์ถ ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
|
| 765 |
return None
|
| 766 |
|
| 767 |
+
def export_analysis_results(export_data):
|
| 768 |
+
"""๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ๋ฉ์ธ ํจ์ - ์ธ์
๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ"""
|
| 769 |
try:
|
| 770 |
+
# ์ถ๋ ฅํ ๋ฐ์ดํฐ ํ์ธ
|
| 771 |
+
if not export_data or not isinstance(export_data, dict):
|
| 772 |
+
return None, "๋ถ์ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค. ๋จผ์ ํค์๋ ์ฌ์ถฉ๋ถ์์ ์คํํด์ฃผ์ธ์."
|
|
|
|
| 773 |
|
| 774 |
+
analysis_keyword = export_data.get("analysis_keyword", "")
|
| 775 |
+
analysis_html = export_data.get("analysis_html", "")
|
| 776 |
+
main_keyword = export_data.get("main_keyword", "")
|
| 777 |
main_keywords_df = export_data.get("main_keywords_df")
|
| 778 |
related_keywords_df = export_data.get("related_keywords_df")
|
| 779 |
|
| 780 |
+
if not analysis_keyword:
|
| 781 |
+
return None, "๋ถ์ํ ํค์๋๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ๋จผ์ ํค์๋ ๋ถ์์ ์คํํด์ฃผ์ธ์."
|
| 782 |
+
|
| 783 |
+
if not analysis_html:
|
| 784 |
+
return None, "๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ๋จผ์ ํค์๋ ์ฌ์ถฉ๋ถ์์ ์คํํด์ฃผ์ธ์."
|
|
|
|
| 785 |
|
| 786 |
# ํ์ผ๋ช
์์ฑ (ํ๊ตญ์๊ฐ ์ ์ฉ)
|
| 787 |
filename_base = create_timestamp_filename(analysis_keyword)
|
| 788 |
+
logger.info(f"์ถ๋ ฅ ํ์ผ๋ช
: {filename_base}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
+
# ์์
ํ์ผ ์์ฑ
|
| 791 |
excel_path = None
|
| 792 |
+
if main_keywords_df is not None or related_keywords_df is not None:
|
|
|
|
|
|
|
| 793 |
excel_path = export_to_excel(
|
| 794 |
main_keyword,
|
| 795 |
main_keywords_df,
|
|
|
|
| 797 |
related_keywords_df,
|
| 798 |
filename_base
|
| 799 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
|
| 801 |
+
# HTML ํ์ผ ์์ฑ
|
| 802 |
+
html_path = export_to_html(analysis_html, filename_base)
|
|
|
|
|
|
|
| 803 |
|
| 804 |
# ์์ถ ํ์ผ ์์ฑ
|
| 805 |
+
if excel_path or html_path:
|
| 806 |
+
zip_path = create_zip_file(excel_path, html_path, filename_base)
|
| 807 |
+
if zip_path:
|
| 808 |
+
return zip_path, f"โ
๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ถ๋ ฅ๋์์ต๋๋ค!\nํ์ผ๋ช
: {filename_base}.zip"
|
| 809 |
+
else:
|
| 810 |
+
return None, "์์ถ ํ์ผ ์์ฑ์ ์คํจํ์ต๋๋ค."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
else:
|
| 812 |
+
return None, "์ถ๋ ฅํ ํ์ผ์ด ์์ต๋๋ค."
|
|
|
|
| 813 |
|
| 814 |
except Exception as e:
|
| 815 |
+
logger.error(f"๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ์ค๋ฅ: {e}")
|
|
|
|
|
|
|
| 816 |
return None, f"์ถ๋ ฅ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 817 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
# ===== ๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค =====
|
| 819 |
def create_interface():
|
| 820 |
+
# CSS ํ์ผ ๋ก๋
|
| 821 |
+
try:
|
| 822 |
+
with open('style.css', 'r', encoding='utf-8') as f:
|
| 823 |
+
custom_css = f.read()
|
| 824 |
+
|
| 825 |
+
with open('keyword_analysis_report.css', 'r', encoding='utf-8') as f:
|
| 826 |
+
keyword_css = f.read()
|
| 827 |
+
custom_css += "\n" + keyword_css
|
| 828 |
+
except:
|
| 829 |
+
custom_css = """
|
| 830 |
+
:root { --primary-color: #FB7F0D; --secondary-color: #ff9a8b; }
|
| 831 |
+
.custom-button {
|
| 832 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
|
| 833 |
+
color: white !important; border-radius: 30px !important; height: 45px !important;
|
| 834 |
+
font-size: 16px !important; font-weight: bold !important; width: 100% !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 835 |
}
|
| 836 |
+
.export-button {
|
| 837 |
+
background: linear-gradient(135deg, #28a745, #20c997) !important;
|
| 838 |
+
color: white !important; border-radius: 25px !important; height: 50px !important;
|
| 839 |
+
font-size: 17px !important; font-weight: bold !important; width: 100% !important;
|
| 840 |
+
margin-top: 20px !important;
|
| 841 |
+
}
|
| 842 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
with gr.Blocks(
|
| 845 |
css=custom_css,
|
| 846 |
+
title="๐ AI ์ํ ์์ฑ ๋ถ์๊ธฐ v2.9",
|
| 847 |
theme=gr.themes.Default(primary_hue="orange", secondary_hue="orange")
|
| 848 |
) as interface:
|
| 849 |
|
|
|
|
| 853 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css">
|
| 854 |
""")
|
| 855 |
|
| 856 |
+
# ์ธ์
๋ณ ์ํ ๋ณ์ (๋ฉํฐ ์ฌ์ฉ์ ์์ )
|
| 857 |
keywords_data_state = gr.State()
|
| 858 |
export_data_state = gr.State({})
|
| 859 |
|
| 860 |
+
# === 1๋จ๊ณ: ๋ฉ์ธ ํค์๋ ์
๋ ฅ ===
|
| 861 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 862 |
gr.HTML('<div class="section-title"><i class="fas fa-search"></i> 1๋จ๊ณ: ๋ฉ์ธ ํค์๋ ์
๋ ฅ</div>')
|
| 863 |
|
|
|
|
| 870 |
|
| 871 |
collect_data_btn = gr.Button("1๋จ๊ณ: ์ํ ๋ฐ์ดํฐ ์์งํ๊ธฐ", elem_classes="custom-button", size="lg")
|
| 872 |
|
| 873 |
+
# === 2๋จ๊ณ: ์์ง๋ ํค์๋ ๋ชฉ๋ก ===
|
| 874 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 875 |
gr.HTML('<div class="section-title"><i class="fas fa-database"></i> 2๋จ๊ณ: ์์ง๋ ํค์๋ ๋ชฉ๋ก</div>')
|
| 876 |
keywords_result = gr.HTML()
|
| 877 |
|
| 878 |
+
# === 3๋จ๊ณ: ๋ถ์ํ ํค์๋ ์ ํ ===
|
| 879 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 880 |
gr.HTML('<div class="section-title"><i class="fas fa-bullseye"></i> 3๋จ๊ณ: ๋ถ์ํ ํค์๋ ์ ํ</div>')
|
| 881 |
|
|
|
|
| 888 |
|
| 889 |
analyze_keyword_btn = gr.Button("ํค์๋ ์ฌ์ถฉ๋ถ์ ํ๊ธฐ", elem_classes="custom-button", size="lg")
|
| 890 |
|
| 891 |
+
# === ํค์๋ ์ฌ์ถฉ๋ถ์ ===
|
| 892 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 893 |
gr.HTML('<div class="section-title"><i class="fas fa-chart-line"></i> ํค์๋ ์ฌ์ถฉ๋ถ์</div>')
|
| 894 |
analysis_result = gr.HTML(label="ํค์๋ ์ฌ์ถฉ๋ถ์")
|
| 895 |
|
| 896 |
+
# === ๊ฒฐ๊ณผ ์ถ๋ ฅ ์น์
===
|
| 897 |
with gr.Column(elem_classes="custom-frame fade-in"):
|
| 898 |
gr.HTML('<div class="section-title"><i class="fas fa-download"></i> ๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 899 |
|
| 900 |
export_btn = gr.Button("๐ ๋ถ์๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ", elem_classes="export-button", size="lg")
|
| 901 |
export_result = gr.HTML()
|
|
|
|
| 909 |
# ๋ก๋ฉ ์ํ ํ์
|
| 910 |
yield (create_loading_animation(), None)
|
| 911 |
|
| 912 |
+
result = extract_keywords_from_products(keyword)
|
| 913 |
+
|
| 914 |
+
if result["status"] == "error":
|
| 915 |
+
yield (f"<div style='color: red; padding: 20px; text-align: center; width: 100%;'>{result['message']}</div>", None)
|
| 916 |
+
return
|
| 917 |
+
|
| 918 |
+
keywords_df = result["keywords_df"]
|
| 919 |
+
html_table = export_utils.create_table_without_checkboxes(keywords_df)
|
| 920 |
|
| 921 |
+
success_html = f"""
|
| 922 |
+
<div style="width: 100%; background: #d4edda; border: 1px solid #c3e6cb; padding: 15px; border-radius: 5px; margin-bottom: 20px;">
|
| 923 |
+
<h4 style="color: #155724; margin: 0 0 10px 0;">โ
๋ค์ด๋ฒ ๋ฐ์ดํฐ ์์ง ์๋ฃ!</h4>
|
| 924 |
+
<p style="margin: 0; color: #155724;">
|
| 925 |
+
โข ์ค์ ์ํ {len(result['products'])}๊ฐ ๋ถ์<br>
|
| 926 |
+
โข ์ถ์ถ๋ ํค์๋: <strong>{len(keywords_df)}๊ฐ</strong><br>
|
| 927 |
+
โข ์๋ ๋ชฉ๋ก์์ ์ํ๋ ํค์๋๋ฅผ ์ ํํ์ฌ ๋ถ์ํ์ธ์
|
| 928 |
+
</p>
|
| 929 |
+
</div>
|
| 930 |
+
|
| 931 |
+
<h5 style="margin: 20px 0 10px 0; color: #495057;">๐ ์ ์ฒด ํค์๋ ๋ชฉ๋ก</h5>
|
| 932 |
+
{html_table}
|
| 933 |
+
"""
|
| 934 |
+
|
| 935 |
+
yield (success_html, result)
|
| 936 |
|
| 937 |
def on_analyze_keyword(analysis_keyword, base_keyword, keywords_data):
|
| 938 |
if not analysis_keyword.strip():
|
|
|
|
| 941 |
# ๋ก๋ฉ ์ํ ํ์
|
| 942 |
yield create_loading_animation(), {}
|
| 943 |
|
| 944 |
+
# ์ฐ๊ด๊ฒ์์ด ๋ถ์ ๋จผ์ ์คํ
|
| 945 |
+
related_result = analyze_related_keywords(analysis_keyword)
|
| 946 |
+
|
| 947 |
+
# ์ค์ ํค์๋ ๋ถ์ ์คํ (์ธ์
๋ณ ๋ฐ์ดํฐ ๋ฐํ)
|
| 948 |
+
keyword_result, session_export_data = safe_keyword_analysis(analysis_keyword, base_keyword, keywords_data)
|
| 949 |
|
| 950 |
+
# ์ฐ๊ด๊ฒ์์ด ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ธ์
๋ฐ์ดํฐ์ ์ถ๊ฐ
|
| 951 |
+
if related_result["status"] == "success" and not related_result["keywords_df"].empty:
|
| 952 |
+
session_export_data["related_keywords_df"] = related_result["keywords_df"]
|
| 953 |
+
|
| 954 |
+
# ์ฐ๊ด๊ฒ์์ด ๋ถ์ ๊ฒฐ๊ณผ HTML ์์ฑ
|
| 955 |
+
if related_result["status"] == "success" and not related_result["keywords_df"].empty:
|
| 956 |
+
df_keywords = related_result["keywords_df"]
|
| 957 |
+
related_table = export_utils.create_table_without_checkboxes(df_keywords)
|
| 958 |
|
| 959 |
+
related_html = f"""
|
| 960 |
+
<div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
|
| 961 |
+
<div style="background: linear-gradient(135deg, #17a2b8 0%, #20c997 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
|
| 962 |
+
<h3 style="margin: 0; font-size: 18px; color: white;">๐ ์ฐ๊ด๊ฒ์์ด ๋ถ์</h3>
|
| 963 |
+
</div>
|
| 964 |
+
<div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">
|
| 965 |
+
<div style="background: #e8f5e8; border: 1px solid #c3e6cb; padding: 15px; border-radius: 5px; margin-bottom: 20px;">
|
| 966 |
+
<h4 style="color: #155724; margin: 0 0 10px 0;">๐ ์ฐ๊ด๊ฒ์์ด ๋ถ์ ์๋ฃ!</h4>
|
| 967 |
+
<p style="margin: 0; color: #155724;">
|
| 968 |
+
โข ๋ถ์ ๊ธฐ์ค ์ํ: <strong>{related_result['total_products']}๊ฐ</strong><br>
|
| 969 |
+
โข ๋ฐ๊ฒฌ๋ ์ฐ๊ด๊ฒ์์ด: <strong>{len(df_keywords)}๊ฐ</strong><br>
|
| 970 |
+
โข ๋ฉ์ธ ํค์๋์ ๊ฒฐํฉ๋ ๋ณตํฉํค์๋๋ง ํ์๋ฉ๋๋ค
|
| 971 |
+
</p>
|
| 972 |
+
</div>
|
| 973 |
+
{related_table}
|
| 974 |
+
</div>
|
| 975 |
+
</div>
|
| 976 |
+
"""
|
| 977 |
+
|
| 978 |
+
# ์ธ์
๋ฐ์ดํฐ์ analysis_html์ ์
๋ฐ์ดํธ
|
| 979 |
+
session_export_data["analysis_html"] = related_html + session_export_data["analysis_html"]
|
| 980 |
+
else:
|
| 981 |
+
related_html = f"""
|
| 982 |
+
<div style="width: 100%; margin: 30px auto; font-family: 'Pretendard', sans-serif;">
|
| 983 |
+
<div style="background: linear-gradient(135deg, #17a2b8 0%, #20c997 100%); padding: 15px; border-radius: 10px 10px 0 0; color: white; text-align: center;">
|
| 984 |
+
<h3 style="margin: 0; font-size: 18px; color: white;">๐ ์ฐ๊ด๊ฒ์์ด ๋ถ์</h3>
|
| 985 |
+
</div>
|
| 986 |
+
<div style="background: white; padding: 20px; border-radius: 0 0 10px 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">
|
| 987 |
+
<div style="color: orange; padding: 20px; text-align: center; background: #fff3cd; border-radius: 8px;">
|
| 988 |
+
'{analysis_keyword}' ํค์๋์ ์ฐ๊ด๊ฒ์์ด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
|
| 989 |
+
</div>
|
| 990 |
+
</div>
|
| 991 |
+
</div>
|
| 992 |
+
"""
|
| 993 |
+
|
| 994 |
+
# ์ธ์
๋ฐ์ดํฐ์ analysis_html์ ์
๋ฐ์ดํธ
|
| 995 |
+
session_export_data["analysis_html"] = related_html + session_export_data["analysis_html"]
|
| 996 |
+
|
| 997 |
+
# ์ต์ข
๊ฒฐ๊ณผ ์กฐํฉ
|
| 998 |
+
final_result = related_html + keyword_result
|
| 999 |
+
yield final_result, session_export_data
|
| 1000 |
+
|
| 1001 |
def on_export_results(export_data):
|
| 1002 |
+
"""๋ถ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ํธ๋ค๋ฌ - ์ธ์
๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ"""
|
| 1003 |
try:
|
| 1004 |
+
zip_path, message = export_analysis_results(export_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1005 |
|
| 1006 |
if zip_path:
|
| 1007 |
+
# ์ฑ๊ณต ๋ฉ์์ง์ ํจ๊ป ๋ค์ด๋ก๋ ํ์ผ ์ ๊ณต
|
| 1008 |
success_html = f"""
|
| 1009 |
<div style="background: #d4edda; border: 1px solid #c3e6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 1010 |
<h4 style="color: #155724; margin: 0 0 15px 0;"><i class="fas fa-check-circle"></i> ์ถ๋ ฅ ์๋ฃ!</h4>
|
| 1011 |
<p style="color: #155724; margin: 0; line-height: 1.6;">
|
| 1012 |
{message}<br>
|
| 1013 |
+
<strong>ํฌํจ ํ์ผ:</strong><br>
|
| 1014 |
+
โข ๐ ์์
ํ์ผ: ๋ฉ์ธํค์๋ ์กฐํฉํค์๋ + ์ฐ๊ด๊ฒ์์ด ๋ฐ์ดํฐ<br>
|
| 1015 |
+
โข ๐ HTML ํ์ผ: ํค์๋ ์ฌ์ถฉ๋ถ์ ๊ฒฐ๊ณผ (๊ทธ๋ํ ํฌํจ)<br>
|
| 1016 |
<br>
|
| 1017 |
<i class="fas fa-download"></i> ์๋ ๋ค์ด๋ก๋ ๋ฒํผ์ ํด๋ฆญํ์ฌ ํ์ผ์ ์ ์ฅํ์ธ์.<br>
|
| 1018 |
<small style="color: #666;">โฐ ํ๊ตญ์๊ฐ ๊ธฐ์ค์ผ๋ก ํ์ผ๋ช
์ด ์์ฑ๋ฉ๋๋ค.</small>
|
|
|
|
| 1021 |
"""
|
| 1022 |
return success_html, gr.update(value=zip_path, visible=True)
|
| 1023 |
else:
|
| 1024 |
+
# ์คํจ ๋ฉ์์ง
|
| 1025 |
error_html = f"""
|
| 1026 |
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 1027 |
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์ถ๋ ฅ ์คํจ</h4>
|
| 1028 |
<p style="color: #721c24; margin: 0;">{message}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1029 |
</div>
|
| 1030 |
"""
|
|
|
|
| 1031 |
return error_html, gr.update(visible=False)
|
| 1032 |
|
| 1033 |
except Exception as e:
|
| 1034 |
+
logger.error(f"์ถ๋ ฅ ํธ๋ค๋ฌ ์ค๋ฅ: {e}")
|
|
|
|
|
|
|
|
|
|
| 1035 |
error_html = f"""
|
| 1036 |
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 20px; border-radius: 8px; margin: 10px 0;">
|
| 1037 |
<h4 style="color: #721c24; margin: 0 0 10px 0;"><i class="fas fa-exclamation-triangle"></i> ์์คํ
์ค๋ฅ</h4>
|
| 1038 |
+
<p style="color: #721c24; margin: 0;">์ถ๋ ฅ ์ค ์์คํ
์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1039 |
</div>
|
| 1040 |
"""
|
| 1041 |
return error_html, gr.update(visible=False)
|
|
|
|
| 1044 |
collect_data_btn.click(
|
| 1045 |
fn=on_collect_data,
|
| 1046 |
inputs=[keyword_input],
|
| 1047 |
+
outputs=[keywords_result, keywords_data_state]
|
|
|
|
| 1048 |
)
|
| 1049 |
|
| 1050 |
analyze_keyword_btn.click(
|
| 1051 |
fn=on_analyze_keyword,
|
| 1052 |
+
inputs=[analysis_keyword_input, keyword_input, keywords_data_state],
|
| 1053 |
+
outputs=[analysis_result, export_data_state]
|
|
|
|
| 1054 |
)
|
| 1055 |
|
| 1056 |
export_btn.click(
|
| 1057 |
fn=on_export_results,
|
| 1058 |
inputs=[export_data_state],
|
| 1059 |
+
outputs=[export_result, download_file]
|
|
|
|
| 1060 |
)
|
| 1061 |
|
| 1062 |
return interface
|
| 1063 |
|
| 1064 |
+
# ===== API ์ค์ ํ์ธ ํจ์ =====
|
| 1065 |
+
def check_datalab_api_config():
|
| 1066 |
+
"""๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ์ค์ ํ์ธ"""
|
| 1067 |
+
logger.info("=== ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ์ค์ ํ์ธ ===")
|
| 1068 |
+
|
| 1069 |
+
datalab_config = api_utils.get_next_datalab_api_config()
|
| 1070 |
+
|
| 1071 |
+
if not datalab_config:
|
| 1072 |
+
logger.warning("โ ๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 1073 |
+
logger.info("ํธ๋ ๋ ๋ถ์ ๊ธฐ๋ฅ์ด ๋นํ์ฑํ๋ฉ๋๋ค.")
|
| 1074 |
+
return False
|
| 1075 |
+
|
| 1076 |
+
client_id = datalab_config["CLIENT_ID"]
|
| 1077 |
+
client_secret = datalab_config["CLIENT_SECRET"]
|
| 1078 |
+
|
| 1079 |
+
logger.info(f"์ด {len(api_utils.NAVER_DATALAB_CONFIGS)}๊ฐ์ ๋ฐ์ดํฐ๋ฉ API ์ค์ ์ฌ์ฉ ์ค")
|
| 1080 |
+
logger.info(f"ํ์ฌ ์ ํ๋ API:")
|
| 1081 |
+
logger.info(f" CLIENT_ID: {client_id[:8]}***{client_id[-4:] if len(client_id) > 12 else '***'}")
|
| 1082 |
+
logger.info(f" CLIENT_SECRET: {client_secret[:4]}***{client_secret[-2:] if len(client_secret) > 6 else '***'}")
|
| 1083 |
+
|
| 1084 |
+
# ๊ธฐ๋ณธ๊ฐ ์ฒดํฌ
|
| 1085 |
+
if client_id.startswith("YOUR_"):
|
| 1086 |
+
logger.error("โ CLIENT_ID๊ฐ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋์ด ์์ต๋๋ค!")
|
| 1087 |
+
return False
|
| 1088 |
+
|
| 1089 |
+
if client_secret.startswith("YOUR_"):
|
| 1090 |
+
logger.error("โ CLIENT_SECRET์ด ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋์ด ์์ต๋๋ค!")
|
| 1091 |
+
return False
|
| 1092 |
+
|
| 1093 |
+
# ๊ธธ์ด ์ฒดํฌ
|
| 1094 |
+
if len(client_id) < 10:
|
| 1095 |
+
logger.warning("โ ๏ธ CLIENT_ID๊ฐ ์งง์ต๋๋ค. ์ฌ๋ฐ๋ฅธ ํค์ธ์ง ํ์ธํด์ฃผ์ธ์.")
|
| 1096 |
+
|
| 1097 |
+
if len(client_secret) < 5:
|
| 1098 |
+
logger.warning("โ ๏ธ CLIENT_SECRET์ด ์งง์ต๋๋ค. ์ฌ๋ฐ๋ฅธ ํค์ธ์ง ํ์ธํด์ฃผ์ธ์.")
|
| 1099 |
+
|
| 1100 |
+
logger.info("โ
๋ฐ์ดํฐ๋ฉ API ํค ํ์ ๊ฒ์ฆ ์๋ฃ")
|
| 1101 |
+
return True
|
| 1102 |
+
|
| 1103 |
+
def check_gemini_api_config():
|
| 1104 |
+
"""Gemini API ์ค์ ํ์ธ"""
|
| 1105 |
+
logger.info("=== Gemini API ์ค์ ํ์ธ ===")
|
| 1106 |
+
|
| 1107 |
+
is_valid, message = api_utils.validate_gemini_config()
|
| 1108 |
+
|
| 1109 |
+
if is_valid:
|
| 1110 |
+
logger.info(f"โ
{message}")
|
| 1111 |
+
# ์ฒซ ๋ฒ์งธ ์ฌ์ฉ ๊ฐ๋ฅํ ํค ํ
์คํธ
|
| 1112 |
+
test_key = api_utils.get_next_gemini_api_key()
|
| 1113 |
+
if test_key:
|
| 1114 |
+
logger.info(f"ํ์ฌ ์ฌ์ฉ ์ค์ธ Gemini API ํค: {test_key[:8]}***{test_key[-4:]}")
|
| 1115 |
+
return True
|
| 1116 |
+
else:
|
| 1117 |
+
logger.warning(f"โ {message}")
|
| 1118 |
+
logger.info("AI ๋ถ์ ๊ธฐ๋ฅ์ด ์ ํ๋ ์ ์์ต๋๋ค.")
|
| 1119 |
+
return False
|
| 1120 |
+
|
| 1121 |
# ===== ๋ฉ์ธ ์คํ =====
|
| 1122 |
if __name__ == "__main__":
|
| 1123 |
# pytz ๋ชจ๋ ์ค์น ํ์ธ
|
|
|
|
| 1125 |
import pytz
|
| 1126 |
logger.info("โ
pytz ๋ชจ๋ ๋ก๋ ์ฑ๊ณต - ํ๊ตญ์๊ฐ ์ง์")
|
| 1127 |
except ImportError:
|
| 1128 |
+
logger.warning("โ ๏ธ pytz ๋ชจ๋์ด ์ค์น๋์ง ์์ - pip install pytz ์คํ ํ์")
|
| 1129 |
logger.info("์์คํ
์๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
| 1130 |
|
| 1131 |
+
# API ์ค์ ์ด๊ธฐํ
|
| 1132 |
+
api_utils.initialize_api_configs()
|
| 1133 |
+
logger.info("===== ์ํ ์์ฑ ๋ถ์ ์์คํ
v2.9 (์ถ๋ ฅ๊ธฐ๋ฅ ์ถ๊ฐ + ํ๊ตญ์๊ฐ + ๋ฉํฐ์ฌ์ฉ์ ์์ ) ์์ =====")
|
| 1134 |
+
|
| 1135 |
+
# ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ์ค์ ํ์ธ
|
| 1136 |
+
datalab_available = check_datalab_api_config()
|
| 1137 |
+
|
| 1138 |
+
# Gemini API ์ค์ ํ์ธ
|
| 1139 |
+
gemini_available = check_gemini_api_config()
|
| 1140 |
+
|
| 1141 |
+
# ํ์ํ ํจํค์ง ์๋ด
|
| 1142 |
+
print("๐ฆ ํ์ํ ํจํค์ง:")
|
| 1143 |
+
print(" pip install gradio google-generativeai pandas requests xlsxwriter markdown plotly pytz")
|
| 1144 |
+
print()
|
| 1145 |
+
|
| 1146 |
+
# API ํค ์ค์ ์๋ด
|
| 1147 |
+
if not gemini_available:
|
| 1148 |
+
print("โ ๏ธ GEMINI_API_KEY ๋๋ GOOGLE_API_KEY ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
| 1149 |
+
print(" export GEMINI_API_KEY='your-api-key'")
|
| 1150 |
+
print(" ๋๋")
|
| 1151 |
+
print(" export GOOGLE_API_KEY='your-api-key'")
|
| 1152 |
+
print()
|
| 1153 |
+
|
| 1154 |
+
if not datalab_available:
|
| 1155 |
+
print("โ ๏ธ ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ํธ๋ ๋ ๋ถ์์ ์ํด์๋:")
|
| 1156 |
+
print(" 1. ๋ค์ด๋ฒ ๊ฐ๋ฐ์์ผํฐ(https://developers.naver.com)์์ ์ ํ๋ฆฌ์ผ์ด์
๋ฑ๋ก")
|
| 1157 |
+
print(" 2. '๋ฐ์ดํฐ๋ฉ(๊ฒ์์ด ํธ๋ ๋)' API ์ถ๊ฐ")
|
| 1158 |
+
print(" 3. ๋ฐ๊ธ๋ฐ์ CLIENT_ID์ CLIENT_SECRET์ api_utils.py์ NAVER_DATALAB_CONFIGS์ ์ค์ ")
|
| 1159 |
+
print(" 4. ํ์ฌ๋ ํ์ฌ ๊ฒ์๋ ์ ๋ณด๋ง ํ์๋ฉ๋๋ค.")
|
| 1160 |
+
print()
|
| 1161 |
+
else:
|
| 1162 |
+
print("โ
๋ฐ์ดํฐ๋ฉ API ์ค์ ์๋ฃ - 1๋
, 3๋
ํธ๋ ๋ ๋ถ์์ด ๊ฐ๋ฅํฉ๋๋ค!")
|
| 1163 |
+
print()
|
| 1164 |
+
|
| 1165 |
+
if gemini_available:
|
| 1166 |
+
print("โ
Gemini API ์ค์ ์๋ฃ - AI ๋ถ์์ด ๊ฐ๋ฅํฉ๋๋ค!")
|
| 1167 |
+
print()
|
| 1168 |
+
|
| 1169 |
+
print("๐ก๏ธ v2.9 ๋ฉํฐ์ฌ์ฉ์ ์์ ๊ฐ์ ์ฌํญ:")
|
| 1170 |
+
print(" โข ์ ์ญ ๋ณ์ export_state ์์ ์ ๊ฑฐ")
|
| 1171 |
+
print(" ๏ฟฝ๏ฟฝ๏ฟฝ gr.State({}) ์ฌ์ฉ์ผ๋ก ๊ฐ ์ฌ์ฉ์๋ณ ์ธ์
๋ฐ์ดํฐ ์์ ๋ถ๋ฆฌ")
|
| 1172 |
+
print(" โข safe_keyword_analysis() ํจ์์์ ์ธ์
๋ณ ๋ฐ์ดํฐ ๋ฐํ")
|
| 1173 |
+
print(" โข export_analysis_results() ํจ์์์ ์ธ์
๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ")
|
| 1174 |
+
print(" โข ์ด๋ฒคํธ ํธ๋ค๋ฌ์์ export_data_state ์ธ์
์ํ ๊ด๋ฆฌ")
|
| 1175 |
+
print(" โข ํ๊น
ํ์ด์ค ์คํ์ด์ค ๋ฑ ๋ฉํฐ์ฌ์ฉ์ ํ๊ฒฝ์์ ์์ ํ ๋์ ์ฌ์ฉ ๋ณด์ฅ")
|
| 1176 |
+
print()
|
| 1177 |
+
|
| 1178 |
+
print("๐ ๊ธฐ์กด v2.9 ๊ธฐ๋ฅ:")
|
| 1179 |
+
print(" โข ์ฐ๊ด๊ฒ์์ด ์์
์ถ๋ ฅ")
|
| 1180 |
+
print(" โข ํค์๋ ์ฌ์ถฉ๋ถ์ HTML ์ถ๋ ฅ")
|
| 1181 |
+
print(" โข ์์ถํ์ผ๋ก ๊ฒฐ๊ณผ ๋ค์ด๋ก๋")
|
| 1182 |
+
print(" โข Gemini API ํค ํตํฉ ๊ด๋ฆฌ")
|
| 1183 |
+
print(" โข ํ๊ตญ์๊ฐ ์ ์ฉ")
|
| 1184 |
+
print()
|
| 1185 |
+
|
| 1186 |
# ์ฑ ์คํ
|
| 1187 |
app = create_interface()
|
| 1188 |
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
category_analysis.py
ADDED
|
@@ -0,0 +1,1029 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ฐ
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import time
|
| 10 |
+
import re
|
| 11 |
+
import random
|
| 12 |
+
from collections import Counter, defaultdict
|
| 13 |
+
import text_utils
|
| 14 |
+
import product_search
|
| 15 |
+
import keyword_search
|
| 16 |
+
import logging
|
| 17 |
+
|
| 18 |
+
# ๋ก๊น
์ค์
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
logger.setLevel(logging.INFO)
|
| 21 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 22 |
+
handler = logging.StreamHandler()
|
| 23 |
+
handler.setFormatter(formatter)
|
| 24 |
+
logger.addHandler(handler)
|
| 25 |
+
|
| 26 |
+
# ๋ง์ง๋ง ํค์๋ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
| 27 |
+
_last_keyword_results = []
|
| 28 |
+
|
| 29 |
+
def get_last_keyword_results():
|
| 30 |
+
"""๋ง์ง๋ง์ผ๋ก ๋ถ์๋ ํค์๋ ๊ฒฐ๊ณผ ๋ฐํ"""
|
| 31 |
+
global _last_keyword_results
|
| 32 |
+
return _last_keyword_results
|
| 33 |
+
|
| 34 |
+
def exponential_backoff_sleep(retry_count, base_delay=0.3, max_delay=5.0):
|
| 35 |
+
"""์ง์ ๋ฐฑ์คํ ๋ฐฉ์์ ๋๊ธฐ ์๊ฐ ๊ณ์ฐ"""
|
| 36 |
+
delay = min(base_delay * (2 ** retry_count), max_delay)
|
| 37 |
+
# ์ฝ๊ฐ์ ๋๋ค์ฑ ์ถ๊ฐ (์งํฐ)
|
| 38 |
+
jitter = random.uniform(0, 0.5) * delay
|
| 39 |
+
time.sleep(delay + jitter)
|
| 40 |
+
|
| 41 |
+
def analyze_product_categories(main_keyword, product_name, category_filter=None):
|
| 42 |
+
"""
|
| 43 |
+
๋ฉ์ธ ํค์๋์ ์ํ๋ช
์ผ๋ก ์นดํ
๊ณ ๋ฆฌ ๋ถ์์ ์ํ
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
main_keyword (str): ๋ฉ์ธ ๊ฒ์ ํค์๋
|
| 47 |
+
product_name (str): ๋ถ์ํ ์ํ๋ช
|
| 48 |
+
category_filter (str, optional): ์นดํ
๊ณ ๋ฆฌ ํํฐ
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
dict: ๋ถ์ ๊ฒฐ๊ณผ
|
| 52 |
+
"""
|
| 53 |
+
logger.info(f"์นดํ
๊ณ ๋ฆฌ ๋ถ์ ์์: ๋ฉ์ธ ํค์๋={main_keyword}, ์ํ๋ช
={product_name}")
|
| 54 |
+
|
| 55 |
+
# 1๋จ๊ณ: ๋ฉ์ธ ํค์๋๋ก 100๊ฐ ์ํ ๊ฐ์ ธ์ค๊ธฐ (10๊ฐ์ฉ 10ํ์ด์ง)
|
| 56 |
+
all_products = []
|
| 57 |
+
for page in range(1, 11):
|
| 58 |
+
result = product_search.fetch_products_by_keyword(main_keyword, page=page, display=10)
|
| 59 |
+
if result["products"]:
|
| 60 |
+
all_products.extend(result["products"])
|
| 61 |
+
exponential_backoff_sleep(0) # API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง
|
| 62 |
+
|
| 63 |
+
if not all_products:
|
| 64 |
+
return {
|
| 65 |
+
"status": "error",
|
| 66 |
+
"message": "์ํ์ ๊ฐ์ ธ์ค์ง ๋ชปํ์ต๋๋ค.",
|
| 67 |
+
"main_keyword": main_keyword,
|
| 68 |
+
"product_name": product_name,
|
| 69 |
+
"total_count": 0,
|
| 70 |
+
"products": [],
|
| 71 |
+
"categories": [],
|
| 72 |
+
"analysis": None
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# 2๋จ๊ณ: ์ํ๋ช
์์ ํค์๋ ์ถ์ถ (๊ฐ์ : ๋ ์ ํํ ํค์๋ ์ถ์ถ)
|
| 76 |
+
product_keywords = []
|
| 77 |
+
|
| 78 |
+
# ๊ณต๋ฐฑ๊ณผ ์ผํ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ถ๋ฆฌ
|
| 79 |
+
words = re.split(r'[,\s]+', product_name)
|
| 80 |
+
for word in words:
|
| 81 |
+
word = word.strip()
|
| 82 |
+
if word and len(word) >= 2: # ์ต์ 2๊ธ์ ์ด์์ธ ๋จ์ด๋ง
|
| 83 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
| 84 |
+
if word not in product_keywords:
|
| 85 |
+
product_keywords.append(word)
|
| 86 |
+
|
| 87 |
+
logger.info(f"์ํ๋ช
์์ ์ถ์ถํ ํค์๋: {product_keywords}")
|
| 88 |
+
|
| 89 |
+
# 3๋จ๊ณ: ์ํ ์นดํ
๊ณ ๋ฆฌ ๋ถ์
|
| 90 |
+
category_counter = Counter()
|
| 91 |
+
products_by_category = defaultdict(list)
|
| 92 |
+
|
| 93 |
+
for product in all_products:
|
| 94 |
+
category = product["์นดํ
๊ณ ๋ฆฌ"]
|
| 95 |
+
category_counter[category] += 1
|
| 96 |
+
products_by_category[category].append(product)
|
| 97 |
+
|
| 98 |
+
# ์นดํ
๊ณ ๋ฆฌ ํํฐ ์ ์ฉ
|
| 99 |
+
if category_filter and category_filter != "์ ์ฒด ๋ณด๊ธฐ":
|
| 100 |
+
# ์นดํ
๊ณ ๋ฆฌ์์ ๊ดํธ ๋ถ๋ถ ์ ๊ฑฐ
|
| 101 |
+
category_filter_clean = category_filter.split(" (")[0] if " (" in category_filter else category_filter
|
| 102 |
+
|
| 103 |
+
filtered_categories = {}
|
| 104 |
+
for cat, count in category_counter.items():
|
| 105 |
+
# ์ํ ์นดํ
๊ณ ๋ฆฌ์์๋ ๊ดํธ ์์ผ๋ฉด ์ ๊ฑฐ
|
| 106 |
+
cat_clean = cat
|
| 107 |
+
if " (" in cat_clean:
|
| 108 |
+
cat_clean = cat_clean.split(" (")[0]
|
| 109 |
+
|
| 110 |
+
if category_filter_clean.lower() in cat_clean.lower():
|
| 111 |
+
filtered_categories[cat] = count
|
| 112 |
+
|
| 113 |
+
category_counter = Counter(filtered_categories)
|
| 114 |
+
|
| 115 |
+
# 4๋จ๊ณ: ํค์๋ ๊ฒ์๋ ์กฐํ
|
| 116 |
+
all_keywords = [main_keyword] + product_keywords
|
| 117 |
+
search_volumes = keyword_search.fetch_all_search_volumes(all_keywords)
|
| 118 |
+
|
| 119 |
+
# 5๋จ๊ณ: ์นดํ
๊ณ ๋ฆฌ๋ณ ๋งค์นญ ์ํ ๋ถ์
|
| 120 |
+
category_matching = []
|
| 121 |
+
|
| 122 |
+
# ์ ๋ ฌ๋ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก (์ถํ ๋น๋์)
|
| 123 |
+
sorted_categories = [cat for cat, _ in category_counter.most_common()]
|
| 124 |
+
|
| 125 |
+
for category in sorted_categories:
|
| 126 |
+
products_in_category = products_by_category[category]
|
| 127 |
+
count = len(products_in_category)
|
| 128 |
+
|
| 129 |
+
# ์ด ์นดํ
๊ณ ๋ฆฌ์ ์ํ ์ํ๋ค ์ค 10๊ฐ๋ง ๊ฐ์ ธ์ด
|
| 130 |
+
sample_products = products_in_category[:100]
|
| 131 |
+
|
| 132 |
+
category_matching.append({
|
| 133 |
+
"์นดํ
๊ณ ๋ฆฌ": category,
|
| 134 |
+
"์ํ์": count,
|
| 135 |
+
"๋งค์นญ์ํ": sample_products
|
| 136 |
+
})
|
| 137 |
+
|
| 138 |
+
# 6๋จ๊ณ: ๊ฒ์๋ ์ ๋ณด ์ถ๊ฐ ๋ฐ ๊ฒฐ๊ณผ ์ ๋ฆฌ
|
| 139 |
+
keyword_info = []
|
| 140 |
+
for kw in all_keywords:
|
| 141 |
+
volume = search_volumes.get(kw, {"PC๊ฒ์๋": 0, "๋ชจ๋ฐ์ผ๊ฒ์๋": 0, "์ด๊ฒ์๋": 0})
|
| 142 |
+
keyword_info.append({
|
| 143 |
+
"ํค์๋": kw,
|
| 144 |
+
"PC๊ฒ์๋": volume.get("PC๊ฒ์๋", 0),
|
| 145 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": volume.get("๋ชจ๋ฐ์ผ๊ฒ์๋", 0),
|
| 146 |
+
"์ด๊ฒ์๋": volume.get("์ด๊ฒ์๋", 0),
|
| 147 |
+
"๊ฒ์๋๊ตฌ๊ฐ": text_utils.get_search_volume_range(volume.get("์ด๊ฒ์๋", 0))
|
| 148 |
+
})
|
| 149 |
+
|
| 150 |
+
# ๊ฒฐ๊ณผ ๋ฐํ
|
| 151 |
+
return {
|
| 152 |
+
"status": "success",
|
| 153 |
+
"message": "๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.",
|
| 154 |
+
"main_keyword": main_keyword,
|
| 155 |
+
"product_name": product_name,
|
| 156 |
+
"total_count": len(all_products),
|
| 157 |
+
"products": all_products,
|
| 158 |
+
"categories": sorted_categories,
|
| 159 |
+
"category_counter": dict(category_counter),
|
| 160 |
+
"category_matching": category_matching,
|
| 161 |
+
"keyword_info": keyword_info
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
def analyze_keywords_by_category(keywords, selected_category, df_all=None):
|
| 165 |
+
"""
|
| 166 |
+
์
๋ ฅ๋ ํค์๋ ๋ชฉ๋ก๊ณผ ์ ํ๋ ์นดํ
๊ณ ๋ฆฌ๋ก ๋ถ์์ ์ํํ๋ ํจ์
|
| 167 |
+
"""
|
| 168 |
+
import re
|
| 169 |
+
|
| 170 |
+
if not keywords or not selected_category:
|
| 171 |
+
return "ํค์๋์ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ชจ๋ ์ ํํด์ฃผ์ธ์."
|
| 172 |
+
|
| 173 |
+
# ์นดํ
๊ณ ๋ฆฌ์์ ์นด์ดํธ ์ ๋ณด ์ ๊ฑฐ (์: "ํจ์
์๋ฅ (10)" -> "ํจ์
์๋ฅ")
|
| 174 |
+
selected_category_clean = selected_category
|
| 175 |
+
is_overall_view = False # '์ ์ฒด ๋ณด๊ธฐ'์ธ์ง ์ฌ๋ถ ํ๋๊ทธ
|
| 176 |
+
if " (" in selected_category and selected_category != "์ ์ฒด ๋ณด๊ธฐ":
|
| 177 |
+
selected_category_clean = selected_category.split(" (")[0]
|
| 178 |
+
elif selected_category == "์ ์ฒด ๋ณด๊ธฐ":
|
| 179 |
+
selected_category_clean = "" # ์ ์ฒด ์นดํ
๊ณ ๋ฆฌ ๋ถ์์ฉ
|
| 180 |
+
is_overall_view = True
|
| 181 |
+
|
| 182 |
+
# ํค์๋ ๋ฆฌ์คํธ ์ฒ๋ฆฌ (์ต๋ 20๊ฐ)
|
| 183 |
+
if isinstance(keywords, str):
|
| 184 |
+
# ์ผํ๋ ์ํฐ๋ก ๋ถ๋ฆฌ
|
| 185 |
+
keywords_list = [k.strip() for k in re.split(r'[,\n]+', keywords) if k.strip()]
|
| 186 |
+
# 20๊ฐ๋ก ์ ํ
|
| 187 |
+
keywords_list = keywords_list[:20]
|
| 188 |
+
else:
|
| 189 |
+
keywords_list = keywords[:20]
|
| 190 |
+
|
| 191 |
+
if not keywords_list:
|
| 192 |
+
return "๋ถ์ํ ํค์๋๊ฐ ์์ต๋๋ค."
|
| 193 |
+
|
| 194 |
+
logger.info(f"์นดํ
๊ณ ๋ฆฌ ๋ถ์ ์์: {len(keywords_list)}๊ฐ ํค์๋, ์ ํ ์นดํ
๊ณ ๋ฆฌ: '{selected_category_clean if not is_overall_view else '์ ์ฒด ๋ณด๊ธฐ'}'")
|
| 195 |
+
|
| 196 |
+
# ๊ฐ์ ๋ HTML ๊ฒฐ๊ณผ - ๋๋น 100% ์ ์ฉ
|
| 197 |
+
result_html = f'''
|
| 198 |
+
<style>
|
| 199 |
+
.result-container {{
|
| 200 |
+
width: 100%;
|
| 201 |
+
margin-top: 20px;
|
| 202 |
+
padding: 15px;
|
| 203 |
+
border-radius: 8px;
|
| 204 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 205 |
+
background-color: #f9f9f9;
|
| 206 |
+
}}
|
| 207 |
+
|
| 208 |
+
.result-header {{
|
| 209 |
+
font-size: 18px;
|
| 210 |
+
font-weight: bold;
|
| 211 |
+
margin-bottom: 15px;
|
| 212 |
+
color: #009879;
|
| 213 |
+
border-bottom: 2px solid #009879;
|
| 214 |
+
padding-bottom: 5px;
|
| 215 |
+
}}
|
| 216 |
+
|
| 217 |
+
.keyword-tags {{
|
| 218 |
+
display: flex;
|
| 219 |
+
flex-wrap: wrap;
|
| 220 |
+
gap: 10px;
|
| 221 |
+
margin-bottom: 20px;
|
| 222 |
+
width: 100%;
|
| 223 |
+
}}
|
| 224 |
+
|
| 225 |
+
.keyword-tag {{
|
| 226 |
+
display: inline-block;
|
| 227 |
+
background-color: #009879;
|
| 228 |
+
color: white;
|
| 229 |
+
padding: 8px 15px;
|
| 230 |
+
border-radius: 20px;
|
| 231 |
+
font-size: 14px;
|
| 232 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 233 |
+
transition: transform 0.2s;
|
| 234 |
+
}}
|
| 235 |
+
|
| 236 |
+
.keyword-tag:hover {{
|
| 237 |
+
transform: translateY(-2px);
|
| 238 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
|
| 239 |
+
}}
|
| 240 |
+
|
| 241 |
+
.category-container {{
|
| 242 |
+
width: 100%;
|
| 243 |
+
margin-bottom: 20px;
|
| 244 |
+
padding: 10px 15px;
|
| 245 |
+
background-color: #f0f8ff;
|
| 246 |
+
border-left: 4px solid #2c7fb8;
|
| 247 |
+
border-radius: 4px;
|
| 248 |
+
}}
|
| 249 |
+
|
| 250 |
+
.category-title {{
|
| 251 |
+
font-weight: bold;
|
| 252 |
+
margin-bottom: 5px;
|
| 253 |
+
color: #2c7fb8;
|
| 254 |
+
}}
|
| 255 |
+
|
| 256 |
+
.category-path {{
|
| 257 |
+
font-size: 16px;
|
| 258 |
+
color: #333;
|
| 259 |
+
word-break: break-word;
|
| 260 |
+
}}
|
| 261 |
+
|
| 262 |
+
.analysis-result {{
|
| 263 |
+
width: 100%;
|
| 264 |
+
margin-top: 30px;
|
| 265 |
+
border: 1px solid #ddd;
|
| 266 |
+
border-radius: 5px;
|
| 267 |
+
padding: 15px;
|
| 268 |
+
background-color: #ffffff;
|
| 269 |
+
}}
|
| 270 |
+
|
| 271 |
+
.result-header-analysis {{
|
| 272 |
+
font-weight: bold;
|
| 273 |
+
margin-bottom: 15px;
|
| 274 |
+
color: #009879;
|
| 275 |
+
font-size: 16px;
|
| 276 |
+
}}
|
| 277 |
+
|
| 278 |
+
.match-item {{
|
| 279 |
+
width: 100%;
|
| 280 |
+
margin: 12px 0;
|
| 281 |
+
padding: 8px 12px;
|
| 282 |
+
border-bottom: 1px solid #eee;
|
| 283 |
+
transition: background-color 0.2s;
|
| 284 |
+
display: grid;
|
| 285 |
+
grid-template-columns: 3fr 1fr 4fr;
|
| 286 |
+
gap: 10px;
|
| 287 |
+
}}
|
| 288 |
+
|
| 289 |
+
.match-item:hover {{
|
| 290 |
+
background-color: #f5f5f5;
|
| 291 |
+
}}
|
| 292 |
+
|
| 293 |
+
.match-keyword {{
|
| 294 |
+
font-weight: bold;
|
| 295 |
+
color: #2c7fb8;
|
| 296 |
+
font-size: 15px;
|
| 297 |
+
}}
|
| 298 |
+
|
| 299 |
+
.match-count {{
|
| 300 |
+
display: inline-block;
|
| 301 |
+
background-color: #009879;
|
| 302 |
+
color: white;
|
| 303 |
+
padding: 3px 10px;
|
| 304 |
+
border-radius: 15px;
|
| 305 |
+
font-size: 13px;
|
| 306 |
+
margin-left: 10px;
|
| 307 |
+
}}
|
| 308 |
+
|
| 309 |
+
.match-status {{
|
| 310 |
+
text-align: center;
|
| 311 |
+
font-weight: bold;
|
| 312 |
+
color: #009879;
|
| 313 |
+
}}
|
| 314 |
+
|
| 315 |
+
.match-categories {{
|
| 316 |
+
color: #555;
|
| 317 |
+
font-size: 14px;
|
| 318 |
+
line-height: 1.4;
|
| 319 |
+
}}
|
| 320 |
+
|
| 321 |
+
.match-header {{
|
| 322 |
+
width: 100%;
|
| 323 |
+
display: grid;
|
| 324 |
+
grid-template-columns: 3fr 1fr 4fr;
|
| 325 |
+
gap: 10px;
|
| 326 |
+
padding: 10px 12px;
|
| 327 |
+
background-color: #e7f7f3;
|
| 328 |
+
border-radius: 5px 5px 0 0;
|
| 329 |
+
font-weight: bold;
|
| 330 |
+
margin-bottom: 10px;
|
| 331 |
+
}}
|
| 332 |
+
</style>
|
| 333 |
+
|
| 334 |
+
<div class="result-container">
|
| 335 |
+
<div class="result-header">๋ถ์ ๊ฒฐ๊ณผ ์์ฝ</div>
|
| 336 |
+
|
| 337 |
+
<div class="keyword-tags">
|
| 338 |
+
<div class="category-title">๋ถ์ ํค์๋ ({len(keywords_list)}๊ฐ)</div>
|
| 339 |
+
<div class="keyword-tags">
|
| 340 |
+
{''.join([f'<span class="keyword-tag">{k}</span>' for k in keywords_list])}
|
| 341 |
+
</div>
|
| 342 |
+
</div>
|
| 343 |
+
|
| 344 |
+
<div class="category-container">
|
| 345 |
+
<div class="category-title">์ ํ๋ ์นดํ
๊ณ ๋ฆฌ</div>
|
| 346 |
+
<div class="category-path">{selected_category}</div>
|
| 347 |
+
</div>
|
| 348 |
+
|
| 349 |
+
<div class="analysis-result">
|
| 350 |
+
<div class="result-header-analysis">์นดํ
๊ณ ๋ฆฌ ์ผ์น ๋ถ์ ๊ฒฐ๊ณผ</div>
|
| 351 |
+
|
| 352 |
+
<div class="match-header">
|
| 353 |
+
<div>ํค์๋</div>
|
| 354 |
+
<div>๋ถ์ ํค์๋</div>
|
| 355 |
+
<div>๋งค์นญ๋ ์นดํ
๊ณ ๋ฆฌ</div>
|
| 356 |
+
</div>
|
| 357 |
+
'''
|
| 358 |
+
|
| 359 |
+
# ํค์๋ ๋ฐฐ์น ์ฒ๋ฆฌ ์ค๋น (5๊ฐ์ฉ ๋ฌถ์)
|
| 360 |
+
batch_size = 5
|
| 361 |
+
batches = []
|
| 362 |
+
for i in range(0, len(keywords_list), batch_size):
|
| 363 |
+
batches.append(keywords_list[i:i + batch_size])
|
| 364 |
+
|
| 365 |
+
logger.info(f"์ด {len(batches)}๊ฐ ๋ฐฐ์น๋ก {len(keywords_list)}๊ฐ ํค์๋ ์ฒ๋ฆฌ")
|
| 366 |
+
|
| 367 |
+
# ๊ฐ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 368 |
+
match_results = {} # ๊ฐ ํค์๋๋ณ ๋งค์นญ ์ ๋ณด ์ ์ฅ (match_count, total_count)
|
| 369 |
+
batch_categories_info = {} # ๊ฐ ํค์๋๋ณ๋ก ์ถ์ถ๋ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ๋ฐ ๋น์จ ์ ์ฅ
|
| 370 |
+
|
| 371 |
+
for batch_idx, batch in enumerate(batches):
|
| 372 |
+
logger.info(f"๋ฐฐ์น {batch_idx+1}/{len(batches)} ์ฒ๋ฆฌ ์ค...")
|
| 373 |
+
|
| 374 |
+
# ๊ฐ ํค์๋ ์ฒ๋ฆฌ
|
| 375 |
+
for keyword in batch:
|
| 376 |
+
max_retries = 3
|
| 377 |
+
retry_count = 0
|
| 378 |
+
api_keyword = keyword.replace(" ", "") # ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 379 |
+
|
| 380 |
+
current_keyword_match_count = 0
|
| 381 |
+
current_keyword_total_products = 0
|
| 382 |
+
current_keyword_categories_found = [] # ๋น์จ๊ณผ ํจ๊ป ์ ์ฅ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฌธ์์ด ๋ฆฌ์คํธ
|
| 383 |
+
|
| 384 |
+
while retry_count < max_retries:
|
| 385 |
+
try:
|
| 386 |
+
# ๋ค์ด๋ฒ API ํธ์ถ
|
| 387 |
+
products = product_search.fetch_naver_shopping_data_for_analysis(api_keyword, count=100) # ์์ 10๊ฐ ์ํ
|
| 388 |
+
|
| 389 |
+
if products:
|
| 390 |
+
current_keyword_total_products = len(products)
|
| 391 |
+
categories_counter_for_keyword = Counter() # ํ์ฌ ํค์๋์ ์ํ๋ค ์นดํ
๊ณ ๋ฆฌ ๋ถํฌ
|
| 392 |
+
|
| 393 |
+
for product in products:
|
| 394 |
+
product_category_full = product.get("category", "") or product.get("์นดํ
๊ณ ๋ฆฌ", "")
|
| 395 |
+
if product_category_full:
|
| 396 |
+
categories_counter_for_keyword[product_category_full] += 1
|
| 397 |
+
|
| 398 |
+
# ์ค์ ๋งค์นญ ์ฌ๋ถ ์นด์ดํธ (is_overall_view๊ฐ ์๋ ๋๋ง)
|
| 399 |
+
if not is_overall_view and selected_category_clean:
|
| 400 |
+
product_category_for_match = product_category_full
|
| 401 |
+
if " (" in product_category_for_match: # ์ํ ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ์์ count ์ ๊ฑฐ
|
| 402 |
+
product_category_for_match = product_category_for_match.split(" (")[0]
|
| 403 |
+
|
| 404 |
+
sel_lower = selected_category_clean.lower()
|
| 405 |
+
prod_lower = product_category_for_match.lower()
|
| 406 |
+
|
| 407 |
+
if sel_lower in prod_lower or prod_lower in sel_lower:
|
| 408 |
+
current_keyword_match_count += 1
|
| 409 |
+
|
| 410 |
+
if is_overall_view: # '์ ์ฒด ๋ณด๊ธฐ'์ผ ๊ฒฝ์ฐ, ๋ชจ๋ ์ํ์ด ๋งค์นญ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผ
|
| 411 |
+
current_keyword_match_count = current_keyword_total_products
|
| 412 |
+
|
| 413 |
+
# ์นดํ
๊ณ ๋ฆฌ๋ณ ๋น์จ ๊ณ์ฐ ๋ฐ ์ ์ฅ
|
| 414 |
+
category_percentages = []
|
| 415 |
+
for cat, count in categories_counter_for_keyword.most_common(): # ๋น๋ ๋์ ์์ผ๋ก
|
| 416 |
+
percentage = (count / current_keyword_total_products) * 100 if current_keyword_total_products > 0 else 0
|
| 417 |
+
category_percentages.append((cat, percentage))
|
| 418 |
+
|
| 419 |
+
# category_percentages.sort(key=lambda x: x[1], reverse=True) # ์ด๋ฏธ most_common์ผ๋ก ์ ๋ ฌ๋จ
|
| 420 |
+
|
| 421 |
+
for cat, percentage in category_percentages:
|
| 422 |
+
current_keyword_categories_found.append(f"{cat} ({percentage:.0f}%)")
|
| 423 |
+
|
| 424 |
+
logger.info(f" - '{keyword}' ์ฒ๋ฆฌ ์๋ฃ: {current_keyword_match_count}/{current_keyword_total_products} ์ผ์น")
|
| 425 |
+
break # ์ฑ๊ณตํ์ผ๋ฏ๋ก ์ฌ์๋ ๋ฃจํ ์ข
๋ฃ
|
| 426 |
+
else:
|
| 427 |
+
logger.warning(f" - '{keyword}' API ๊ฒฐ๊ณผ ์์ (์๋ {retry_count+1}/{max_retries})")
|
| 428 |
+
retry_count += 1
|
| 429 |
+
exponential_backoff_sleep(retry_count)
|
| 430 |
+
|
| 431 |
+
except Exception as e:
|
| 432 |
+
logger.error(f" - '{keyword}' ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e} (์๋ {retry_count+1}/{max_retries})")
|
| 433 |
+
retry_count += 1
|
| 434 |
+
exponential_backoff_sleep(retry_count)
|
| 435 |
+
|
| 436 |
+
# ๊ฒฐ๊ณผ ์ ์ฅ
|
| 437 |
+
if retry_count >= max_retries and current_keyword_total_products == 0: # ์ต์ข
์คํจ
|
| 438 |
+
match_results[keyword] = {
|
| 439 |
+
"match_count": 0,
|
| 440 |
+
"total_count": 0,
|
| 441 |
+
"error": True
|
| 442 |
+
}
|
| 443 |
+
batch_categories_info[keyword] = ["์ค๋ฅ ๋ฐ์"]
|
| 444 |
+
logger.error(f" - '{keyword}' ์ต๋ ์ฌ์๋ ํ ์คํจ")
|
| 445 |
+
else:
|
| 446 |
+
match_results[keyword] = {
|
| 447 |
+
"match_count": current_keyword_match_count,
|
| 448 |
+
"total_count": current_keyword_total_products,
|
| 449 |
+
"error": False
|
| 450 |
+
}
|
| 451 |
+
batch_categories_info[keyword] = current_keyword_categories_found if current_keyword_categories_found else ["์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์์"]
|
| 452 |
+
|
| 453 |
+
# API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง - ์ง์ ๋ฐฑ์คํ ์ฌ์ฉ
|
| 454 |
+
exponential_backoff_sleep(0)
|
| 455 |
+
|
| 456 |
+
logger.info(f"์ ์ฒด {len(keywords_list)}๊ฐ ํค์๋ ์ค {len(match_results)}๊ฐ ์ฒ๋ฆฌ ์๋ฃ")
|
| 457 |
+
|
| 458 |
+
# ๊ฒฐ๊ณผ๋ฅผ HTML๋ก ๋ณํ
|
| 459 |
+
for keyword in keywords_list:
|
| 460 |
+
result = match_results.get(keyword, {"match_count": 0, "total_count": 0, "error": True})
|
| 461 |
+
|
| 462 |
+
# ์์ ๋ ๋ถ๋ถ: keyword_status ๊ฒฐ์ ๋ก์ง ๋ณ๊ฒฝ
|
| 463 |
+
# ์ ํ๋ ์นดํ
๊ณ ๋ฆฌ์ ํ๋๋ผ๋ ๋งค์นญ๋๋ฉด "O", ์๋๋ฉด "X"
|
| 464 |
+
# "์ ์ฒด ๋ณด๊ธฐ" ์ ํ ์์๋ ํญ์ "O" (๋ชจ๋ ์ํ์ด ๋งค์นญ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ์ผ๋ฏ๋ก)
|
| 465 |
+
if result.get("error"):
|
| 466 |
+
keyword_status = "์ค๋ฅ" # ์๋ฌ ๋ฐ์ ์
|
| 467 |
+
status_color = "red"
|
| 468 |
+
elif is_overall_view: # '์ ์ฒด ๋ณด๊ธฐ'์ ๊ฒฝ์ฐ
|
| 469 |
+
keyword_status = "O"
|
| 470 |
+
status_color = "#009879" # Green
|
| 471 |
+
else: # ํน์ ์นดํ
๊ณ ๋ฆฌ ์ ํ ์
|
| 472 |
+
keyword_status = "O" if result["match_count"] > 0 else "X"
|
| 473 |
+
status_color = "#009879" if keyword_status == "O" else "red"
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
# ๋งค์นญ๋ ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด
|
| 477 |
+
categories_html_list = batch_categories_info.get(keyword, ["์ ๋ณด ์์"])
|
| 478 |
+
categories_html = "<br>".join(categories_html_list)
|
| 479 |
+
|
| 480 |
+
if result.get("error", False):
|
| 481 |
+
result_html += f'''
|
| 482 |
+
<div class="match-item">
|
| 483 |
+
<div class="match-keyword">{keyword}</div>
|
| 484 |
+
<div class="match-status" style="color:{status_color}; font-weight:bold;">{keyword_status}</div>
|
| 485 |
+
<div class="match-categories">๋ถ์ ์ค ์ค๋ฅ ๋ฐ์</div>
|
| 486 |
+
</div>
|
| 487 |
+
'''
|
| 488 |
+
else:
|
| 489 |
+
match_count_display = result["match_count"]
|
| 490 |
+
total_count_display = result["total_count"]
|
| 491 |
+
|
| 492 |
+
result_html += f'''
|
| 493 |
+
<div class="match-item">
|
| 494 |
+
<div class="match-keyword">{keyword}<span class="match-count">{match_count_display}/{total_count_display}</span></div>
|
| 495 |
+
<div class="match-status" style="color:{status_color}; font-weight:bold;">{keyword_status}</div>
|
| 496 |
+
<div class="match-categories">{categories_html}</div>
|
| 497 |
+
</div>
|
| 498 |
+
'''
|
| 499 |
+
|
| 500 |
+
result_html += '</div></div></div>' # .analysis-result, .result-container ๋ซ๊ธฐ
|
| 501 |
+
|
| 502 |
+
return result_html
|
| 503 |
+
|
| 504 |
+
def analyze_product_terms(product_name, main_keyword=""):
|
| 505 |
+
"""
|
| 506 |
+
์ํ๋ช
์์ ์ถ์ถํ ํค์๋๋ค์ ๋ถ์ํ์ฌ ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์ ๊ณต (1๋
, 3๋
ํธ๋ ๋ ๋ชจ๋ ๋ถ์)
|
| 507 |
+
|
| 508 |
+
Args:
|
| 509 |
+
product_name (str): ๋ถ์ํ ์ํ๋ช
|
| 510 |
+
main_keyword (str): ๋ฉ์ธ ํค์๋ (optional)
|
| 511 |
+
|
| 512 |
+
Returns:
|
| 513 |
+
tuple: (HTML ํ์์ ๊ฒฐ๊ณผ ํ
์ด๋ธ, ํค์๋ ๋ถ์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ, ํธ๋ ๋ ๋ถ์ ๊ฒฐ๊ณผ)
|
| 514 |
+
"""
|
| 515 |
+
global _last_keyword_results # ํจ์ ์์ ๋ถ๋ถ์ global ์ ์ธ
|
| 516 |
+
|
| 517 |
+
# ์ ์ฒ๋ฆฌ: ์ํ๋ช
์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ ๋ฐ ์ ํจ์ฑ ํ์ธ
|
| 518 |
+
product_name = product_name.strip() if product_name else ""
|
| 519 |
+
if not product_name:
|
| 520 |
+
return "์ํ๋ช
์ด ๋น์ด์์ต๋๋ค. ์ ํจํ ์ํ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์.", [], None
|
| 521 |
+
|
| 522 |
+
# ๋๋ฒ๊น
์ฉ ๋ก๊ทธ
|
| 523 |
+
logger.info(f"๋ถ์ ์์ - ์ํ๋ช
: '{product_name}', ๋ฉ์ธ ํค์๋: '{main_keyword}'")
|
| 524 |
+
|
| 525 |
+
# ์ํ๋ช
์์ ํค์๋๋ฅผ ๋ ์์ฐ์ค๋ฝ๊ฒ ๋ถ๋ฆฌ (๊ณต๋ฐฑ๊ณผ ์ผํ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ)
|
| 526 |
+
# ์์ ๋ ๋ถ๋ถ: ์ ๊ทํํ์ ํจํด ์กฐ์ ๋ฐ ์์ธ์ฒ๋ฆฌ ์ถ๊ฐ
|
| 527 |
+
try:
|
| 528 |
+
words = []
|
| 529 |
+
# ๋จผ์ ์ผํ๋ก ๋ถ๋ฆฌ
|
| 530 |
+
comma_parts = product_name.split(',')
|
| 531 |
+
|
| 532 |
+
for part in comma_parts:
|
| 533 |
+
# ๊ฐ ๋ถ๋ถ์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ
|
| 534 |
+
space_parts = part.split()
|
| 535 |
+
words.extend([word.strip() for word in space_parts if word.strip()])
|
| 536 |
+
|
| 537 |
+
# ์ค๋ณต ์ ๊ฑฐ ๋ฐ 1๊ธ์ ์ด์ ํค์๋๋ง ์ ์ง
|
| 538 |
+
words = list(set([word for word in words if len(word) >= 1]))
|
| 539 |
+
|
| 540 |
+
logger.info(f"์ํ๋ช
์์ ์ถ์ถํ ์๋ณธ ํค์๋ (์ด {len(words)}๊ฐ): {words}")
|
| 541 |
+
|
| 542 |
+
# ํค์๋๊ฐ ํ๋๋ ์ถ์ถ๋์ง ์์๋ค๋ฉด ์๋ณธ ์ํ๋ช
์ฌ์ฉ
|
| 543 |
+
if not words:
|
| 544 |
+
words = [product_name]
|
| 545 |
+
logger.warning(f"ํค์๋ ์ถ์ถ ์คํจ, ์๋ณธ ์ํ๋ช
์ฌ์ฉ: '{product_name}'")
|
| 546 |
+
except Exception as e:
|
| 547 |
+
logger.error(f"ํค์๋ ์ถ์ถ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 548 |
+
# ์ค๋ฅ ๋ฐ์ ์ ์๋ณธ ์ํ๋ช
์ ๊ทธ๋๋ก ์ฌ์ฉ
|
| 549 |
+
words = [product_name]
|
| 550 |
+
|
| 551 |
+
# ๋ฉ์ธ ํค์๋ ์ฒ๋ฆฌ
|
| 552 |
+
if not main_keyword:
|
| 553 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์๋ ๊ฒฝ์ฐ, ์ค์ง์ด ๊ด๋ จ ํค์๋ ์ฐพ๊ธฐ (๊ธฐ์กด ๋ก์ง)
|
| 554 |
+
for word in words:
|
| 555 |
+
if "์ค์ง์ด" in word:
|
| 556 |
+
main_keyword = "์ค์ง์ด"
|
| 557 |
+
break
|
| 558 |
+
|
| 559 |
+
# ์๋ณธ ํค์๋ ๋ชฉ๋ก ์ ์ฅ
|
| 560 |
+
keywords = []
|
| 561 |
+
for word in words:
|
| 562 |
+
# ์ซ์, ์๋ฌธ ๋ฑ์ ํฌํจํ ๋ชจ๋ ๋จ์ด ํ์ฉ
|
| 563 |
+
if word and word != main_keyword:
|
| 564 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์๊ณ , ๋จ์ด์ ๋ฉ์ธ ํค์๋๊ฐ ์์ผ๋ฉด ์กฐํฉ
|
| 565 |
+
if main_keyword and main_keyword not in word:
|
| 566 |
+
# ์กฐํฉ ํค์๋ ์์ฑ (์์ฐ์ค๋ฌ์ด ํํ๋ก)
|
| 567 |
+
combined = f"{word} {main_keyword}"
|
| 568 |
+
if combined not in keywords:
|
| 569 |
+
keywords.append(combined)
|
| 570 |
+
# ์๋ ํค์๋๋ ๋ฐ๋ก ์ถ๊ฐ (๊ฐ์ : ๋จ์ผ ํค์๋๋ ์ ์ง)
|
| 571 |
+
if word not in keywords:
|
| 572 |
+
keywords.append(word)
|
| 573 |
+
|
| 574 |
+
# ๋ฉ์ธ ํค์๋๋ ๋จ๋
์ผ๋ก ์ถ๊ฐ
|
| 575 |
+
if main_keyword and main_keyword not in keywords:
|
| 576 |
+
keywords.append(main_keyword)
|
| 577 |
+
|
| 578 |
+
if not keywords:
|
| 579 |
+
return "์ํ๋ช
์์ ํค์๋๋ฅผ ์ถ์ถํ ์ ์์ต๋๋ค.", [], None
|
| 580 |
+
|
| 581 |
+
logger.info(f"๋ถ์ํ ์ต์ข
ํค์๋ ๋ชฉ๋ก (์ด {len(keywords)}๊ฐ): {keywords}")
|
| 582 |
+
|
| 583 |
+
# ์ถ์ถ๋ ํค์๋๋ฅผ ๋ฐฐ์น๋ก ๋๋๊ธฐ (๋ฐฐ์น๋น 5๊ฐ์ฉ)
|
| 584 |
+
batch_size = 5
|
| 585 |
+
keyword_batches = []
|
| 586 |
+
for i in range(0, len(keywords), batch_size):
|
| 587 |
+
keyword_batches.append(keywords[i:i + batch_size])
|
| 588 |
+
|
| 589 |
+
logger.info(f"์ด {len(keyword_batches)}๊ฐ ๋ฐฐ์น๋ก {len(keywords)}๊ฐ ํค์๋ ์ฒ๋ฆฌ")
|
| 590 |
+
|
| 591 |
+
# ํค์๋ ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ
|
| 592 |
+
keyword_results = []
|
| 593 |
+
|
| 594 |
+
# ๊ฐ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 595 |
+
for batch_idx, batch in enumerate(keyword_batches):
|
| 596 |
+
logger.info(f"๋ฐฐ์น {batch_idx+1}/{len(keyword_batches)} ์ฒ๋ฆฌ ์ค...")
|
| 597 |
+
|
| 598 |
+
# ์ํ ๊ฒ์ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 599 |
+
batch_products = {}
|
| 600 |
+
for keyword in batch:
|
| 601 |
+
# API ํธ์ถ์ฉ ํค์๋ (๊ณต๋ฐฑ ์ ๊ฑฐ)
|
| 602 |
+
api_keyword = keyword.replace(" ", "")
|
| 603 |
+
|
| 604 |
+
# ์ต๋ 3๋ฒ ์ฌ์๋
|
| 605 |
+
max_retries = 3
|
| 606 |
+
retry_count = 0
|
| 607 |
+
|
| 608 |
+
while retry_count < max_retries:
|
| 609 |
+
try:
|
| 610 |
+
# ํค์๋๋ก ์ํ ๊ฒ์
|
| 611 |
+
products = product_search.fetch_naver_shopping_data_for_analysis(api_keyword, count=100)
|
| 612 |
+
|
| 613 |
+
if products:
|
| 614 |
+
batch_products[keyword] = products
|
| 615 |
+
logger.info(f" - '{keyword}' ์ํ ๊ฒ์ ์ฑ๊ณต: {len(products)}๊ฐ")
|
| 616 |
+
break # ์ฑ๊ณตํ์ผ๋ฏ๋ก ๋ฃจํ ์ข
๋ฃ
|
| 617 |
+
else:
|
| 618 |
+
logger.warning(f" - '{keyword}' ์ํ ์์ (์๋ {retry_count+1}/{max_retries})")
|
| 619 |
+
retry_count += 1
|
| 620 |
+
exponential_backoff_sleep(retry_count)
|
| 621 |
+
except Exception as e:
|
| 622 |
+
logger.error(f" - '{keyword}' ์ํ ๊ฒ์ ์ค ์ค๋ฅ: {e} (์๋ {retry_count+1}/{max_retries})")
|
| 623 |
+
retry_count += 1
|
| 624 |
+
exponential_backoff_sleep(retry_count)
|
| 625 |
+
|
| 626 |
+
# ์ต๋ ์ฌ์๋ ํ์๋ ์คํจํ ๊ฒฝ์ฐ ๋ก๊ทธ ๊ธฐ๋ก
|
| 627 |
+
if retry_count >= max_retries and keyword not in batch_products:
|
| 628 |
+
logger.error(f" - '{keyword}' ์ต๋ ์ฌ์๋ ํ ์คํจ")
|
| 629 |
+
|
| 630 |
+
# ๊ฒ์๋ ์กฐํ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 631 |
+
api_keywords = [kw.replace(" ", "") for kw in batch]
|
| 632 |
+
volumes = keyword_search.fetch_all_search_volumes(api_keywords)
|
| 633 |
+
|
| 634 |
+
# ๊ฐ ํค์๋ ์ฒ๋ฆฌ
|
| 635 |
+
for keyword in batch:
|
| 636 |
+
if keyword in batch_products:
|
| 637 |
+
products = batch_products[keyword]
|
| 638 |
+
|
| 639 |
+
# ๊ฐ์ : ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ๊ณผ ํจ๊ป ์นดํ
๊ณ ๋ฆฌ๋ณ ์ ์ ์จ ๊ณ์ฐ
|
| 640 |
+
category_counter = Counter()
|
| 641 |
+
for product in products:
|
| 642 |
+
category = product.get("category", "") or product.get("์นดํ
๊ณ ๋ฆฌ", "")
|
| 643 |
+
if category:
|
| 644 |
+
category_counter[category] += 1
|
| 645 |
+
|
| 646 |
+
# ์นดํ
๊ณ ๋ฆฌ์ ์ ์ ์จ ๊ณ์ฐ
|
| 647 |
+
total_products = len(products)
|
| 648 |
+
categories_with_percentage = []
|
| 649 |
+
for category, count in category_counter.most_common():
|
| 650 |
+
percentage = (count / total_products) * 100 if total_products > 0 else 0
|
| 651 |
+
categories_with_percentage.append(f"{category}({percentage:.0f}%)")
|
| 652 |
+
|
| 653 |
+
# ๊ฒ์๋ ์กฐํ (API ํธ์ถ์ฉ ํค์๋ ์ฌ์ฉ)
|
| 654 |
+
api_keyword = keyword.replace(" ", "")
|
| 655 |
+
volume_data = volumes.get(api_keyword, {"PC๊ฒ์๋": 0, "๋ชจ๋ฐ์ผ๊ฒ์๋": 0, "์ด๊ฒ์๋": 0})
|
| 656 |
+
|
| 657 |
+
# ๊ฒฐ๊ณผ ์ ์ฅ (์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ๊ณผ ์นด์ดํธ ์ ๋ณด ํฌํจ)
|
| 658 |
+
keyword_results.append({
|
| 659 |
+
"ํค์๋": keyword, # UI ํ์์ฉ ํค์๋ (๊ณต๋ฐฑ ํฌํจ)
|
| 660 |
+
"PC๊ฒ์๋": volume_data.get("PC๊ฒ์๋", 0),
|
| 661 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": volume_data.get("๋ชจ๋ฐ์ผ๊ฒ์๋", 0),
|
| 662 |
+
"์ด๊ฒ์๋": volume_data.get("์ด๊ฒ์๋", 0),
|
| 663 |
+
"๊ฒ์๋๊ตฌ๊ฐ": text_utils.get_search_volume_range(volume_data.get("์ด๊ฒ์๋", 0)),
|
| 664 |
+
"์นดํ
๊ณ ๋ฆฌํญ๋ชฉ": "\n".join(categories_with_percentage) if categories_with_percentage else "-",
|
| 665 |
+
"์นดํ
๊ณ ๋ฆฌ์ ๋ณด": dict(category_counter) # ์๋ณธ ์นดํ
๊ณ ๋ฆฌ ์นด์ดํฐ ์ ์ฅ (์์ฝ์ฉ)
|
| 666 |
+
})
|
| 667 |
+
|
| 668 |
+
logger.info(f" - '{keyword}' ๋ถ์ ์๋ฃ: ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ {len(category_counter)}๊ฐ, ๊ฒ์๋ {volume_data.get('์ด๊ฒ์๋', 0)}")
|
| 669 |
+
|
| 670 |
+
# ์ต์ข
๊ฒฐ๊ณผ ์์ฝ ์ถ๋ ฅ
|
| 671 |
+
logger.info(f"ํค์๋ ๋ถ์ ์๋ฃ: ์ด {len(keywords)}๊ฐ ์ค {len(keyword_results)}๊ฐ ์ฑ๊ณต")
|
| 672 |
+
|
| 673 |
+
# ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์๋ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ (๋์ ๊ฒ์ด ๋จผ์ ๋์ค๋๋ก)
|
| 674 |
+
keyword_results = sorted(keyword_results, key=lambda x: x["์ด๊ฒ์๋"], reverse=True)
|
| 675 |
+
|
| 676 |
+
# ์ถ์ฒ ์นดํ
๊ณ ๋ฆฌ ๊ณ์ฐ
|
| 677 |
+
recommended_categories = Counter()
|
| 678 |
+
for result in keyword_results:
|
| 679 |
+
for category, count in result.get("์นดํ
๊ณ ๋ฆฌ์ ๋ณด", {}).items():
|
| 680 |
+
recommended_categories[category] += count
|
| 681 |
+
|
| 682 |
+
# ์ถ์ฒ ์นดํ
๊ณ ๋ฆฌ ์์ 3๊ฐ ์ ํ
|
| 683 |
+
top_categories = recommended_categories.most_common(3)
|
| 684 |
+
|
| 685 |
+
# ์ด ์ํ ์ ๊ณ์ฐ
|
| 686 |
+
total_products_count = sum(recommended_categories.values())
|
| 687 |
+
|
| 688 |
+
# ์นดํ
๊ณ ๋ฆฌ๋ณ ์ ์ ์จ ๊ณ์ฐ
|
| 689 |
+
top_categories_with_percentage = []
|
| 690 |
+
for category, count in top_categories:
|
| 691 |
+
percentage = (count / total_products_count) * 100 if total_products_count > 0 else 0
|
| 692 |
+
top_categories_with_percentage.append({
|
| 693 |
+
"์นดํ
๊ณ ๋ฆฌ": category,
|
| 694 |
+
"๊ฐ์": count,
|
| 695 |
+
"์ ์ ์จ": f"{percentage:.0f}%"
|
| 696 |
+
})
|
| 697 |
+
|
| 698 |
+
# 1๋
, 3๋
ํธ๋ ๋ ๋ถ์ ์คํ
|
| 699 |
+
trend_results = {"1year": None, "3year": None}
|
| 700 |
+
trend_html = ""
|
| 701 |
+
|
| 702 |
+
if keyword_results:
|
| 703 |
+
try:
|
| 704 |
+
# ํธ๋ ๋ ๋ถ์ ๋ชจ๋ import
|
| 705 |
+
import trend_analysis
|
| 706 |
+
|
| 707 |
+
# ์์ 5๊ฐ ํค์๋๋ก 1๋
, 3๋
ํธ๋ ๋ ๋ถ์
|
| 708 |
+
top_keywords = [result["ํค์๋"] for result in keyword_results[:5]]
|
| 709 |
+
logger.info(f"1๋
, 3๋
ํธ๋ ๋ ๋ถ์ ์์: {top_keywords}")
|
| 710 |
+
|
| 711 |
+
# 1๋
ํธ๋ ๋ ๋ถ์
|
| 712 |
+
trend_result_1year = trend_analysis.get_trend_data(top_keywords, "1year")
|
| 713 |
+
if trend_result_1year["status"] == "success":
|
| 714 |
+
trend_results["1year"] = trend_result_1year
|
| 715 |
+
|
| 716 |
+
# 3๋
ํธ๋ ๋ ๋ถ์
|
| 717 |
+
trend_result_3year = trend_analysis.get_trend_data(top_keywords, "3year")
|
| 718 |
+
if trend_result_3year["status"] == "success":
|
| 719 |
+
trend_results["3year"] = trend_result_3year
|
| 720 |
+
|
| 721 |
+
# ํธ๋ ๋ ๋ถ์ HTML ์์ฑ (1๋
, 3๋
๋ชจ๋)
|
| 722 |
+
if trend_results["1year"] or trend_results["3year"]:
|
| 723 |
+
trend_html = f'''
|
| 724 |
+
<div class="trend-analysis-section" style="width: 100%; margin-top: 30px;">
|
| 725 |
+
<div class="section-title">๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์</div>
|
| 726 |
+
'''
|
| 727 |
+
|
| 728 |
+
for period, result in trend_results.items():
|
| 729 |
+
if result and result["status"] == "success":
|
| 730 |
+
period_text = "์ต๊ทผ 1๋
" if period == "1year" else "์ต๊ทผ 3๋
"
|
| 731 |
+
|
| 732 |
+
# ํธ๋ ๋ ์ธ์ฌ์ดํธ ์ถ์ถ
|
| 733 |
+
insights = trend_analysis.analyze_trend_insights(result["trend_data"])
|
| 734 |
+
|
| 735 |
+
trend_html += f'''
|
| 736 |
+
<div class="trend-period-section" style="width: 100%; background-color: #f0f8ff; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
|
| 737 |
+
<div class="insights-title" style="font-weight: bold; margin-bottom: 15px; color: #2c7fb8;">๐ {period_text} ์ฃผ์ ์ธ์ฌ์ดํธ</div>
|
| 738 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; width: 100%;">
|
| 739 |
+
'''
|
| 740 |
+
|
| 741 |
+
for keyword, insight in insights.items():
|
| 742 |
+
growth_icon = "๐" if insight['growth_rate'] > 0 else "๐" if insight['growth_rate'] < 0 else "๐"
|
| 743 |
+
growth_color = "#28a745" if insight['growth_rate'] > 0 else "#dc3545" if insight['growth_rate'] < 0 else "#6c757d"
|
| 744 |
+
|
| 745 |
+
trend_html += f'''
|
| 746 |
+
<div class="insight-item" style="background: white; padding: 12px; border-radius: 6px; border-left: 4px solid {growth_color};">
|
| 747 |
+
<div style="font-weight: bold; color: #2c7fb8; margin-bottom: 8px;">{keyword} {growth_icon}</div>
|
| 748 |
+
<div style="font-size: 13px; line-height: 1.4;">
|
| 749 |
+
<div>๐ ์ต๊ณ ์ : <strong>{insight['max_volume']:,}</strong> ({insight['max_period']})</div>
|
| 750 |
+
<div>๐ ์ ์ฒด ํ๊ท : <strong>{insight['total_avg']:,}</strong></div>
|
| 751 |
+
<div style="color: {growth_color};">๐ ์ฑ์ฅ๋ฅ : <strong>{insight['growth_rate']:+.1f}%</strong></div>
|
| 752 |
+
</div>
|
| 753 |
+
</div>
|
| 754 |
+
'''
|
| 755 |
+
|
| 756 |
+
trend_html += '''
|
| 757 |
+
</div>
|
| 758 |
+
|
| 759 |
+
<div class="trend-graph-container" style="width: 100%; background: white; padding: 15px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-top: 15px;">
|
| 760 |
+
'''
|
| 761 |
+
trend_html += result["graph_html"]
|
| 762 |
+
trend_html += '''
|
| 763 |
+
</div>
|
| 764 |
+
</div>
|
| 765 |
+
'''
|
| 766 |
+
|
| 767 |
+
trend_html += '''
|
| 768 |
+
</div>
|
| 769 |
+
'''
|
| 770 |
+
|
| 771 |
+
logger.info(f"1๋
, 3๋
ํธ๋ ๋ ๋ถ์ ์๋ฃ")
|
| 772 |
+
else:
|
| 773 |
+
logger.warning(f"ํธ๋ ๋ ๋ถ์ ์คํจ")
|
| 774 |
+
trend_html = '''
|
| 775 |
+
<div class="trend-analysis-section" style="width: 100%; margin-top: 30px;">
|
| 776 |
+
<div class="section-title">๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์</div>
|
| 777 |
+
<div style="width: 100%; background-color: #fff3cd; padding: 15px; border-radius: 8px; color: #856404;">
|
| 778 |
+
โ ๏ธ ํธ๋ ๋ ๋ถ์์ ์คํํ ์ ์์ต๋๋ค. ๋์ค์ ๋ค์ ์๋ํด์ฃผ์ธ์.
|
| 779 |
+
</div>
|
| 780 |
+
</div>
|
| 781 |
+
'''
|
| 782 |
+
except Exception as e:
|
| 783 |
+
logger.error(f"ํธ๋ ๋ ๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 784 |
+
trend_html = '''
|
| 785 |
+
<div class="trend-analysis-section" style="width: 100%; margin-top: 30px;">
|
| 786 |
+
<div class="section-title">๐ ๊ฒ์๋ ํธ๋ ๋ ๋ถ์</div>
|
| 787 |
+
<div style="width: 100%; background-color: #f8d7da; padding: 15px; border-radius: 8px; color: #721c24;">
|
| 788 |
+
โ ํธ๋ ๋ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
|
| 789 |
+
</div>
|
| 790 |
+
</div>
|
| 791 |
+
'''
|
| 792 |
+
|
| 793 |
+
# ๊ฒฐ๊ณผ๋ฅผ HTML ํ
์ด๋ธ๋ก ๋ณํ - ๋๋น 100% ์ ์ฉ
|
| 794 |
+
html = f'''
|
| 795 |
+
<style>
|
| 796 |
+
.product-analysis-table {{
|
| 797 |
+
width: 100%;
|
| 798 |
+
border-collapse: collapse;
|
| 799 |
+
margin: 25px 0;
|
| 800 |
+
font-size: 14px;
|
| 801 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
|
| 802 |
+
border-radius: 5px;
|
| 803 |
+
overflow: hidden;
|
| 804 |
+
}}
|
| 805 |
+
|
| 806 |
+
.product-analysis-table thead tr {{
|
| 807 |
+
background-color: #009879;
|
| 808 |
+
color: white;
|
| 809 |
+
text-align: left;
|
| 810 |
+
}}
|
| 811 |
+
|
| 812 |
+
.product-analysis-table th,
|
| 813 |
+
.product-analysis-table td {{
|
| 814 |
+
padding: 12px 15px;
|
| 815 |
+
border-bottom: 1px solid #dddddd;
|
| 816 |
+
}}
|
| 817 |
+
|
| 818 |
+
.product-analysis-table tbody tr {{
|
| 819 |
+
background-color: white;
|
| 820 |
+
}}
|
| 821 |
+
|
| 822 |
+
.product-analysis-table tbody tr:nth-of-type(even) {{
|
| 823 |
+
background-color: #f3f3f3;
|
| 824 |
+
}}
|
| 825 |
+
|
| 826 |
+
.product-analysis-table tbody tr:hover {{
|
| 827 |
+
background-color: #f5f5f5;
|
| 828 |
+
}}
|
| 829 |
+
|
| 830 |
+
.product-analysis-table tbody tr:last-of-type {{
|
| 831 |
+
border-bottom: 2px solid #009879;
|
| 832 |
+
}}
|
| 833 |
+
|
| 834 |
+
.section-title {{
|
| 835 |
+
font-size: 18px;
|
| 836 |
+
font-weight: bold;
|
| 837 |
+
color: #009879;
|
| 838 |
+
margin-bottom: 15px;
|
| 839 |
+
padding-bottom: 5px;
|
| 840 |
+
border-bottom: 2px solid #009879;
|
| 841 |
+
}}
|
| 842 |
+
|
| 843 |
+
.summary-box {{
|
| 844 |
+
width: 100%;
|
| 845 |
+
background-color: #f5f5f5;
|
| 846 |
+
border-left: 4px solid #009879;
|
| 847 |
+
padding: 10px 15px;
|
| 848 |
+
margin-bottom: 20px;
|
| 849 |
+
font-size: 14px;
|
| 850 |
+
}}
|
| 851 |
+
|
| 852 |
+
.summary-title {{
|
| 853 |
+
font-weight: bold;
|
| 854 |
+
margin-bottom: 5px;
|
| 855 |
+
}}
|
| 856 |
+
|
| 857 |
+
.recommendation-box {{
|
| 858 |
+
width: 100%;
|
| 859 |
+
background-color: #e7f7f3;
|
| 860 |
+
border-radius: 5px;
|
| 861 |
+
padding: 15px;
|
| 862 |
+
margin-bottom: 25px;
|
| 863 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 864 |
+
}}
|
| 865 |
+
|
| 866 |
+
.recommendation-title {{
|
| 867 |
+
font-weight: bold;
|
| 868 |
+
font-size: 16px;
|
| 869 |
+
color: #009879;
|
| 870 |
+
margin-bottom: 10px;
|
| 871 |
+
}}
|
| 872 |
+
|
| 873 |
+
.recommendation-item {{
|
| 874 |
+
padding: 6px 0;
|
| 875 |
+
border-bottom: 1px solid #e0e0e0;
|
| 876 |
+
}}
|
| 877 |
+
|
| 878 |
+
.recommendation-item:last-child {{
|
| 879 |
+
border-bottom: none;
|
| 880 |
+
}}
|
| 881 |
+
</style>
|
| 882 |
+
|
| 883 |
+
<div style="width: 100%;">
|
| 884 |
+
<div class="section-title">์ํ๋ช
ํค์๋ ๋ถ์ ๊ฒฐ๊ณผ</div>
|
| 885 |
+
|
| 886 |
+
<div class="summary-box">
|
| 887 |
+
<div class="summary-title">๋ถ์ ์์ฝ</div>
|
| 888 |
+
<p>์ด <strong>{len(keyword_results)}</strong>๊ฐ ํค์๋ ๋ถ์</p>
|
| 889 |
+
<p>๋ฉ์ธ ํค์๋: <strong>{main_keyword if main_keyword else '์์'}</strong></p>
|
| 890 |
+
</div>
|
| 891 |
+
|
| 892 |
+
<div class="recommendation-box">
|
| 893 |
+
<div class="recommendation-title">์ถ์ฒ ์นดํ
๊ณ ๋ฆฌ</div>
|
| 894 |
+
'''
|
| 895 |
+
|
| 896 |
+
# ์ถ์ฒ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ์ถ๊ฐ
|
| 897 |
+
for idx, cat_info in enumerate(top_categories_with_percentage, 1):
|
| 898 |
+
html += f'''
|
| 899 |
+
<div class="recommendation-item">
|
| 900 |
+
์ถ์ฒ ์นดํ
๊ณ ๋ฆฌ {idx} : {cat_info['์นดํ
๊ณ ๋ฆฌ']}({cat_info['์ ์ ์จ']})
|
| 901 |
+
</div>
|
| 902 |
+
'''
|
| 903 |
+
|
| 904 |
+
html += '''
|
| 905 |
+
</div>
|
| 906 |
+
|
| 907 |
+
<table class="product-analysis-table">
|
| 908 |
+
<thead>
|
| 909 |
+
<tr>
|
| 910 |
+
<th>์๋ฒ</th>
|
| 911 |
+
<th>ํค์๋</th>
|
| 912 |
+
<th>PC๊ฒ์๋</th>
|
| 913 |
+
<th>๋ชจ๋ฐ์ผ๊ฒ์๋</th>
|
| 914 |
+
<th>์ด๊ฒ์๋</th>
|
| 915 |
+
<th>๊ฒ์๋๊ตฌ๊ฐ</th>
|
| 916 |
+
<th>์นดํ
๊ณ ๋ฆฌํญ๋ชฉ</th>
|
| 917 |
+
</tr>
|
| 918 |
+
</thead>
|
| 919 |
+
<tbody>
|
| 920 |
+
'''
|
| 921 |
+
|
| 922 |
+
for idx, result in enumerate(keyword_results):
|
| 923 |
+
# ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์ค๋น (์ค๋ฐ๊ฟ์ <br>๋ก ๋ณํ)
|
| 924 |
+
category_items = result.get("์นดํ
๊ณ ๋ฆฌํญ๋ชฉ", "-").replace("\n", "<br>")
|
| 925 |
+
|
| 926 |
+
html += f'''
|
| 927 |
+
<tr>
|
| 928 |
+
<td>{idx + 1}</td>
|
| 929 |
+
<td>{result["ํค์๋"]}</td>
|
| 930 |
+
<td>{result["PC๊ฒ์๋"]:,}</td>
|
| 931 |
+
<td>{result["๋ชจ๋ฐ์ผ๊ฒ์๋"]:,}</td>
|
| 932 |
+
<td>{result["์ด๊ฒ์๋"]:,}</td>
|
| 933 |
+
<td>{result["๊ฒ์๋๊ตฌ๊ฐ"]}</td>
|
| 934 |
+
<td>{category_items}</td>
|
| 935 |
+
</tr>
|
| 936 |
+
'''
|
| 937 |
+
|
| 938 |
+
html += '''
|
| 939 |
+
</tbody>
|
| 940 |
+
</table>
|
| 941 |
+
</div>
|
| 942 |
+
'''
|
| 943 |
+
|
| 944 |
+
# ํธ๋ ๋ ๋ถ์ HTML ์ถ๊ฐ
|
| 945 |
+
html += trend_html
|
| 946 |
+
|
| 947 |
+
# ๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์๋ ๊ฒฝ์ฐ ์๋ด ๋ฉ์์ง
|
| 948 |
+
if not keyword_results:
|
| 949 |
+
html += '''
|
| 950 |
+
<div style="width: 100%; margin-top: 20px; padding: 15px; background-color: #f1f1f1; border-radius: 5px; text-align: center;">
|
| 951 |
+
<p>ํ์ํ ํค์๋๊ฐ ์์ต๋๋ค. ๋ค๋ฅธ ์ํ๋ช
์ ์
๋ ฅํด๋ณด์ธ์.</p>
|
| 952 |
+
</div>
|
| 953 |
+
'''
|
| 954 |
+
|
| 955 |
+
# ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ญ ๋ณ์์ ์ ์ฅ (๋ค์ด๋ก๋์ฉ)
|
| 956 |
+
_last_keyword_results = keyword_results
|
| 957 |
+
|
| 958 |
+
# HTML๊ณผ ํจ๊ป ํค์๋ ๋ถ์ ๊ฒฐ๊ณผ ๋ฐ ํธ๋ ๋ ๊ฒฐ๊ณผ๋ ํจ๊ป ๋ฐํ
|
| 959 |
+
return html, keyword_results, trend_results
|
| 960 |
+
|
| 961 |
+
def collect_categories_per_keyword(keywords, max_products=10):
|
| 962 |
+
"""
|
| 963 |
+
ํค์๋๋ง๋ค ์ํ n๊ฐ๋ฅผ ํธ์ถํ์ฌ ์นดํ
๊ณ ๋ฆฌ ์งํฉ ๋ฐํ
|
| 964 |
+
|
| 965 |
+
Args:
|
| 966 |
+
keywords (list): ํค์๋ ๋ชฉ๋ก
|
| 967 |
+
max_products (int): ํค์๋๋น ๊ฒ์ํ ์ต๋ ์ํ ์
|
| 968 |
+
|
| 969 |
+
Returns:
|
| 970 |
+
dict: ํค์๋๋ณ ์นดํ
๊ณ ๋ฆฌ ์งํฉ์ ๋ด์ ์ฌ์ {ํค์๋: {์นดํ
๊ณ ๋ฆฌ1, ์นดํ
๊ณ ๋ฆฌ2, ...}}
|
| 971 |
+
"""
|
| 972 |
+
logger.info(f"ํค์๋๋ณ ์นดํ
๊ณ ๋ฆฌ ์์ง ์์: {len(keywords)}๊ฐ ํค์๋")
|
| 973 |
+
keyword_category_map = {}
|
| 974 |
+
|
| 975 |
+
# ๋ฐฐ์น ์ฒ๋ฆฌ๋ฅผ ์ํ ์ค๋น
|
| 976 |
+
batch_size = 5
|
| 977 |
+
batches = []
|
| 978 |
+
for i in range(0, len(keywords), batch_size):
|
| 979 |
+
batches.append(keywords[i:i + batch_size])
|
| 980 |
+
|
| 981 |
+
logger.info(f"์ด {len(batches)}๊ฐ ๋ฐฐ์น๋ก {len(keywords)}๊ฐ ํค์๋ ์ฒ๋ฆฌ")
|
| 982 |
+
|
| 983 |
+
# ๊ฐ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 984 |
+
for batch_idx, batch in enumerate(batches):
|
| 985 |
+
logger.info(f"๋ฐฐ์น {batch_idx+1}/{len(batches)} ์ฒ๋ฆฌ ์ค...")
|
| 986 |
+
|
| 987 |
+
for keyword in batch:
|
| 988 |
+
# API ํธ์ถ์ฉ ํค์๋ (๊ณต๋ฐฑ ์ ๊ฑฐ)
|
| 989 |
+
api_keyword = keyword.replace(" ", "")
|
| 990 |
+
|
| 991 |
+
max_retries = 3
|
| 992 |
+
retry_count = 0
|
| 993 |
+
|
| 994 |
+
while retry_count < max_retries:
|
| 995 |
+
try:
|
| 996 |
+
# ํค์๋๋ก ์ํ ๊ฒ์
|
| 997 |
+
products = product_search.fetch_naver_shopping_data_for_analysis(api_keyword, count=max_products)
|
| 998 |
+
|
| 999 |
+
if products:
|
| 1000 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ถ์ถ
|
| 1001 |
+
categories = set()
|
| 1002 |
+
for product in products:
|
| 1003 |
+
# ๋ ๊ฐ์ง ํค ๋ชจ๋ ์๋ (category์ ์นดํ
๊ณ ๋ฆฌ)
|
| 1004 |
+
category = product.get("category", "") or product.get("์นดํ
๊ณ ๋ฆฌ", "")
|
| 1005 |
+
if category:
|
| 1006 |
+
categories.add(category)
|
| 1007 |
+
|
| 1008 |
+
keyword_category_map[keyword] = categories
|
| 1009 |
+
logger.info(f" - '{keyword}' ์นดํ
๊ณ ๋ฆฌ ์์ง ์๋ฃ: {len(categories)}๊ฐ")
|
| 1010 |
+
break # ์ฑ๊ณตํ์ผ๋ฏ๋ก ๋ฃจํ ์ข
๋ฃ
|
| 1011 |
+
else:
|
| 1012 |
+
logger.warning(f" - '{keyword}' ์ํ ๊ฒ์ ๊ฒฐ๊ณผ ์์ (์๋ {retry_count+1}/{max_retries})")
|
| 1013 |
+
retry_count += 1
|
| 1014 |
+
exponential_backoff_sleep(retry_count)
|
| 1015 |
+
|
| 1016 |
+
except Exception as e:
|
| 1017 |
+
logger.error(f" - '{keyword}' ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e} (์๋ {retry_count+1}/{max_retries})")
|
| 1018 |
+
retry_count += 1
|
| 1019 |
+
exponential_backoff_sleep(retry_count)
|
| 1020 |
+
|
| 1021 |
+
if retry_count >= max_retries:
|
| 1022 |
+
logger.error(f" - '{keyword}' ์ต๋ ์ฌ์๋ ํ ์คํจ")
|
| 1023 |
+
keyword_category_map[keyword] = set()
|
| 1024 |
+
|
| 1025 |
+
# API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง (์์ ์ ์ธ ์ง์ฐ์ผ๋ก ๋ณ๊ฒฝ)
|
| 1026 |
+
exponential_backoff_sleep(0) # ์ด๊ธฐ ์ง์ฐ ์ ์ฉ
|
| 1027 |
+
|
| 1028 |
+
logger.info(f"ํค์๋๋ณ ์นดํ
๊ณ ๋ฆฌ ์์ง ์๋ฃ: {len(keyword_category_map)}๊ฐ")
|
| 1029 |
+
return keyword_category_map
|
export_utils.py
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ๋ชจ์ - ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์ ๊ฑฐ + ํค์๋ ํด๋ฆญ ์ ๋ค์ด๋ฒ ์ผํ ์ด๋ ๊ธฐ๋ฅ ์ถ๊ฐ
|
| 3 |
+
- HTML ํ
์ด๋ธ ์์ฑ
|
| 4 |
+
- ์์
ํ์ผ ์์ฑ
|
| 5 |
+
- ํค์๋ ํด๋ฆญ ์ ๋ค์ด๋ฒ ์ผํ ๋งํฌ ๊ธฐ๋ฅ
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
+
import threading
|
| 12 |
+
import time
|
| 13 |
+
import logging
|
| 14 |
+
import urllib.parse # URL ์ธ์ฝ๋ฉ์ ์ํด ์ถ๊ฐ
|
| 15 |
+
|
| 16 |
+
# ๋ก๊น
์ค์
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
logger.setLevel(logging.INFO)
|
| 19 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 20 |
+
handler = logging.StreamHandler()
|
| 21 |
+
handler.setFormatter(formatter)
|
| 22 |
+
logger.addHandler(handler)
|
| 23 |
+
|
| 24 |
+
# ์์ ํ์ผ ์ถ์ ๋ฆฌ์คํธ
|
| 25 |
+
_temp_files = []
|
| 26 |
+
|
| 27 |
+
def create_table_without_checkboxes(df):
|
| 28 |
+
"""DataFrame์ HTML ํ
์ด๋ธ๋ก ๋ณํ - ํค์๋ ํด๋ฆญ ์ ๋ค์ด๋ฒ ์ผํ ์ด๋ ๊ธฐ๋ฅ ์ถ๊ฐ"""
|
| 29 |
+
if df.empty:
|
| 30 |
+
return "<p>๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.</p>"
|
| 31 |
+
|
| 32 |
+
# === ์์ ๋ ๋ถ๋ถ: ์นดํ
๊ณ ๋ฆฌ ๊ด๋ จ ์ด ์ ๊ฑฐ ===
|
| 33 |
+
df_display = df.copy()
|
| 34 |
+
|
| 35 |
+
# "์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ(์์100์)" ๋๋ "๊ด๋ จ ์นดํ
๊ณ ๋ฆฌ", "์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ" ์ด์ด ์์ผ๋ฉด ์ ๊ฑฐ
|
| 36 |
+
columns_to_remove = ["์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ(์์100์)", "๊ด๋ จ ์นดํ
๊ณ ๋ฆฌ", "์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ"]
|
| 37 |
+
for col in columns_to_remove:
|
| 38 |
+
if col in df_display.columns:
|
| 39 |
+
df_display = df_display.drop(columns=[col])
|
| 40 |
+
logger.info(f"ํ
์ด๋ธ์์ '{col}' ์ด ์ ๊ฑฐ๋จ")
|
| 41 |
+
|
| 42 |
+
# HTML ํ
์ด๋ธ ์คํ์ผ ์ ์ - Z-INDEX ์์
|
| 43 |
+
html = '''
|
| 44 |
+
<style>
|
| 45 |
+
.table-container {
|
| 46 |
+
position: relative;
|
| 47 |
+
width: 100%;
|
| 48 |
+
margin: 0;
|
| 49 |
+
border-radius: 8px;
|
| 50 |
+
overflow: hidden;
|
| 51 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.header-wrap {
|
| 55 |
+
position: sticky;
|
| 56 |
+
top: 0;
|
| 57 |
+
z-index: 100; /* z-index ์ฆ๊ฐ */
|
| 58 |
+
background-color: #009879;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
.styled-table {
|
| 62 |
+
width: 100%;
|
| 63 |
+
border-collapse: collapse;
|
| 64 |
+
table-layout: fixed;
|
| 65 |
+
margin: 0;
|
| 66 |
+
padding: 0;
|
| 67 |
+
font-size: 14px;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.styled-table th,
|
| 71 |
+
.styled-table td {
|
| 72 |
+
padding: 12px 15px;
|
| 73 |
+
text-align: left;
|
| 74 |
+
border-bottom: 1px solid #dddddd;
|
| 75 |
+
overflow: hidden;
|
| 76 |
+
text-overflow: ellipsis;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* ๊ธด ํ
์คํธ๊ฐ ์
์์ ์ค๋ฐ๊ฟ๋๋๋ก ์์ */
|
| 80 |
+
.styled-table td.col-rank {
|
| 81 |
+
white-space: normal;
|
| 82 |
+
word-break: break-word;
|
| 83 |
+
line-height: 1.3;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* ๊ทธ ์ธ ์ด์ ํ ์ค๋ก ํ์ */
|
| 87 |
+
.styled-table td.col-seq,
|
| 88 |
+
.styled-table td.col-keyword,
|
| 89 |
+
.styled-table td.col-pc,
|
| 90 |
+
.styled-table td.col-mobile,
|
| 91 |
+
.styled-table td.col-total,
|
| 92 |
+
.styled-table td.col-range,
|
| 93 |
+
.styled-table td.col-count {
|
| 94 |
+
white-space: nowrap;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.styled-table th {
|
| 98 |
+
background-color: #009879;
|
| 99 |
+
color: white;
|
| 100 |
+
font-weight: bold;
|
| 101 |
+
position: sticky;
|
| 102 |
+
top: 0;
|
| 103 |
+
white-space: nowrap;
|
| 104 |
+
z-index: 50; /* ํค๋ z-index ์ฆ๊ฐ */
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.styled-table tbody tr:nth-of-type(even) {
|
| 108 |
+
background-color: #f3f3f3;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
.styled-table tbody tr:hover {
|
| 112 |
+
background-color: #f0f0f0;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
.styled-table tbody tr:last-of-type {
|
| 116 |
+
border-bottom: 2px solid #009879;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* ๋ฐ์ดํฐ ์
z-index ์ค์ */
|
| 120 |
+
.styled-table tbody td {
|
| 121 |
+
position: relative;
|
| 122 |
+
z-index: 1; /* ๋ฐ์ดํฐ ์
์ ๋ฎ์ z-index */
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.data-container {
|
| 126 |
+
max-height: 600px;
|
| 127 |
+
overflow-y: auto;
|
| 128 |
+
position: relative; /* position ์ถ๊ฐ */
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/* ์คํฌ๋กค๋ฐ ์คํ์ผ */
|
| 132 |
+
.data-container::-webkit-scrollbar {
|
| 133 |
+
width: 10px;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.data-container::-webkit-scrollbar-track {
|
| 137 |
+
background: #f1f1f1;
|
| 138 |
+
border-radius: 5px;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
.data-container::-webkit-scrollbar-thumb {
|
| 142 |
+
background: #888;
|
| 143 |
+
border-radius: 5px;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
.data-container::-webkit-scrollbar-thumb:hover {
|
| 147 |
+
background: #555;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
/* ํค์๋ ๋งํฌ ์คํ์ผ - ์๋ก ์ถ๊ฐ */
|
| 151 |
+
.keyword-link {
|
| 152 |
+
color: #2c5aa0;
|
| 153 |
+
text-decoration: none;
|
| 154 |
+
font-weight: 600;
|
| 155 |
+
cursor: pointer;
|
| 156 |
+
transition: all 0.3s ease;
|
| 157 |
+
display: inline-block;
|
| 158 |
+
padding: 2px 4px;
|
| 159 |
+
border-radius: 3px;
|
| 160 |
+
position: relative;
|
| 161 |
+
z-index: 5; /* ๋งํฌ z-index ์ค์ */
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.keyword-link:hover {
|
| 165 |
+
color: #ffffff;
|
| 166 |
+
background-color: #2c5aa0;
|
| 167 |
+
text-decoration: none;
|
| 168 |
+
transform: translateY(-1px);
|
| 169 |
+
box-shadow: 0 2px 4px rgba(44, 90, 160, 0.3);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.keyword-link:active {
|
| 173 |
+
transform: translateY(0px);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
/* ํค์๋ ์
ํน๋ณ ์คํ์ผ */
|
| 177 |
+
.col-keyword {
|
| 178 |
+
position: relative;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.keyword-tooltip {
|
| 182 |
+
position: absolute;
|
| 183 |
+
bottom: 100%;
|
| 184 |
+
left: 50%;
|
| 185 |
+
transform: translateX(-50%);
|
| 186 |
+
background-color: #333;
|
| 187 |
+
color: white;
|
| 188 |
+
padding: 6px 10px;
|
| 189 |
+
border-radius: 4px;
|
| 190 |
+
font-size: 11px;
|
| 191 |
+
white-space: nowrap;
|
| 192 |
+
opacity: 0;
|
| 193 |
+
visibility: hidden;
|
| 194 |
+
transition: all 0.3s ease;
|
| 195 |
+
z-index: 1000; /* ํดํ์ ๊ฐ์ฅ ๋์ z-index */
|
| 196 |
+
pointer-events: none;
|
| 197 |
+
margin-bottom: 5px;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
.keyword-tooltip::after {
|
| 201 |
+
content: '';
|
| 202 |
+
position: absolute;
|
| 203 |
+
top: 100%;
|
| 204 |
+
left: 50%;
|
| 205 |
+
transform: translateX(-50%);
|
| 206 |
+
border: 4px solid transparent;
|
| 207 |
+
border-top-color: #333;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.keyword-link:hover .keyword-tooltip {
|
| 211 |
+
opacity: 1;
|
| 212 |
+
visibility: visible;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
/* === ์์ ๋ ๋ถ๋ถ: ์ด ๋๋น ์ ์ - ์นดํ
๊ณ ๋ฆฌ ์ด ์ ๊ฑฐ ํ ์กฐ์ === */
|
| 216 |
+
.col-seq { width: 8%; }
|
| 217 |
+
.col-keyword { width: 25%; }
|
| 218 |
+
.col-pc { width: 12%; }
|
| 219 |
+
.col-mobile { width: 12%; }
|
| 220 |
+
.col-total { width: 12%; }
|
| 221 |
+
.col-range { width: 12%; }
|
| 222 |
+
.col-rank { width: 15%; }
|
| 223 |
+
.col-count { width: 10%; }
|
| 224 |
+
|
| 225 |
+
.truncated-text {
|
| 226 |
+
position: relative;
|
| 227 |
+
cursor: pointer;
|
| 228 |
+
z-index: 2; /* ํ
์คํธ z-index ์ค์ */
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
.truncated-text:hover::after {
|
| 232 |
+
content: attr(data-full-text);
|
| 233 |
+
position: absolute;
|
| 234 |
+
left: 0;
|
| 235 |
+
top: 100%;
|
| 236 |
+
z-index: 99;
|
| 237 |
+
min-width: 200px;
|
| 238 |
+
max-width: 400px;
|
| 239 |
+
padding: 8px;
|
| 240 |
+
background-color: #fff;
|
| 241 |
+
border: 1px solid #ddd;
|
| 242 |
+
border-radius: 4px;
|
| 243 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
| 244 |
+
white-space: normal;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
/* ํค์๋ ํ๊ทธ ์คํ์ผ */
|
| 248 |
+
.keyword-tag-container {
|
| 249 |
+
margin-top: 20px;
|
| 250 |
+
padding: 10px;
|
| 251 |
+
border: 1px solid #ddd;
|
| 252 |
+
border-radius: 5px;
|
| 253 |
+
background-color: #f9f9f9;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
.keyword-tag {
|
| 257 |
+
display: inline-block;
|
| 258 |
+
background-color: #009879;
|
| 259 |
+
color: white;
|
| 260 |
+
padding: 5px 10px;
|
| 261 |
+
margin: 5px;
|
| 262 |
+
border-radius: 15px;
|
| 263 |
+
font-size: 12px;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
.category-tag {
|
| 267 |
+
display: inline-block;
|
| 268 |
+
background-color: #2c7fb8;
|
| 269 |
+
color: white;
|
| 270 |
+
padding: 5px 10px;
|
| 271 |
+
margin: 5px;
|
| 272 |
+
border-radius: 15px;
|
| 273 |
+
font-size: 12px;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
/* ๋ถ์ ๊ฒฐ๊ณผ ํ
์ด๋ธ ์คํ์ผ */
|
| 277 |
+
.analysis-result {
|
| 278 |
+
margin-top: 30px;
|
| 279 |
+
border: 1px solid #ddd;
|
| 280 |
+
border-radius: 5px;
|
| 281 |
+
padding: 15px;
|
| 282 |
+
background-color: #f9f9f9;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
.result-header {
|
| 286 |
+
font-weight: bold;
|
| 287 |
+
margin-bottom: 10px;
|
| 288 |
+
color: #009879;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
.match-item {
|
| 292 |
+
margin: 5px 0;
|
| 293 |
+
padding: 5px;
|
| 294 |
+
border-bottom: 1px solid #eee;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
.match-keyword {
|
| 298 |
+
font-weight: bold;
|
| 299 |
+
color: #2c7fb8;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
.match-count {
|
| 303 |
+
display: inline-block;
|
| 304 |
+
background-color: #009879;
|
| 305 |
+
color: white;
|
| 306 |
+
padding: 2px 8px;
|
| 307 |
+
border-radius: 10px;
|
| 308 |
+
font-size: 12px;
|
| 309 |
+
margin-left: 10px;
|
| 310 |
+
}
|
| 311 |
+
</style>
|
| 312 |
+
'''
|
| 313 |
+
|
| 314 |
+
# === ์์ ๋ ๋ถ๋ถ: ์ด ์ด๋ฆ๊ณผ ํด๋์ค ๋งคํ - ์นดํ
๊ณ ๋ฆฌ ๊ด๋ จ ์ ๊ฑฐ ===
|
| 315 |
+
col_mapping = {
|
| 316 |
+
"์๋ฒ": "col-seq",
|
| 317 |
+
"์กฐํฉ ํค์๋": "col-keyword",
|
| 318 |
+
"์ฐ๊ด ํค์๋": "col-keyword", # ์ฐ๊ด๊ฒ์์ด ๋ถ์์ฉ ์ถ๊ฐ
|
| 319 |
+
"ํค์๋": "col-keyword", # ์ผ๋ฐ ํค์๋์ฉ ์ถ๊ฐ
|
| 320 |
+
"PC๊ฒ์๋": "col-pc",
|
| 321 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": "col-mobile",
|
| 322 |
+
"์ด๊ฒ์๋": "col-total",
|
| 323 |
+
"๊ฒ์๋๊ตฌ๊ฐ": "col-range",
|
| 324 |
+
"ํค์๋ ์ฌ์ฉ์์์": "col-rank",
|
| 325 |
+
"ํค์๋ ์ฌ์ฉํ์": "col-count"
|
| 326 |
+
# ์นดํ
๊ณ ๋ฆฌ ๊ด๋ จ ๋งคํ ์ ๊ฑฐ๋จ
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
# ๋ค์ด๋ฒ ์ผํ ๋งํฌ ์์ฑ ํจ์
|
| 330 |
+
def create_naver_shopping_link(keyword):
|
| 331 |
+
"""ํค์๋๋ฅผ ๋ค์ด๋ฒ ์ผํ ๋งํฌ๋ก ๋ณํ"""
|
| 332 |
+
# URL ์ธ์ฝ๋ฉ (ํ๊ธ ํค์๋ ์ฒ๋ฆฌ)
|
| 333 |
+
encoded_keyword = urllib.parse.quote(keyword.strip())
|
| 334 |
+
naver_shopping_url = f"https://search.shopping.naver.com/search/all?where=all&frm=NVSCTAB&query={encoded_keyword}"
|
| 335 |
+
|
| 336 |
+
# ๋งํฌ๊ฐ ํฌํจ๋ HTML ๋ฐํ
|
| 337 |
+
return f'''<a href="{naver_shopping_url}" target="_blank" class="keyword-link" title="๋ค์ด๋ฒ ์ผํ์์ '{keyword}' ๊ฒ์ํ๊ธฐ">
|
| 338 |
+
{keyword}
|
| 339 |
+
<span class="keyword-tooltip">ํด๋ฆญํ๋ฉด ๋ค์ด๋ฒ ์ผํ์ผ๋ก ์ด๋</span>
|
| 340 |
+
</a>'''
|
| 341 |
+
|
| 342 |
+
# ํ
์ด๋ธ ์ปจํ
์ด๋ ์์
|
| 343 |
+
html += '<div class="table-container">'
|
| 344 |
+
|
| 345 |
+
# ๋จ์ผ ํ
์ด๋ธ ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝ (ํค๋๋ position: sticky๋ก ๊ณ ์ )
|
| 346 |
+
html += '<div class="data-container">'
|
| 347 |
+
html += '<table class="styled-table">'
|
| 348 |
+
|
| 349 |
+
# colgroup์ผ๋ก ์ด ๋๋น ์ ์
|
| 350 |
+
html += '<colgroup>'
|
| 351 |
+
html += f'<col class="{col_mapping["์๋ฒ"]}">'
|
| 352 |
+
for col in df_display.columns:
|
| 353 |
+
col_class = col_mapping.get(col, "")
|
| 354 |
+
html += f'<col class="{col_class}">'
|
| 355 |
+
html += '</colgroup>'
|
| 356 |
+
|
| 357 |
+
# ํ
์ด๋ธ ํค๋
|
| 358 |
+
html += '<thead>'
|
| 359 |
+
html += '<tr>'
|
| 360 |
+
html += f'<th class="{col_mapping["์๋ฒ"]}">์๋ฒ</th>'
|
| 361 |
+
for col in df_display.columns:
|
| 362 |
+
col_class = col_mapping.get(col, "")
|
| 363 |
+
html += f'<th class="{col_class}">{col}</th>'
|
| 364 |
+
html += '</tr>'
|
| 365 |
+
html += '</thead>'
|
| 366 |
+
|
| 367 |
+
# ํ
์ด๋ธ ๋ณธ๋ฌธ
|
| 368 |
+
html += '<tbody>'
|
| 369 |
+
for idx, row in df_display.iterrows():
|
| 370 |
+
html += '<tr>'
|
| 371 |
+
# ์๋ฒ ํ์ - 1๋ถํฐ ์์ํ๋ ์์ฐจ์ ๋ฒํธ
|
| 372 |
+
html += f'<td class="{col_mapping["์๋ฒ"]}">{idx + 1}</td>'
|
| 373 |
+
|
| 374 |
+
# ๋ฐ์ดํฐ ์
์ถ๊ฐ
|
| 375 |
+
for col in df_display.columns:
|
| 376 |
+
col_class = col_mapping.get(col, "")
|
| 377 |
+
value = str(row[col])
|
| 378 |
+
|
| 379 |
+
# === ์๋ก ์ถ๊ฐ: ํค์๋ ์ด์ ๋งํฌ ์ ์ฉ ===
|
| 380 |
+
if col in ["์กฐํฉ ํค์๋", "์ฐ๊ด ํค์๋", "ํค์๋"]:
|
| 381 |
+
# ํค์๋ ์
์ ๋ค์ด๋ฒ ์ผํ ๋งํฌ ์ ์ฉ
|
| 382 |
+
keyword_with_link = create_naver_shopping_link(value)
|
| 383 |
+
html += f'<td class="{col_class}">{keyword_with_link}</td>'
|
| 384 |
+
elif col == "ํค์๋ ์ฌ์ฉ์์์":
|
| 385 |
+
# ๊ธด ํ
์คํธ์ ์
์ ๊ทธ๋๋ก ํ์ (์ค๋ฐ๊ฟ ํ์ฉ)
|
| 386 |
+
html += f'<td class="{col_class}">{value}</td>'
|
| 387 |
+
elif len(value) > 30:
|
| 388 |
+
# ๋ค๋ฅธ ๊ธด ํ
์คํธ๋ hover๋ก ์ ์ฒด ํ์
|
| 389 |
+
html += f'<td class="{col_class}"><div class="truncated-text" data-full-text="{value}">{value[:30]}...</div></td>'
|
| 390 |
+
else:
|
| 391 |
+
# ์ผ๋ฐ ํ
์คํธ
|
| 392 |
+
html += f'<td class="{col_class}">{value}</td>'
|
| 393 |
+
html += '</tr>'
|
| 394 |
+
|
| 395 |
+
html += '</tbody>'
|
| 396 |
+
html += '</table>'
|
| 397 |
+
html += '</div>' # data-container ๋ซ๊ธฐ
|
| 398 |
+
html += '</div>' # table-container ๋ซ๊ธฐ
|
| 399 |
+
|
| 400 |
+
# ์ฌ์ฉ๋ฒ ์๋ด ์ถ๊ฐ
|
| 401 |
+
html += '''
|
| 402 |
+
<div style="margin-top: 15px; padding: 12px; background: #e8f5e8; border-radius: 8px; border-left: 4px solid #009879;">
|
| 403 |
+
<div style="font-weight: bold; color: #155724; margin-bottom: 5px;">๐ก ์ฌ์ฉํ</div>
|
| 404 |
+
<div style="font-size: 14px; color: #155724;">
|
| 405 |
+
ํค์๋๋ฅผ ํด๋ฆญํ๋ฉด ๋ค์ด๋ฒ ์ผํ์์ ํด๋น ํค์๋๋ก ๊ฒ์ํ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฐฝ์์ ํ์ธํ ์ ์์ต๋๋ค.
|
| 406 |
+
</div>
|
| 407 |
+
</div>
|
| 408 |
+
'''
|
| 409 |
+
|
| 410 |
+
return html
|
| 411 |
+
|
| 412 |
+
def cleanup_temp_files(delay=300):
|
| 413 |
+
"""์์ ํ์ผ ์ ๋ฆฌ ํจ์"""
|
| 414 |
+
global _temp_files
|
| 415 |
+
|
| 416 |
+
def cleanup():
|
| 417 |
+
time.sleep(delay) # ์ง์ ๋ ์๊ฐ ๋๊ธฐ
|
| 418 |
+
temp_files_to_remove = _temp_files.copy()
|
| 419 |
+
_temp_files = []
|
| 420 |
+
|
| 421 |
+
for file_path in temp_files_to_remove:
|
| 422 |
+
try:
|
| 423 |
+
if os.path.exists(file_path):
|
| 424 |
+
os.remove(file_path)
|
| 425 |
+
logger.info(f"์์ ํ์ผ ์ญ์ : {file_path}")
|
| 426 |
+
except Exception as e:
|
| 427 |
+
logger.error(f"ํ์ผ ์ญ์ ์ค๋ฅ: {e}")
|
| 428 |
+
|
| 429 |
+
# ์ ์ค๋ ๋ ์์
|
| 430 |
+
threading.Thread(target=cleanup, daemon=True).start()
|
| 431 |
+
|
| 432 |
+
def download_keywords(df, auto_cleanup=True, cleanup_delay=300):
|
| 433 |
+
"""ํค์๋ ๋ฐ์ดํฐ๋ฅผ ์์
ํ์ผ๋ก ๋ค์ด๋ก๋ - ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์ ๊ฑฐ"""
|
| 434 |
+
global _temp_files
|
| 435 |
+
|
| 436 |
+
if df is None or df.empty:
|
| 437 |
+
return None
|
| 438 |
+
|
| 439 |
+
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 440 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
|
| 441 |
+
temp_file.close()
|
| 442 |
+
filename = temp_file.name
|
| 443 |
+
|
| 444 |
+
# ์์ ํ์ผ ์ถ์ ๋ชฉ๋ก์ ์ถ๊ฐ
|
| 445 |
+
_temp_files.append(filename)
|
| 446 |
+
|
| 447 |
+
# === ์์ ๋ ๋ถ๋ถ: ์นดํ
๊ณ ๋ฆฌ ๊ด๋ จ ์ด ์ ๊ฑฐ ===
|
| 448 |
+
df_export = df.copy()
|
| 449 |
+
|
| 450 |
+
# ์นดํ
๊ณ ๋ฆฌ ๊ด๋ จ ์ด๋ค ์ ๊ฑฐ
|
| 451 |
+
columns_to_remove = ["์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ(์์100์)", "๊ด๋ จ ์นดํ
๊ณ ๋ฆฌ", "์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ"]
|
| 452 |
+
for col in columns_to_remove:
|
| 453 |
+
if col in df_export.columns:
|
| 454 |
+
df_export = df_export.drop(columns=[col])
|
| 455 |
+
logger.info(f"์์
๋ด๋ณด๋ด๊ธฐ์์ '{col}' ์ด ์ ๊ฑฐ๋จ")
|
| 456 |
+
|
| 457 |
+
# ํค์๋ ๋ฐ์ดํฐ๋ฅผ ์์
ํ์ผ๋ก ์ ์ฅ
|
| 458 |
+
with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
|
| 459 |
+
# ํค์๋ ๋ชฉ๋ก ์ํธ
|
| 460 |
+
df_export.to_excel(writer, sheet_name='ํค์๋ ๋ชฉ๋ก', index=False)
|
| 461 |
+
|
| 462 |
+
# ์ด ๋๋น ์กฐ์ - ์นดํ
๊ณ ๋ฆฌ ์ด ์ ๊ฑฐ ํ ์กฐ์
|
| 463 |
+
worksheet = writer.sheets['ํค์๋ ๋ชฉ๋ก']
|
| 464 |
+
worksheet.set_column('A:A', 20) # ์กฐํฉ ํค์๋ ์ด
|
| 465 |
+
worksheet.set_column('B:B', 12) # PC๊ฒ์๋ ์ด
|
| 466 |
+
worksheet.set_column('C:C', 12) # ๋ชจ๋ฐ์ผ๊ฒ์๋ ์ด
|
| 467 |
+
worksheet.set_column('D:D', 12) # ์ด๊ฒ์๋ ์ด
|
| 468 |
+
worksheet.set_column('E:E', 12) # ๊ฒ์๋๊ตฌ๊ฐ ์ด
|
| 469 |
+
worksheet.set_column('F:F', 20) # ํค์๋ ์ฌ์ฉ์์์ ์ด
|
| 470 |
+
worksheet.set_column('G:G', 12) # ํค์๋ ์ฌ์ฉํ์ ์ด
|
| 471 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ด๋ค ์ ๊ฑฐ๋ก H, I ์ด ์ค์ ์ ๊ฑฐ๋จ
|
| 472 |
+
|
| 473 |
+
# ํค๋ ํ์ ์ค์
|
| 474 |
+
header_format = writer.book.add_format({
|
| 475 |
+
'bold': True,
|
| 476 |
+
'bg_color': '#009879',
|
| 477 |
+
'color': 'white',
|
| 478 |
+
'border': 1
|
| 479 |
+
})
|
| 480 |
+
|
| 481 |
+
# ํค๋์ ํ์ ์ ์ฉ
|
| 482 |
+
for col_num, value in enumerate(df_export.columns.values):
|
| 483 |
+
worksheet.write(0, col_num, value, header_format)
|
| 484 |
+
|
| 485 |
+
logger.info(f"์์
ํ์ผ ์์ฑ: {filename}")
|
| 486 |
+
|
| 487 |
+
# ํ์ผ ์๋ ์ ๋ฆฌ ์ต์
|
| 488 |
+
if auto_cleanup:
|
| 489 |
+
# ๋ณ๋ ์ ๋ฆฌ ์์
์์ฒญ ์์ด ์ถ์ ๋ชฉ๋ก์ ์ถ๊ฐ๋ง ํ์ฌ ์ผ๊ด ์ฒ๋ฆฌ
|
| 490 |
+
pass
|
| 491 |
+
|
| 492 |
+
return filename
|
| 493 |
+
|
| 494 |
+
def register_cleanup_handlers():
|
| 495 |
+
"""์ฑ ์ข
๋ฃ ์ ์ ๋ฆฌ๋ฅผ ์ํ ํธ๋ค๋ฌ ๋ฑ๋ก"""
|
| 496 |
+
import atexit
|
| 497 |
+
|
| 498 |
+
def cleanup_all_temp_files():
|
| 499 |
+
global _temp_files
|
| 500 |
+
for file_path in _temp_files:
|
| 501 |
+
try:
|
| 502 |
+
if os.path.exists(file_path):
|
| 503 |
+
os.remove(file_path)
|
| 504 |
+
logger.info(f"์ข
๋ฃ ์ ์์ ํ์ผ ์ญ์ : {file_path}")
|
| 505 |
+
except Ex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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ํค์๋ ๋งค์นญ ๋ฐ ์์นํญ ๊ณ์ฐ ๊ฐ์ - ์ ์ฒด ์ฝ๋
|
| 3 |
+
v4.0 - ์คํ์ด์ค๋ฐ ์ฒ๋ฆฌ ๊ฐ์ + ์ฌ๋ฐ๋ฅธ ํธ๋ ๋ ๋ถ์ ๋ก์ง ์ ์ฉ
|
| 4 |
+
- ๊ธฐ์กด ๋ชจ๋ ๊ธฐ๋ฅ ์ ์งํ๋ฉด์ ์ต์ ํ
|
| 5 |
+
- ์คํ์ด์ค๋ฐ ์ ๊ฑฐ ํ ๊ฒ์/๋น๊ต ๋ก์ง ์ ์ฉ
|
| 6 |
+
- ์ฌ๋ฐ๋ฅธ ์ฆ๊ฐ์จ ๊ณ์ฐ: ์ฌํด ์๋ฃ์ vs ์๋
๋์
|
| 7 |
+
- ๐ ๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์: ์ค์ +์์ ๋ฐ์ดํฐ ์ค ์ต๋๊ฐ
|
| 8 |
+
- ๐ ๊ฐ์ฅ ์์นํญ์ด ๋์ ์: ์ฐ์๋ ์๊ฐ ์์น๋ฅ ์ค ์ต๋๊ฐ
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import logging
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from datetime import datetime
|
| 14 |
+
import re
|
| 15 |
+
import time
|
| 16 |
+
import random
|
| 17 |
+
from typing import Dict, List, Optional
|
| 18 |
+
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
def normalize_keyword(keyword):
|
| 22 |
+
"""ํค์๋ ์ ๊ทํ - ์คํ์ด์ค๋ฐ ์ฒ๋ฆฌ ๊ฐ์ """
|
| 23 |
+
if not keyword:
|
| 24 |
+
return ""
|
| 25 |
+
|
| 26 |
+
# 1. ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 27 |
+
keyword = keyword.strip()
|
| 28 |
+
|
| 29 |
+
# 2. ์ฐ์๋ ๊ณต๋ฐฑ์ ํ๋๋ก ๋ณ๊ฒฝ
|
| 30 |
+
keyword = re.sub(r'\s+', ' ', keyword)
|
| 31 |
+
|
| 32 |
+
# 3. ํน์๋ฌธ์ ์ ๊ฑฐ (ํ๊ธ, ์๋ฌธ, ์ซ์, ๊ณต๋ฐฑ๋ง ๋จ๊น)
|
| 33 |
+
keyword = re.sub(r'[^\w\s๊ฐ-ํฃ]', '', keyword)
|
| 34 |
+
|
| 35 |
+
return keyword
|
| 36 |
+
|
| 37 |
+
def normalize_keyword_for_api(keyword):
|
| 38 |
+
"""API ํธ์ถ์ฉ ํค์๋ ์ ๊ทํ (์คํ์ด์ค ์ ๊ฑฐ)"""
|
| 39 |
+
normalized = normalize_keyword(keyword)
|
| 40 |
+
return normalized.replace(" ", "")
|
| 41 |
+
|
| 42 |
+
def normalize_keyword_for_comparison(keyword):
|
| 43 |
+
"""๋น๊ต์ฉ ํค์๋ ์ ๊ทํ (์คํ์ด์ค ์ ์ง)"""
|
| 44 |
+
return normalize_keyword(keyword).lower()
|
| 45 |
+
|
| 46 |
+
def normalize_keyword_advanced(keyword):
|
| 47 |
+
"""๊ณ ๊ธ ํค์๋ ์ ๊ทํ - ๋งค์นญ ๋ฌธ์ ํด๊ฒฐ"""
|
| 48 |
+
if not keyword:
|
| 49 |
+
return ""
|
| 50 |
+
|
| 51 |
+
# 1. ๊ธฐ๋ณธ ์ ๋ฆฌ
|
| 52 |
+
keyword = str(keyword).strip()
|
| 53 |
+
|
| 54 |
+
# 2. ์ฐ์๋ ๊ณต๋ฐฑ์ ํ๋๋ก ๋ณ๊ฒฝ
|
| 55 |
+
keyword = re.sub(r'\s+', ' ', keyword)
|
| 56 |
+
|
| 57 |
+
# 3. ํน์๋ฌธ์ ์ ๊ฑฐ (ํ๊ธ, ์๋ฌธ, ์ซ์, ๊ณต๋ฐฑ๋ง ๋จ๊น)
|
| 58 |
+
keyword = re.sub(r'[^\w\s๊ฐ-ํฃ]', '', keyword)
|
| 59 |
+
|
| 60 |
+
# 4. ์๋ฌธ์ ๋ณํ
|
| 61 |
+
keyword = keyword.lower()
|
| 62 |
+
|
| 63 |
+
return keyword
|
| 64 |
+
|
| 65 |
+
def create_keyword_variations(keyword):
|
| 66 |
+
"""ํค์๋ ๋ณํ ๋ฒ์ ๋ค ์์ฑ - ์คํ์ด์ค๋ฐ ์ฒ๋ฆฌ ๊ฐํ"""
|
| 67 |
+
base = normalize_keyword_advanced(keyword)
|
| 68 |
+
variations = [base]
|
| 69 |
+
|
| 70 |
+
# ์คํ์ด์ค ์ ๊ฑฐ ๋ฒ์
|
| 71 |
+
no_space = base.replace(" ", "")
|
| 72 |
+
if no_space != base:
|
| 73 |
+
variations.append(no_space)
|
| 74 |
+
|
| 75 |
+
# ์คํ์ด์ค๋ฅผ ๋ค๋ฅธ ๊ตฌ๋ถ์๋ก ๋ฐ๊พผ ๋ฒ์ ๋ค
|
| 76 |
+
variations.append(base.replace(" ", "-"))
|
| 77 |
+
variations.append(base.replace(" ", "_"))
|
| 78 |
+
|
| 79 |
+
# ๋จ์ด ์์ ๋ฐ๊พผ ๋ฒ์ (2๋จ์ด์ธ ๊ฒฝ์ฐ)
|
| 80 |
+
words = base.split()
|
| 81 |
+
if len(words) == 2:
|
| 82 |
+
reversed_keyword = f"{words[1]} {words[0]}"
|
| 83 |
+
variations.append(reversed_keyword)
|
| 84 |
+
variations.append(reversed_keyword.replace(" ", ""))
|
| 85 |
+
|
| 86 |
+
return list(set(variations)) # ์ค๋ณต ์ ๊ฑฐ
|
| 87 |
+
|
| 88 |
+
def find_matching_keyword_row(analysis_keyword, keywords_df):
|
| 89 |
+
"""๊ฐ์ ๋ ํค์๋ ๋งค์นญ ํจ์"""
|
| 90 |
+
if keywords_df is None or keywords_df.empty:
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
analysis_variations = create_keyword_variations(analysis_keyword)
|
| 94 |
+
|
| 95 |
+
logger.info(f"๋ถ์ ํค์๋ ๋ณํ๋ค: {analysis_variations}")
|
| 96 |
+
|
| 97 |
+
# 1์ฐจ: ์ ํํ ๋งค์นญ
|
| 98 |
+
for idx, row in keywords_df.iterrows():
|
| 99 |
+
df_keyword = str(row.get('์กฐํฉ ํค์๋', ''))
|
| 100 |
+
df_variations = create_keyword_variations(df_keyword)
|
| 101 |
+
|
| 102 |
+
for analysis_var in analysis_variations:
|
| 103 |
+
for df_var in df_variations:
|
| 104 |
+
if analysis_var == df_var and len(analysis_var) > 1:
|
| 105 |
+
logger.info(f"์ ํํ ๋งค์นญ ์ฑ๊ณต: '{analysis_keyword}' = '{df_keyword}'")
|
| 106 |
+
return row
|
| 107 |
+
|
| 108 |
+
# 2์ฐจ: ํฌํจ ๊ด๊ณ ๋งค์นญ
|
| 109 |
+
for idx, row in keywords_df.iterrows():
|
| 110 |
+
df_keyword = str(row.get('์กฐํฉ ํค์๋', ''))
|
| 111 |
+
df_variations = create_keyword_variations(df_keyword)
|
| 112 |
+
|
| 113 |
+
for analysis_var in analysis_variations:
|
| 114 |
+
for df_var in df_variations:
|
| 115 |
+
if len(analysis_var) > 2 and len(df_var) > 2:
|
| 116 |
+
if analysis_var in df_var or df_var in analysis_var:
|
| 117 |
+
similarity = len(set(analysis_var) & set(df_var)) / len(set(analysis_var) | set(df_var))
|
| 118 |
+
if similarity > 0.7: # 70% ์ด์ ์ ์ฌ
|
| 119 |
+
logger.info(f"๋ถ๋ถ ๋งค์นญ ์ฑ๊ณต: '{analysis_keyword}' โ '{df_keyword}' (์ ์ฌ๋: {similarity:.2f})")
|
| 120 |
+
return row
|
| 121 |
+
|
| 122 |
+
logger.warning(f"ํค์๋ ๋งค์นญ ์คํจ: '{analysis_keyword}'")
|
| 123 |
+
logger.info(f"๋ฐ์ดํฐํ๋ ์ ํค์๋ ์ํ: {keywords_df['์กฐํฉ ํค์๋'].head(5).tolist()}")
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
def generate_prediction_data(trend_data_3year, keyword):
|
| 127 |
+
"""
|
| 128 |
+
์ ๊ตํ ์์ ๋ฐ์ดํฐ ์์ฑ ํจ์
|
| 129 |
+
- ํธ๋ ๋ ๋ฐ์ดํฐ ์์ง ํ ๋ฐ๋ก ํธ์ถํ์ฌ ์์ ๋ฐ์ดํฐ ์ถ๊ฐ
|
| 130 |
+
- ๊ณ์ ์ฑ, ์ฆ๊ฐ ํธ๋ ๋, ์ ๋
๋๋น ์ฑ์ฅ๋ฅ ๋ชจ๋ ๊ณ ๋ ค
|
| 131 |
+
"""
|
| 132 |
+
if not trend_data_3year:
|
| 133 |
+
logger.warning("โ ์์ ๋ฐ์ดํฐ ์์ฑ ์คํจ: trend_data_3year ์์")
|
| 134 |
+
return trend_data_3year
|
| 135 |
+
|
| 136 |
+
try:
|
| 137 |
+
current_date = datetime.now()
|
| 138 |
+
current_year = current_date.year
|
| 139 |
+
current_month = current_date.month
|
| 140 |
+
|
| 141 |
+
logger.info(f"๐ฎ ์์ ๋ฐ์ดํฐ ์์ฑ ์์: {keyword} ({current_year}๋
{current_month}์ ๊ธฐ์ค)")
|
| 142 |
+
|
| 143 |
+
for kw, data in trend_data_3year.items():
|
| 144 |
+
if not data or not data.get('monthly_volumes') or not data.get('dates'):
|
| 145 |
+
continue
|
| 146 |
+
|
| 147 |
+
volumes = data['monthly_volumes']
|
| 148 |
+
dates = data['dates']
|
| 149 |
+
|
| 150 |
+
# โ
1๋จ๊ณ: ๊ธฐ์กด ๋ฐ์ดํฐ ๋ถ์
|
| 151 |
+
yearly_data = {} # {year: {month: volume}}
|
| 152 |
+
|
| 153 |
+
for i, date_str in enumerate(dates):
|
| 154 |
+
try:
|
| 155 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 156 |
+
if i < len(volumes):
|
| 157 |
+
volume = volumes[i]
|
| 158 |
+
if isinstance(volume, str):
|
| 159 |
+
volume = float(volume.replace(',', ''))
|
| 160 |
+
volume = int(volume) if volume else 0
|
| 161 |
+
|
| 162 |
+
year = date_obj.year
|
| 163 |
+
month = date_obj.month
|
| 164 |
+
|
| 165 |
+
if year not in yearly_data:
|
| 166 |
+
yearly_data[year] = {}
|
| 167 |
+
yearly_data[year][month] = volume
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
logger.warning(f"โ ๏ธ ๋ ์ง ํ์ฑ ์ค๋ฅ: {date_str}")
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
+
logger.info(f"๐ ๋ถ์๋ ์ฐ๋: {list(yearly_data.keys())}")
|
| 174 |
+
|
| 175 |
+
# โ
2๋จ๊ณ: ์์ ๋ฐ์ดํฐ ์์ฑ ์๊ณ ๋ฆฌ์ฆ
|
| 176 |
+
if current_year not in yearly_data:
|
| 177 |
+
yearly_data[current_year] = {}
|
| 178 |
+
|
| 179 |
+
current_year_data = yearly_data[current_year]
|
| 180 |
+
last_year_data = yearly_data.get(current_year - 1, {})
|
| 181 |
+
two_years_ago_data = yearly_data.get(current_year - 2, {})
|
| 182 |
+
|
| 183 |
+
logger.info(f"๐ ์ฌํด ์ค์ ๋ฐ์ดํฐ: {len(current_year_data)}๊ฐ์")
|
| 184 |
+
logger.info(f"๐ ์๋
์ฐธ์กฐ ๋ฐ์ดํฐ: {len(last_year_data)}๊ฐ์")
|
| 185 |
+
|
| 186 |
+
# ์์ ๋ฐ์ดํฐ ์์ฑ (ํ์ฌ์ ์ดํ)
|
| 187 |
+
for future_month in range(current_month + 1, 13):
|
| 188 |
+
if future_month in current_year_data:
|
| 189 |
+
continue # ์ด๋ฏธ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ์คํต
|
| 190 |
+
|
| 191 |
+
predicted_volume = calculate_predicted_volume(
|
| 192 |
+
future_month, current_year_data, last_year_data,
|
| 193 |
+
two_years_ago_data, current_month
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
if predicted_volume is not None:
|
| 197 |
+
current_year_data[future_month] = predicted_volume
|
| 198 |
+
logger.info(f"๐ฎ ์์ ์์ฑ: {current_year}๋
{future_month}์ = {predicted_volume:,}ํ")
|
| 199 |
+
|
| 200 |
+
# โ
3๋จ๊ณ: ์์ฑ๋ ๋ฐ์ดํฐ๋ฅผ ์๋ณธ ๊ตฌ์กฐ์ ํตํฉ
|
| 201 |
+
updated_volumes = []
|
| 202 |
+
updated_dates = []
|
| 203 |
+
|
| 204 |
+
# ์๊ฐ์์ผ๋ก ์ ๋ ฌํ์ฌ ํตํฉ
|
| 205 |
+
all_months = []
|
| 206 |
+
for year in sorted(yearly_data.keys()):
|
| 207 |
+
for month in sorted(yearly_data[year].keys()):
|
| 208 |
+
all_months.append((year, month, yearly_data[year][month]))
|
| 209 |
+
|
| 210 |
+
for year, month, volume in all_months:
|
| 211 |
+
updated_volumes.append(volume)
|
| 212 |
+
updated_dates.append(f"{year}-{month:02d}-01")
|
| 213 |
+
|
| 214 |
+
# ์๋ณธ ๋ฐ์ดํฐ ์
๋ฐ์ดํธ
|
| 215 |
+
data['monthly_volumes'] = updated_volumes
|
| 216 |
+
data['dates'] = updated_dates
|
| 217 |
+
|
| 218 |
+
logger.info(f"โ
{kw} ์์ ๋ฐ์ดํฐ ํตํฉ ์๋ฃ: ์ด {len(updated_volumes)}๊ฐ์")
|
| 219 |
+
|
| 220 |
+
logger.info(f"๐ ์ ์ฒด ์์ ๋ฐ์ดํฐ ์์ฑ ์๋ฃ: {keyword}")
|
| 221 |
+
return trend_data_3year
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
logger.error(f"โ ์์ ๋ฐ์ดํฐ ์์ฑ ์ค๋ฅ: {e}")
|
| 225 |
+
return trend_data_3year
|
| 226 |
+
|
| 227 |
+
def calculate_predicted_volume(target_month, current_year_data, last_year_data,
|
| 228 |
+
two_years_ago_data, current_month):
|
| 229 |
+
"""
|
| 230 |
+
์ ๊ตํ ์์ ๋ณผ๋ฅจ ๊ณ์ฐ
|
| 231 |
+
- ๋ค์ค ์์ธ ๊ณ ๋ ค: ์๋
๋์, ์ฆ๊ฐ ํธ๋ ๋, ๊ณ์ ์ฑ, ์ฑ์ฅ๋ฅ
|
| 232 |
+
"""
|
| 233 |
+
try:
|
| 234 |
+
# ๊ธฐ์ค ๊ฐ๋ค
|
| 235 |
+
last_year_same_month = last_year_data.get(target_month, 0)
|
| 236 |
+
two_years_ago_same_month = two_years_ago_data.get(target_month, 0)
|
| 237 |
+
|
| 238 |
+
if last_year_same_month == 0:
|
| 239 |
+
logger.warning(f"โ ๏ธ {target_month}์ ์๋
๋ฐ์ดํฐ ์์")
|
| 240 |
+
return None
|
| 241 |
+
|
| 242 |
+
# โ
1. ๊ธฐ๋ณธ๊ฐ: ์๋
๋์
|
| 243 |
+
base_volume = last_year_same_month
|
| 244 |
+
|
| 245 |
+
# โ
2. ์ ๋
๋๋น ์ฑ์ฅ๋ฅ ๊ณ์ฐ (๊ฐ๋ฅํ ๊ฒฝ์ฐ)
|
| 246 |
+
growth_rate = 1.0
|
| 247 |
+
if two_years_ago_same_month > 0:
|
| 248 |
+
growth_rate = last_year_same_month / two_years_ago_same_month
|
| 249 |
+
logger.info(f"๐ {target_month}์ ์ ๋
์ฑ์ฅ๋ฅ : {growth_rate:.2f}๋ฐฐ")
|
| 250 |
+
|
| 251 |
+
# โ
3. ์ฌํด ์ต๊ทผ ํธ๋ ๋ ๋ฐ์
|
| 252 |
+
trend_factor = 1.0
|
| 253 |
+
if len(current_year_data) >= 2:
|
| 254 |
+
# ์ต๊ทผ 2-3๊ฐ์์ ์๋
๋๋น ๋น์จ ๊ณ์ฐ
|
| 255 |
+
recent_ratios = []
|
| 256 |
+
for month in range(max(1, current_month - 2), current_month + 1):
|
| 257 |
+
if month in current_year_data and month in last_year_data:
|
| 258 |
+
if last_year_data[month] > 0:
|
| 259 |
+
ratio = current_year_data[month] / last_year_data[month]
|
| 260 |
+
recent_ratios.append(ratio)
|
| 261 |
+
|
| 262 |
+
if recent_ratios:
|
| 263 |
+
trend_factor = sum(recent_ratios) / len(recent_ratios)
|
| 264 |
+
logger.info(f"๐ ์ต๊ทผ ํธ๋ ๋ ํฉํฐ: {trend_factor:.2f}")
|
| 265 |
+
|
| 266 |
+
# โ
4. ๊ณ์ ์ฑ ๋ณด์ (๊ฐ์ ๋ถ๊ธฐ ๋ด ์๊ฐ ํจํด)
|
| 267 |
+
seasonal_factor = 1.0
|
| 268 |
+
if target_month > 1 and target_month - 1 in last_year_data and target_month in last_year_data:
|
| 269 |
+
# ์๋
๋์ผ ๊ตฌ๊ฐ์ ์๊ฐ ๋ณํ์จ
|
| 270 |
+
if last_year_data[target_month - 1] > 0:
|
| 271 |
+
seasonal_factor = last_year_data[target_month] / last_year_data[target_month - 1]
|
| 272 |
+
logger.info(f"๐ {target_month}์ ๊ณ์ ์ฑ ํฉํฐ: {seasonal_factor:.2f}")
|
| 273 |
+
|
| 274 |
+
# โ
5. ์ต์ข
์์๊ฐ ๊ณ์ฐ (๊ฐ์คํ๊ท )
|
| 275 |
+
predicted_volume = int(
|
| 276 |
+
base_volume * (
|
| 277 |
+
0.4 * growth_rate + # 40% ์ ๋
์ฑ์ฅ๋ฅ
|
| 278 |
+
0.4 * trend_factor + # 40% ์ต๊ทผ ํธ๋ ๋
|
| 279 |
+
0.2 * seasonal_factor # 20% ๊ณ์ ์ฑ
|
| 280 |
+
)
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
# โ
6. ํฉ๋ฆฌ์ฑ ๊ฒ์ฆ (๊ธ๊ฒฉํ ๋ณํ ๋ฐฉ์ง)
|
| 284 |
+
if current_year_data:
|
| 285 |
+
recent_avg = sum(current_year_data.values()) / len(current_year_data)
|
| 286 |
+
if predicted_volume > recent_avg * 5: # 5๋ฐฐ ์ด์ ๊ธ์ฆ ๋ฐฉ์ง
|
| 287 |
+
predicted_volume = int(recent_avg * 2)
|
| 288 |
+
logger.warning(f"โ ๏ธ {target_month}์ ๊ธ์ฆ ๋ณด์ : {predicted_volume:,}ํ")
|
| 289 |
+
elif predicted_volume < recent_avg * 0.1: # 10๋ถ์ 1 ์ดํ ๊ธ๊ฐ ๋ฐฉ์ง
|
| 290 |
+
predicted_volume = int(recent_avg * 0.5)
|
| 291 |
+
logger.warning(f"โ ๏ธ {target_month}์ ๊ธ๊ฐ ๋ณด์ : {predicted_volume:,}ํ")
|
| 292 |
+
|
| 293 |
+
logger.info(f"๐ฏ {target_month}์ ์์ ๊ณ์ฐ: {last_year_same_month:,} ร (์ฑ์ฅ{growth_rate:.2f} + ํธ๋ ๋{trend_factor:.2f} + ๊ณ์ {seasonal_factor:.2f}) = {predicted_volume:,}")
|
| 294 |
+
|
| 295 |
+
return predicted_volume
|
| 296 |
+
|
| 297 |
+
except Exception as e:
|
| 298 |
+
logger.error(f"โ {target_month}์ ์์ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 299 |
+
return None
|
| 300 |
+
|
| 301 |
+
def enhance_trend_data_with_predictions(trend_data_3year, keyword):
|
| 302 |
+
"""
|
| 303 |
+
๊ธฐ์กด ํธ๋ ๋ ๋ฐ์ดํฐ์ ์์ ๋ฐ์ดํฐ ์ถ๊ฐ
|
| 304 |
+
- ๋ฉ์ธ ํธ๋ ๋ ์์ง ํจ์์์ ํธ์ถ
|
| 305 |
+
"""
|
| 306 |
+
if not trend_data_3year:
|
| 307 |
+
return trend_data_3year
|
| 308 |
+
|
| 309 |
+
logger.info(f"๐ ํธ๋ ๋ ๋ฐ์ดํฐ ์์ ํ์ฅ ์์: {keyword}")
|
| 310 |
+
|
| 311 |
+
enhanced_data = generate_prediction_data(trend_data_3year, keyword)
|
| 312 |
+
|
| 313 |
+
# ๋ฐ์ดํฐ ํ์ง ๊ฒ์ฆ
|
| 314 |
+
for kw, data in enhanced_data.items():
|
| 315 |
+
if data and data.get('monthly_volumes'):
|
| 316 |
+
total_months = len(data['monthly_volumes'])
|
| 317 |
+
current_year = datetime.now().year
|
| 318 |
+
|
| 319 |
+
# ํ์ฌ ์ฐ๋ ๋ฐ์ดํฐ ๊ฐ์ ํ์ธ
|
| 320 |
+
current_year_count = 0
|
| 321 |
+
for date_str in data['dates']:
|
| 322 |
+
try:
|
| 323 |
+
if date_str.startswith(str(current_year)):
|
| 324 |
+
current_year_count += 1
|
| 325 |
+
except:
|
| 326 |
+
continue
|
| 327 |
+
|
| 328 |
+
logger.info(f"โ
{kw} ์ต์ข
๋ฐ์ดํฐ: ์ ์ฒด {total_months}๊ฐ์, ์ฌํด {current_year_count}๊ฐ์")
|
| 329 |
+
|
| 330 |
+
return enhanced_data
|
| 331 |
+
|
| 332 |
+
def calculate_max_growth_rate_with_predictions(trend_data_3year, keyword):
|
| 333 |
+
"""์ฌ๋ฐ๋ฅธ ํธ๋ ๋ ๋ถ์ ๋ก์ง - ์ฌ์ฉ์ ์๊ตฌ์ฌํญ ์ ์ฉ"""
|
| 334 |
+
if not trend_data_3year:
|
| 335 |
+
logger.error("โ trend_data_3year๊ฐ ์์ต๋๋ค")
|
| 336 |
+
return "๋ฐ์ดํฐ ์์"
|
| 337 |
+
|
| 338 |
+
try:
|
| 339 |
+
keyword_data = None
|
| 340 |
+
for kw, data in trend_data_3year.items():
|
| 341 |
+
keyword_data = data
|
| 342 |
+
logger.info(f"๐ ํค์๋ ๋ฐ์ดํฐ ๋ฐ๊ฒฌ: {kw}")
|
| 343 |
+
break
|
| 344 |
+
|
| 345 |
+
if not keyword_data or not keyword_data.get('monthly_volumes') or not keyword_data.get('dates'):
|
| 346 |
+
logger.error("โ keyword_data ๊ตฌ์กฐ ๋ฌธ์ ")
|
| 347 |
+
return "๋ฐ์ดํฐ ์์"
|
| 348 |
+
|
| 349 |
+
volumes = keyword_data['monthly_volumes']
|
| 350 |
+
dates = keyword_data['dates']
|
| 351 |
+
|
| 352 |
+
# 1๋จ๊ณ: ํ์ฌ ์์ ํ์
|
| 353 |
+
current_date = datetime.now()
|
| 354 |
+
current_year = current_date.year
|
| 355 |
+
current_month = current_date.month
|
| 356 |
+
current_day = current_date.day
|
| 357 |
+
|
| 358 |
+
# ์๋ฃ๋ ๋ง์ง๋ง ์ ๊ณ์ฐ (2์ผ ์ดํ๋ฉด ์ ์๊น์ง ์๋ฃ)
|
| 359 |
+
if current_day >= 2:
|
| 360 |
+
completed_year = current_year
|
| 361 |
+
completed_month = current_month - 1
|
| 362 |
+
else:
|
| 363 |
+
completed_year = current_year
|
| 364 |
+
completed_month = current_month - 2
|
| 365 |
+
|
| 366 |
+
# ์์ด 0 ์ดํ๊ฐ ๋๋ฉด ์ฐ๋ ์กฐ์
|
| 367 |
+
while completed_month <= 0:
|
| 368 |
+
completed_month += 12
|
| 369 |
+
completed_year -= 1
|
| 370 |
+
|
| 371 |
+
logger.info(f"๐
ํ์ฌ: {current_year}๋
{current_month}์ {current_day}์ผ")
|
| 372 |
+
logger.info(f"๐ ์๋ฃ๋ ๋ง์ง๋ง ๋ฐ์ดํฐ: {completed_year}๋
{completed_month}์")
|
| 373 |
+
|
| 374 |
+
# 2๋จ๊ณ: ๋ฐ์ดํฐ ๋ถ๋ฅ ๋ฐ ์์ง
|
| 375 |
+
all_data = []
|
| 376 |
+
|
| 377 |
+
for i, date_str in enumerate(dates):
|
| 378 |
+
try:
|
| 379 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 380 |
+
|
| 381 |
+
if i < len(volumes):
|
| 382 |
+
volume = volumes[i]
|
| 383 |
+
if isinstance(volume, str):
|
| 384 |
+
volume = float(volume.replace(',', ''))
|
| 385 |
+
volume = int(volume) if volume else 0
|
| 386 |
+
|
| 387 |
+
all_data.append({
|
| 388 |
+
'year': date_obj.year,
|
| 389 |
+
'month': date_obj.month,
|
| 390 |
+
'volume': volume,
|
| 391 |
+
'date_str': date_str,
|
| 392 |
+
'date_obj': date_obj,
|
| 393 |
+
'sort_key': f"{date_obj.year:04d}{date_obj.month:02d}"
|
| 394 |
+
})
|
| 395 |
+
|
| 396 |
+
except Exception as e:
|
| 397 |
+
logger.warning(f"โ ๏ธ ๋ ์ง ํ์ฑ ์ค๋ฅ: {date_str} - {e}")
|
| 398 |
+
continue
|
| 399 |
+
|
| 400 |
+
# ์๊ฐ ์์๋๋ก ์ ๋ ฌ
|
| 401 |
+
all_data = sorted(all_data, key=lambda x: x['sort_key'])
|
| 402 |
+
|
| 403 |
+
# 3๋จ๊ณ: ์ฆ๊ฐ์จ ๊ณ์ฐ (์ฌํด ์๋ฃ์ vs ์๋
๋์)
|
| 404 |
+
this_year_completed_volume = None
|
| 405 |
+
last_year_same_month_volume = None
|
| 406 |
+
|
| 407 |
+
for data in all_data:
|
| 408 |
+
# ์ฌํด ์๋ฃ๋ ๋ง์ง๋ง ์ ์ฐพ๊ธฐ
|
| 409 |
+
if data['year'] == completed_year and data['month'] == completed_month:
|
| 410 |
+
this_year_completed_volume = data['volume']
|
| 411 |
+
logger.info(f"๐ ์ฌํด {completed_month}์ ์ค๋ฐ์ดํฐ: {this_year_completed_volume:,}ํ")
|
| 412 |
+
|
| 413 |
+
# ์๋
๋์ ์ฐพ๊ธฐ
|
| 414 |
+
if data['year'] == completed_year - 1 and data['month'] == completed_month:
|
| 415 |
+
last_year_same_month_volume = data['volume']
|
| 416 |
+
logger.info(f"๐ ์๋
{completed_month}์ ์ค๋ฐ์ดํฐ: {last_year_same_month_volume:,}ํ")
|
| 417 |
+
|
| 418 |
+
# ์ฆ๊ฐ์จ ๊ณ์ฐ
|
| 419 |
+
growth_rate = 0
|
| 420 |
+
if this_year_completed_volume is not None and last_year_same_month_volume is not None and last_year_same_month_volume > 0:
|
| 421 |
+
growth_rate = (this_year_completed_volume - last_year_same_month_volume) / last_year_same_month_volume
|
| 422 |
+
logger.info(f"๐ ๊ณ์ฐ๋ ์ฆ๊ฐ์จ: {growth_rate:+.3f} ({growth_rate * 100:+.1f}%)")
|
| 423 |
+
else:
|
| 424 |
+
logger.warning("โ ๏ธ ์ฆ๊ฐ์จ ๊ณ์ฐ์ ์ํ ๋ฐ์ดํฐ๊ฐ ๋ถ์กฑํฉ๋๋ค.")
|
| 425 |
+
|
| 426 |
+
# 4๋จ๊ณ: ์์๋ฐ์ดํฐ ์์ฑ (ํ์ฌ์ ์ดํ)
|
| 427 |
+
combined_data = []
|
| 428 |
+
month_names = ["", "1์", "2์", "3์", "4์", "5์", "6์", "7์", "8์", "9์", "10์", "11์", "12์"]
|
| 429 |
+
|
| 430 |
+
# ์๋
12์ ๋ฐ์ดํฐ ์ถ๊ฐ (์ฐ์์ฑ์ ์ํด)
|
| 431 |
+
for data in all_data:
|
| 432 |
+
if data['year'] == completed_year - 1 and data['month'] == 12:
|
| 433 |
+
combined_data.append({
|
| 434 |
+
'year': data['year'],
|
| 435 |
+
'month': data['month'],
|
| 436 |
+
'volume': data['volume'],
|
| 437 |
+
'data_type': '์๋
์ค์ ',
|
| 438 |
+
'sort_key': f"{data['year']:04d}{data['month']:02d}"
|
| 439 |
+
})
|
| 440 |
+
logger.info(f"๐ ์๋
12์ ์ค๋ฐ์ดํฐ: {data['volume']:,}ํ")
|
| 441 |
+
break
|
| 442 |
+
|
| 443 |
+
# ์ฌํด 1์๋ถํฐ ์๋ฃ์๊น์ง ์ค์ ๋ฐ์ดํฐ ์ถ๊ฐ
|
| 444 |
+
for month in range(1, completed_month + 1):
|
| 445 |
+
for data in all_data:
|
| 446 |
+
if data['year'] == completed_year and data['month'] == month:
|
| 447 |
+
combined_data.append({
|
| 448 |
+
'year': data['year'],
|
| 449 |
+
'month': data['month'],
|
| 450 |
+
'volume': data['volume'],
|
| 451 |
+
'data_type': '์ค์ ',
|
| 452 |
+
'sort_key': f"{data['year']:04d}{data['month']:02d}"
|
| 453 |
+
})
|
| 454 |
+
logger.info(f"๐ {month}์ ์ค๋ฐ์ดํฐ: {data['volume']:,}ํ")
|
| 455 |
+
break
|
| 456 |
+
|
| 457 |
+
# ์ฌํด ๋ฏธ์๋ฃ์(ํ์ฌ์+1 ~ 12์) ์์๋ฐ์ดํฐ ์์ฑ
|
| 458 |
+
for month in range(completed_month + 1, 13):
|
| 459 |
+
# ์๋
๋์ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
|
| 460 |
+
last_year_volume = None
|
| 461 |
+
for data in all_data:
|
| 462 |
+
if data['year'] == completed_year - 1 and data['month'] == month:
|
| 463 |
+
last_year_volume = data['volume']
|
| 464 |
+
break
|
| 465 |
+
|
| 466 |
+
if last_year_volume is not None:
|
| 467 |
+
# ์์ ๊ฒ์๋ = ์๋
๋์ ร (1 + ์ฆ๊ฐ์จ)
|
| 468 |
+
predicted_volume = int(last_year_volume * (1 + growth_rate))
|
| 469 |
+
predicted_volume = max(predicted_volume, 0) # ์์ ๋ฐฉ์ง
|
| 470 |
+
|
| 471 |
+
combined_data.append({
|
| 472 |
+
'year': completed_year,
|
| 473 |
+
'month': month,
|
| 474 |
+
'volume': predicted_volume,
|
| 475 |
+
'data_type': '์์',
|
| 476 |
+
'sort_key': f"{completed_year:04d}{month:02d}"
|
| 477 |
+
})
|
| 478 |
+
|
| 479 |
+
logger.info(f"๐ฎ {month}์ ์์๋ฐ์ดํฐ: {predicted_volume:,}ํ (์๋
{last_year_volume:,}ํ ร {1 + growth_rate:.3f})")
|
| 480 |
+
|
| 481 |
+
# ์๊ฐ ์์๋๋ก ์ ๋ ฌ
|
| 482 |
+
combined_data = sorted(combined_data, key=lambda x: x['sort_key'])
|
| 483 |
+
|
| 484 |
+
# 5๋จ๊ณ: ๐ ๊ฐ์ฅ ์์นํญ์ด ๋์ ์ ์ฐพ๊ธฐ (์ฐ์๋ ์๊ฐ ์์น๋ฅ )
|
| 485 |
+
max_growth_rate = 0
|
| 486 |
+
max_growth_info = "๋ฐ์ดํฐ ์์"
|
| 487 |
+
|
| 488 |
+
for i in range(len(combined_data) - 1):
|
| 489 |
+
start_data = combined_data[i]
|
| 490 |
+
end_data = combined_data[i + 1]
|
| 491 |
+
|
| 492 |
+
if start_data['volume'] > 0:
|
| 493 |
+
month_growth_rate = ((end_data['volume'] - start_data['volume']) / start_data['volume']) * 100
|
| 494 |
+
|
| 495 |
+
# ์์นํ ๊ฒฝ์ฐ๋ง ๊ณ ๋ ค
|
| 496 |
+
if month_growth_rate > max_growth_rate:
|
| 497 |
+
max_growth_rate = month_growth_rate
|
| 498 |
+
|
| 499 |
+
start_month_name = month_names[start_data['month']]
|
| 500 |
+
end_month_name = month_names[end_data['month']]
|
| 501 |
+
|
| 502 |
+
# ์ฐ๋ ์ ํ ๊ณ ๋ ค
|
| 503 |
+
if start_data['year'] != end_data['year']:
|
| 504 |
+
period_desc = f"{start_data['year']}๋
{start_month_name}({start_data['volume']:,}ํ)์์ {end_data['year']}๋
{end_month_name}({end_data['volume']:,}ํ)์ผ๋ก"
|
| 505 |
+
else:
|
| 506 |
+
period_desc = f"{start_month_name}({start_data['volume']:,}ํ)์์ {end_month_name}({end_data['volume']:,}ํ)์ผ๋ก"
|
| 507 |
+
|
| 508 |
+
# ๋ฐ์ดํฐ ์ ํ ํ๋จ
|
| 509 |
+
if start_data['data_type'] in ['์์'] and end_data['data_type'] in ['์์']:
|
| 510 |
+
data_type = "์์ ๊ธฐ๋ฐ"
|
| 511 |
+
elif start_data['data_type'] in ['์ค์ ', '์๋
์ค์ '] and end_data['data_type'] in ['์ค์ ', '์๋
์ค์ ']:
|
| 512 |
+
data_type = "์ค์ ๊ธฐ๋ฐ"
|
| 513 |
+
else:
|
| 514 |
+
data_type = "์ค์ โ์์ ๊ธฐ๋ฐ"
|
| 515 |
+
|
| 516 |
+
max_growth_info = f"{period_desc} {max_growth_rate:.1f}% ์์น ({data_type})"
|
| 517 |
+
|
| 518 |
+
# ์์น ๊ตฌ๊ฐ์ด ์๋ ๊ฒฝ์ฐ ์ต์ ํ๋ฝ๋ฅ ํ์
|
| 519 |
+
if max_growth_rate == 0:
|
| 520 |
+
min_decline_rate = float('inf')
|
| 521 |
+
for i in range(len(combined_data) - 1):
|
| 522 |
+
start_data = combined_data[i]
|
| 523 |
+
end_data = combined_data[i + 1]
|
| 524 |
+
|
| 525 |
+
if start_data['volume'] > 0:
|
| 526 |
+
month_growth_rate = ((end_data['volume'] - start_data['volume']) / start_data['volume']) * 100
|
| 527 |
+
|
| 528 |
+
if abs(month_growth_rate) < abs(min_decline_rate):
|
| 529 |
+
min_decline_rate = month_growth_rate
|
| 530 |
+
|
| 531 |
+
start_month_name = month_names[start_data['month']]
|
| 532 |
+
end_month_name = month_names[end_data['month']]
|
| 533 |
+
|
| 534 |
+
if start_data['year'] != end_data['year']:
|
| 535 |
+
period_desc = f"{start_data['year']}๋
{start_month_name}({start_data['volume']:,}ํ)์์ {end_data['year']}๋
{end_month_name}({end_data['volume']:,}ํ)์ผ๋ก"
|
| 536 |
+
else:
|
| 537 |
+
period_desc = f"{start_month_name}({start_data['volume']:,}ํ)์์ {end_month_name}({end_data['volume']:,}ํ)์ผ๋ก"
|
| 538 |
+
|
| 539 |
+
if start_data['data_type'] in ['์์'] and end_data['data_type'] in ['์์']:
|
| 540 |
+
data_type = "์์ ๊ธฐ๋ฐ"
|
| 541 |
+
elif start_data['data_type'] in ['์ค์ ', '์๋
์ค์ '] and end_data['data_type'] in ['์ค์ ', '์๋
์ค์ ']:
|
| 542 |
+
data_type = "์ค์ ๊ธฐ๋ฐ"
|
| 543 |
+
else:
|
| 544 |
+
data_type = "์ค์ โ์์ ๊ธฐ๋ฐ"
|
| 545 |
+
|
| 546 |
+
max_growth_info = f"{period_desc} {abs(min_decline_rate):.1f}% ๊ฐ์ ({data_type})"
|
| 547 |
+
|
| 548 |
+
logger.info(f"๐ ๊ฐ์ฅ ์์นํญ์ด ๋์ ์: {max_growth_info}")
|
| 549 |
+
return max_growth_info
|
| 550 |
+
|
| 551 |
+
except Exception as e:
|
| 552 |
+
logger.error(f"โ ์์นํญ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 553 |
+
import traceback
|
| 554 |
+
logger.error(f"โ ์คํ ํธ๋ ์ด์ค: {traceback.format_exc()}")
|
| 555 |
+
return "๊ณ์ฐ ์ค๋ฅ"
|
| 556 |
+
|
| 557 |
+
def get_peak_month_with_predictions(trend_data_3year, keyword):
|
| 558 |
+
"""๐ ๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์ ์ฐพ๊ธฐ - ์ค์ +์์ ๋ฐ์ดํฐ ํ์ฉ"""
|
| 559 |
+
if not trend_data_3year:
|
| 560 |
+
return "์ฐ์ค"
|
| 561 |
+
|
| 562 |
+
try:
|
| 563 |
+
keyword_data = None
|
| 564 |
+
for kw, data in trend_data_3year.items():
|
| 565 |
+
keyword_data = data
|
| 566 |
+
break
|
| 567 |
+
|
| 568 |
+
if not keyword_data or not keyword_data.get('monthly_volumes') or not keyword_data.get('dates'):
|
| 569 |
+
return "์ฐ์ค"
|
| 570 |
+
|
| 571 |
+
volumes = keyword_data['monthly_volumes']
|
| 572 |
+
dates = keyword_data['dates']
|
| 573 |
+
|
| 574 |
+
# ํ์ฌ ์์ ํ์
|
| 575 |
+
current_date = datetime.now()
|
| 576 |
+
current_year = current_date.year
|
| 577 |
+
current_month = current_date.month
|
| 578 |
+
current_day = current_date.day
|
| 579 |
+
|
| 580 |
+
# ์๋ฃ๋ ๋ง์ง๋ง ์ ๊ณ์ฐ
|
| 581 |
+
if current_day >= 2:
|
| 582 |
+
completed_year = current_year
|
| 583 |
+
completed_month = current_month - 1
|
| 584 |
+
else:
|
| 585 |
+
completed_year = current_year
|
| 586 |
+
completed_month = current_month - 2
|
| 587 |
+
|
| 588 |
+
while completed_month <= 0:
|
| 589 |
+
completed_month += 12
|
| 590 |
+
completed_year -= 1
|
| 591 |
+
|
| 592 |
+
# ๋ฐ์ดํฐ ์์ง
|
| 593 |
+
all_data = []
|
| 594 |
+
for i, date_str in enumerate(dates):
|
| 595 |
+
try:
|
| 596 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 597 |
+
if i < len(volumes):
|
| 598 |
+
volume = volumes[i]
|
| 599 |
+
if isinstance(volume, str):
|
| 600 |
+
volume = float(volume.replace(',', ''))
|
| 601 |
+
volume = int(volume) if volume else 0
|
| 602 |
+
|
| 603 |
+
all_data.append({
|
| 604 |
+
'year': date_obj.year,
|
| 605 |
+
'month': date_obj.month,
|
| 606 |
+
'volume': volume
|
| 607 |
+
})
|
| 608 |
+
except:
|
| 609 |
+
continue
|
| 610 |
+
|
| 611 |
+
# ์ฆ๊ฐ์จ ๊ณ์ฐ
|
| 612 |
+
this_year_completed_volume = None
|
| 613 |
+
last_year_same_month_volume = None
|
| 614 |
+
|
| 615 |
+
for data in all_data:
|
| 616 |
+
if data['year'] == completed_year and data['month'] == completed_month:
|
| 617 |
+
this_year_completed_volume = data['volume']
|
| 618 |
+
if data['year'] == completed_year - 1 and data['month'] == completed_month:
|
| 619 |
+
last_year_same_month_volume = data['volume']
|
| 620 |
+
|
| 621 |
+
growth_rate = 0
|
| 622 |
+
if this_year_completed_volume is not None and last_year_same_month_volume is not None and last_year_same_month_volume > 0:
|
| 623 |
+
growth_rate = (this_year_completed_volume - last_year_same_month_volume) / last_year_same_month_volume
|
| 624 |
+
|
| 625 |
+
# ์ฌํด ๋ฐ์ดํฐ ์ค๋น (์ค์ + ์์)
|
| 626 |
+
year_data = []
|
| 627 |
+
month_names = ["", "1์", "2์", "3์", "4์", "5์", "6์", "7์", "8์", "9์", "10์", "11์", "12์"]
|
| 628 |
+
|
| 629 |
+
# ์ค์ ๋ฐ์ดํฐ ์ถ๊ฐ (1์~์๋ฃ์)
|
| 630 |
+
for month in range(1, completed_month + 1):
|
| 631 |
+
for data in all_data:
|
| 632 |
+
if data['year'] == completed_year and data['month'] == month:
|
| 633 |
+
year_data.append({
|
| 634 |
+
'month': month,
|
| 635 |
+
'volume': data['volume'],
|
| 636 |
+
'data_type': '์ค์ '
|
| 637 |
+
})
|
| 638 |
+
break
|
| 639 |
+
|
| 640 |
+
# ์์ ๋ฐ์ดํฐ ์ถ๊ฐ (์๋ฃ์+1~12์)
|
| 641 |
+
for month in range(completed_month + 1, 13):
|
| 642 |
+
last_year_volume = None
|
| 643 |
+
for data in all_data:
|
| 644 |
+
if data['year'] == completed_year - 1 and data['month'] == month:
|
| 645 |
+
last_year_volume = data['volume']
|
| 646 |
+
break
|
| 647 |
+
|
| 648 |
+
if last_year_volume is not None:
|
| 649 |
+
predicted_volume = int(last_year_volume * (1 + growth_rate))
|
| 650 |
+
predicted_volume = max(predicted_volume, 0)
|
| 651 |
+
|
| 652 |
+
year_data.append({
|
| 653 |
+
'month': month,
|
| 654 |
+
'volume': predicted_volume,
|
| 655 |
+
'data_type': '์์'
|
| 656 |
+
})
|
| 657 |
+
|
| 658 |
+
# ๊ฐ์ฅ ๋์ ๊ฒ์๋ ์ฐพ๊ธฐ
|
| 659 |
+
if not year_data:
|
| 660 |
+
return "์ฐ์ค"
|
| 661 |
+
|
| 662 |
+
max_data = max(year_data, key=lambda x: x['volume'])
|
| 663 |
+
month_name = month_names[max_data['month']]
|
| 664 |
+
data_type_suffix = " - ์์" if max_data['data_type'] == '์์' else ""
|
| 665 |
+
|
| 666 |
+
return f"{month_name}({max_data['volume']:,}ํ){data_type_suffix}"
|
| 667 |
+
|
| 668 |
+
except Exception as e:
|
| 669 |
+
logger.error(f"ํผํฌ์ ๋ถ์ ์ค๋ฅ: {e}")
|
| 670 |
+
return "์ฐ์ค"
|
| 671 |
+
|
| 672 |
+
def calculate_3year_growth_rate_improved(volumes):
|
| 673 |
+
"""์๋
๋๋น ์ฆ๊ฐ์จ ๊ณ์ฐ (3๋
๋ฐ์ดํฐ์ฉ)"""
|
| 674 |
+
if len(volumes) < 24:
|
| 675 |
+
return 0
|
| 676 |
+
|
| 677 |
+
try:
|
| 678 |
+
# ์ฒซ ํด์ ๋ง์ง๋ง ํด ๋น๊ต
|
| 679 |
+
first_year = volumes[:12]
|
| 680 |
+
last_year = volumes[-12:]
|
| 681 |
+
|
| 682 |
+
first_year_avg = sum(first_year) / len(first_year)
|
| 683 |
+
last_year_avg = sum(last_year) / len(last_year)
|
| 684 |
+
|
| 685 |
+
if first_year_avg == 0:
|
| 686 |
+
return 0
|
| 687 |
+
|
| 688 |
+
growth_rate = ((last_year_avg - first_year_avg) / first_year_avg) * 100
|
| 689 |
+
return min(max(growth_rate, -50), 200) # -50% ~ 200% ๋ฒ์๋ก ์ ํ
|
| 690 |
+
|
| 691 |
+
except Exception as e:
|
| 692 |
+
logger.error(f"์๋
๋๋น ์ฆ๊ฐ์จ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 693 |
+
return 0
|
| 694 |
+
|
| 695 |
+
def calculate_max_growth_rate_pure_logic(trend_data_3year, keyword):
|
| 696 |
+
"""์์ ๋ก์ง์ผ๋ก ์ต๋ ์์นํญ ๊ณ์ฐ - ์์ ๋ฐ์ดํฐ ํฌํจ ๋ฒ์ """
|
| 697 |
+
return calculate_max_growth_rate_with_predictions(trend_data_3year, keyword)
|
| 698 |
+
|
| 699 |
+
def analyze_season_cycle_with_llm(trend_data_3year, keyword, total_volume, gemini_model):
|
| 700 |
+
"""LLM์ ์ด์ฉํ ์์ฆ ์ํ ์์ฑ ์ฌ์ดํด ๋ถ์"""
|
| 701 |
+
if not trend_data_3year or not gemini_model:
|
| 702 |
+
return "๋น์์ฆ์ํ", "์ธ์ ๋ ์ง ์ง์
๊ฐ๋ฅ", "๋ฐ์ดํฐ ๋ถ์กฑ"
|
| 703 |
+
|
| 704 |
+
try:
|
| 705 |
+
keyword_data = None
|
| 706 |
+
for kw, data in trend_data_3year.items():
|
| 707 |
+
keyword_data = data
|
| 708 |
+
break
|
| 709 |
+
|
| 710 |
+
if not keyword_data or not keyword_data.get('monthly_volumes'):
|
| 711 |
+
return "๋น์์ฆ์ํ", "์ธ์ ๋ ์ง ์ง์
๊ฐ๋ฅ", "๋ฐ์ดํฐ ๋ถ์กฑ"
|
| 712 |
+
|
| 713 |
+
volumes = keyword_data['monthly_volumes']
|
| 714 |
+
dates = keyword_data['dates']
|
| 715 |
+
|
| 716 |
+
recent_12_volumes = volumes[-12:] if len(volumes) >= 12 else volumes
|
| 717 |
+
recent_12_dates = dates[-12:] if len(dates) >= 12 else dates
|
| 718 |
+
|
| 719 |
+
if len(recent_12_volumes) < 12:
|
| 720 |
+
return "๋น์์ฆ์ํ", "์ธ์ ๋ ์ง ์ง์
๊ฐ๋ฅ", "๋ฐ์ดํฐ ๋ถ์กฑ"
|
| 721 |
+
|
| 722 |
+
monthly_data_str = ""
|
| 723 |
+
max_volume = 0
|
| 724 |
+
max_month = ""
|
| 725 |
+
for i, (date, volume) in enumerate(zip(recent_12_dates, recent_12_volumes)):
|
| 726 |
+
try:
|
| 727 |
+
date_obj = datetime.strptime(date, "%Y-%m-%d")
|
| 728 |
+
month_name = f"{date_obj.year}๋
{date_obj.month}์"
|
| 729 |
+
monthly_data_str += f"{month_name}: {volume:,}ํ\n"
|
| 730 |
+
|
| 731 |
+
# ์ต๋ ๊ฒ์๋ ์ ์ฐพ๊ธฐ
|
| 732 |
+
if volume > max_volume:
|
| 733 |
+
max_volume = volume
|
| 734 |
+
max_month = f"{date_obj.month}์({volume:,}ํ)"
|
| 735 |
+
|
| 736 |
+
except:
|
| 737 |
+
monthly_data_str += f"์-{i+1}: {volume:,}ํ\n"
|
| 738 |
+
|
| 739 |
+
current_date = datetime.now()
|
| 740 |
+
current_month = current_date.month
|
| 741 |
+
|
| 742 |
+
prompt = f"""
|
| 743 |
+
ํค์๋: '{keyword}'
|
| 744 |
+
ํ์ฌ ๊ฒ์๋: {total_volume:,}ํ
|
| 745 |
+
ํ์ฌ ์์ : {current_date.year}๋
{current_month}์
|
| 746 |
+
์๋ณ ๊ฒ์๋ ๋ฐ์ดํฐ (์ต๊ทผ 12๊ฐ์):
|
| 747 |
+
{monthly_data_str}
|
| 748 |
+
๋ค์ ํ์์ผ๋ก๋ง ๋ต๋ณํ์ธ์:
|
| 749 |
+
์ํ์ ํ: [๋ด์์ฆ์ํ/์ฌ๋ฆ์์ฆ์ํ/๊ฐ์์์ฆ์ํ/๊ฒจ์ธ์์ฆ์ํ/๋น์์ฆ์ํ/ํฌ๋ฆฌ์ค๋ง์ค์ด๋ฒคํธ์ํ/๋ฐธ๋ฐํ์ธ์ด๋ฒคํธ์ํ/์ด๋ฒ์ด๋ ์ด๋ฒคํธ์ํ/์ํ๊ธฐ์ด๋ฒคํธ์ํ/๊ธฐํ์ด๋ฒคํธ์ํ]
|
| 750 |
+
ํผํฌ์: [X์] (๊ฒ์๋์ด ๊ฐ์ฅ ๋์ ์, ์ค์ ์์น ํฌํจ)
|
| 751 |
+
์ฑ์ฅ์: [X์] (์ฆ๊ฐํญ์ด ๊ฐ์ฅ ๋์ ์)
|
| 752 |
+
ํ์ฌ์ํ: {current_month}์ ๊ธฐ์ค [๋์
๊ธฐ/์ฑ์ฅ๊ธฐ/์์ ๊ธฐ/์ ํด๊ธฐ/๋น์์ฆ๊ธฐ๊ฐ]
|
| 753 |
+
์ง์
์ถ์ฒ: [๊ตฌ์ฒด์ ์ ์ ์]
|
| 754 |
+
"""
|
| 755 |
+
|
| 756 |
+
response = gemini_model.generate_content(prompt)
|
| 757 |
+
result_text = response.text.strip()
|
| 758 |
+
|
| 759 |
+
lines = result_text.split('\n')
|
| 760 |
+
product_type = "๋น์์ฆ์ํ"
|
| 761 |
+
peak_month = max_month if max_month else "์ฐ์ค"
|
| 762 |
+
growth_month = "์ฐ์ค"
|
| 763 |
+
current_status = "์์ ๊ธฐ"
|
| 764 |
+
entry_recommendation = "์ธ์ ๋ ์ง ์ง์
๊ฐ๋ฅ"
|
| 765 |
+
|
| 766 |
+
for line in lines:
|
| 767 |
+
line = line.strip()
|
| 768 |
+
if line.startswith('์ํ์ ํ:'):
|
| 769 |
+
product_type = line.replace('์ํ์ ํ:', '').strip()
|
| 770 |
+
elif line.startswith('ํผํฌ์:'):
|
| 771 |
+
extracted_peak = line.replace('ํผํฌ์:', '').strip()
|
| 772 |
+
if '(' in extracted_peak and ')' in extracted_peak:
|
| 773 |
+
peak_month = extracted_peak
|
| 774 |
+
else:
|
| 775 |
+
peak_month = max_month if max_month else extracted_peak
|
| 776 |
+
elif line.startswith('์ฑ์ฅ์:'):
|
| 777 |
+
growth_month = line.replace('์ฑ์ฅ์:', '').strip()
|
| 778 |
+
elif line.startswith('ํ์ฌ์ํ:'):
|
| 779 |
+
current_status = line.replace('ํ์ฌ์ํ:', '').strip()
|
| 780 |
+
elif line.startswith('์ง์
์ถ์ฒ:'):
|
| 781 |
+
entry_recommendation = line.replace('์ง์
์ถ์ฒ:', '').strip()
|
| 782 |
+
|
| 783 |
+
detail_info = f"์ํ์ ํ: {product_type} | ํผํฌ์: {peak_month} | ์ฑ์ฅ์: {growth_month} | ํ์ฌ์ํ: {current_status}"
|
| 784 |
+
|
| 785 |
+
logger.info(f"LLM ์์ฆ ๋ถ์ ์๋ฃ: {product_type}, {entry_recommendation}")
|
| 786 |
+
return product_type, entry_recommendation, detail_info
|
| 787 |
+
|
| 788 |
+
except Exception as e:
|
| 789 |
+
logger.error(f"LLM ์์ฆ ์ฌ์ดํด ๋ถ์ ์ค๋ฅ: {e}")
|
| 790 |
+
return "๋น์์ฆ์ํ", "์ธ์ ๋ ์ง ์ง์
๊ฐ๋ฅ", "LLM ๋ถ์ ์ค๋ฅ"
|
| 791 |
+
|
| 792 |
+
def analyze_sourcing_strategy_improved(keyword, volume_data, trend_data_1year, trend_data_3year, filtered_keywords_df, gemini_model):
|
| 793 |
+
"""๊ฐ์ ๋ ์์ฑ์ ๋ต ๋ถ์ - ํฌ๋งทํ
์์ ๋ฐ ๊ด์ฌ๋ ๋ถ์ ๊ฐํ"""
|
| 794 |
+
|
| 795 |
+
total_volume = volume_data.get('์ด๊ฒ์๋', 0)
|
| 796 |
+
current_date = datetime.now()
|
| 797 |
+
current_month = current_date.month
|
| 798 |
+
current_year = current_date.year
|
| 799 |
+
|
| 800 |
+
# โ
์์ : ์ฌ๋ฐ๋ฅธ ๋ก์ง์ผ๋ก ์์นํญ ๊ณ์ฐ
|
| 801 |
+
growth_analysis = calculate_max_growth_rate_with_predictions(trend_data_3year, keyword)
|
| 802 |
+
|
| 803 |
+
# โ
์์ : ์ฌ๋ฐ๋ฅธ ๋ก์ง์ผ๋ก ํผํฌ์ ๊ณ์ฐ (์ค์ +์์ ๋ฐ์ดํฐ ํ์ฉ)
|
| 804 |
+
peak_month_with_volume = get_peak_month_with_predictions(trend_data_3year, keyword)
|
| 805 |
+
|
| 806 |
+
# LLM์ผ๋ก ์์ฆ ๋ถ์ (๊ธฐ์กด ์ ์ง)
|
| 807 |
+
if gemini_model:
|
| 808 |
+
product_type, entry_timing, season_detail = analyze_season_cycle_with_llm(trend_data_3year, keyword, total_volume, gemini_model)
|
| 809 |
+
else:
|
| 810 |
+
# ๊ธฐ๋ณธ๊ฐ
|
| 811 |
+
product_type = "์ฐ์ค์ํ"
|
| 812 |
+
if total_volume > 50000:
|
| 813 |
+
product_type = "์ธ๊ธฐ์ํ"
|
| 814 |
+
elif total_volume > 10000:
|
| 815 |
+
product_type = "์ค๊ฐ์ํ"
|
| 816 |
+
elif total_volume > 0:
|
| 817 |
+
product_type = "ํ์์ํ"
|
| 818 |
+
|
| 819 |
+
# 2. ๊ด์ฌ๋ ๋ถ์ ์ถ๊ฐ - ์ด๋ณด์๊ฐ ํ๋งค๊ฐ๋ฅํ ์์ฑ ๊ธฐ์ค (๊ฐ์ ๋ ๊ธฐ์ค ์ ์ฉ)
|
| 820 |
+
involvement_level = analyze_involvement_level(keyword, total_volume, gemini_model)
|
| 821 |
+
|
| 822 |
+
# ํธ๋ ๋ ๊ฒฝ๊ณ ๋ฉ์์ง
|
| 823 |
+
trend_warning = ""
|
| 824 |
+
if not trend_data_3year:
|
| 825 |
+
trend_warning = "\n\n๐ก ๋ ์ ํํ ํธ๋ ๋ ๋ฐ์ดํฐ๋ฅผ ์ํด \"1๋จ๊ณ: ๊ธฐ๋ณธ ํค์๋ ์
๋ ฅ\"์ ์คํํด๋ณด์ธ์."
|
| 826 |
+
|
| 827 |
+
# ๊ฒฐ๊ณผ ํฌ๋งทํ
์์ - ๊ตฌ๋ถ์ ๊ณผ ํญ๋ชฉ ๋ถ๋ฆฌ
|
| 828 |
+
result_content = f"""**๐ ์ํ์ ํ**
|
| 829 |
+
{product_type}
|
| 830 |
+
{involvement_level}
|
| 831 |
+
**๐ ๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์**
|
| 832 |
+
{peak_month_with_volume}
|
| 833 |
+
**๐ ๊ฐ์ฅ ์์นํญ์ด ๋์ ์**
|
| 834 |
+
{growth_analysis}{trend_warning}"""
|
| 835 |
+
|
| 836 |
+
try:
|
| 837 |
+
return {"status": "success", "content": result_content}
|
| 838 |
+
except Exception as e:
|
| 839 |
+
logger.error(f"์์ฑ์ ๋ต ๋ถ์ ์ค๋ฅ: {e}")
|
| 840 |
+
return {"status": "error", "content": "์์ฑ์ ๋ต ๋ถ์์ ์๋ฃํ ์ ์์ต๋๋ค."}
|
| 841 |
+
|
| 842 |
+
def analyze_involvement_level(keyword, total_volume, gemini_model):
|
| 843 |
+
"""๊ด์ฌ๋ ๋ถ์ ํจ์ - ์ด๋ณด์๊ฐ ํ๋งค๊ฐ๋ฅํ ์์ฑ ๊ธฐ์ค"""
|
| 844 |
+
try:
|
| 845 |
+
# ๊ธฐ๋ณธ ๊ท์น ๊ธฐ๋ฐ ๋ถ์
|
| 846 |
+
basic_involvement = get_basic_involvement_level(keyword, total_volume)
|
| 847 |
+
|
| 848 |
+
# Gemini๊ฐ ์์ผ๋ฉด LLM ๋ถ์๋ ์ํ
|
| 849 |
+
if gemini_model:
|
| 850 |
+
llm_involvement = get_llm_involvement_analysis(keyword, total_volume, gemini_model)
|
| 851 |
+
return llm_involvement
|
| 852 |
+
else:
|
| 853 |
+
return basic_involvement
|
| 854 |
+
|
| 855 |
+
except Exception as e:
|
| 856 |
+
logger.error(f"๊ด์ฌ๋ ๋ถ์ ์ค๋ฅ: {e}")
|
| 857 |
+
return "๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)"
|
| 858 |
+
|
| 859 |
+
def get_basic_involvement_level(keyword, total_volume):
|
| 860 |
+
"""๊ธฐ๋ณธ ๊ท์น ๊ธฐ๋ฐ ๊ด์ฌ๋ ๋ถ์ - ์ด๋ณด์ ํ๋งค ๊ด์ """
|
| 861 |
+
|
| 862 |
+
# ์ ๊ด์ฌ ์ํ ํค์๋ ํจํด (์ด๋ณด์ ์ง์
๊ฐ๋ฅํ ๋ถํธํด์ ์ ํ)
|
| 863 |
+
low_involvement_keywords = [
|
| 864 |
+
# ๋ถํธํด์/์ ๋ฆฌ์๋ฉ
|
| 865 |
+
"๊ฑฐ์น๋", "๋ฐ์นจ๋", "์ ๋ฆฌํจ", "์ ๋ฆฌ๋", "์๋ฉ", "ํ๋", "์คํ ๋",
|
| 866 |
+
"์ฟ ์
", "๋ฒ ๊ฐ", "๋ชฉ๋ฒ ๊ฐ", "๋ฐฉ์", "๋งคํธ", "ํจ๋",
|
| 867 |
+
# ์ผ์ด๋ธ/์ ์ ๊ด๋ฆฌ
|
| 868 |
+
"์ผ์ด๋ธ", "์ ์ ๋ฆฌ", "์ฝ๋", "์ถฉ์ ๊ธฐ", "์ด๋ํฐ",
|
| 869 |
+
# ์ฒญ์/์์ (๋๊ธฐ์
์ ํ ์ ์ธ)
|
| 870 |
+
"์ฒญ์์", "์ฒญ์๊ธฐ", "๊ฑธ๋ ", "ํ์ฌ", "๋ธ๋ฌ์",
|
| 871 |
+
# ์๋์ฐจ/์ค์ฉ์ฉํ
|
| 872 |
+
"์ฐจ๋์ฉ", "์๋์ฐจ", "ํธ๋ํฐ", "์ค๋งํธํฐ", "ํ๋ธ๋ฆฟ",
|
| 873 |
+
# ๊ฐ๋จํ ๋๊ตฌ/์ก์ธ์๋ฆฌ
|
| 874 |
+
"์ง๊ฒ", "ํํฌ", "์์", "ํด๋ฆฝ", "๊ณ ๋ฆฌ", "๋ง", "ํ๋",
|
| 875 |
+
# ๋ฏธ๋๋ผ๋ฐฉ์ง/์์
|
| 876 |
+
"๋ฏธ๋๋ผ", "๋
ผ์ฌ๋ฆฝ", "๋ฐฉ์ง", "๋ณดํธ", "์ปค๋ฒ", "์ผ์ด์ค"
|
| 877 |
+
]
|
| 878 |
+
|
| 879 |
+
# ๊ณ ๊ด์ฌ ์ํ ํค์๋ ํจํด (๋๊ธฐ์
๋
์ ๋๋ ๊ณ ๊ฐ/์ ๋ฌธ ์ ํ)
|
| 880 |
+
high_involvement_keywords = [
|
| 881 |
+
# ๋๊ธฐ์
๋
์ ์ํํ
|
| 882 |
+
"ํด์ง", "ํ์ฅ์ง", "๋ฌผํฐ์", "๋ง์คํฌ", "์ธ์ ", "์ดํธ", "๋ฆฐ์ค", "๋น๋",
|
| 883 |
+
"์น์ฝ", "์นซ์", "๊ธฐ์ ๊ท", "์๋ฆฌ๋", "์ฝ๋",
|
| 884 |
+
# ์ํ/์๋ฃ (๋ธ๋๋ ๋ฏผ๊ฐ)
|
| 885 |
+
"๋ผ๋ฉด", "๊ณผ์", "์๋ฃ", "์ปคํผ", "์ฐจ", "์ฐ์ ", "์๊ตฌ๋ฅดํธ",
|
| 886 |
+
"์", "๊น", "์ฐธ๊ธฐ๋ฆ", "๊ฐ์ฅ", "๊ณ ์ถ์ฅ", "๋์ฅ",
|
| 887 |
+
# ๊ณ ๊ฐ ์ ์์ ํ
|
| 888 |
+
"๋
ธํธ๋ถ", "์ปดํจํฐ", "์ค๋งํธํฐ", "ํ๋ธ๋ฆฟ", "์นด๋ฉ๋ผ", "TV", "๋ชจ๋ํฐ",
|
| 889 |
+
"๋์ฅ๊ณ ", "์ธํ๊ธฐ", "์์ด์ปจ", "์ฒญ์๊ธฐ", "์ ์๋ ์ธ์ง",
|
| 890 |
+
# ์๋ฃ/๊ฑด๊ฐ (์ธ์ฆ ํ์)
|
| 891 |
+
"์๋ฃ", "๊ฑด๊ฐ์ํ", "์์์ ", "๋นํ๋ฏผ", "์ฝ", "์์ฝํ",
|
| 892 |
+
# ๋ช
ํ/๋ธ๋๋
|
| 893 |
+
"๋ช
ํ", "๋ธ๋๋", "๋ญ์
๋ฆฌ", "์๊ณ", "๋ณด์", "๊ธ", "์", "๋ค์ด์๋ชฌ๋"
|
| 894 |
+
]
|
| 895 |
+
|
| 896 |
+
keyword_lower = keyword.lower()
|
| 897 |
+
|
| 898 |
+
# ์ ๊ด์ฌ ์ํ ์ฒดํฌ (๋ถํธํด์ ํค์๋ ์ฐ์ )
|
| 899 |
+
for low_kw in low_involvement_keywords:
|
| 900 |
+
if low_kw in keyword_lower:
|
| 901 |
+
return "์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ)"
|
| 902 |
+
|
| 903 |
+
# ๊ณ ๊ด์ฌ ์ํ ์ฒดํฌ (๋๊ธฐ์
๋
์ /๋ธ๋๋ ๋ฏผ๊ฐ ํค์๋)
|
| 904 |
+
for high_kw in high_involvement_keywords:
|
| 905 |
+
if high_kw in keyword_lower:
|
| 906 |
+
return "๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ)"
|
| 907 |
+
|
| 908 |
+
# ๊ฒ์๋ ๊ธฐ๋ฐ ์ถ๊ฐ ํ๋จ
|
| 909 |
+
if total_volume > 100000:
|
| 910 |
+
# ๊ฒ์๋์ด ๋งค์ฐ ๋์ผ๋ฉด ๋๊ธฐ์
์ด ๊ด์ฌ ๊ฐ์ง ๋งํ ์์ฅ
|
| 911 |
+
return "๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ)"
|
| 912 |
+
elif total_volume > 50000:
|
| 913 |
+
return "๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)"
|
| 914 |
+
elif total_volume > 5000:
|
| 915 |
+
return "๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)"
|
| 916 |
+
else:
|
| 917 |
+
# ๊ฒ์๋์ด ๋ฎ์ผ๋ฉด ํ์ ์์ฅ, ์ด๋ณด์๋ ์ง์
๊ฐ๋ฅ
|
| 918 |
+
return "์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ)"
|
| 919 |
+
|
| 920 |
+
def get_llm_involvement_analysis(keyword, total_volume, gemini_model):
|
| 921 |
+
"""LLM์ ์ด์ฉํ ์ ๊ตํ ๊ด์ฌ๋ ๋ถ์ - ์ด๋ณด์ ํ๋งค ๊ด์ ๊ธฐ์ค ์ ์ฉ"""
|
| 922 |
+
try:
|
| 923 |
+
prompt = f"""
|
| 924 |
+
'{keyword}' ์ํ์ ๊ด์ฌ๋๋ฅผ ์ด๋ณด์ ํ๋งค ๊ด์ ์์ ๋ถ์ํด์ฃผ์ธ์.
|
| 925 |
+
๊ฒ์๋: {total_volume:,}ํ
|
| 926 |
+
๊ด์ฌ๋ ์ ์ (์ด๋ณด์๊ฐ ํ๋งค๊ฐ๋ฅํ ์์ฑ ๊ธฐ์ค):
|
| 927 |
+
์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ):
|
| 928 |
+
- ๋๊ธฐ์
๋
์ ์ด ์๋ ์์ญ
|
| 929 |
+
- ์ฆ์ ๋ถํธํด์ํ๋ ์ ํ (์ง๊ธ ๋ฐ๋ก ํ์ํ ๋ฌธ์ ํด๊ฒฐ)
|
| 930 |
+
- ๋ธ๋๋ ์๊ด์์ด ๊ธฐ๋ฅ๋ง ๋๋ฉด ๊ตฌ๋งคํ๋ ์ ํ
|
| 931 |
+
- 1๋ง์~3๋ง์๋ ๊ฐ๊ฒฉ, ์๋(100๊ฐ ์ดํ) ์์ ๊ฐ๋ฅ
|
| 932 |
+
- ์์: ๋ชฉ๋ฒ ๊ฐ, ์ค๋งํธํฐ๊ฑฐ์น๋, ์๋์ ๋ฆฌํจ, ์ผ์ด๋ธ์ ๋ฆฌ๊ธฐ
|
| 933 |
+
๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ):
|
| 934 |
+
- ๋๊ธฐ์
/๋ธ๋๋๊ฐ ์์ฅ์ ๋
์ ํ๋ ์์ญ (์ด๋ณด์ ์ง์
๋ถ๊ฐ)
|
| 935 |
+
- ์ํํ(ํด์ง, ์ธ์ , ๋ง์คํฌ ๋ฑ) - ๋ธ๋๋ ์ถฉ์ฑ๋ ๋์
|
| 936 |
+
- ๊ณ ๊ฐ ์ ํ(10๋ง์ ์ด์), ์ ๋ฌธ์ฑ/์ธ์ฆ ํ์
|
| 937 |
+
- ๋์๋ณธ ํ์ํ ์์ดํ
|
| 938 |
+
- ์์: ์ ์์ ํ, ๊ฐ์ , ๋ธ๋๋ ์ํํ, ์๋ฃ์ฉํ
|
| 939 |
+
๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง):
|
| 940 |
+
- ๊ฐ๊ฒฉ๋๋ณ๋ก ์ ๊ฐํ(์ ๊ด์ฌ)๊ณผ ๊ณ ๊ฐํ(๊ณ ๊ด์ฌ)์ด ๊ณต์กด
|
| 941 |
+
- ํ๊ฒ์ด๋ ์ฉ๋์ ๋ฐ๋ผ ๊ด์ฌ๋๊ฐ ๊ทน๋ช
ํ๊ฒ ๋ฌ๋ผ์ง
|
| 942 |
+
- ์์: ์๋ฅ, ์ด๋์ฉํ, ๋ทฐํฐ์ฉํ ๋ฑ
|
| 943 |
+
๋ณตํฉ๊ด์ฌ๋์ํ์ผ๋ก ํ๋จํ ๊ฒฝ์ฐ, ๋ฐ๋์ ๊ตฌ์ฒด์ ์ธ ์ด์ ๋ฅผ ์ค๋ช
ํ์ธ์:
|
| 944 |
+
- ๊ฐ๊ฒฉ๋๋ณ ๋ถํ: "1-3๋ง์ ์ค๊ตญ์ฐ(์ ๊ด์ฌ) vs 10-15๋ง์ ๊ตญ์ฐ ์์ (๊ณ ๊ด์ฌ)"
|
| 945 |
+
- ํ๊ฒ๋ณ ์ฐจ์ด: "์ผ๋ฐ์ธ์ ์ ๊ด์ฌ vs ์ ๋ฌธ๊ฐ๋ ๊ณ ๊ด์ฌ"
|
| 946 |
+
- ์ฉ๋๋ณ ์ฐจ์ด: "์์์ฉ์ ์ ๊ด์ฌ vs ์ฅ๊ธฐ์ฉ์ ๊ณ ๊ด์ฌ"
|
| 947 |
+
๋ค์ ํ์์ผ๋ก ๋ต๋ณํ์ธ์:
|
| 948 |
+
[๊ด์ฌ๋ ์ ํ]
|
| 949 |
+
[๊ตฌ์ฒด์ ์ธ ํ๋จ ์ด์ - ๊ฐ๊ฒฉ๋/ํ๊ฒ/๋ธ๋๋ ๋
์ ์ฌ๋ถ ๋ฑ์ ๋ช
ํํ ์ ์]
|
| 950 |
+
์ ํ์ง:
|
| 951 |
+
์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ)
|
| 952 |
+
๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)
|
| 953 |
+
๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ)
|
| 954 |
+
"""
|
| 955 |
+
|
| 956 |
+
response = gemini_model.generate_content(prompt)
|
| 957 |
+
result = response.text.strip()
|
| 958 |
+
|
| 959 |
+
# ๊ฒฐ๊ณผ ํํฐ๋ง - ์ ํํ ํ์๋ง ํ์ฉ
|
| 960 |
+
if "์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ)" in result:
|
| 961 |
+
return "์ ๊ด์ฌ์ํ(์ด๋ณด์์ฉ)"
|
| 962 |
+
elif "๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ)" in result:
|
| 963 |
+
return "๊ณ ๊ด์ฌ์ํ(๊ณ ๊ธ์์ฉ)"
|
| 964 |
+
elif "๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)" in result:
|
| 965 |
+
return "๋ณตํฉ๊ด์ฌ๋์ํ(์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)"
|
| 966 |
+
else:
|
| 967 |
+
# LLM ์๋ต์ด ๋ถ์ ํํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๊ท์น์ผ๋ก ํด๋ฐฑ
|
| 968 |
+
return get_basic_involvement_level(keyword, total_volume)
|
| 969 |
+
|
| 970 |
+
except Exception as e:
|
| 971 |
+
logger.error(f"LLM ๊ด์ฌ๋ ๋ถ์ ์ค๋ฅ: {e}")
|
| 972 |
+
return get_basic_involvement_level(keyword, total_volume)
|
| 973 |
+
|
| 974 |
+
|
| 975 |
+
class CompactKeywordAnalyzer:
|
| 976 |
+
"""๊ฐ๊ฒฐํ 7๋จ๊ณ ํค์๋ ๋ถ์๊ธฐ"""
|
| 977 |
+
|
| 978 |
+
def __init__(self, gemini_model):
|
| 979 |
+
self.gemini_model = gemini_model
|
| 980 |
+
self.max_retries = 3
|
| 981 |
+
|
| 982 |
+
def call_llm_with_retry(self, prompt: str, step_name: str = "") -> str:
|
| 983 |
+
"""์ฌ์๋ ๋ก์ง์ด ์ ์ฉ๋ LLM ํธ์ถ"""
|
| 984 |
+
last_error = None
|
| 985 |
+
|
| 986 |
+
for attempt in range(self.max_retries):
|
| 987 |
+
try:
|
| 988 |
+
logger.info(f"{step_name} ์๋ {attempt + 1}/{self.max_retries}")
|
| 989 |
+
response = self.gemini_model.generate_content(prompt)
|
| 990 |
+
result = response.text.strip()
|
| 991 |
+
|
| 992 |
+
if result and len(result) > 20:
|
| 993 |
+
logger.info(f"{step_name} ์ฑ๊ณต")
|
| 994 |
+
return result
|
| 995 |
+
else:
|
| 996 |
+
raise Exception("์๋ต์ด ๋๋ฌด ์งง๊ฑฐ๋ ๋น์ด์์")
|
| 997 |
+
|
| 998 |
+
except Exception as e:
|
| 999 |
+
last_error = e
|
| 1000 |
+
logger.warning(f"{step_name} ์คํจ (์๋ {attempt + 1}): {e}")
|
| 1001 |
+
|
| 1002 |
+
if attempt < self.max_retries - 1:
|
| 1003 |
+
delay = 1.0 * (attempt + 1) + random.uniform(0, 0.5)
|
| 1004 |
+
time.sleep(delay)
|
| 1005 |
+
|
| 1006 |
+
logger.error(f"{step_name} ๋ชจ๋ ์ฌ์๋ ์คํจ: {last_error}")
|
| 1007 |
+
return f"{step_name} ๋ถ์์ ์๋ฃํ ์ ์์ต๋๋ค."
|
| 1008 |
+
|
| 1009 |
+
def clean_markdown_and_bold(self, text: str) -> str:
|
| 1010 |
+
"""๋งํฌ๋ค์ด๊ณผ ๋ณผ๋ ์ฒ๋ฆฌ๋ฅผ ์์ ํ ์ ๊ฑฐ"""
|
| 1011 |
+
text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
|
| 1012 |
+
text = re.sub(r'\*(.+?)\*', r'\1', text)
|
| 1013 |
+
text = re.sub(r'__(.+?)__', r'\1', text)
|
| 1014 |
+
text = re.sub(r'_(.+?)_', r'\1', text)
|
| 1015 |
+
text = re.sub(r'##\s*(.+)', r'\1', text)
|
| 1016 |
+
text = re.sub(r'#\s*(.+)', r'\1', text)
|
| 1017 |
+
text = re.sub(r'\*+', '', text)
|
| 1018 |
+
text = re.sub(r'_+', '', text)
|
| 1019 |
+
return text.strip()
|
| 1020 |
+
|
| 1021 |
+
def analyze_sourcing_strategy(self, keyword: str, volume_data: dict, keywords_df: Optional[pd.DataFrame], trend_data_1year=None, trend_data_3year=None) -> str:
|
| 1022 |
+
"""๊ฐ์ ๋ ์์ฑ์ ๋ต ๋ถ์ - ๊ฒฝ์๋ฐ์ดํฐ ์ ๊ฑฐ"""
|
| 1023 |
+
|
| 1024 |
+
try:
|
| 1025 |
+
sourcing_analysis = analyze_sourcing_strategy_improved(
|
| 1026 |
+
keyword, volume_data, trend_data_1year, trend_data_3year, keywords_df, self.gemini_model
|
| 1027 |
+
)
|
| 1028 |
+
if sourcing_analysis["status"] == "success":
|
| 1029 |
+
return self.clean_markdown_and_bold(sourcing_analysis["content"])
|
| 1030 |
+
else:
|
| 1031 |
+
return sourcing_analysis["content"]
|
| 1032 |
+
except Exception as e:
|
| 1033 |
+
logger.error(f"์์ฑ์ ๋ต ๋ถ์ ์ค๋ฅ: {e}")
|
| 1034 |
+
return "์์ฑ์ ๋ต ๋ถ์์ ์๋ฃํ ์ ์์ต๋๋ค."
|
| 1035 |
+
|
| 1036 |
+
def analyze_step1_product_type(self, keyword: str, keywords_df: Optional[pd.DataFrame]) -> str:
|
| 1037 |
+
"""1๋จ๊ณ. ์ํ์ ํ ๋ถ์"""
|
| 1038 |
+
|
| 1039 |
+
related_keywords = ""
|
| 1040 |
+
if keywords_df is not None and not keywords_df.empty:
|
| 1041 |
+
top_keywords = keywords_df.head(10)['์กฐํฉ ํค์๋'].tolist()
|
| 1042 |
+
related_keywords = f"์ฐ๊ดํค์๋: {', '.join(top_keywords)}"
|
| 1043 |
+
|
| 1044 |
+
prompt = f"""
|
| 1045 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1046 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1047 |
+
{related_keywords}
|
| 1048 |
+
1๋จ๊ณ. ์ํ์ ํ ๋ถ์
|
| 1049 |
+
์ํ ์ ํ ๋ถ๋ฅ ๊ธฐ์ค:
|
| 1050 |
+
- ๋ถํธํด๊ฒฐ์ํ: ํน์ ๋ฌธ์ ๋ ๋ถํธํจ์ ์ฆ๊ฐ ํด๊ฒฐํ๋ ์ ํ
|
| 1051 |
+
- ์
๊ทธ๋ ์ด๋์ํ: ์ถ์ ์ง๊ณผ ๋ง์กฑ๋๋ฅผ ํฅ์์ํค๋ ์ ํ
|
| 1052 |
+
- ํ์์ํ: ์ผ์์์ ๋ฐ๋์ ํ์ํ๊ณ ๋ฐ๋ณต ๊ตฌ๋งค๋๋ ์ ํ
|
| 1053 |
+
- ์ทจํฅ์ ๊ฒฉ์ํ: ๊ฐ์ฑ์ , ๊ฐ์ฑ์ ์๊ตฌ๋ฅผ ์๊ทนํ๋ ์ ํ
|
| 1054 |
+
- ์ตํฉ์ํ: ์ 2๊ฐ ์ด์์ ์ ํ์ด ๊ฒฐํฉ๋ ์ ํ
|
| 1055 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1056 |
+
์ฃผ์์ ํ: [์ ํ๋ช
]
|
| 1057 |
+
{keyword}๋ [๊ตฌ์ฒด์ ์ค๋ช
- ์ ์ด ์ ํ์ธ์ง ๋ณธ์ง์ ๊ฐ์น์ ํด๊ฒฐํ๋ ๋ฌธ์ ๋ฅผ ์ค์ฌ์ผ๋ก 2-3๋ฌธ์ฅ]
|
| 1058 |
+
๋ณด์กฐ์ ํ: [ํด๋น ์ ํ๋ค]
|
| 1059 |
+
[์ ํ1]
|
| 1060 |
+
- [์ด ์ ํ์ ํด๋นํ๋ ์ด์ 1๋ฌธ์ฅ]
|
| 1061 |
+
[์ ํ2]
|
| 1062 |
+
- [์ด ์ ํ์ ํด๋นํ๋ ์ด์ 1๋ฌธ์ฅ]
|
| 1063 |
+
"""
|
| 1064 |
+
|
| 1065 |
+
result = self.call_llm_with_retry(prompt, f"1๋จ๊ณ-์ํ์ ํ๋ถ์-{keyword}")
|
| 1066 |
+
return self.clean_markdown_and_bold(result)
|
| 1067 |
+
|
| 1068 |
+
def analyze_step2_target_customer(self, keyword: str, step1_result: str) -> str:
|
| 1069 |
+
"""2๋จ๊ณ. ์๋น์ ํ๊ฒ ์ค์ """
|
| 1070 |
+
|
| 1071 |
+
prompt = f"""
|
| 1072 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1073 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1074 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1075 |
+
{step1_result}
|
| 1076 |
+
2๋จ๊ณ. ์๋น์ ํ๊ฒ ์ค์
|
| 1077 |
+
๋ค์ ํ์์ผ๋ก ๊ฐ๊ฒฐํ๊ฒ ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1078 |
+
๊ณ ๊ฐ์ํฉ
|
| 1079 |
+
- [๊ตฌ์ฒด์ ์ธ ๊ตฌ๋งค ์ํฉ๋ค์ ๊ฐ๋จํ]
|
| 1080 |
+
ํ๋ฅด์๋
|
| 1081 |
+
- [์ฐ๋ น๋, ์ฑ๋ณ, ๋ผ์ดํ์คํ์ผ์ ํตํฉํ์ฌ 1-2์ค๋ก ๊ฐ๊ฒฐํ๊ฒ]
|
| 1082 |
+
์ฃผ์ ๋์ฆ
|
| 1083 |
+
- [ํต์ฌ ๋์ฆ ํ์ค๋ง]
|
| 1084 |
+
"""
|
| 1085 |
+
|
| 1086 |
+
result = self.call_llm_with_retry(prompt, f"2๋จ๊ณ-ํ๊ฒ์ค์ -{keyword}")
|
| 1087 |
+
return self.clean_markdown_and_bold(result)
|
| 1088 |
+
|
| 1089 |
+
def analyze_step3_sourcing_strategy(self, keyword: str, previous_results: str) -> str:
|
| 1090 |
+
"""3๋จ๊ณ. ํ๊ฒ๋ณ ์ฐจ๋ณํ๋ ์์ฑ ์ ๋ต ์ ์"""
|
| 1091 |
+
|
| 1092 |
+
prompt = f"""
|
| 1093 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1094 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1095 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1096 |
+
{previous_results}
|
| 1097 |
+
3๋จ๊ณ. ํ๊ฒ๋ณ ์ฐจ๋ณํ๋ ์์ฑ ์ ๋ต ์ ์
|
| 1098 |
+
ํ์ค์ ์ผ๋ก ์จ๋ผ์ธ์์ ์์ฑ ๊ฐ๋ฅํ ์ฐจ๋ณํ ์ ๋ต์ ์ ์ํด์ฃผ์ธ์.
|
| 1099 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1100 |
+
ํต์ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์์ 5๊ฐ์ง
|
| 1101 |
+
1. [์์1 ๊ฐ๋จํ]
|
| 1102 |
+
2. [์์2 ๊ฐ๋จํ]
|
| 1103 |
+
3. [์์3 ๊ฐ๋จํ]
|
| 1104 |
+
4. [์์4 ๊ฐ๋จํ]
|
| 1105 |
+
5. [์์5 ๊ฐ๋จํ]
|
| 1106 |
+
์ฐจ๋ณํ ์์ฑ ์ ๋ต
|
| 1107 |
+
1. [์ ๋ต๋ช
]
|
| 1108 |
+
- [ํ์ค์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ๋ฐฉ๋ฒ ํ์ค]
|
| 1109 |
+
2. [์ ๋ต๋ช
]
|
| 1110 |
+
- [ํ์ค์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ๋ฐฉ๋ฒ ํ์ค]
|
| 1111 |
+
3. [์ ๋ต๋ช
]
|
| 1112 |
+
- [ํ์ค์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ๋ฐฉ๋ฒ ํ์ค]
|
| 1113 |
+
4. [์ ๋ต๋ช
]
|
| 1114 |
+
- [ํ์ค์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ๋ฐฉ๋ฒ ํ์ค]
|
| 1115 |
+
5. [์ ๋ต๋ช
]
|
| 1116 |
+
- [ํ์ค์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ๋ฐฉ๋ฒ ํ์ค]
|
| 1117 |
+
"""
|
| 1118 |
+
|
| 1119 |
+
result = self.call_llm_with_retry(prompt, f"3๋จ๊ณ-์์ฑ์ ๋ต-{keyword}")
|
| 1120 |
+
return self.clean_markdown_and_bold(result)
|
| 1121 |
+
|
| 1122 |
+
def analyze_step4_product_recommendation(self, keyword: str, previous_results: str) -> str:
|
| 1123 |
+
"""4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ"""
|
| 1124 |
+
|
| 1125 |
+
prompt = f"""
|
| 1126 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1127 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1128 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1129 |
+
{previous_results}
|
| 1130 |
+
4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ
|
| 1131 |
+
3๋จ๊ณ์์ ๋์ถํ ์ฐจ๋ณํ ์์๋ฅผ ๋ฐ์ํ์ฌ ๋งค์ถ ๊ฐ๋ฅ์ฑ์ด ๋์ ์์๋๋ก ๋ถ์ํด์ฃผ์ธ์.
|
| 1132 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1133 |
+
์ฐจ๋ณํ ์ํ ์ถ์ฒ
|
| 1134 |
+
1. [๊ตฌ์ฒด์ ์ธ ์ํ๋ช
๊ณผ ์ธ๋ถ ํน์ง]
|
| 1135 |
+
- [์ฃผ์ ํน์ง๋ค, ํ๊ฒ ๊ณ ๊ฐ, ์ฐจ๋ณํ ํฌ์ธํธ๋ฅผ ํ๋ฌธ์ฅ์ผ๋ก]
|
| 1136 |
+
2. [๊ตฌ์ฒด์ ์ธ ์ํ๋ช
๊ณผ ์ธ๋ถ ํน์ง]
|
| 1137 |
+
- [์ฃผ์ ํน์ง๋ค, ํ๊ฒ ๊ณ ๊ฐ, ์ฐจ๋ณํ ํฌ์ธํธ๋ฅผ ํ๋ฌธ์ฅ์ผ๋ก]
|
| 1138 |
+
3. [๊ตฌ์ฒด์ ์ธ ์ํ๋ช
๊ณผ ์ธ๋ถ ํน์ง]
|
| 1139 |
+
- [์ฃผ์ ํน์ง๋ค, ํ๊ฒ ๊ณ ๊ฐ, ์ฐจ๋ณํ ํฌ์ธํธ๋ฅผ ํ๋ฌธ์ฅ์ผ๋ก]
|
| 1140 |
+
4. [๊ตฌ์ฒด์ ์ธ ์ํ๋ช
๊ณผ ์ธ๋ถ ํน์ง]
|
| 1141 |
+
- [์ฃผ์ ํน์ง๋ค, ํ๊ฒ ๊ณ ๊ฐ, ์ฐจ๋ณํ ํฌ์ธํธ๋ฅผ ํ๋ฌธ์ฅ์ผ๋ก]
|
| 1142 |
+
5. [๊ตฌ์ฒด์ ์ธ ์ํ๋ช
๊ณผ ์ธ๋ถ ํน์ง]
|
| 1143 |
+
- [์ฃผ์ ํน์ง๋ค, ํ๊ฒ ๊ณ ๊ฐ, ์ฐจ๋ณํ ํฌ์ธํธ๋ฅผ ํ๋ฌธ์ฅ์ผ๋ก]
|
| 1144 |
+
๋ํ์ด๋ฏธ์ง ์ถ์ฒ
|
| 1145 |
+
1. [์ฒซ ๋ฒ์งธ ์ํ๋ช
]
|
| 1146 |
+
* [๊ฐ๋จํ ์ดฌ์ ์ปจ์
๊ณผ ํต์ฌ ํฌ์ธํธ ํ์ค]
|
| 1147 |
+
2. [๋ ๋ฒ์งธ ์ํ๋ช
]
|
| 1148 |
+
* [๊ฐ๋จํ ์ดฌ์ ์ปจ์
๊ณผ ํต์ฌ ํฌ์ธํธ ํ์ค]
|
| 1149 |
+
3. [์ธ ๋ฒ์งธ ์ํ๋ช
]
|
| 1150 |
+
* [๊ฐ๋จํ ์ดฌ์ ์ปจ์
๊ณผ ํต์ฌ ํฌ์ธํธ ํ์ค]
|
| 1151 |
+
4. [๋ค ๋ฒ์งธ ์ํ๋ช
]
|
| 1152 |
+
* [๊ฐ๋จํ ์ดฌ์ ์ปจ์
๊ณผ ํต์ฌ ํฌ์ธํธ ํ์ค]
|
| 1153 |
+
5. [๋ค์ฏ ๋ฒ์งธ ์ํ๋ช
]
|
| 1154 |
+
* [๊ฐ๋จํ ์ดฌ์ ์ปจ์
๊ณผ ํต์ฌ ํฌ์ธํธ ํ์ค]
|
| 1155 |
+
"""
|
| 1156 |
+
|
| 1157 |
+
result = self.call_llm_with_retry(prompt, f"4๋จ๊ณ-์ํ์ถ์ฒ-{keyword}")
|
| 1158 |
+
return self.clean_markdown_and_bold(result)
|
| 1159 |
+
|
| 1160 |
+
def analyze_step5_trust_building(self, keyword: str, previous_results: str) -> str:
|
| 1161 |
+
"""5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง"""
|
| 1162 |
+
|
| 1163 |
+
prompt = f"""
|
| 1164 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1165 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1166 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1167 |
+
{previous_results}
|
| 1168 |
+
5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง
|
| 1169 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1170 |
+
1. [์ ๋ขฐ์ฑ ์์1]
|
| 1171 |
+
- [๊ตฌ์ฒด์ ๋ฐฉ๋ฒ๊ณผ ์ ์ฉ ์์]
|
| 1172 |
+
2. [์ ๋ขฐ์ฑ ์์2]
|
| 1173 |
+
- [๊ตฌ์ฒด์ ๋ฐฉ๋ฒ๊ณผ ์ ์ฉ ์์]
|
| 1174 |
+
3. [์ ๋ขฐ์ฑ ์์3]
|
| 1175 |
+
- [๊ตฌ์ฒด์ ๋ฐฉ๋ฒ๊ณผ ์ ์ฉ ์์]
|
| 1176 |
+
4. [์ ๋ขฐ์ฑ ์์4]
|
| 1177 |
+
- [๊ตฌ์ฒด์ ๋ฐฉ๋ฒ๊ณผ ์ ์ฉ ์์]
|
| 1178 |
+
5. [์ ๋ขฐ์ฑ ์์5]
|
| 1179 |
+
- [๊ตฌ์ฒด์ ๋ฐฉ๋ฒ๊ณผ ์ ์ฉ ์์]
|
| 1180 |
+
"""
|
| 1181 |
+
|
| 1182 |
+
result = self.call_llm_with_retry(prompt, f"5๋จ๊ณ-์ ๋ขฐ์ฑ๊ตฌ์ถ-{keyword}")
|
| 1183 |
+
return self.clean_markdown_and_bold(result)
|
| 1184 |
+
|
| 1185 |
+
def analyze_step6_usp_development(self, keyword: str, previous_results: str) -> str:
|
| 1186 |
+
"""6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง"""
|
| 1187 |
+
|
| 1188 |
+
prompt = f"""
|
| 1189 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1190 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1191 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1192 |
+
{previous_results}
|
| 1193 |
+
6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง
|
| 1194 |
+
4๋จ๊ณ์์ ์ถ์ฒํ 5๊ฐ์ง ์ํ๊ณผ ์ฐ๊ฒฐํ์ฌ ๊ฐ๊ฐ์ USP๋ฅผ ์ ์ํด์ฃผ์ธ์.
|
| 1195 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด ์ฌ์ฉ ๊ธ์ง):
|
| 1196 |
+
1. [์ฒซ ๋ฒ์งธ ์ํ์ USP ์ ๋ชฉ]
|
| 1197 |
+
- [ํต์ฌ ๊ฐ์น ์ ์๊ณผ ์ฐจ๋ณํ ํฌ์ธํธ ๊ตฌ์ฒด์ ์ค๋ช
]
|
| 1198 |
+
2. [๋ ๋ฒ์งธ ์ํ์ USP ์ ๋ชฉ]
|
| 1199 |
+
- [ํต์ฌ ๊ฐ์น ์ ์๊ณผ ์ฐจ๋ณํ ํฌ์ธํธ ๊ตฌ์ฒด์ ์ค๋ช
]
|
| 1200 |
+
3. [์ธ ๋ฒ์งธ ์ํ์ USP ์ ๋ชฉ]
|
| 1201 |
+
- [ํต์ฌ ๊ฐ์น ์ ์๊ณผ ์ฐจ๋ณํ ํฌ์ธํธ ๊ตฌ์ฒด์ ์ค๋ช
]
|
| 1202 |
+
4. [๋ค ๋ฒ์งธ ์ํ์ USP ์ ๋ชฉ]
|
| 1203 |
+
- [ํต์ฌ ๊ฐ์น ์ ์๊ณผ ์ฐจ๋ณํ ํฌ์ธํธ ๊ตฌ์ฒด์ ์ค๋ช
]
|
| 1204 |
+
5. [๋ค์ฏ ๋ฒ์งธ ์ํ์ USP ์ ๋ชฉ]
|
| 1205 |
+
- [ํต์ฌ ๊ฐ์น ์ ์๊ณผ ์ฐจ๋ณํ ํฌ์ธํธ ๊ตฌ์ฒด์ ์ค๋ช
]
|
| 1206 |
+
"""
|
| 1207 |
+
|
| 1208 |
+
result = self.call_llm_with_retry(prompt, f"6๋จ๊ณ-USP๊ฐ๋ฐ-{keyword}")
|
| 1209 |
+
return self.clean_markdown_and_bold(result)
|
| 1210 |
+
|
| 1211 |
+
def analyze_step7_copy_creation(self, keyword: str, previous_results: str) -> str:
|
| 1212 |
+
"""7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ - ์ด๋ชจํฐ์ฝ ์ ๊ฑฐ"""
|
| 1213 |
+
|
| 1214 |
+
prompt = f"""
|
| 1215 |
+
๋น์ ์ ์ด๋ณด ์
๋ฌ๊ฐ ์ํ ํ๋งค ์ฑ๊ณต์ ๋น ๋ฅด๊ฒ ์ด๋ฃฐ ์ ์๋๋ก ๋๋ ์ต๊ณ ์ ์ํ ์์ฑ ๋ฐ ์ํ๊ธฐํ ์ปจ์คํดํธ AI์
๋๋ค.
|
| 1216 |
+
๋ถ์ ํค์๋: '{keyword}'
|
| 1217 |
+
์ด์ ๋ถ์ ๊ฒฐ๊ณผ:
|
| 1218 |
+
{previous_results}
|
| 1219 |
+
7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ
|
| 1220 |
+
6๋จ๊ณ์์ ์ ์ํ 5๊ฐ์ง USP์ ์ฐ๊ฒฐํ์ฌ ๊ฐ๊ฐ์ ํค๋ ์นดํผ๋ฅผ ์ ์ํด์ฃผ์ธ์.
|
| 1221 |
+
๋ค์ ํ์์ผ๋ก ๋ถ์ํด์ฃผ์ธ์ (๋ณผ๋, ๋งํฌ๋ค์ด, ์ด๋ชจํฐ์ฝ ์ฌ์ฉ ๊ธ์ง):
|
| 1222 |
+
1. [์ฒซ ๋ฒ์งธ USP ์ฐ๊ฒฐ ์นดํผ]
|
| 1223 |
+
2. [๋ ๋ฒ์งธ USP ์ฐ๊ฒฐ ์นดํผ]
|
| 1224 |
+
3. [์ธ ๋ฒ์งธ USP ์ฐ๊ฒฐ ์นดํผ]
|
| 1225 |
+
4. [๋ค ๋ฒ์งธ USP ์ฐ๊ฒฐ ์นดํผ]
|
| 1226 |
+
5. [๋ค์ฏ ๋ฒ์งธ USP ์ฐ๊ฒฐ ์นดํผ]
|
| 1227 |
+
์ค์:
|
| 1228 |
+
- 30์ ๋ฏธ๋ง์ ๊ฐ๊ฒฐํ ํํน ๋ฌธ์ฅ๋ง ์ถ๋ ฅ
|
| 1229 |
+
- ์ด๋ชจํฐ์ฝ ์ ๋ ์ฌ์ฉ ๊ธ์ง (๐, ๐จ, โจ, ๐, ๐ ๋ฑ)
|
| 1230 |
+
- ์ํ ํ๋งค๋ฅผ ์ํ ์์ ํค๋์นดํผ๋ง ์์ฑ
|
| 1231 |
+
"""
|
| 1232 |
+
|
| 1233 |
+
result = self.call_llm_with_retry(prompt, f"7๋จ๊ณ-์นดํผ์ ์-{keyword}")
|
| 1234 |
+
return self.clean_markdown_and_bold(result)
|
| 1235 |
+
|
| 1236 |
+
def analyze_conclusion_enhanced(self, keyword: str, previous_results: str, sourcing_strategy_result: str) -> str:
|
| 1237 |
+
"""๊ฐ์ ๋ ๊ฒฐ๋ก ๋ถ์ - ๊ตฌ์ฒด์ ์๋ณ ์ง์
ํ์ด๋ฐ + 1-7๋จ๊ณ ์ข
ํฉ๋ถ์ ๊ฐํ"""
|
| 1238 |
+
|
| 1239 |
+
logger.info(f"๊ฐ์ ๋ ๊ฒฐ๋ก ๋ถ์ ์์: ํค์๋='{keyword}'")
|
| 1240 |
+
|
| 1241 |
+
# ์
๋ ฅ ๋ฐ์ดํฐ ์์ ์ฑ ํ์ธ
|
| 1242 |
+
if not sourcing_strategy_result or len(sourcing_strategy_result.strip()) < 10:
|
| 1243 |
+
logger.warning("์์ฑ์ ๋ต ๊ฒฐ๊ณผ๊ฐ ๋ถ์กฑํฉ๋๋ค.")
|
| 1244 |
+
sourcing_strategy_result = "๊ธฐ๋ณธ ์์ฑ์ ๋ต ๋ถ์"
|
| 1245 |
+
|
| 1246 |
+
if not previous_results or len(previous_results.strip()) < 10:
|
| 1247 |
+
logger.warning("7๋จ๊ณ ๋ถ์ ๊ฒฐ๊ณผ๊ฐ ๋ถ์กฑํฉ๋๋ค.")
|
| 1248 |
+
previous_results = "๊ธฐ๋ณธ 7๋จ๊ณ ๋ถ์"
|
| 1249 |
+
|
| 1250 |
+
# ํ์ฌ ์๊ณผ ์ฐ๋ ์ ๋ณด
|
| 1251 |
+
current_date = datetime.now()
|
| 1252 |
+
current_month = current_date.month
|
| 1253 |
+
current_year = current_date.year
|
| 1254 |
+
|
| 1255 |
+
# 1-7๋จ๊ณ ํต์ฌ ๋ด์ฉ ์ถ์ถ์ ์ํ ํ๋กฌํํธ - ์ค์ง์ ๋์ ์ค์ฌ
|
| 1256 |
+
comprehensive_prompt = f"""
|
| 1257 |
+
'{keyword}' ํค์๋์ ๋ํ ์ด๋ณด์
๋ฌ ๋ง์ถค ์ข
ํฉ ๊ฒฐ๋ก ์ ์์ฑํ์ธ์.
|
| 1258 |
+
ํ์ฌ ์์ : {current_year}๋
{current_month}์
|
| 1259 |
+
์ค์ ๋ฐ์ดํฐ: {sourcing_strategy_result}
|
| 1260 |
+
์ ์ฒด ๋ถ์ ๊ฒฐ๊ณผ: {previous_results}
|
| 1261 |
+
๋ค์ ๊ตฌ์กฐ๋ก 700-800์ ๋ถ๋์ ์ค์ง์ ๋์์ด ๋๋ ๊ฒฐ๋ก ์ ์์ฑํ์ธ์:
|
| 1262 |
+
1. ์ฒซ ๋ฒ์งธ ๋ฌธ๋จ (350์ ๋ด์ธ) - ์ค์ ๋ฐ์ดํฐ ๊ธฐ๋ฐ ์ง์
๋ถ์:
|
| 1263 |
+
- '{keyword}'๋ [์ค์ ๊ฒ์๋ ์์น]ํ ๊ฒ์๋๋ ์ํ์ผ๋ก [์ํ ํน์ฑ]
|
| 1264 |
+
- **๊ด์ฌ๋ ํ๋จ ์ด์ ๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช
**:
|
| 1265 |
+
* ์ ๊ด์ฌ์ธ ๊ฒฝ์ฐ: "๋๊ธฐ์
๋
์ ์ด ์๊ณ , ๊ณ ๊ฐ์ด ๋ธ๋๋ ์๊ด์์ด [๊ตฌ์ฒด์ ๊ธฐ๋ฅ]๋ง ๋๋ฉด ๋ฐ๋ก ๊ตฌ๋งคํ๋ ํน์ฑ"
|
| 1266 |
+
* ๊ณ ๊ด์ฌ์ธ ๊ฒฝ์ฐ: "[ํน์ ๋๊ธฐ์
/๋ธ๋๋]๊ฐ ์์ฅ์ ๋
์ ํ๊ณ ์์ด ๊ณ ๊ฐ์ด [๊ตฌ์ฒด์ ์์]๋ฅผ ์ ์คํ ๋น๊ต๊ฒํ ํ๋ ํน์ฑ"
|
| 1267 |
+
* ๋ณตํฉ๊ด์ฌ์ธ ๊ฒฝ์ฐ: "[๊ตฌ์ฒด์ ๊ฐ๊ฒฉ๋] ์ ๊ฐํ์ ์ ๊ด์ฌ, [๊ตฌ์ฒด์ ๊ฐ๊ฒฉ๋] ๊ณ ๊ฐํ์ ๊ณ ๊ด์ฌ๋ก ๋๋๋ ํน์ฑ"
|
| 1268 |
+
- ํ์ฌ {current_month}์ ๊ธฐ์ค [์ค์ ํผํฌ์ ๋ฐ์ดํฐ]์์ ํ์ธ๋ ๋ฐ์ ๊ฐ์ด [๊ตฌ์ฒด์ ์ง์
ํ์ด๋ฐ]
|
| 1269 |
+
- [์ค์ ์์นํญ ๋ฐ์ดํฐ]๋ฅผ ๊ณ ๋ คํ ๋ [๊ตฌ์ฒด์ ์๋ณ ์ค๋น ์ผ์ ]
|
| 1270 |
+
2. ๋ ๋ฒ์งธ ๋ฌธ๋จ (350์ ๋ด์ธ) - ๋ถ์ ๊ธฐ๋ฐ ์คํ ์ ๋ต:
|
| 1271 |
+
- ๋ถ์๋ ์ํ ํน์ฑ์ [๊ตฌ์ฒด์ ํ๊ฒ ๊ณ ๊ฐ๊ณผ ๊ทธ๋ค์ ์ค์ ๋์ฆ]๊ฐ ํต์ฌ์ด๋ฉฐ
|
| 1272 |
+
- [์ค์ ๋ถ์๋ ์ฐจ๋ณํ ํฌ์ธํธ]๋ฅผ ํ์ฉํ [๊ตฌ์ฒด์ ์์ฑ ๋ฐฉํฅ์ฑ]์ด ์ค์ํฉ๋๋ค
|
| 1273 |
+
- [๋ถ์๋ ์ ๋ขฐ์ฑ ์์์ USP]๋ฅผ ํตํด [์ค์ ์ ์ฉ ๊ฐ๋ฅํ ๋ง์ผํ
๋ฐฉ๋ฒ]
|
| 1274 |
+
- ์ด๋ณด์
๋ฌ๋ [๊ตฌ์ฒด์ ์๋ณธ ๊ท๋ชจ์ ๋ฆฌ์คํฌ]๋ฅผ ๊ณ ๋ คํ์ฌ [์ค์ ํ๋ ๊ฐ์ด๋]
|
| 1275 |
+
์ค์์ฌํญ:
|
| 1276 |
+
- ์ค์ ๊ฒ์๋, ํผํฌ์, ์์น๋ฅ ๋ฑ ๊ตฌ์ฒด์ ์์น ํ์ฉ
|
| 1277 |
+
- "๋ช๋จ๊ณ" ํํ ๊ธ์ง, ์์ฐ์ค๋ฌ์ด ๋ฌธ์ฅ์ผ๋ก ์ฐ๊ฒฐ
|
| 1278 |
+
- ์ถ์์ ํํ ๋์ ์ด๋ณด์
๋ฌ๊ฐ ๋ฐ๋ก ์ ์ฉํ ์ ์๋ ๊ตฌ์ฒด์ ๊ฐ์ด๋
|
| 1279 |
+
- ํ์์ ๋ด์ฉ ์ ๊ฑฐ, ์ค์ง์ ๋์์ด ๋๋ ๋ด์ฉ๋ง ํฌํจ
|
| 1280 |
+
- ํ์ฌ ์({current_month}์) ๊ธฐ์ค ์ฆ์ ์คํ ๊ฐ๋ฅํ ํ๋ ๊ณํ ์ ์
|
| 1281 |
+
"""
|
| 1282 |
+
|
| 1283 |
+
try:
|
| 1284 |
+
logger.info("๊ฐ์ ๋ ๊ฒฐ๋ก LLM ํธ์ถ ์์")
|
| 1285 |
+
|
| 1286 |
+
if self.gemini_model:
|
| 1287 |
+
response = self.gemini_model.generate_content(comprehensive_prompt)
|
| 1288 |
+
result = response.text.strip() if response and response.text else ""
|
| 1289 |
+
|
| 1290 |
+
if result and len(result) > 50:
|
| 1291 |
+
cleaned_result = self.clean_markdown_and_bold(result)
|
| 1292 |
+
logger.info(f"๊ฐ์ ๋ ๊ฒฐ๋ก ๋ถ์ ์ฑ๊ณต: {len(cleaned_result)} ๋ฌธ์")
|
| 1293 |
+
return cleaned_result
|
| 1294 |
+
else:
|
| 1295 |
+
logger.warning("LLM ์๋ต์ด ๋น์ด์๊ฑฐ๋ ๋๋ฌด ์งง์ต๋๋ค.")
|
| 1296 |
+
else:
|
| 1297 |
+
logger.error("Gemini ๋ชจ๋ธ์ด ์์ต๋๋ค.")
|
| 1298 |
+
|
| 1299 |
+
except Exception as e:
|
| 1300 |
+
logger.error(f"๊ฐ์ ๋ ๊ฒฐ๋ก ๋ถ์ LLM ํธ์ถ ์ค๋ฅ: {e}")
|
| 1301 |
+
|
| 1302 |
+
# ํด๋ฐฑ ๊ฒฐ๋ก ์์ฑ
|
| 1303 |
+
logger.info("ํด๋ฐฑ ๊ฒฐ๋ก ์์ฑ")
|
| 1304 |
+
return f"""'{keyword}'๋ ์ 15,000ํ ์ด์ ๊ฒ์๋๋ ์์ ์ ์ธ ์ํ์ผ๋ก, ํ์ฌ {current_month}์ ๊ธฐ์ค ์ธ์ ๋ ์ง์
๊ฐ๋ฅํ ์ฐ์ค ์ํ์
๋๋ค. ๊ฒ์๋ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ข
ํฉํ๋ฉด ์ด๋ณด์
๋ฌ์๊ฒ ๋ฆฌ์คํฌ๊ฐ ๋ฎ๊ณ ๊พธ์คํ ์์๋ฅผ ํ๋ณดํ ์ ์๋ ์์ดํ
์ผ๋ก ํ๋จ๋ฉ๋๋ค. ์ฒซ ๋ฌ 100-200๊ฐ ์๋ ์์์ผ๋ก ์์ฅ ๋ฐ์์ ํ์ธํ ํ ์ ์ง์ ์ผ๋ก ํ๋ํ๋ ๊ฒ์ด ์์ ํ ์ ๊ทผ๋ฒ์
๋๋ค.
|
| 1305 |
+
๋ถ์๋ ์ํ ํน์ฑ์ ํ์ง๊ณผ ๋ด๊ตฌ์ฑ์ ์ค์ํ๋ ์ค์ฉ์ ๊ตฌ๋งค์ธต์ด ์ฃผ ํ๊ฒ์ด๋ฉฐ, AS ์๋น์ค์ ํ์ง๋ณด์ฆ์ ์ ๊ณต์ด ์ฐจ๋ณํ์ ํต์ฌ์
๋๋ค. ๊ณ ๊ฐ ์ ๋ขฐ๋ ๊ตฌ์ถ์ ์ํด์๋ ์๋ฃ์ง ์ถ์ฒ์ด๋ ๊ณ ๊ฐ ์ฒดํ๋ด ํ์ฉ์ด ํจ๊ณผ์ ์ด๋ฉฐ, ์ด๋ณด์
๋ฌ๋ 10-20๋ง์ ์์ค์ ์์ก ํฌ์๋ก ์์ํ์ฌ ์ฌ๊ตฌ๋งค์จ ํฅ์๊ณผ ์ฐ๊ด ์ํ ํ์ฅ์ ํตํ ์์ ์ ๋งค์ถ ํ๋ณด๊ฐ ๊ถ์ฅ๋ฉ๋๋ค."""
|
| 1306 |
+
|
| 1307 |
+
def parse_step_sections(self, content: str, step_number: int) -> Dict[str, str]:
|
| 1308 |
+
"""๋จ๊ณ๋ณ ์ํญ๋ชฉ ์น์
ํ์ฑ"""
|
| 1309 |
+
|
| 1310 |
+
if step_number >= 5:
|
| 1311 |
+
return {"๋ด์ฉ": content}
|
| 1312 |
+
|
| 1313 |
+
lines = content.split('\n')
|
| 1314 |
+
sections = {}
|
| 1315 |
+
current_section = None
|
| 1316 |
+
current_content = []
|
| 1317 |
+
|
| 1318 |
+
for line in lines:
|
| 1319 |
+
line = line.strip()
|
| 1320 |
+
if not line:
|
| 1321 |
+
continue
|
| 1322 |
+
|
| 1323 |
+
is_section_title = False
|
| 1324 |
+
|
| 1325 |
+
if step_number == 0:
|
| 1326 |
+
if any(keyword in line for keyword in ['์ํ์ ํ', '๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์', '๊ฐ์ฅ ์์นํญ์ด ๋์ ์']):
|
| 1327 |
+
is_section_title = True
|
| 1328 |
+
elif step_number == 1:
|
| 1329 |
+
if any(keyword in line for keyword in ['์ฃผ์์ ํ', '๋ณด์กฐ์ ํ']):
|
| 1330 |
+
is_section_title = True
|
| 1331 |
+
elif step_number == 2:
|
| 1332 |
+
if any(keyword in line for keyword in ['๊ณ ๊ฐ์ํฉ', 'ํ๋ฅด์๋', '์ฃผ์ ๋์ฆ', '์ฃผ์๋์ฆ']):
|
| 1333 |
+
is_section_title = True
|
| 1334 |
+
elif step_number == 3:
|
| 1335 |
+
if any(keyword in line for keyword in ['ํต์ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์์', '์ฐจ๋ณํ ์์ฑ ์ ๋ต', '๊ตฌ๋งค ๊ณ ๋ ค ์์', '์์ฑ ์ ๋ต']):
|
| 1336 |
+
is_section_title = True
|
| 1337 |
+
elif step_number == 4:
|
| 1338 |
+
if any(keyword in line for keyword in ['์ฐจ๋ณํ ์ํ ์ถ์ฒ', '๋ํ์ด๋ฏธ์ง ์ถ์ฒ']):
|
| 1339 |
+
is_section_title = True
|
| 1340 |
+
elif line.endswith(':'):
|
| 1341 |
+
is_section_title = True
|
| 1342 |
+
|
| 1343 |
+
if is_section_title:
|
| 1344 |
+
if current_section and current_content:
|
| 1345 |
+
sections[current_section] = '\n'.join(current_content)
|
| 1346 |
+
|
| 1347 |
+
current_section = line.replace(':', '').strip()
|
| 1348 |
+
current_content = []
|
| 1349 |
+
else:
|
| 1350 |
+
current_content.append(line)
|
| 1351 |
+
|
| 1352 |
+
if current_section and current_content:
|
| 1353 |
+
sections[current_section] = '\n'.join(current_content)
|
| 1354 |
+
|
| 1355 |
+
if not sections:
|
| 1356 |
+
return {"๋ด์ฉ": content}
|
| 1357 |
+
|
| 1358 |
+
return sections
|
| 1359 |
+
|
| 1360 |
+
def format_section_content(self, content: str) -> str:
|
| 1361 |
+
"""์น์
๋ด์ฉ ํฌ๋งทํ
- ์ฌํํ ์์ด์ฝ์ผ๋ก ๋ณ๊ฒฝ"""
|
| 1362 |
+
lines = content.split('\n')
|
| 1363 |
+
formatted_lines = []
|
| 1364 |
+
|
| 1365 |
+
for line in lines:
|
| 1366 |
+
line = line.strip()
|
| 1367 |
+
if not line:
|
| 1368 |
+
continue
|
| 1369 |
+
|
| 1370 |
+
skip_patterns = [
|
| 1371 |
+
'์์ฑ์ ๋ต ๋ถ์', '1๋จ๊ณ. ์ํ์ ํ ๋ถ์', '4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ',
|
| 1372 |
+
'5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง', '6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง',
|
| 1373 |
+
'7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ', '๊ฒฐ๋ก '
|
| 1374 |
+
]
|
| 1375 |
+
|
| 1376 |
+
should_skip = False
|
| 1377 |
+
for pattern in skip_patterns:
|
| 1378 |
+
if pattern in line:
|
| 1379 |
+
should_skip = True
|
| 1380 |
+
break
|
| 1381 |
+
|
| 1382 |
+
if should_skip:
|
| 1383 |
+
continue
|
| 1384 |
+
|
| 1385 |
+
# ํต์ฌ ์ ๋ชฉ๋ค
|
| 1386 |
+
if any(keyword in line for keyword in ['์ํ์ ํ:', '๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์:', '๊ฐ์ฅ ์์นํญ์ด ๋์ ์:', '์ฃผ์์ ํ:', '๋ณด์กฐ์ ํ:', '๊ณ ๊ฐ์ํฉ:', 'ํ๋ฅด์๋:', '์ฃผ์ ๋์ฆ:', 'ํต์ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์์', '์ฐจ๋ณํ ์์ฑ ์ ๋ต', '์ฐจ๋ณํ ์ํ ์ถ์ฒ', '๋ํ์ด๋ฏธ์ง ์ถ์ฒ']):
|
| 1387 |
+
|
| 1388 |
+
emoji_map = {
|
| 1389 |
+
'์ํ์ ํ:': '๐๏ธ',
|
| 1390 |
+
'๊ฐ์ฅ ๊ฒ์๋์ด ๋ง์ ์:': '๐',
|
| 1391 |
+
'๊ฐ์ฅ ์์นํญ์ด ๋์ ์:': '๐',
|
| 1392 |
+
'์ฃผ์์ ํ:': '๐ฏ',
|
| 1393 |
+
'๋ณด์กฐ์ ํ:': '๐',
|
| 1394 |
+
'๊ณ ๊ฐ์ํฉ:': '๐ค',
|
| 1395 |
+
'ํ๋ฅด์๋:': '๐ญ',
|
| 1396 |
+
'์ฃผ์ ๋์ฆ:': '๐ก',
|
| 1397 |
+
'ํต์ฌ ๊ตฌ๋งค ๊ณ ๋ ค ์์': '๐',
|
| 1398 |
+
'์ฐจ๋ณํ ์์ฑ ์ ๋ต': '๐ฏ',
|
| 1399 |
+
'์ฐจ๋ณํ ์ํ ์ถ์ฒ': '๐',
|
| 1400 |
+
'๋ํ์ด๋ฏธ์ง ์ถ์ฒ': '๐ท'
|
| 1401 |
+
}
|
| 1402 |
+
|
| 1403 |
+
emoji = ""
|
| 1404 |
+
for key, value in emoji_map.items():
|
| 1405 |
+
if key in line:
|
| 1406 |
+
emoji = value + " "
|
| 1407 |
+
break
|
| 1408 |
+
|
| 1409 |
+
formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 22px; font-weight: 700; color: #2c5aa0; margin: 25px 0 12px 0; line-height: 1.4;">{emoji}{line}</div>')
|
| 1410 |
+
|
| 1411 |
+
# ๋ฒํธ ๋ฆฌ์คํธ ์ฒ๋ฆฌ
|
| 1412 |
+
elif re.match(r'^\d+\.', line):
|
| 1413 |
+
number = re.match(r'^(\d+)\.', line).group(1)
|
| 1414 |
+
number_emoji = ['1๏ธโฃ', '2๏ธโฃ', '3๏ธโฃ', '4๏ธโฃ', '5๏ธโฃ'][int(number)-1] if int(number) <= 5 else f"{number}."
|
| 1415 |
+
formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 20px; font-weight: 600; color: #2c5aa0; margin: 18px 0 10px 0; line-height: 1.4;">{number_emoji} {line[len(number)+1:].strip()}</div>')
|
| 1416 |
+
|
| 1417 |
+
# - ๋๋ โข ๋ก ์์ํ๋ ์ค๋ช
- ์ฌํํ ์์ด์ฝ
|
| 1418 |
+
elif line.startswith('-') or line.startswith('โข'):
|
| 1419 |
+
clean_line = re.sub(r'^[-โข]\s*', '', line)
|
| 1420 |
+
formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 17px; margin: 10px 0 10px 25px; color: #555; line-height: 1.6;">โข {clean_line}</div>')
|
| 1421 |
+
|
| 1422 |
+
# * ๋ก ์์ํ๋ ๋ํ์ด๋ฏธ์ง ์ค๋ช
|
| 1423 |
+
elif line.startswith('*'):
|
| 1424 |
+
clean_line = re.sub(r'^\*\s*', '', line)
|
| 1425 |
+
formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 16px; margin: 8px 0 8px 40px; color: #e67e22; line-height: 1.5;">๐ธ {clean_line}</div>')
|
| 1426 |
+
|
| 1427 |
+
# ๋ค์ฌ์ฐ๊ธฐ๋ ์ค๋ช
|
| 1428 |
+
elif line.startswith(' ') or line.startswith('\t'):
|
| 1429 |
+
clean_line = line.lstrip()
|
| 1430 |
+
formatted_lines.append(f'<div style="font-family: \'Malgun Gothic\', sans-serif; font-size: 16px; margin: 8px 0 8px 40px; color: #666; line-height: 1.5;">โ {clean_line}</div>')
|
| 1431 |
+
|
| 1432 |
+
# ์ผ๋ฐ ํ
์คํธ
|
| 1433 |
+
else:
|
| 1434 |
+
formatted_lines.append(f'<div style="font-family: \'Noto Sans KR\', sans-serif; font-size: 17px; margin: 12px 0; color: #333; line-height: 1.6;">{line}</div>')
|
| 1435 |
+
|
| 1436 |
+
return ''.join(formatted_lines)
|
| 1437 |
+
|
| 1438 |
+
def generate_step_html(self, step_title: str, content: str, step_number: int) -> str:
|
| 1439 |
+
"""๊ฐ๋ณ ๋จ๊ณ HTML ์์ฑ"""
|
| 1440 |
+
sections = self.parse_step_sections(content, step_number)
|
| 1441 |
+
|
| 1442 |
+
sections_html = ""
|
| 1443 |
+
|
| 1444 |
+
if step_number >= 5:
|
| 1445 |
+
sections_html = self.format_section_content(content)
|
| 1446 |
+
else:
|
| 1447 |
+
if sections:
|
| 1448 |
+
for section_title, section_content in sections.items():
|
| 1449 |
+
sections_html += f"""
|
| 1450 |
+
<div style="margin-bottom: 25px; border: 1px solid #e0e0e0; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
|
| 1451 |
+
<div style="background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); padding: 15px; border-bottom: 1px solid #e0e0e0;">
|
| 1452 |
+
<div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 18px; font-weight: 600; color: #495057;">๐ {section_title}</div>
|
| 1453 |
+
</div>
|
| 1454 |
+
<div style="padding: 20px; background: #fefefe;">
|
| 1455 |
+
{self.format_section_content(section_content)}
|
| 1456 |
+
</div>
|
| 1457 |
+
</div>
|
| 1458 |
+
"""
|
| 1459 |
+
else:
|
| 1460 |
+
sections_html = self.format_section_content(content)
|
| 1461 |
+
|
| 1462 |
+
step_emoji_map = {
|
| 1463 |
+
"์์ฑ์ ๋ต ๋ถ์": "๐",
|
| 1464 |
+
"1๋จ๊ณ. ์ํ์ ํ ๋ถ์": "๐ฏ",
|
| 1465 |
+
"2๋จ๊ณ. ์๋น์ ํ๊ฒ ์ค์ ": "๐ฅ",
|
| 1466 |
+
"3๋จ๊ณ. ํ๊ฒ๋ณ ์ฐจ๋ณํ๋ ์์ฑ ์ ๋ต ์ ์": "๐",
|
| 1467 |
+
"4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ": "๐",
|
| 1468 |
+
"5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง": "๐ก๏ธ",
|
| 1469 |
+
"6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง": "โญ",
|
| 1470 |
+
"7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ": "โ๏ธ",
|
| 1471 |
+
"๊ฒฐ๋ก ": "๐"
|
| 1472 |
+
}
|
| 1473 |
+
|
| 1474 |
+
step_emoji = step_emoji_map.get(step_title, "๐")
|
| 1475 |
+
|
| 1476 |
+
return f"""
|
| 1477 |
+
<div style="margin-bottom: 35px; border: 2px solid #dee2e6; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
|
| 1478 |
+
<div style="background: linear-gradient(135deg, #6c757d 0%, #495057 100%); padding: 20px; border-bottom: 2px solid #dee2e6;">
|
| 1479 |
+
<div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 22px; font-weight: 700; color: white;">{step_emoji} {step_title}</div>
|
| 1480 |
+
</div>
|
| 1481 |
+
<div style="padding: 30px; background: white;">
|
| 1482 |
+
{sections_html}
|
| 1483 |
+
</div>
|
| 1484 |
+
</div>
|
| 1485 |
+
"""
|
| 1486 |
+
|
| 1487 |
+
def generate_final_html(self, keyword: str, all_steps: Dict[str, str]) -> str:
|
| 1488 |
+
"""์ต์ข
HTML ๋ฆฌํฌํธ ์์ฑ"""
|
| 1489 |
+
|
| 1490 |
+
steps_html = ""
|
| 1491 |
+
step_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
| 1492 |
+
|
| 1493 |
+
for i, (step_title, content) in enumerate(all_steps.items(), 1):
|
| 1494 |
+
step_number = step_numbers[i-1] if i <= len(step_numbers) else i
|
| 1495 |
+
steps_html += self.generate_step_html(step_title, content, step_number)
|
| 1496 |
+
|
| 1497 |
+
return f"""
|
| 1498 |
+
<div style="max-width: 1000px; margin: 0 auto; padding: 25px; font-family: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif; background: #f8f9fa;">
|
| 1499 |
+
<div style="text-align: center; padding: 30px; margin-bottom: 35px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 12px; color: white; box-shadow: 0 6px 12px rgba(0,0,0,0.15);">
|
| 1500 |
+
<div style="margin: 0; font-family: 'Malgun Gothic', sans-serif; font-size: 28px; font-weight: 700; color: white;">๐ {keyword} ํค์๋ ๋ถ์ ๋ฆฌํฌํธ</div>
|
| 1501 |
+
<div style="margin: 15px 0 0 0; font-size: 18px; color: #e9ecef;">์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๊ฒฐ ๋ถ์ ๊ฒฐ๊ณผ</div>
|
| 1502 |
+
</div>
|
| 1503 |
+
|
| 1504 |
+
{steps_html}
|
| 1505 |
+
|
| 1506 |
+
<div style="text-align: center; padding: 20px; margin-top: 30px; background: #e9ecef; border-radius: 8px; color: #6c757d;">
|
| 1507 |
+
<div style="font-size: 14px;">๐ AI ์ํ ์์ฑ ๋ถ์๊ธฐ v4.0 - ์คํ์ด์ค๋ฐ ์ฒ๋ฆฌ ๊ฐ์ + ์ฌ๋ฐ๋ฅธ ํธ๋ ๋ ๋ถ์ ๋ก์ง</div>
|
| 1508 |
+
</div>
|
| 1509 |
+
</div>
|
| 1510 |
+
"""
|
| 1511 |
+
|
| 1512 |
+
def analyze_keyword_complete(self, keyword: str, volume_data: Dict,
|
| 1513 |
+
keywords_df: Optional[pd.DataFrame], trend_data_1year=None, trend_data_3year=None) -> Dict[str, str]:
|
| 1514 |
+
"""์ ์ฒด 8๋จ๊ณ ํค์๋ ๋ถ์ ์คํ (์์ฑ์ ๋ต + 7๋จ๊ณ + ๊ฐ์ ๋ ๊ฒฐ๋ก )"""
|
| 1515 |
+
|
| 1516 |
+
logger.info(f"8๋จ๊ณ ํค์๋ ๋ถ์ ์์: '{keyword}'")
|
| 1517 |
+
|
| 1518 |
+
# 0๋จ๊ณ: ๊ฐ์ ๋ ์์ฑ์ ๋ต ๋ถ์
|
| 1519 |
+
sourcing_result = self.analyze_sourcing_strategy(keyword, volume_data, keywords_df, trend_data_1year, trend_data_3year)
|
| 1520 |
+
sourcing_html = self.generate_step_html("์์ฑ์ ๋ต ๋ถ์", sourcing_result, 0)
|
| 1521 |
+
|
| 1522 |
+
# 1-7๋จ๊ณ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๋์
๋๋ฆฌ
|
| 1523 |
+
step_results = {}
|
| 1524 |
+
|
| 1525 |
+
# 1๋จ๊ณ: ์ํ์ ํ ๋ถ์
|
| 1526 |
+
step1_result = self.analyze_step1_product_type(keyword, keywords_df)
|
| 1527 |
+
step_results["1๋จ๊ณ"] = step1_result
|
| 1528 |
+
step1_html = self.generate_step_html("1๋จ๊ณ. ์ํ์ ํ ๋ถ์", step1_result, 1)
|
| 1529 |
+
|
| 1530 |
+
# 2๋จ๊ณ: ์๋น์ ํ๊ฒ ์ค์
|
| 1531 |
+
step2_result = self.analyze_step2_target_customer(keyword, step1_result)
|
| 1532 |
+
step_results["2๋จ๊ณ"] = step2_result
|
| 1533 |
+
step2_html = self.generate_step_html("2๋จ๊ณ. ์๋น์ ํ๊ฒ ์ค์ ", step2_result, 2)
|
| 1534 |
+
|
| 1535 |
+
# 3๋จ๊ณ: ์์ฑ ์ ๋ต
|
| 1536 |
+
previous_results = f"{step1_result}\n\n{step2_result}"
|
| 1537 |
+
step3_result = self.analyze_step3_sourcing_strategy(keyword, previous_results)
|
| 1538 |
+
step_results["3๋จ๊ณ"] = step3_result
|
| 1539 |
+
step3_html = self.generate_step_html("3๋จ๊ณ. ํ๊ฒ๋ณ ์ฐจ๋ณํ๋ ์์ฑ ์ ๋ต ์ ์", step3_result, 3)
|
| 1540 |
+
|
| 1541 |
+
# 4๋จ๊ณ: ์ํ ์ถ์ฒ
|
| 1542 |
+
previous_results += f"\n\n{step3_result}"
|
| 1543 |
+
step4_result = self.analyze_step4_product_recommendation(keyword, previous_results)
|
| 1544 |
+
step_results["4๋จ๊ณ"] = step4_result
|
| 1545 |
+
step4_html = self.generate_step_html("4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ", step4_result, 4)
|
| 1546 |
+
|
| 1547 |
+
# 5๋จ๊ณ: ์ ๋ขฐ์ฑ ๊ตฌ์ถ
|
| 1548 |
+
previous_results += f"\n\n{step4_result}"
|
| 1549 |
+
step5_result = self.analyze_step5_trust_building(keyword, previous_results)
|
| 1550 |
+
step_results["5๋จ๊ณ"] = step5_result
|
| 1551 |
+
step5_html = self.generate_step_html("5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง", step5_result, 5)
|
| 1552 |
+
|
| 1553 |
+
# 6๋จ๊ณ: USP ๊ฐ๋ฐ
|
| 1554 |
+
previous_results += f"\n\n{step5_result}"
|
| 1555 |
+
step6_result = self.analyze_step6_usp_development(keyword, previous_results)
|
| 1556 |
+
step_results["6๋จ๊ณ"] = step6_result
|
| 1557 |
+
step6_html = self.generate_step_html("6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง", step6_result, 6)
|
| 1558 |
+
|
| 1559 |
+
# 7๋จ๊ณ: ์นดํผ ์ ์
|
| 1560 |
+
previous_results += f"\n\n{step6_result}"
|
| 1561 |
+
step7_result = self.analyze_step7_copy_creation(keyword, previous_results)
|
| 1562 |
+
step_results["7๋จ๊ณ"] = step7_result
|
| 1563 |
+
step7_html = self.generate_step_html("7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ", step7_result, 7)
|
| 1564 |
+
|
| 1565 |
+
# ๊ฐ์ ๋ ๊ฒฐ๋ก : ๊ตฌ์ฒด์ ์๋ณ ์ง์
ํ์ด๋ฐ + 1-7๋จ๊ณ ์ข
ํฉ๋ถ์ ๊ฐํ
|
| 1566 |
+
conclusion_result = self.analyze_conclusion_enhanced(keyword, previous_results + f"\n\n{step7_result}", sourcing_result)
|
| 1567 |
+
conclusion_html = self.generate_step_html("๊ฒฐ๋ก ", conclusion_result, 8)
|
| 1568 |
+
|
| 1569 |
+
# ์ ์ฒด HTML ์์ฑ (์์ฑ์ ๋ต์ด ๋งจ ์์ ์์น)
|
| 1570 |
+
all_steps = {
|
| 1571 |
+
"์์ฑ์ ๋ต ๋ถ์": sourcing_result,
|
| 1572 |
+
"1๋จ๊ณ. ์ํ์ ํ ๋ถ์": step1_result,
|
| 1573 |
+
"2๋จ๊ณ. ์๋น์ ํ๊ฒ ์ค์ ": step2_result,
|
| 1574 |
+
"3๋จ๊ณ. ํ๊ฒ๋ณ ์ฐจ๋ณํ๋ ์์ฑ ์ ๋ต ์ ์": step3_result,
|
| 1575 |
+
"4๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ ์ํ 5๊ฐ์ง ์ถ์ฒ": step4_result,
|
| 1576 |
+
"5๋จ๊ณ. ์ ๋ขฐ์ฑ์ ์ค ์ ์๋ ์์ 5๊ฐ์ง": step5_result,
|
| 1577 |
+
"6๋จ๊ณ. ์ฐจ๋ณํ ์์๋ณ USP 5๊ฐ์ง": step6_result,
|
| 1578 |
+
"7๋จ๊ณ. USP๋ณ ์์ธํ์ด์ง ํค๋ ์นดํผ": step7_result,
|
| 1579 |
+
"๊ฒฐ๋ก ": conclusion_result
|
| 1580 |
+
}
|
| 1581 |
+
|
| 1582 |
+
full_html = self.generate_final_html(keyword, all_steps)
|
| 1583 |
+
|
| 1584 |
+
# ๊ฐ๋ณ ๋จ๊ณ HTML๊ณผ ์ ์ฒด HTML ๋ฐํ
|
| 1585 |
+
return {
|
| 1586 |
+
"sourcing_html": self.generate_step_html("์์ฑ์ ๋ต ๋ถ์", sourcing_result, 0),
|
| 1587 |
+
"step1_html": step1_html,
|
| 1588 |
+
"step2_html": step2_html,
|
| 1589 |
+
"step3_html": step3_html,
|
| 1590 |
+
"step4_html": step4_html,
|
| 1591 |
+
"step5_html": step5_html,
|
| 1592 |
+
"step6_html": step6_html,
|
| 1593 |
+
"step7_html": step7_html,
|
| 1594 |
+
"conclusion_html": conclusion_html,
|
| 1595 |
+
"full_html": full_html,
|
| 1596 |
+
"results": all_steps
|
| 1597 |
+
}
|
| 1598 |
+
|
| 1599 |
+
|
| 1600 |
+
# ===== ๋ฉ์ธ ๋ถ์ ํจ์๋ค =====
|
| 1601 |
+
|
| 1602 |
+
def analyze_keyword_for_sourcing(analysis_keyword, volume_data, trend_data_1year=None,
|
| 1603 |
+
trend_data_3year=None, filtered_keywords_df=None,
|
| 1604 |
+
target_categories=None, gemini_model=None):
|
| 1605 |
+
"""
|
| 1606 |
+
๋ฉ์ธ ๋ถ์ ํจ์ - ์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๊ฒฐ ๋ถ์
|
| 1607 |
+
๊ธฐ์กด ํจ์๋ช
์ ์งํ์ฌ ํธํ์ฑ ํ๋ณด
|
| 1608 |
+
"""
|
| 1609 |
+
|
| 1610 |
+
if not gemini_model:
|
| 1611 |
+
return generate_error_response("Gemini AI ๋ชจ๋ธ์ด ์ด๊ธฐํ๋์ง ์์์ต๋๋ค.")
|
| 1612 |
+
|
| 1613 |
+
try:
|
| 1614 |
+
logger.info(f"์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๊ฒฐ ํค์๋ ๋ถ์ ์์: '{analysis_keyword}'")
|
| 1615 |
+
|
| 1616 |
+
analyzer = CompactKeywordAnalyzer(gemini_model)
|
| 1617 |
+
result = analyzer.analyze_keyword_complete(analysis_keyword, volume_data, filtered_keywords_df, trend_data_1year, trend_data_3year)
|
| 1618 |
+
|
| 1619 |
+
logger.info(f"์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๊ฒฐ ํค์๋ ๋ถ์ ์๋ฃ: '{analysis_keyword}'")
|
| 1620 |
+
|
| 1621 |
+
# ๊ธฐ์กด ํธํ์ฑ์ ์ํด full_html ๋ฐํ
|
| 1622 |
+
return result["full_html"]
|
| 1623 |
+
|
| 1624 |
+
except Exception as e:
|
| 1625 |
+
logger.error(f"ํค์๋ ๋ถ์ ์ค๋ฅ: {e}")
|
| 1626 |
+
return generate_error_response(f"ํค์๋ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 1627 |
+
|
| 1628 |
+
def analyze_keyword_with_individual_steps(analysis_keyword, volume_data, trend_data_1year=None,
|
| 1629 |
+
trend_data_3year=None, filtered_keywords_df=None,
|
| 1630 |
+
target_categories=None, gemini_model=None):
|
| 1631 |
+
"""
|
| 1632 |
+
๊ฐ๋ณ ๋จ๊ณ HTML์ ํฌํจํ ์ ์ฒด ๋ถ์ ํจ์
|
| 1633 |
+
์์ฑ์ ๋ต + ๊ฐ 7๋จ๊ณ๋ณ ๊ฐ๋ณ HTML๊ณผ ์ ์ฒด HTML์ ๋ชจ๋ ๋ฐํ
|
| 1634 |
+
"""
|
| 1635 |
+
|
| 1636 |
+
if not gemini_model:
|
| 1637 |
+
error_html = generate_error_response("Gemini AI ๋ชจ๋ธ์ด ์ด๊ธฐํ๋์ง ์์์ต๋๋ค.")
|
| 1638 |
+
return {
|
| 1639 |
+
"sourcing_html": error_html, "step1_html": error_html, "step2_html": error_html, "step3_html": error_html,
|
| 1640 |
+
"step4_html": error_html, "step5_html": error_html, "step6_html": error_html,
|
| 1641 |
+
"step7_html": error_html, "conclusion_html": error_html, "full_html": error_html,
|
| 1642 |
+
"results": {}
|
| 1643 |
+
}
|
| 1644 |
+
|
| 1645 |
+
try:
|
| 1646 |
+
logger.info(f"์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๋ณ ํค์๋ ๋ถ์ ์์: '{analysis_keyword}'")
|
| 1647 |
+
|
| 1648 |
+
analyzer = CompactKeywordAnalyzer(gemini_model)
|
| 1649 |
+
result = analyzer.analyze_keyword_complete(analysis_keyword, volume_data, filtered_keywords_df, trend_data_1year, trend_data_3year)
|
| 1650 |
+
|
| 1651 |
+
logger.info(f"์์ฑ์ ๋ต + 7๋จ๊ณ ๊ฐ๋ณ ํค์๋ ๋ถ์ ์๋ฃ: '{analysis_keyword}'")
|
| 1652 |
+
return result
|
| 1653 |
+
|
| 1654 |
+
except Exception as e:
|
| 1655 |
+
logger.error(f"ํค์๋ ๋ถ์ ์ค๋ฅ: {e}")
|
| 1656 |
+
error_html = generate_error_response(f"ํค์๋ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 1657 |
+
return {
|
| 1658 |
+
"sourcing_html": error_html, "step1_html": error_html, "step2_html": error_html, "step3_html": error_html,
|
| 1659 |
+
"step4_html": error_html, "step5_html": error_html, "step6_html": error_html,
|
| 1660 |
+
"step7_html": error_html, "conclusion_html": error_html, "full_html": error_html,
|
| 1661 |
+
"results": {}
|
| 1662 |
+
}
|
| 1663 |
+
|
| 1664 |
+
def generate_error_response(error_message):
|
| 1665 |
+
"""์๋ฌ ๋ฉ์์ง๋ฅผ ํ์ค์ ์คํ์ผ๋ก ์์ฑ"""
|
| 1666 |
+
return f'''
|
| 1667 |
+
<div style="color: #721c24; padding: 30px; text-align: center; width: 100%;
|
| 1668 |
+
background-color: #f8d7da; border-radius: 12px; border: 1px solid #f5c6cb; font-family: 'Pretendard', sans-serif;">
|
| 1669 |
+
<h3 style="margin-bottom: 15px; color: #721c24;">โ ๋ถ์ ์คํจ</h3>
|
| 1670 |
+
<p style="margin-bottom: 20px; font-size: 16px;">{error_message}</p>
|
| 1671 |
+
|
| 1672 |
+
<div style="background: white; padding: 20px; border-radius: 8px; color: #333; text-align: left;">
|
| 1673 |
+
<h4 style="color: #721c24; margin-bottom: 15px;">๐ง ํด๊ฒฐ ๋ฐฉ๋ฒ</h4>
|
| 1674 |
+
<ul style="padding-left: 20px; line-height: 1.8;">
|
| 1675 |
+
<li>๐ ํค์๋ ํ์ธ: ์ฌ๋ฐ๋ฅธ ํ๊ธ ํค์๋์ธ์ง ํ์ธ</li>
|
| 1676 |
+
<li>๐ ๊ฒ์๋ ํ์ธ: ๋๋ฌด ์์ํ ํค์๋๋ ๋ฐ์ดํฐ๊ฐ ์์ ์ ์์</li>
|
| 1677 |
+
<li>๐ ๋คํธ์ํฌ ์ํ: ์ธํฐ๋ท ์ฐ๊ฒฐ ์ํ ํ์ธ</li>
|
| 1678 |
+
<li>๐ง API ์ํ: ๋ค์ด๋ฒ API ์๋ฒ ์ํ ํ์ธ</li>
|
| 1679 |
+
<li>๐ ์ฌ์๋: ์ ์ ํ ๋ค์ ์๋ํด๋ณด์ธ์</li>
|
| 1680 |
+
</ul>
|
| 1681 |
+
</div>
|
| 1682 |
+
|
| 1683 |
+
<div style="margin-top: 15px; padding: 10px; background: #d1ecf1; border-radius: 6px; color: #0c5460; font-size: 14px;">
|
| 1684 |
+
๐ก ํ: 2๋จ๊ณ์์ ์ถ์ถ๋ ํค์๋ ๋ชฉ๋ก์ ์ฐธ๊ณ ํ์ฌ ๊ฒ์ฆ๋ ํค์๋๋ฅผ ์ฌ์ฉํด๋ณด์ธ์.
|
| 1685 |
+
</div>
|
| 1686 |
+
</div>
|
| 1687 |
+
'''
|
keyword_analysis_report.css
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ํค์๋ ๋ถ์ ๋ณด๊ณ ์ ์ ์ฉ CSS - ๋คํฌ๋ชจ๋ ์ ์ฉ */
|
| 2 |
+
|
| 3 |
+
/* CSS ๋ณ์ ์ ์ (๋ผ์ดํธ๋ชจ๋) */
|
| 4 |
+
:root {
|
| 5 |
+
--primary-color: #FB7F0D;
|
| 6 |
+
--text-color: #333;
|
| 7 |
+
--bg-color: #f8f9fa;
|
| 8 |
+
--card-bg: #ffffff;
|
| 9 |
+
--border-color: #ecf0f1;
|
| 10 |
+
--section-bg: #ffffff;
|
| 11 |
+
--insight-bg: #fff5e6;
|
| 12 |
+
--warning-bg: #fff3cd;
|
| 13 |
+
--warning-text: #856404;
|
| 14 |
+
--checklist-bg: #fff3cd;
|
| 15 |
+
--cross-sell-bg: #f8f9fa;
|
| 16 |
+
--cross-sell-border: #17a2b8;
|
| 17 |
+
--cross-sell-text: #0c5460;
|
| 18 |
+
--product-bg: white;
|
| 19 |
+
--trend-bg: #e3f2fd;
|
| 20 |
+
--trend-border: #2196f3;
|
| 21 |
+
--metric-bg: #f8f9fa;
|
| 22 |
+
--metric-border: #dee2e6;
|
| 23 |
+
--highlight-bg: #fff3cd;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/* ๋คํฌ๋ชจ๋ ์์ ๋ณ์ (์๋ ๊ฐ์ง) */
|
| 27 |
+
@media (prefers-color-scheme: dark) {
|
| 28 |
+
:root {
|
| 29 |
+
--text-color: #e5e5e5;
|
| 30 |
+
--bg-color: #1a1a1a;
|
| 31 |
+
--card-bg: #2d2d2d;
|
| 32 |
+
--border-color: #404040;
|
| 33 |
+
--section-bg: #2d2d2d;
|
| 34 |
+
--insight-bg: #3d2817;
|
| 35 |
+
--warning-bg: #3d3317;
|
| 36 |
+
--warning-text: #d4b75f;
|
| 37 |
+
--checklist-bg: #3d3317;
|
| 38 |
+
--cross-sell-bg: #1a1a1a;
|
| 39 |
+
--cross-sell-border: #17a2b8;
|
| 40 |
+
--cross-sell-text: #4dd0e1;
|
| 41 |
+
--product-bg: #2d2d2d;
|
| 42 |
+
--trend-bg: #1a2332;
|
| 43 |
+
--trend-border: #2196f3;
|
| 44 |
+
--metric-bg: #2d2d2d;
|
| 45 |
+
--metric-border: #404040;
|
| 46 |
+
--highlight-bg: #3d3317;
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/* ์๋ ๋คํฌ๋ชจ๋ ํด๋์ค */
|
| 51 |
+
[data-theme="dark"],
|
| 52 |
+
.dark,
|
| 53 |
+
.gr-theme-dark {
|
| 54 |
+
--text-color: #e5e5e5;
|
| 55 |
+
--bg-color: #1a1a1a;
|
| 56 |
+
--card-bg: #2d2d2d;
|
| 57 |
+
--border-color: #404040;
|
| 58 |
+
--section-bg: #2d2d2d;
|
| 59 |
+
--insight-bg: #3d2817;
|
| 60 |
+
--warning-bg: #3d3317;
|
| 61 |
+
--warning-text: #d4b75f;
|
| 62 |
+
--checklist-bg: #3d3317;
|
| 63 |
+
--cross-sell-bg: #1a1a1a;
|
| 64 |
+
--cross-sell-border: #17a2b8;
|
| 65 |
+
--cross-sell-text: #4dd0e1;
|
| 66 |
+
--product-bg: #2d2d2d;
|
| 67 |
+
--trend-bg: #1a2332;
|
| 68 |
+
--trend-border: #2196f3;
|
| 69 |
+
--metric-bg: #2d2d2d;
|
| 70 |
+
--metric-border: #404040;
|
| 71 |
+
--highlight-bg: #3d3317;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.keyword-analysis-report {
|
| 75 |
+
font-family: 'Pretendard', 'Noto Sans KR', sans-serif;
|
| 76 |
+
line-height: 1.6;
|
| 77 |
+
color: var(--text-color);
|
| 78 |
+
margin: 0;
|
| 79 |
+
padding: 0;
|
| 80 |
+
background-color: var(--bg-color);
|
| 81 |
+
transition: background-color 0.3s ease, color 0.3s ease;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.report-container {
|
| 85 |
+
max-width: 900px;
|
| 86 |
+
margin: 20px auto;
|
| 87 |
+
padding: 0;
|
| 88 |
+
background: transparent;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
.report-title {
|
| 92 |
+
text-align: center;
|
| 93 |
+
font-size: 2.2em;
|
| 94 |
+
margin-bottom: 30px;
|
| 95 |
+
color: var(--text-color);
|
| 96 |
+
font-weight: 700;
|
| 97 |
+
border-bottom: 3px solid var(--primary-color);
|
| 98 |
+
padding-bottom: 15px;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/* ๊ฐ ๋ถ์ ํญ๋ชฉ๋ณ ์น์
๋ธ๋ก */
|
| 102 |
+
.analysis-section-block {
|
| 103 |
+
background-color: var(--section-bg);
|
| 104 |
+
padding: 25px 30px;
|
| 105 |
+
margin-bottom: 25px;
|
| 106 |
+
border-radius: 12px;
|
| 107 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
|
| 108 |
+
border-left: 5px solid var(--primary-color);
|
| 109 |
+
color: var(--text-color);
|
| 110 |
+
transition: background-color 0.3s ease, color 0.3s ease;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.analysis-section-block:nth-child(1) { border-left-color: #3498db; }
|
| 114 |
+
.analysis-section-block:nth-child(2) { border-left-color: #e74c3c; }
|
| 115 |
+
.analysis-section-block:nth-child(3) { border-left-color: #f39c12; }
|
| 116 |
+
.analysis-section-block:nth-child(4) { border-left-color: #9b59b6; }
|
| 117 |
+
.analysis-section-block:nth-child(5) { border-left-color: #1abc9c; }
|
| 118 |
+
.analysis-section-block:nth-child(6) { border-left-color: #34495e; }
|
| 119 |
+
.analysis-section-block:nth-child(7) { border-left-color: #e67e22; }
|
| 120 |
+
|
| 121 |
+
.analysis-section-title {
|
| 122 |
+
margin: 0 0 20px 0;
|
| 123 |
+
color: var(--text-color);
|
| 124 |
+
font-size: 1.6em;
|
| 125 |
+
font-weight: 700;
|
| 126 |
+
display: flex;
|
| 127 |
+
align-items: center;
|
| 128 |
+
border-bottom: 2px solid var(--border-color);
|
| 129 |
+
padding-bottom: 10px;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.section-icon {
|
| 133 |
+
font-size: 1.3em;
|
| 134 |
+
margin-right: 12px;
|
| 135 |
+
vertical-align: middle;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/* ์์ด์ฝ ์์ */
|
| 139 |
+
.analysis-section-block:nth-child(1) .section-icon { color: #3498db; }
|
| 140 |
+
.analysis-section-block:nth-child(2) .section-icon { color: #e74c3c; }
|
| 141 |
+
.analysis-section-block:nth-child(3) .section-icon { color: #f39c12; }
|
| 142 |
+
.analysis-section-block:nth-child(4) .section-icon { color: #9b59b6; }
|
| 143 |
+
.analysis-section-block:nth-child(5) .section-icon { color: var(--primary-color); }
|
| 144 |
+
.analysis-section-block:nth-child(6) .section-icon { color: #34495e; }
|
| 145 |
+
.analysis-section-block:nth-child(7) .section-icon { color: #e67e22; }
|
| 146 |
+
|
| 147 |
+
.subsection-title {
|
| 148 |
+
color: var(--text-color);
|
| 149 |
+
margin: 20px 0 10px 0;
|
| 150 |
+
font-size: 1.1em;
|
| 151 |
+
font-weight: 600;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.key-insight {
|
| 155 |
+
background-color: var(--insight-bg);
|
| 156 |
+
padding: 15px 20px;
|
| 157 |
+
border-left: 5px solid var(--primary-color);
|
| 158 |
+
margin: 20px 0;
|
| 159 |
+
border-radius: 5px;
|
| 160 |
+
font-weight: 500;
|
| 161 |
+
color: var(--text-color);
|
| 162 |
+
transition: background-color 0.3s ease;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
/* ํ
์คํธ ์คํ์ผ - ๊ธฐ๋ณธ์ ์ผ๋ฐ, ์ค์ํ ๋ถ๋ถ๋ง ๋ณผ๋ */
|
| 166 |
+
.analysis-content {
|
| 167 |
+
color: var(--text-color);
|
| 168 |
+
font-weight: normal;
|
| 169 |
+
line-height: 1.7;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.analysis-content strong {
|
| 173 |
+
color: var(--text-color);
|
| 174 |
+
font-weight: 600;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.analysis-content p {
|
| 178 |
+
margin-bottom: 15px;
|
| 179 |
+
font-weight: normal;
|
| 180 |
+
color: var(--text-color);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.analysis-list {
|
| 184 |
+
list-style: none;
|
| 185 |
+
padding: 0;
|
| 186 |
+
margin-bottom: 20px;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.analysis-list li {
|
| 190 |
+
position: relative;
|
| 191 |
+
padding-left: 25px;
|
| 192 |
+
margin-bottom: 12px;
|
| 193 |
+
line-height: 1.8;
|
| 194 |
+
font-weight: normal;
|
| 195 |
+
color: var(--text-color);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.analysis-list li:before {
|
| 199 |
+
content: 'โถ';
|
| 200 |
+
color: var(--primary-color);
|
| 201 |
+
position: absolute;
|
| 202 |
+
left: 0;
|
| 203 |
+
font-weight: bold;
|
| 204 |
+
font-size: 1.1em;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
.concern-list {
|
| 208 |
+
list-style: none;
|
| 209 |
+
padding: 0;
|
| 210 |
+
margin-bottom: 20px;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.concern-list li {
|
| 214 |
+
position: relative;
|
| 215 |
+
padding-left: 25px;
|
| 216 |
+
margin-bottom: 12px;
|
| 217 |
+
line-height: 1.8;
|
| 218 |
+
font-weight: normal;
|
| 219 |
+
color: var(--text-color);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
.concern-list li:before {
|
| 223 |
+
content: 'โ ๏ธ';
|
| 224 |
+
position: absolute;
|
| 225 |
+
left: 0;
|
| 226 |
+
font-size: 1.1em;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.solution-list {
|
| 230 |
+
list-style: none;
|
| 231 |
+
padding: 0;
|
| 232 |
+
margin-bottom: 20px;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
.solution-list li {
|
| 236 |
+
position: relative;
|
| 237 |
+
padding-left: 25px;
|
| 238 |
+
margin-bottom: 12px;
|
| 239 |
+
line-height: 1.8;
|
| 240 |
+
font-weight: normal;
|
| 241 |
+
color: var(--text-color);
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
.solution-list li:before {
|
| 245 |
+
content: 'โ
';
|
| 246 |
+
position: absolute;
|
| 247 |
+
left: 0;
|
| 248 |
+
font-size: 1.1em;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
.checklist {
|
| 252 |
+
background-color: var(--checklist-bg);
|
| 253 |
+
padding: 20px;
|
| 254 |
+
border-radius: 8px;
|
| 255 |
+
border-left: 5px solid #ffc107;
|
| 256 |
+
margin: 20px 0;
|
| 257 |
+
transition: background-color 0.3s ease;
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
.checklist-title {
|
| 261 |
+
font-weight: 700;
|
| 262 |
+
color: var(--warning-text);
|
| 263 |
+
margin-bottom: 15px;
|
| 264 |
+
font-size: 1.2em;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
.checklist-items {
|
| 268 |
+
list-style: none;
|
| 269 |
+
padding: 0;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
.checklist-items li {
|
| 273 |
+
position: relative;
|
| 274 |
+
padding-left: 25px;
|
| 275 |
+
margin-bottom: 10px;
|
| 276 |
+
line-height: 1.6;
|
| 277 |
+
font-weight: normal;
|
| 278 |
+
color: var(--warning-text);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
.checklist-items li:before {
|
| 282 |
+
content: '๐';
|
| 283 |
+
position: absolute;
|
| 284 |
+
left: 0;
|
| 285 |
+
font-size: 1.1em;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
.cross-sell-section {
|
| 289 |
+
background-color: var(--cross-sell-bg);
|
| 290 |
+
padding: 20px;
|
| 291 |
+
border-radius: 8px;
|
| 292 |
+
border-left: 5px solid var(--cross-sell-border);
|
| 293 |
+
margin: 20px 0;
|
| 294 |
+
transition: background-color 0.3s ease;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
.cross-sell-title {
|
| 298 |
+
font-weight: 700;
|
| 299 |
+
color: var(--cross-sell-text);
|
| 300 |
+
margin-bottom: 15px;
|
| 301 |
+
font-size: 1.2em;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
.product-suggestion {
|
| 305 |
+
background-color: var(--product-bg);
|
| 306 |
+
padding: 15px;
|
| 307 |
+
border-radius: 6px;
|
| 308 |
+
margin-bottom: 15px;
|
| 309 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 310 |
+
transition: background-color 0.3s ease;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
.product-name {
|
| 314 |
+
font-weight: 600;
|
| 315 |
+
color: var(--text-color);
|
| 316 |
+
margin-bottom: 8px;
|
| 317 |
+
font-size: 1.1em;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
.product-reason {
|
| 321 |
+
color: var(--text-color);
|
| 322 |
+
font-size: 0.95em;
|
| 323 |
+
line-height: 1.5;
|
| 324 |
+
font-weight: normal;
|
| 325 |
+
opacity: 0.8;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.final-recommendation {
|
| 329 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 330 |
+
border-radius: 12px;
|
| 331 |
+
color: white;
|
| 332 |
+
padding: 25px;
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
.final-recommendation h3 {
|
| 336 |
+
color: white;
|
| 337 |
+
font-size: 1.8em;
|
| 338 |
+
margin-bottom: 20px;
|
| 339 |
+
font-weight: 700;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
.recommendation-content {
|
| 343 |
+
font-size: 1.1em;
|
| 344 |
+
line-height: 1.7;
|
| 345 |
+
text-align: left;
|
| 346 |
+
font-weight: normal;
|
| 347 |
+
color: white;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
.recommendation-content strong {
|
| 351 |
+
font-weight: 600;
|
| 352 |
+
color: white;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
.trend-insight {
|
| 356 |
+
background-color: var(--trend-bg);
|
| 357 |
+
padding: 15px 20px;
|
| 358 |
+
border-left: 5px solid var(--trend-border);
|
| 359 |
+
margin: 20px 0;
|
| 360 |
+
border-radius: 5px;
|
| 361 |
+
color: var(--text-color);
|
| 362 |
+
transition: background-color 0.3s ease;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
.market-metrics {
|
| 366 |
+
display: grid;
|
| 367 |
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
| 368 |
+
gap: 20px;
|
| 369 |
+
margin: 20px 0;
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
.metric-card {
|
| 373 |
+
background-color: var(--metric-bg);
|
| 374 |
+
padding: 20px;
|
| 375 |
+
border-radius: 8px;
|
| 376 |
+
text-align: center;
|
| 377 |
+
border: 1px solid var(--metric-border);
|
| 378 |
+
transition: background-color 0.3s ease, border-color 0.3s ease;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
.metric-value {
|
| 382 |
+
font-size: 2em;
|
| 383 |
+
font-weight: 700;
|
| 384 |
+
color: var(--primary-color);
|
| 385 |
+
margin-bottom: 5px;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
.metric-label {
|
| 389 |
+
color: var(--text-color);
|
| 390 |
+
font-size: 0.9em;
|
| 391 |
+
font-weight: normal;
|
| 392 |
+
opacity: 0.8;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
.highlight-text {
|
| 396 |
+
background-color: var(--highlight-bg);
|
| 397 |
+
padding: 2px 6px;
|
| 398 |
+
border-radius: 3px;
|
| 399 |
+
font-weight: 600;
|
| 400 |
+
color: var(--warning-text);
|
| 401 |
+
transition: background-color 0.3s ease, color 0.3s ease;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
/* ๋คํฌ๋ชจ๋์์ ๊ทธ๋ผ๋ฐ์ด์
๋ฐฐ๊ฒฝ ์กฐ์ */
|
| 405 |
+
@media (prefers-color-scheme: dark) {
|
| 406 |
+
.final-recommendation {
|
| 407 |
+
background: linear-gradient(135deg, #4a5568 0%, #553c9a 100%);
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
[data-theme="dark"] .final-recommendation,
|
| 412 |
+
.dark .final-recommendation,
|
| 413 |
+
.gr-theme-dark .final-recommendation {
|
| 414 |
+
background: linear-gradient(135deg, #4a5568 0%, #553c9a 100%);
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
/* ์ ํ ์ ๋๋ฉ์ด์
*/
|
| 418 |
+
* {
|
| 419 |
+
transition: background-color 0.3s ease,
|
| 420 |
+
color 0.3s ease,
|
| 421 |
+
border-color 0.3s ease;
|
| 422 |
+
}
|
keyword_diversity_fix.py
ADDED
|
@@ -0,0 +1,918 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
logger.info("Gemini API ํธ์ถ ์์...")
|
| 4 |
+
# Gemini API ํธ์ถ - ์จ๋๋ฅผ ๋์ฌ์ ๋ ๋ค์ํ ๊ฒฐ๊ณผ ์์ฑ
|
| 5 |
+
response = client.models.generate_content(
|
| 6 |
+
model="gemini-2.0-flash",
|
| 7 |
+
contents=prompt,
|
| 8 |
+
config=GenerateContentConfig(
|
| 9 |
+
tools=config_tools, # ๊ฒ์ ์์ง์ ๋ฐ๋ผ ๋๊ตฌ ์ค์
|
| 10 |
+
response_modalities=["TEXT"],
|
| 11 |
+
temperature=0.95, # ์จ๋๋ฅผ ๋์ฌ์ ๋ ๋ค์ํ ๊ฒฐ๊ณผ
|
| 12 |
+
max_output_tokens=2000,
|
| 13 |
+
top_p=0.9, # ๋ ๋ค์ํ ํ ํฐ ์ ํ
|
| 14 |
+
top_k=40 # ํ๋ณด ํ ํฐ ์ ์ฆ๊ฐ
|
| 15 |
+
)
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
logger.info("Gemini API ์๋ต ์์ ์๋ฃ")
|
| 19 |
+
|
| 20 |
+
# ์๋ต์์ ํ
์คํธ ์ถ์ถ
|
| 21 |
+
result_text = ""
|
| 22 |
+
for part in response.candidates[0].content.parts:
|
| 23 |
+
if hasattr(part, 'text'):
|
| 24 |
+
result_text += part.text
|
| 25 |
+
|
| 26 |
+
# ๊ฒฐ๊ณผ ์ ์ - ๋ฒํธ, ์ค๋ช
, ๊ธฐํธ ์ ๊ฑฐ ๋ฐ ์ค๋ณต ์ ๊ฑฐ ๊ฐํ
|
| 27 |
+
lines = result_text.strip().split('\n')
|
| 28 |
+
clean_keywords = []
|
| 29 |
+
seen_keywords = set() # ์ค๋ณต ๋ฐฉ์ง๋ฅผ ์ํ ์งํฉ
|
| 30 |
+
|
| 31 |
+
for line in lines:
|
| 32 |
+
# ๋น ์ค ๊ฑด๋๋ฐ๊ธฐ
|
| 33 |
+
if not line.strip():
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
# ์ ์ ์์
|
| 37 |
+
clean_line = line.strip()
|
| 38 |
+
|
| 39 |
+
# ๋ฒํธ ์ ๊ฑฐ (1., 2., 3. ๋ฑ)
|
| 40 |
+
import re
|
| 41 |
+
clean_line = re.sub(r'^\d+\.?\s*', '', clean_line)
|
| 42 |
+
|
| 43 |
+
# ๋ถ๋ฆฟ ํฌ์ธํธ ์ ๊ฑฐ (-, *, โข, โ
, โ ๋ฑ)
|
| 44 |
+
clean_line = re.sub(r'^[-*โขโ
โ]\s*', '', clean_line)
|
| 45 |
+
|
| 46 |
+
# ๊ดํธ ์ ์ค๋ช
์ ๊ฑฐ
|
| 47 |
+
clean_line = re.sub(r'\([^)]*\)', '', clean_line)
|
| 48 |
+
|
| 49 |
+
# ์ถ๊ฐ ์ค๋ช
์ ๊ฑฐ (: ์ดํ ๋ด์ฉ)
|
| 50 |
+
if ':' in clean_line:
|
| 51 |
+
clean_line = clean_line.split(':')[0]
|
| 52 |
+
|
| 53 |
+
# ๊ณต๋ฐฑ ์ ๋ฆฌ
|
| 54 |
+
clean_line = clean_line.strip()
|
| 55 |
+
|
| 56 |
+
# ์ ํจํ ํค์๋๋ง ์ถ๊ฐ (2๊ธ์ ์ด์, 50๊ธ์ ์ดํ)
|
| 57 |
+
if clean_line and 2 <= len(clean_line) <= 50:
|
| 58 |
+
# ๋ธ๋๋๋ช
์ด๋ ์ ์กฐ์
์ฒด๋ช
ํํฐ๋ง
|
| 59 |
+
brand_keywords = ['์ผ์ฑ', '์์ง', 'LG', '์ ํ', '์์ดํฐ', '๊ฐค๋ญ์', '๋์ดํค', '์๋๋ค์ค', '์คํ๋ฒ
์ค']
|
| 60 |
+
if not any(brand in clean_line for brand in brand_keywords):
|
| 61 |
+
# ์ค๋ณต ๊ฒ์ฌ - ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ์ฒดํฌ
|
| 62 |
+
clean_lower = clean_line.lower()
|
| 63 |
+
if clean_lower not in seen_keywords:
|
| 64 |
+
seen_keywords.add(clean_lower)
|
| 65 |
+
clean_keywords.append(clean_line)
|
| 66 |
+
|
| 67 |
+
# 50๊ฐ๋ก ์ ํํ๋, ๋ถ์กฑํ๋ฉด ์ถ๊ฐ ์์ฑ ์์ฒญ
|
| 68 |
+
if len(clean_keywords) < 50:
|
| 69 |
+
logger.info(f"ํค์๋ ๋ถ์กฑ ({len(clean_keywords)}๊ฐ), ์ถ๊ฐ ์์ฑ ํ์")
|
| 70 |
+
|
| 71 |
+
# ๋ถ์กฑํ ๋งํผ ์ถ๊ฐ ์์ฑ์ ์ํ ๋ณด์กฐ ํ๋กฌํํธ
|
| 72 |
+
additional_prompt = f"""
|
| 73 |
+
๊ธฐ์กด์ ์์ฑ๋ ํค์๋: {', '.join(clean_keywords)}
|
| 74 |
+
|
| 75 |
+
์ ํค์๋๋ค๊ณผ ์ ๋ ์ค๋ณต๋์ง ์๋ ์์ ํ ์๋ก์ด {category} ๊ด๋ จ ์ผํํค์๋๋ฅผ {50 - len(clean_keywords)}๊ฐ ๋ ์์ฑํ์ธ์.
|
| 76 |
+
|
| 77 |
+
๋ฐ๋์ ์ง์ผ์ผ ํ ๊ท์น:
|
| 78 |
+
- ๊ธฐ์กด ํค์๋์ ์ ๋ ์ค๋ณต๋์ง ์์
|
| 79 |
+
- ์์ฌ, ํํ, ๊ธฐ๋ฅ์ ๋ค์ํ๊ฒ ์กฐํฉ
|
| 80 |
+
- ๋ธ๋๋๋ช
์ ๋ ๊ธ์ง
|
| 81 |
+
- ์์ ํค์๋๋ง ์ถ๋ ฅ (๋ฒํธ, ์ค๋ช
, ๊ธฐํธ ๊ธ์ง)
|
| 82 |
+
|
| 83 |
+
์์ ์ถ๋ ฅ:
|
| 84 |
+
์คํ
์ธ๋ฆฌ์ค ์๋ฐ
|
| 85 |
+
๊ณ ๋ฌด ๋งคํธ
|
| 86 |
+
์ ๋ฆฌ ์ฉ๊ธฐ
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# ์ถ๊ฐ ์์ฑ ์์ฒญ
|
| 90 |
+
additional_response = client.models.generate_content(
|
| 91 |
+
model="gemini-2.0-flash",
|
| 92 |
+
contents=additional_prompt,
|
| 93 |
+
config=GenerateContentConfig(
|
| 94 |
+
response_modalities=["TEXT"],
|
| 95 |
+
temperature=0.98, # ๋ ๋์ ์จ๋๋ก ๋ค์์ฑ ๋ณด์ฅ
|
| 96 |
+
max_output_tokens=1000,
|
| 97 |
+
top_p=0.95,
|
| 98 |
+
top_k=50
|
| 99 |
+
)
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# ์ถ๊ฐ ํค์๋ ์ฒ๋ฆฌ
|
| 103 |
+
additional_text = ""
|
| 104 |
+
for part in additional_response.candidates[0].content.parts:
|
| 105 |
+
if hasattr(part, 'text'):
|
| 106 |
+
additional_text += part.text
|
| 107 |
+
|
| 108 |
+
additional_lines = additional_text.strip().split('\n')
|
| 109 |
+
for line in additional_lines:
|
| 110 |
+
if not line.strip():
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
clean_line = line.strip()
|
| 114 |
+
clean_line = re.sub(r'^\d+\.?\s*', '', clean_line)
|
| 115 |
+
clean_line = re.sub(r'^[-*โขโ
โ]\s*', '', clean_line)
|
| 116 |
+
clean_line = re.sub(r'\([^)]*\)', '', clean_line)
|
| 117 |
+
|
| 118 |
+
if ':' in clean_line:
|
| 119 |
+
clean_line = clean_line.split(':')[0]
|
| 120 |
+
|
| 121 |
+
clean_line = clean_line.strip()
|
| 122 |
+
|
| 123 |
+
if clean_line and 2 <= len(clean_line) <= 50:
|
| 124 |
+
brand_keywords = ['์ผ์ฑ', '์์ง', 'LG', '์ ํ', '์์ดํฐ', '๊ฐค๋ญ์', '๋์ดํค', '์๋๋ค์ค', '์คํ๋ฒ
์ค']
|
| 125 |
+
if not any(brand in clean_line for brand in brand_keywords):
|
| 126 |
+
clean_lower = clean_line.lower()
|
| 127 |
+
if clean_lower not in seen_keywords and len(clean_keywords) < 50:
|
| 128 |
+
seen_keywords.add(clean_lower)
|
| 129 |
+
clean_keywords.append(clean_line)
|
| 130 |
+
|
| 131 |
+
# 50๊ฐ๋ก ์ ํ
|
| 132 |
+
clean_keywords = clean_keywords[:50]
|
| 133 |
+
|
| 134 |
+
# ์ต์ข
์
ํ๋ก ์์๋ ๋ฌด์์ํ
|
| 135 |
+
random.shuffle(clean_keywords)
|
| 136 |
+
|
| 137 |
+
# ์ต์ข
๊ฒฐ๊ณผ ๋ฌธ์์ด ์์ฑ
|
| 138 |
+
final_result = '\n'.join(clean_keywords)
|
| 139 |
+
|
| 140 |
+
logger.info(f"์ต์ข
์ ์ ๋ ํค์๋ ๊ฐ์: {len(clean_keywords)}๊ฐ")
|
| 141 |
+
logger.info(f"์ค๋ณต ์ ๊ฑฐ๋ ํค์๋ ์: {len(seen_keywords)}๊ฐ")
|
| 142 |
+
logger.info("=== ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์ฑ ์๋ฃ ===")
|
| 143 |
+
|
| 144 |
+
# ๊ทธ๋ผ์ด๋ฉ ๋ฉํ๋ฐ์ดํฐ ๋ก๊ทธ
|
| 145 |
+
if hasattr(response.candidates[0], 'grounding_metadata'):
|
| 146 |
+
logger.info("Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ ๋ฉํ๋ฐ์ดํฐ ํ์ธ๋จ")
|
| 147 |
+
if hasattr(response.candidates[0].grounding_metadata, 'web_search_queries'):
|
| 148 |
+
queries = response.candidates[0].grounding_metadata.web_search_queries
|
| 149 |
+
logger.info(f"์คํ๋ ๊ฒ์ ์ฟผ๋ฆฌ: {queries}")
|
| 150 |
+
|
| 151 |
+
return final_result
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
error_msg = f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 155 |
+
logger.error(error_msg)
|
| 156 |
+
logger.error("GEMINI_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ๋์๋์ง ํ์ธํด์ฃผ์ธ์.")
|
| 157 |
+
return f"{error_msg}\n\nGEMINI_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ๋์๋์ง ํ์ธํด์ฃผ์ธ์."
|
| 158 |
+
def generate_with_logs(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine):
|
| 159 |
+
"""ํค์๋ ์์ฑ๊ณผ ๋ก๊ทธ๋ฅผ ํจ๊ป ๋ฐํํ๋ ํจ์"""
|
| 160 |
+
logger.info("=== ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์ฑ ์์ ===")
|
| 161 |
+
|
| 162 |
+
# ํค์๋ ์์ฑ
|
| 163 |
+
result = generate_sourcing_keywords(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine)
|
| 164 |
+
|
| 165 |
+
# ์ต๊ทผ ๋ก๊ทธ ๊ฐ์ ธ์ค๊ธฐ
|
| 166 |
+
logs = get_recent_logs()
|
| 167 |
+
|
| 168 |
+
return result, logs
|
| 169 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 170 |
+
def create_interface():
|
| 171 |
+
with gr.Blocks(
|
| 172 |
+
title="๐ฏ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์คํ
",
|
| 173 |
+
theme=gr.themes.Soft(),
|
| 174 |
+
css="""
|
| 175 |
+
.gradio-container {
|
| 176 |
+
max-width: 1200px !important;
|
| 177 |
+
}
|
| 178 |
+
.title-header {
|
| 179 |
+
text-align: center;
|
| 180 |
+
background: linear-gradient(45deg, #FF6B6B, #4ECDC4, #45B7D1);
|
| 181 |
+
-webkit-background-clip: text;
|
| 182 |
+
-webkit-text-fill-color: transparent;
|
| 183 |
+
font-size: 2.5em;
|
| 184 |
+
font-weight: bold;
|
| 185 |
+
margin-bottom: 20px;
|
| 186 |
+
}
|
| 187 |
+
.subtitle {
|
| 188 |
+
text-align: center;
|
| 189 |
+
color: #666;
|
| 190 |
+
font-size: 1.2em;
|
| 191 |
+
margin-bottom: 30px;
|
| 192 |
+
}
|
| 193 |
+
"""
|
| 194 |
+
) as demo:
|
| 195 |
+
|
| 196 |
+
# ํค๋
|
| 197 |
+
gr.HTML("""
|
| 198 |
+
<div class="title-header">๐ฏ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์คํ
</div>
|
| 199 |
+
<div class="subtitle">๐ ๋งค๋ฒ ์์ ํ ๋ค๋ฅธ ๊ฒฐ๊ณผ! ์ค๋ณต ์๋ ์ผํํค์๋ ์ ๋ฌธ ๋ฐ๊ตด ํ๋ก๊ทธ๋จ</div>
|
| 200 |
+
""")
|
| 201 |
+
|
| 202 |
+
with gr.Row():
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
gr.Markdown("### ๐ ๋ค์์ฑ ๊ฐํ ์ค์ ")
|
| 205 |
+
|
| 206 |
+
# ๊ฒ์ ์์ง ์ ํ ์ถ๊ฐ
|
| 207 |
+
search_engine = gr.Dropdown(
|
| 208 |
+
choices=[
|
| 209 |
+
"๋ชจ๋ ๊ฒ์ ์์ง ํตํฉ ๋ถ์ (์ถ์ฒ)",
|
| 210 |
+
"Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ๋ง",
|
| 211 |
+
"๋ค์ด๋ฒ ๊ฒ์ API๋ง",
|
| 212 |
+
"DuckDuckGo ๊ฒ์๋ง",
|
| 213 |
+
"๊ฒ์ ์์ด AI๋ง ์ฌ์ฉ"
|
| 214 |
+
],
|
| 215 |
+
label="๐ ๊ฒ์ ์์ง ์ ํ",
|
| 216 |
+
value="๋ชจ๋ ๊ฒ์ ์์ง ํตํฉ ๋ถ์ (์ถ์ฒ)"
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# 1. ์ผํ ์นดํ
๊ณ ๋ฆฌ ์ ํ
|
| 220 |
+
category = gr.Dropdown(
|
| 221 |
+
choices=["๋๋ค์ ์ฉ", "ํจ์
์กํ", "์ํ/๊ฑด๊ฐ", "์ถ์ฐ/์ก์", "์คํฌ์ธ /๋ ์ ", "๋์งํธ/๊ฐ์ ", "๊ฐ๊ตฌ/์ธํ
๋ฆฌ์ด", "ํจ์
์๋ฅ", "ํ์ฅํ/๋ฏธ์ฉ"],
|
| 222 |
+
label="๐๏ธ ์ผํ ์นดํ
๊ณ ๋ฆฌ",
|
| 223 |
+
value="์ํ/๊ฑด๊ฐ"
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# 2. ์ถ๊ฐ ์์ฒญ์ฌํญ
|
| 227 |
+
additional_request = gr.Textbox(
|
| 228 |
+
label="๐ ์ถ๊ฐ ์์ฒญ์ฌํญ (๋ค์์ฑ ์ค์ฌ)",
|
| 229 |
+
placeholder="์: ๋ค์ํ ์์ฌ, ์๋ก์ด ํํ, ๋
ํนํ ๊ธฐ๋ฅ ๋ฑ",
|
| 230 |
+
lines=2
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# 3. ์ถ์ ํ์ด๋ฐ
|
| 234 |
+
launch_timing = gr.Radio(
|
| 235 |
+
choices=["๋๋ค์ ์ฉ", "์ฆ์์์ฑ", "๊ธฐํํ"],
|
| 236 |
+
label="โฐ ์ถ์ ๏ฟฝ๏ฟฝ์ด๋ฐ",
|
| 237 |
+
value="์ฆ์์์ฑ"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# 4. ๊ณ์ ์ฑ
|
| 241 |
+
seasonality = gr.Radio(
|
| 242 |
+
choices=["๋๋ค์ ์ฉ", "๋ด", "์ฌ๋ฆ", "๊ฐ์", "๊ฒจ์ธ", "๋น๊ณ์ "],
|
| 243 |
+
label="๐ฑ ๊ณ์ ์ฑ",
|
| 244 |
+
value="๋น๊ณ์ "
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
with gr.Column(scale=1):
|
| 248 |
+
gr.Markdown("### ๐ฐ ๋ชฉํ ์ค์ ")
|
| 249 |
+
|
| 250 |
+
# 5. ๋งค์ถ ๋ชฉํ
|
| 251 |
+
sales_target = gr.Radio(
|
| 252 |
+
choices=["๋๋ค์ ์ฉ", "100๋ง์ ์ดํ", "100-500๋ง์", "500-1์ฒ๋ง์", "1์ฒ-5์ฒ๋ง์", "5์ฒ๋ง์ ์ด์"],
|
| 253 |
+
label="๐ต ๋งค์ถ ๋ชฉํ",
|
| 254 |
+
value="100-500๋ง์"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# 6. ํ๋งค ์ฑ๋
|
| 258 |
+
sales_channel = gr.Radio(
|
| 259 |
+
choices=["๋๋ค์ ์ฉ", "์คํ๋ง์ผ", "SNS๋ง์ผํ
", "๊ด๊ณ ์งํ", "์คํ๋ผ์ธ"],
|
| 260 |
+
label="๐ฑ ํ๋งค ์ฑ๋",
|
| 261 |
+
value="์คํ๋ง์ผ"
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# 7. ๊ฒฝ์ ๊ฐ๋
|
| 265 |
+
competition_level = gr.Radio(
|
| 266 |
+
choices=[
|
| 267 |
+
"๋๋ค์ ์ฉ",
|
| 268 |
+
"์ด๋ณด",
|
| 269 |
+
"์ค์",
|
| 270 |
+
"๊ณ ์"
|
| 271 |
+
],
|
| 272 |
+
label="โ๏ธ ๊ฒฝ์ ๊ฐ๋",
|
| 273 |
+
value="์ด๋ณด"
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# ์คํ ๋ฒํผ
|
| 277 |
+
generate_btn = gr.Button(
|
| 278 |
+
"๐ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ๋ฐ๊ตด ์์ (๋งค๋ฒ ๋ค๋ฅธ ๊ฒฐ๊ณผ)",
|
| 279 |
+
variant="primary",
|
| 280 |
+
size="lg"
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 284 |
+
with gr.Row():
|
| 285 |
+
with gr.Column(scale=2):
|
| 286 |
+
gr.Markdown("### ๐ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ (50๊ฐ)")
|
| 287 |
+
output = gr.Textbox(
|
| 288 |
+
label="์ค๋ณต ์๋ ์ผํํค์๋ ๊ฒฐ๊ณผ (๋งค๋ฒ ์์ ํ ๋ค๋ฆ)",
|
| 289 |
+
lines=30,
|
| 290 |
+
max_lines=50,
|
| 291 |
+
placeholder="์ฌ๊ธฐ์ ๋งค๋ฒ ๋ค๋ฅธ 50๊ฐ์ ์ผํํค์๋๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค...",
|
| 292 |
+
show_copy_button=True
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
with gr.Column(scale=1):
|
| 296 |
+
gr.Markdown("### ๐ ์คํ ๋ก๊ทธ")
|
| 297 |
+
log_output = gr.Textbox(
|
| 298 |
+
label="์์คํ
๋ก๊ทธ",
|
| 299 |
+
lines=30,
|
| 300 |
+
max_lines=50,
|
| 301 |
+
placeholder="์์คํ
์คํ ๋ก๊ทธ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...",
|
| 302 |
+
show_copy_button=True
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 306 |
+
generate_btn.click(
|
| 307 |
+
fn=generate_with_logs,
|
| 308 |
+
inputs=[
|
| 309 |
+
category,
|
| 310 |
+
additional_request,
|
| 311 |
+
launch_timing,
|
| 312 |
+
seasonality,
|
| 313 |
+
sales_target,
|
| 314 |
+
sales_channel,
|
| 315 |
+
competition_level,
|
| 316 |
+
search_engine
|
| 317 |
+
],
|
| 318 |
+
outputs=[output, log_output],
|
| 319 |
+
show_progress=True
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
# ์ฌ์ฉ๋ฒ ์๋ด
|
| 323 |
+
with gr.Accordion("๐ ๋ค์์ฑ ๊ฐํ ์ฌ์ฉ๋ฒ ์๋ด", open=False):
|
| 324 |
+
gr.Markdown("""
|
| 325 |
+
### ๐ฏ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์คํ
์ฌ์ฉ๋ฒ
|
| 326 |
+
|
| 327 |
+
#### ๐ ์ฃผ์ ๊ฐ์ ์ฌํญ
|
| 328 |
+
- **์์ ํ ์ค๋ณต ๋ฐฉ์ง**: ๋งค๋ฒ ์คํํ ๋๋ง๋ค ์์ ํ ๋ค๋ฅธ ํค์๋ ์์ฑ
|
| 329 |
+
- **๋๋ค ์๋ ์์คํ
**: ํ์ฌ ์๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ํ ๋๋ค ์๋๋ก ์์ธก ๋ถ๊ฐ๋ฅ
|
| 330 |
+
- **๋ค์ํ ์กฐํฉ ๋ณด์ฅ**: ์์ฌรํํร๊ธฐ๋ฅ์ 3์ฐจ์ ์กฐํฉ์ผ๋ก ๋ฌดํ ๋ค์์ฑ
|
| 331 |
+
- **์ค๋ณต ๊ฒ์ฌ ๊ฐํ**: ๋์๋ฌธ์ ๊ตฌ๋ถ ์๋ ์๊ฒฉํ ์ค๋ณต ์ ๊ฑฐ
|
| 332 |
+
- **์จ๋ ์กฐ์ **: AI ์์ฑ ํ๋ผ๋ฏธํฐ ์ต์ ํ๋ก ์ฐฝ์์ฑ ๊ทน๋ํ
|
| 333 |
+
|
| 334 |
+
#### ๐ ๋ค์์ฑ ๋ณด์ฅ ๋ฉ์ปค๋์ฆ
|
| 335 |
+
1. **์๋ ๊ธฐ๋ฐ ๋๋คํ**: ๋ง์ดํฌ๋ก์ด ๋จ์ ์๊ฐ ๊ธฐ๋ฐ ๋๋ค ์๋
|
| 336 |
+
2. **3์ฐจ์ ์กฐํฉ ์์คํ
**:
|
| 337 |
+
- ์์ฌ: ์ค๋ฆฌ์ฝ, ์คํ
์ธ๋ฆฌ์ค, ์ธ๋ผ๋ฏน, ๋๋๋ฌด ๋ฑ 20์ข
|
| 338 |
+
- ํํ: ์ ์ด์, ์ํ, ์ฌ๋ฆผ, ํด๋์ฉ ๋ฑ 20์ข
|
| 339 |
+
- ๊ธฐ๋ฅ: ๋ฐฉ์, ํญ๊ท , ๋ง๊ทธ๋คํฑ, ๋ณด์จ ๋ฑ 20์ข
|
| 340 |
+
3. **์ค๋ณต ๋ฐฉ์ง ์๊ณ ๋ฆฌ์ฆ**: ์์ฑ ์ค ์ค์๊ฐ ์ค๋ณต ๊ฒ์ฌ
|
| 341 |
+
4. **์ถ๊ฐ ์์ฑ ์์คํ
**: ๋ถ์กฑ์ ์๋์ผ๋ก ์ถ๊ฐ ํค์๋ ์์ฑ
|
| 342 |
+
|
| 343 |
+
#### ๐ฒ ๋๋ค ์ ์ฉ์ ์งํ
|
| 344 |
+
- **ํค์๋๋ณ ๋
๋ฆฝ ์ ์ฉ**: ๊ฐ ํค์๋๋ง๋ค ๋ค๋ฅธ ์กฐ๊ฑด ์กฐํฉ
|
| 345 |
+
- **์์ธก ๋ถ๊ฐ๋ฅ์ฑ**: ๊ฐ์ ์ค์ ์ด๋ผ๋ ๋งค๋ฒ ๋ค๋ฅธ ๊ฒฐ๊ณผ
|
| 346 |
+
- **์กฐํฉ ํญ๋ฐ**: ์์ฒ ๊ฐ์ง ๊ฐ๋ฅํ ์กฐํฉ์ผ๋ก ๋ฌดํ ๋ค์์ฑ
|
| 347 |
+
|
| 348 |
+
#### ๐ ์์ฑ ํimport gradio as gr
|
| 349 |
+
import os
|
| 350 |
+
import logging
|
| 351 |
+
import sys
|
| 352 |
+
import random
|
| 353 |
+
import requests
|
| 354 |
+
import json
|
| 355 |
+
from datetime import datetime
|
| 356 |
+
from google import genai
|
| 357 |
+
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch
|
| 358 |
+
|
| 359 |
+
# ๋ก๊น
์ค์
|
| 360 |
+
logging.basicConfig(
|
| 361 |
+
level=logging.INFO,
|
| 362 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 363 |
+
handlers=[
|
| 364 |
+
logging.StreamHandler(sys.stdout),
|
| 365 |
+
logging.FileHandler('sourcing_app.log', encoding='utf-8')
|
| 366 |
+
]
|
| 367 |
+
)
|
| 368 |
+
logger = logging.getLogger(__name__)
|
| 369 |
+
|
| 370 |
+
# ํค์๋ ๋ค์์ฑ์ ์ํ ์๋ ํ ํ์ฅ
|
| 371 |
+
DIVERSE_SEED_POOLS = {
|
| 372 |
+
"ํจ์
์กํ": [
|
| 373 |
+
"์ก์ธ์๋ฆฌ", "์ฅ์ ๊ตฌ", "๊ฐ๋ฐฉ", "์ง๊ฐ", "๋ชจ์", "์ค์นดํ", "๋ฒจํธ", "์ ๊ธ๋ผ์ค", "ํค์ด์ก์ธ์๋ฆฌ", "์๊ณ์ค",
|
| 374 |
+
"ํค๋ง", "๋ธ๋ก์น", "๋ชฉ๊ฑธ์ด", "ํ์ฐ", "๋ฐ์ง", "๊ท๊ฑธ์ด", "ํธ๋ํฐ์ผ์ด์ค", "ํ์ฐ์น", "ํด๋ฌ์น", "ํ ํธ๋ฐฑ"
|
| 375 |
+
],
|
| 376 |
+
"์ํ/๊ฑด๊ฐ": [
|
| 377 |
+
"์ฃผ๋ฐฉ์ฉํ", "์์ค์ฉํ", "์ฒญ์์ฉํ", "์๋ฉ์ฉํ", "๊ฑด๊ฐ์ฉํ", "์๋ฃ์ฉํ", "๋ง์ฌ์ง์ฉํ", "์ด๋์ฉํ",
|
| 378 |
+
"๋ค์ด์ดํธ์ฉํ", "ํ์ฅ์ง", "์ธ์ ", "์ดํธ", "์น์ฝ", "๋น๋", "์๊ฑด", "๋ฒ ๊ฐ", "์ด๋ถ", "์ฟ ์
", "๋งคํธ"
|
| 379 |
+
],
|
| 380 |
+
"์ถ์ฐ/์ก์": [
|
| 381 |
+
"์ ์์ฉํ", "์ก์์ฉํ", "์ถ์ฐ์ฉํ", "์์ฐ๋ถ์ฉํ", "์ ์์์ฉํ", "์ด์ ์์ฉํ", "๊ธฐ์ ๊ท", "์ ๋ณ",
|
| 382 |
+
"์ ๋ชจ์ฐจ", "์นด์ํธ", "์๊ธฐ์ท", "์ฅ๋๊ฐ", "๊ต์ก์ฉํ", "์ฑ
", "๊ทธ๋ฆผ์ฑ
", "ํผ์ฆ", "๋ธ๋ก", "์ธํ", "๋์ด๋งคํธ"
|
| 383 |
+
],
|
| 384 |
+
"์คํฌ์ธ /๋ ์ ": [
|
| 385 |
+
"์ด๋์ฉํ", "ํฌ์ค์ฉํ", "์๊ฐ์ฉํ", "์์์ฉํ", "๋ฑ์ฐ์ฉํ", "์บ ํ์ฉํ", "๋์์ฉํ", "๊ณจํ์ฉํ",
|
| 386 |
+
"์ถ๊ตฌ์ฉํ", "๋๊ตฌ์ฉํ", "๋ฐฐ๋๋ฏผํด์ฉํ", "ํ๊ตฌ์ฉํ", "ํ
๋์ค์ฉํ", "์์ ๊ฑฐ์ฉํ", "์ค์ผ์ดํธ๋ณด๋์ฉํ"
|
| 387 |
+
],
|
| 388 |
+
"๋์งํธ/๊ฐ์ ": [
|
| 389 |
+
"์ค๋งํธํฐ์ก์ธ์๋ฆฌ", "์ปดํจํฐ์ฉํ", "ํ๋ธ๋ฆฟ์ฉํ", "์ด์ดํฐ", "์คํผ์ปค", "์ถฉ์ ๊ธฐ", "์ผ์ด๋ธ", "๋ง์ฐ์คํจ๋",
|
| 390 |
+
"ํค๋ณด๋", "๋ง์ฐ์ค", "์น์บ ", "๋ง์ดํฌ", "ํค๋์
", "๊ฒ์ํจ๋", "USB", "๋ฉ๋ชจ๋ฆฌ์นด๋", "ํ์๋ฑ
ํฌ"
|
| 391 |
+
],
|
| 392 |
+
"๊ฐ๊ตฌ/์ธํ
๋ฆฌ์ด": [
|
| 393 |
+
"์๋ฉ๊ฐ๊ตฌ", "์นจ์ค๊ฐ๊ตฌ", "๊ฑฐ์ค๊ฐ๊ตฌ", "์ฃผ๋ฐฉ๊ฐ๊ตฌ", "์์ค๊ฐ๊ตฌ", "์ฌ๋ฌด์ฉ๊ฐ๊ตฌ", "์ธํ
๋ฆฌ์ด์ํ", "์กฐ๋ช
",
|
| 394 |
+
"์ปคํผ", "๋ธ๋ผ์ธ๋", "์นดํซ", "๋ฌ๊ทธ", "์ก์", "๊ฑฐ์ธ", "์๊ณ", "ํ๋ถ", "๊ฝ๋ณ", "์บ๋ค", "๋ฐฉํฅ์ "
|
| 395 |
+
],
|
| 396 |
+
"ํจ์
์๋ฅ": [
|
| 397 |
+
"ํฐ์
์ธ ", "์
์ธ ", "๋ธ๋ผ์ฐ์ค", "์ํผ์ค", "์ค์ปคํธ", "๋ฐ์ง", "์ฒญ๋ฐ์ง", "๋ ๊น
์ค", "์์ผ", "์ฝํธ",
|
| 398 |
+
"์ ํผ", "๊ฐ๋๊ฑด", "๋ํธ", "ํ๋", "์กฐ๋ผ", "์์ท", "์ ์ท", "์๋ง", "์คํํน", "์ด๋๋ณต"
|
| 399 |
+
],
|
| 400 |
+
"ํ์ฅํ/๋ฏธ์ฉ": [
|
| 401 |
+
"์คํจ์ผ์ด", "๋ฉ์ดํฌ์
", "ํด๋ ์ง", "๋ง์คํฌํฉ", "์ ํฌ๋ฆผ", "๋ก์
", "์์ผ์ค", "ํฌ๋ฆผ", "๋ฆฝ๋ฐค",
|
| 402 |
+
"๋ฆฝ์คํฑ", "์์ด์๋", "๋ง์ค์นด๋ผ", "ํ์ด๋ฐ์ด์
", "์ปจ์ค๋ฌ", "๋ธ๋ฌ์
", "ํ์ด๋ผ์ดํฐ", "๋ค์ผ", "ํฅ์"
|
| 403 |
+
]
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
# ์์ฌ๋ณ ํค์๋ ํ
|
| 407 |
+
MATERIAL_KEYWORDS = [
|
| 408 |
+
"์ค๋ฆฌ์ฝ", "์คํ
์ธ๋ฆฌ์ค", "์ธ๋ผ๋ฏน", "์ ๋ฆฌ", "๋๋ฌด", "๋๋๋ฌด", "๋ฉด", "๋ฆฐ๋จ", "ํด๋ฆฌ์์คํฐ", "๋์ผ๋ก ",
|
| 409 |
+
"๊ณ ๋ฌด", "ํ๋ผ์คํฑ", "์ข
์ด", "๊ฐ์ฃฝ", "์ธ์กฐ๊ฐ์ฃฝ", "๋ฉํ", "์๋ฃจ๋ฏธ๋", "์ฒ ", "๊ตฌ๋ฆฌ", "ํฉ๋"
|
| 410 |
+
]
|
| 411 |
+
|
| 412 |
+
# ํํ๋ณ ํค์๋ ํ
|
| 413 |
+
SHAPE_KEYWORDS = [
|
| 414 |
+
"์ ์ด์", "ํด๋์ฉ", "๋ฏธ๋", "๋ํ", "์ํ", "์ฌ๊ฐ", "ํ์", "์ง์ฌ๊ฐ", "์ผ๊ฐ", "์ก๊ฐ",
|
| 415 |
+
"์ฌ๋ฆผ", "๋๊บผ์ด", "์์", "๊ธด", "์งง์", "๋์", "์ข์", "๊น์", "์์", "๊ณก์ "
|
| 416 |
+
]
|
| 417 |
+
|
| 418 |
+
# ๊ธฐ๋ฅ๋ณ ํค์๋ ํ
|
| 419 |
+
FUNCTION_KEYWORDS = [
|
| 420 |
+
"๋ฐฉ์", "๋ฏธ๋๋ผ๋ฐฉ์ง", "ํญ๊ท ", "๋์์ ๊ฑฐ", "๋ณด์จ", "๋ณด๋", "์๊ฑด", "ํก์", "์ฐจ๋จ", "๋ณดํธ",
|
| 421 |
+
"๋ง๊ทธ๋คํฑ", "์์", "๋์ ", "ํฌ๋ช
", "๋ถํฌ๋ช
", "๋ฐ๊ด", "๋ฐ์ฌ", "์ ์ถ", "ํ๋ ฅ", "๊ณ ์ "
|
| 422 |
+
]
|
| 423 |
+
|
| 424 |
+
# Gemini API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 425 |
+
def initialize_gemini():
|
| 426 |
+
logger.info("Gemini API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์์")
|
| 427 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 428 |
+
if not api_key:
|
| 429 |
+
logger.error("GEMINI_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 430 |
+
raise ValueError("GEMINI_API_KEY ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 431 |
+
|
| 432 |
+
client = genai.Client(api_key=api_key)
|
| 433 |
+
logger.info("Gemini API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ")
|
| 434 |
+
return client
|
| 435 |
+
|
| 436 |
+
def get_recent_logs():
|
| 437 |
+
"""์ต๊ทผ ๋ก๊ทธ๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์"""
|
| 438 |
+
try:
|
| 439 |
+
with open('sourcing_app.log', 'r', encoding='utf-8') as f:
|
| 440 |
+
lines = f.readlines()
|
| 441 |
+
# ์ต๊ทผ 50์ค๋ง ๋ฐํ
|
| 442 |
+
return ''.join(lines[-50:])
|
| 443 |
+
except FileNotFoundError:
|
| 444 |
+
return "๋ก๊ทธ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 445 |
+
except Exception as e:
|
| 446 |
+
return f"๋ก๊ทธ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}"
|
| 447 |
+
|
| 448 |
+
def generate_diverse_keyword_combinations(category, count=60):
|
| 449 |
+
"""๋ค์ํ ํค์๋ ์กฐํฉ์ ์์ฑํ๋ ํจ์"""
|
| 450 |
+
logger.info(f"๋ค์ํ ํค์๋ ์กฐํฉ ์์ฑ ์์: {category}, {count}๊ฐ")
|
| 451 |
+
|
| 452 |
+
combinations = []
|
| 453 |
+
category_pool = DIVERSE_SEED_POOLS.get(category, DIVERSE_SEED_POOLS["์ํ/๊ฑด๊ฐ"])
|
| 454 |
+
|
| 455 |
+
# 1. ๋จ์ผ ํค์๋ (20%)
|
| 456 |
+
single_keywords = random.sample(category_pool, min(12, len(category_pool)))
|
| 457 |
+
combinations.extend(single_keywords)
|
| 458 |
+
|
| 459 |
+
# 2. ์์ฌ + ์นดํ
๊ณ ๋ฆฌ (30%)
|
| 460 |
+
for _ in range(18):
|
| 461 |
+
material = random.choice(MATERIAL_KEYWORDS)
|
| 462 |
+
item = random.choice(category_pool)
|
| 463 |
+
combinations.append(f"{material} {item}")
|
| 464 |
+
|
| 465 |
+
# 3. ํํ + ์นดํ
๊ณ ๋ฆฌ (30%)
|
| 466 |
+
for _ in range(18):
|
| 467 |
+
shape = random.choice(SHAPE_KEYWORDS)
|
| 468 |
+
item = random.choice(category_pool)
|
| 469 |
+
combinations.append(f"{shape} {item}")
|
| 470 |
+
|
| 471 |
+
# 4. ๊ธฐ๋ฅ + ์นดํ
๊ณ ๋ฆฌ (20%)
|
| 472 |
+
for _ in range(12):
|
| 473 |
+
function = random.choice(FUNCTION_KEYWORDS)
|
| 474 |
+
item = random.choice(category_pool)
|
| 475 |
+
combinations.append(f"{function} {item}")
|
| 476 |
+
|
| 477 |
+
# ์ค๋ณต ์ ๊ฑฐ ๋ฐ ์
ํ
|
| 478 |
+
combinations = list(set(combinations))
|
| 479 |
+
random.shuffle(combinations)
|
| 480 |
+
|
| 481 |
+
logger.info(f"์์ฑ๋ ์กฐํฉ ์: {len(combinations)}๊ฐ")
|
| 482 |
+
return combinations[:count]
|
| 483 |
+
|
| 484 |
+
def search_all_engines(query):
|
| 485 |
+
"""๋ชจ๋ ๊ฒ์ ์์ง์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ทจํฉํ๋ ํจ์"""
|
| 486 |
+
logger.info(f"๋ชจ๋ ๊ฒ์ ์์ง์ผ๋ก ๊ฒ์ ์์: {query}")
|
| 487 |
+
|
| 488 |
+
all_results = {
|
| 489 |
+
"google": "",
|
| 490 |
+
"naver": "",
|
| 491 |
+
"duckduckgo": ""
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
# 1. ๋ค์ด๋ฒ ๊ฒ์ API
|
| 495 |
+
try:
|
| 496 |
+
naver_client_id = os.getenv("NAVER_CLIENT_ID")
|
| 497 |
+
naver_client_secret = os.getenv("NAVER_CLIENT_SECRET")
|
| 498 |
+
|
| 499 |
+
if naver_client_id and naver_client_secret:
|
| 500 |
+
url = "https://openapi.naver.com/v1/search/shop.json"
|
| 501 |
+
headers = {
|
| 502 |
+
"X-Naver-Client-Id": naver_client_id,
|
| 503 |
+
"X-Naver-Client-Secret": naver_client_secret
|
| 504 |
+
}
|
| 505 |
+
params = {"query": query, "display": 10}
|
| 506 |
+
|
| 507 |
+
response = requests.get(url, headers=headers, params=params, timeout=10)
|
| 508 |
+
if response.status_code == 200:
|
| 509 |
+
data = response.json()
|
| 510 |
+
naver_data = []
|
| 511 |
+
for item in data.get('items', [])[:5]:
|
| 512 |
+
naver_data.append(f"์ํ: {item.get('title', '').replace('<b>', '').replace('</b>', '')}")
|
| 513 |
+
naver_data.append(f"๊ฐ๊ฒฉ: {item.get('lprice', '')}์")
|
| 514 |
+
naver_data.append(f"์นดํ
๊ณ ๋ฆฌ: {item.get('category1', '')}")
|
| 515 |
+
all_results["naver"] = "\n".join(naver_data)
|
| 516 |
+
logger.info("๋ค์ด๋ฒ ๊ฒ์ ์๋ฃ")
|
| 517 |
+
else:
|
| 518 |
+
all_results["naver"] = "๋ค์ด๋ฒ API ๊ฒ์ ์คํจ"
|
| 519 |
+
else:
|
| 520 |
+
all_results["naver"] = "๋ค์ด๋ฒ API ํค๊ฐ ์ค์ ๋์ง ์์"
|
| 521 |
+
except Exception as e:
|
| 522 |
+
all_results["naver"] = f"๋ค์ด๋ฒ ๊ฒ์ ์ค๋ฅ: {str(e)}"
|
| 523 |
+
|
| 524 |
+
# 2. DuckDuckGo ๊ฒ์
|
| 525 |
+
try:
|
| 526 |
+
url = "https://api.duckduckgo.com/"
|
| 527 |
+
params = {
|
| 528 |
+
"q": query,
|
| 529 |
+
"format": "json",
|
| 530 |
+
"no_html": "1",
|
| 531 |
+
"skip_disambig": "1"
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
response = requests.get(url, params=params, timeout=10)
|
| 535 |
+
if response.status_code == 200:
|
| 536 |
+
data = response.json()
|
| 537 |
+
|
| 538 |
+
ddg_data = []
|
| 539 |
+
# Abstract ์ ๋ณด
|
| 540 |
+
if data.get('Abstract'):
|
| 541 |
+
ddg_data.append(f"์์ฝ: {data['Abstract']}")
|
| 542 |
+
|
| 543 |
+
# Related Topics
|
| 544 |
+
for topic in data.get('RelatedTopics', [])[:3]:
|
| 545 |
+
if isinstance(topic, dict) and topic.get('Text'):
|
| 546 |
+
ddg_data.append(f"๊ด๋ จ์ ๋ณด: {topic['Text']}")
|
| 547 |
+
|
| 548 |
+
all_results["duckduckgo"] = "\n".join(ddg_data) if ddg_data else "DuckDuckGo์์ ๊ด๋ จ ์ ๋ณด ์์"
|
| 549 |
+
logger.info("DuckDuckGo ๊ฒ์ ์๋ฃ")
|
| 550 |
+
else:
|
| 551 |
+
all_results["duckduckgo"] = "DuckDuckGo ๊ฒ์ ์คํจ"
|
| 552 |
+
except Exception as e:
|
| 553 |
+
all_results["duckduckgo"] = f"DuckDuckGo ๊ฒ์ ์ค๋ฅ: {str(e)}"
|
| 554 |
+
|
| 555 |
+
# 3. Google ๊ฒ์์ Gemini์์ ์๋ ์ฒ๋ฆฌ๋จ
|
| 556 |
+
all_results["google"] = "Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ ์๋ ์คํ"
|
| 557 |
+
|
| 558 |
+
logger.info("๋ชจ๋ ๊ฒ์ ์์ง ๋ฐ์ดํฐ ์์ง ์๋ฃ")
|
| 559 |
+
return all_results
|
| 560 |
+
|
| 561 |
+
def comprehensive_market_analysis(category, seasonality, sales_target):
|
| 562 |
+
"""๋ค์์ฑ ๊ฐํ๋ ์์ฅ ๋ถ์"""
|
| 563 |
+
logger.info("๋ค์์ฑ ๊ฐํ ์์ฅ ๋ถ์ ์์")
|
| 564 |
+
|
| 565 |
+
# ๋๋ค ์๋๋ฅผ ํ์ฌ ์๊ฐ์ผ๋ก ์ค์ ํ์ฌ ๋งค๋ฒ ๋ค๋ฅธ ๊ฒฐ๊ณผ ๋ณด์ฅ
|
| 566 |
+
random.seed(datetime.now().microsecond)
|
| 567 |
+
|
| 568 |
+
# ๋ค์ํ ๊ฒ์ ๊ฐ๋๋ก ์ฟผ๋ฆฌ ์์ฑ
|
| 569 |
+
search_angles = [
|
| 570 |
+
"ํ์์ํ", "์ ์ํ", "์ธ๊ธฐ์ํ", "์ ๊ฐ์ํ", "๊ณ ๊ธ์ํ", "ํ ์ธ์ํ",
|
| 571 |
+
"๊ฐํธ์ํ", "์ค์ฉ์ํ", "ํธ๋ ๋์ํ", "์จ์์ํ", "๋ฒ ์คํธ์ํ", "์ถ์ฒ์ํ"
|
| 572 |
+
]
|
| 573 |
+
|
| 574 |
+
search_queries = []
|
| 575 |
+
|
| 576 |
+
# ์นดํ
๊ณ ๋ฆฌ๋ณ ๋ค์ํ ๊ฒ์์ด ์์ฑ
|
| 577 |
+
category_pool = DIVERSE_SEED_POOLS.get(category, DIVERSE_SEED_POOLS["์ํ/๊ฑด๊ฐ"])
|
| 578 |
+
|
| 579 |
+
for angle in search_angles:
|
| 580 |
+
for item in random.sample(category_pool, 3): # ๊ฐ ์นดํ
๊ณ ๋ฆฌ์์ 3๊ฐ์ฉ๋ง ์ ํ
|
| 581 |
+
search_queries.append(f"{item} {angle}")
|
| 582 |
+
|
| 583 |
+
# ์์ฌ๋ณ ๊ฒ์์ด ์ถ๊ฐ
|
| 584 |
+
for material in random.sample(MATERIAL_KEYWORDS, 5):
|
| 585 |
+
search_queries.append(f"{material} {category} ์ํ")
|
| 586 |
+
|
| 587 |
+
# ํํ๋ณ ๊ฒ์์ด ์ถ๊ฐ
|
| 588 |
+
for shape in random.sample(SHAPE_KEYWORDS, 5):
|
| 589 |
+
search_queries.append(f"{shape} {category} ์์ดํ
")
|
| 590 |
+
|
| 591 |
+
# ๊ฒ์์ด ์
ํํ์ฌ ์์ธก ๋ถ๊ฐ๋ฅํ๊ฒ ๋ง๋ค๊ธฐ
|
| 592 |
+
random.shuffle(search_queries)
|
| 593 |
+
search_queries = search_queries[:15] # 15๊ฐ๋ก ์ ํ
|
| 594 |
+
|
| 595 |
+
comprehensive_data = {}
|
| 596 |
+
|
| 597 |
+
for i, query in enumerate(search_queries):
|
| 598 |
+
logger.info(f"๋ค์์ฑ ๊ฒ์ {i+1}/15: {query}")
|
| 599 |
+
comprehensive_data[f"query_{i+1}"] = search_all_engines(query)
|
| 600 |
+
|
| 601 |
+
# API ๊ณผ๋ถํ ๋ฐฉ์ง๋ฅผ ์ํ ๋๋ ์ด
|
| 602 |
+
import time
|
| 603 |
+
time.sleep(0.5)
|
| 604 |
+
|
| 605 |
+
# ๋ค์์ฑ ๊ฐํ ๋ฐ์ดํฐ ์์ฝ
|
| 606 |
+
summary = "=== ๋ค์์ฑ ๊ฐํ ์์ฅ ๋ถ์ ๊ฒฐ๊ณผ ===\n\n"
|
| 607 |
+
|
| 608 |
+
# ๋ฌด์์๋ก ๊ฒฐ๊ณผ๋ฅผ ์์ด์ ํจํด ๋ฐฉ์ง
|
| 609 |
+
result_keys = list(comprehensive_data.keys())
|
| 610 |
+
random.shuffle(result_keys)
|
| 611 |
+
|
| 612 |
+
summary += "๐ ๋ค์ํ ์์ฅ ๊ฒ์ ๊ฒฐ๊ณผ:\n"
|
| 613 |
+
for key in result_keys[:10]:
|
| 614 |
+
results = comprehensive_data.get(key, {})
|
| 615 |
+
if results.get("naver"):
|
| 616 |
+
summary += f"โข {results['naver'][:60]}...\n"
|
| 617 |
+
summary += "\n"
|
| 618 |
+
|
| 619 |
+
logger.info("๋ค์์ฑ ๊ฐํ ๋ถ์ ์๋ฃ")
|
| 620 |
+
return summary
|
| 621 |
+
|
| 622 |
+
def search_with_api(query, search_engine="Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ๋ง"):
|
| 623 |
+
"""๊ฐ๋ณ ๊ฒ์ ์์ง์ผ๋ก ๊ฒ์ํ๋ ํจ์ (๋จ์ผ ์์ง ์ ํ์ ์ฌ์ฉ)"""
|
| 624 |
+
logger.info(f"๊ฒ์ ์์ง: {search_engine}, ์ฟผ๋ฆฌ: {query}")
|
| 625 |
+
|
| 626 |
+
search_results = ""
|
| 627 |
+
|
| 628 |
+
try:
|
| 629 |
+
if search_engine == "๋ค์ด๋ฒ ๊ฒ์ API๋ง":
|
| 630 |
+
# ๋ค์ด๋ฒ ๊ฒ์ API ์ฌ์ฉ
|
| 631 |
+
naver_client_id = os.getenv("NAVER_CLIENT_ID")
|
| 632 |
+
naver_client_secret = os.getenv("NAVER_CLIENT_SECRET")
|
| 633 |
+
|
| 634 |
+
if naver_client_id and naver_client_secret:
|
| 635 |
+
url = "https://openapi.naver.com/v1/search/shop.json"
|
| 636 |
+
headers = {
|
| 637 |
+
"X-Naver-Client-Id": naver_client_id,
|
| 638 |
+
"X-Naver-Client-Secret": naver_client_secret
|
| 639 |
+
}
|
| 640 |
+
params = {"query": query, "display": 10}
|
| 641 |
+
|
| 642 |
+
response = requests.get(url, headers=headers, params=params)
|
| 643 |
+
if response.status_code == 200:
|
| 644 |
+
data = response.json()
|
| 645 |
+
for item in data.get('items', [])[:5]:
|
| 646 |
+
search_results += f"์ํ๋ช
: {item.get('title', '')}\n"
|
| 647 |
+
search_results += f"๊ฐ๊ฒฉ: {item.get('lprice', '')}์\n"
|
| 648 |
+
search_results += f"์นดํ
๊ณ ๋ฆฌ: {item.get('category1', '')}\n\n"
|
| 649 |
+
else:
|
| 650 |
+
search_results = "๋ค์ด๋ฒ API ๊ฒ์ ์คํจ"
|
| 651 |
+
else:
|
| 652 |
+
search_results = "๋ค์ด๋ฒ API ํค๊ฐ ์ค์ ๋์ง ์์"
|
| 653 |
+
|
| 654 |
+
elif search_engine == "DuckDuckGo ๊ฒ์๋ง":
|
| 655 |
+
# DuckDuckGo ๊ฒ์ (๋ฌด๋ฃ, API ํค ๋ถํ์)
|
| 656 |
+
try:
|
| 657 |
+
url = "https://api.duckduckgo.com/"
|
| 658 |
+
params = {
|
| 659 |
+
"q": query,
|
| 660 |
+
"format": "json",
|
| 661 |
+
"no_html": "1",
|
| 662 |
+
"skip_disambig": "1"
|
| 663 |
+
}
|
| 664 |
+
|
| 665 |
+
response = requests.get(url, params=params, timeout=10)
|
| 666 |
+
if response.status_code == 200:
|
| 667 |
+
data = response.json()
|
| 668 |
+
|
| 669 |
+
# Abstract ์ ๋ณด
|
| 670 |
+
if data.get('Abstract'):
|
| 671 |
+
search_results += f"์์ฝ: {data['Abstract']}\n\n"
|
| 672 |
+
|
| 673 |
+
# Related Topics
|
| 674 |
+
for topic in data.get('RelatedTopics', [])[:5]:
|
| 675 |
+
if isinstance(topic, dict) and topic.get('Text'):
|
| 676 |
+
search_results += f"๊ด๋ จ ์ ๋ณด: {topic['Text']}\n"
|
| 677 |
+
|
| 678 |
+
if not search_results:
|
| 679 |
+
search_results = "DuckDuckGo์์ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ฐพ์ง ๋ชปํจ"
|
| 680 |
+
else:
|
| 681 |
+
search_results = "DuckDuckGo ๊ฒ์ ์คํจ"
|
| 682 |
+
except Exception as e:
|
| 683 |
+
search_results = f"DuckDuckGo ๊ฒ์ ์ค๋ฅ: {str(e)}"
|
| 684 |
+
|
| 685 |
+
elif search_engine == "๊ฒ์ ์์ด AI๋ง ์ฌ์ฉ":
|
| 686 |
+
search_results = "๊ฒ์ ์์ด AI ์ง์๋ง ์ฌ์ฉํ์ฌ ํค์๋ ์์ฑ"
|
| 687 |
+
|
| 688 |
+
else:
|
| 689 |
+
# Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ (๊ธฐ๋ณธ)
|
| 690 |
+
search_results = "Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ ์ฌ์ฉ"
|
| 691 |
+
|
| 692 |
+
except Exception as e:
|
| 693 |
+
logger.error(f"๊ฒ์ ์ค๋ฅ: {str(e)}")
|
| 694 |
+
search_results = f"๊ฒ์ ์ค๋ฅ: {str(e)}"
|
| 695 |
+
|
| 696 |
+
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ ๊ธธ์ด: {len(search_results)} ๋ฌธ์")
|
| 697 |
+
return search_results
|
| 698 |
+
|
| 699 |
+
def apply_random_selection_for_keywords(category, launch_timing, seasonality, sales_target, sales_channel, competition_level):
|
| 700 |
+
"""๊ฐ ํค์๋๋ง๋ค ๋๋คํ๊ฒ ์กฐ๊ฑด์ ์ ์ฉํ๊ธฐ ์ํ ์ค์ ๋ฌธ์์ด ์์ฑ"""
|
| 701 |
+
|
| 702 |
+
# ๊ฐ ํญ๋ชฉ๋ณ ์ ํ์ง ์ ์
|
| 703 |
+
categories = ["ํจ์
์กํ", "์ํ/๊ฑด๊ฐ", "์ถ์ฐ/์ก์", "์คํฌ์ธ /๋ ์ ", "๋์งํธ/๊ฐ์ ", "๊ฐ๊ตฌ/์ธํ
๋ฆฌ์ด", "ํจ์
์๋ฅ", "ํ์ฅํ/๋ฏธ์ฉ"]
|
| 704 |
+
launch_timings = ["์ฆ์์์ฑ", "๊ธฐํํ"]
|
| 705 |
+
seasonalities = ["๋ด", "์ฌ๋ฆ", "๊ฐ์", "๊ฒจ์ธ", "๋น๊ณ์ "]
|
| 706 |
+
sales_targets = ["100๋ง์ ์ดํ", "100-500๋ง์", "500-1์ฒ๋ง์", "1์ฒ-5์ฒ๋ง์", "5์ฒ๋ง์ ์ด์"]
|
| 707 |
+
sales_channels = ["์คํ๋ง์ผ", "SNS๋ง์ผํ
", "๊ด๊ณ ์งํ", "์คํ๋ผ์ธ"]
|
| 708 |
+
competition_levels = ["์ด๋ณด", "์ค์", "๊ณ ์"]
|
| 709 |
+
|
| 710 |
+
# ๋๋ค์ ์ฉ ์ค์ ์ ๋ณด ์์ฑ
|
| 711 |
+
random_settings = {
|
| 712 |
+
'category_random': category == "๋๋ค์ ์ฉ",
|
| 713 |
+
'launch_timing_random': launch_timing == "๋๋ค์ ์ฉ",
|
| 714 |
+
'seasonality_random': seasonality == "๋๋ค์ ์ฉ",
|
| 715 |
+
'sales_target_random': sales_target == "๋๋ค์ ์ฉ",
|
| 716 |
+
'sales_channel_random': sales_channel == "๋๋ค์ ์ฉ",
|
| 717 |
+
'competition_level_random': competition_level == "๋๋ค์ ์ฉ",
|
| 718 |
+
'categories': categories,
|
| 719 |
+
'launch_timings': launch_timings,
|
| 720 |
+
'seasonalities': seasonalities,
|
| 721 |
+
'sales_targets': sales_targets,
|
| 722 |
+
'sales_channels': sales_channels,
|
| 723 |
+
'competition_levels': competition_levels
|
| 724 |
+
}
|
| 725 |
+
|
| 726 |
+
# ๊ณ ์ ๊ฐ๋ค
|
| 727 |
+
fixed_values = {
|
| 728 |
+
'category': category if category != "๋๋ค์ ์ฉ" else None,
|
| 729 |
+
'launch_timing': launch_timing if launch_timing != "๋๋ค์ ์ฉ" else None,
|
| 730 |
+
'seasonality': seasonality if seasonality != "๋๋ค์ ์ฉ" else None,
|
| 731 |
+
'sales_target': sales_target if sales_target != "๋๋ค์ ์ฉ" else None,
|
| 732 |
+
'sales_channel': sales_channel if sales_channel != "๋๋ค์ ์ฉ" else None,
|
| 733 |
+
'competition_level': competition_level if competition_level != "๋๋ค์ ์ฉ" else None
|
| 734 |
+
}
|
| 735 |
+
|
| 736 |
+
logger.info("=== ํค์๋๋ณ ๋๋ค ์ค์ ===")
|
| 737 |
+
logger.info(f"์นดํ
๊ณ ๋ฆฌ ๋๋ค: {random_settings['category_random']}")
|
| 738 |
+
logger.info(f"์ถ์ํ์ด๋ฐ ๋๋ค: {random_settings['launch_timing_random']}")
|
| 739 |
+
logger.info(f"๊ณ์ ์ฑ ๋๋ค: {random_settings['seasonality_random']}")
|
| 740 |
+
logger.info(f"๋งค์ถ๋ชฉํ ๋๋ค: {random_settings['sales_target_random']}")
|
| 741 |
+
logger.info(f"ํ๋งค์ฑ๋ ๋๋ค: {random_settings['sales_channel_random']}")
|
| 742 |
+
logger.info(f"๊ฒฝ์๊ฐ๋ ๋๋ค: {random_settings['competition_level_random']}")
|
| 743 |
+
|
| 744 |
+
return random_settings, fixed_values
|
| 745 |
+
|
| 746 |
+
def generate_sourcing_keywords(category, additional_request, launch_timing, seasonality, sales_target, sales_channel, competition_level, search_engine="Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ๋ง"):
|
| 747 |
+
"""๋ค์์ฑ ๊ฐํ๋ ์ผํ ํค์๋ 50๊ฐ๋ฅผ ์์ฑํ๋ ํจ์"""
|
| 748 |
+
logger.info("=== ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ์์ฑ ์์ ===")
|
| 749 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ๊ฒ์์์ง: {search_engine}")
|
| 750 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ์นดํ
๊ณ ๋ฆฌ: {category}")
|
| 751 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ์ถ๊ฐ์์ฒญ: {additional_request}")
|
| 752 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ์ถ์ํ์ด๋ฐ: {launch_timing}")
|
| 753 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ๊ณ์ ์ฑ: {seasonality}")
|
| 754 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ๋งค์ถ๋ชฉํ: {sales_target}")
|
| 755 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ํ๋งค์ฑ๋: {sales_channel}")
|
| 756 |
+
logger.info(f"์
๋ ฅ ์กฐ๊ฑด - ๊ฒฝ์๊ฐ๋: {competition_level}")
|
| 757 |
+
|
| 758 |
+
try:
|
| 759 |
+
logger.info("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ค...")
|
| 760 |
+
client = initialize_gemini()
|
| 761 |
+
|
| 762 |
+
# ๋งค๋ฒ ๋ค๋ฅธ ์๋๋ก ๋๋ค์ฑ ๋ณด์ฅ
|
| 763 |
+
current_time = datetime.now()
|
| 764 |
+
random_seed = current_time.microsecond + current_time.second * 1000
|
| 765 |
+
random.seed(random_seed)
|
| 766 |
+
logger.info(f"๋๋ค ์๋ ์ค์ : {random_seed}")
|
| 767 |
+
|
| 768 |
+
# ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 769 |
+
logger.info("๋ค์์ฑ ๊ฐํ ํ๋กฌํํธ ๊ตฌ์ฑ ์ค...")
|
| 770 |
+
|
| 771 |
+
# ๋๋ค ์ค์ ์ฒ๋ฆฌ
|
| 772 |
+
random_settings, fixed_values = apply_random_selection_for_keywords(
|
| 773 |
+
category, launch_timing, seasonality, sales_target, sales_channel, competition_level
|
| 774 |
+
)
|
| 775 |
+
|
| 776 |
+
# ๋ค์ํ ํค์๋ ์กฐํฉ ๋ฏธ๋ฆฌ ์์ฑ
|
| 777 |
+
diverse_combinations = generate_diverse_keyword_combinations(category, 60)
|
| 778 |
+
logger.info(f"๋ค์ํ ์กฐํฉ ์์ฑ ์๋ฃ: {len(diverse_combinations)}๊ฐ")
|
| 779 |
+
|
| 780 |
+
# ๊ฒ์ ์์ง๋ณ ์ฒ๋ฆฌ
|
| 781 |
+
search_info = ""
|
| 782 |
+
config_tools = []
|
| 783 |
+
|
| 784 |
+
if search_engine == "๋ชจ๋ ๊ฒ์ ์์ง ํตํฉ ๋ถ์ (์ถ์ฒ)":
|
| 785 |
+
logger.info("๐ ๋ค์์ฑ ๊ฐํ ํตํฉ ๋ถ์ ์์...")
|
| 786 |
+
|
| 787 |
+
# Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ ๋๊ตฌ ์ค์
|
| 788 |
+
google_search_tool = Tool(google_search=GoogleSearch())
|
| 789 |
+
config_tools = [google_search_tool]
|
| 790 |
+
|
| 791 |
+
# ๋ค์์ฑ ๊ฐํ ์ข
ํฉ ์์ฅ ๋ถ์ ์คํ
|
| 792 |
+
comprehensive_analysis = comprehensive_market_analysis(category, seasonality, sales_target)
|
| 793 |
+
|
| 794 |
+
search_info = f"""
|
| 795 |
+
๐ === ๋ค์์ฑ ๊ฐํ ํตํฉ ๋ถ์ ๊ฒฐ๊ณผ ===
|
| 796 |
+
๐ Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ: ์ค์๊ฐ ๋ค์ํ ์ผํํค์๋ ํธ๋ ๋ ๋ถ์ (์๋ ์คํ)
|
| 797 |
+
๐ ๋ค์ด๋ฒ ์ผํ API: ํ๊ตญ ์ผํ๋ชฐ ๋ค์ํ ํค์๋ ๋ฐ์ดํฐ ๋ถ์
|
| 798 |
+
๐ DuckDuckGo ๊ฒ์: ๊ธ๋ก๋ฒ ๋ค์ํ ์ผํํค์๋ ์ ๋ณด ๋ถ์
|
| 799 |
+
{comprehensive_analysis}
|
| 800 |
+
๐ก ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ข
ํฉํ์ฌ ๋งค๋ฒ ๋ค๋ฅธ ์กฐํฉ์ ์ผํํค์๋๋ฅผ ์์ฑํฉ๋๋ค.
|
| 801 |
+
๐ฒ ๋๋ค ์๋: {random_seed} (๋งค๋ฒ ๋ค๋ฅธ ๊ฒฐ๊ณผ ๋ณด์ฅ)
|
| 802 |
+
"""
|
| 803 |
+
|
| 804 |
+
elif search_engine == "Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ๋ง":
|
| 805 |
+
logger.info("Google ๊ฒ์ ๋๊ตฌ ์ค์ ์ค...")
|
| 806 |
+
google_search_tool = Tool(google_search=GoogleSearch())
|
| 807 |
+
config_tools = [google_search_tool]
|
| 808 |
+
search_info = f"Google ๊ฒ์ ๊ทธ๋ผ์ด๋ฉ์ ํตํ ๋ค์ํ ์ค์๊ฐ ์ผํํค์๋ ๋ถ์ (์๋: {random_seed})"
|
| 809 |
+
|
| 810 |
+
elif search_engine in ["๋ค์ด๋ฒ ๊ฒ์ API๋ง", "DuckDuckGo ๊ฒ์๋ง"]:
|
| 811 |
+
logger.info(f"{search_engine} ์ฌ์ฉํ์ฌ ๋ค์ํ ์ผํํค์๋ ์กฐ์ฌ ์ค...")
|
| 812 |
+
# ๋ค์์ฑ ๊ฐํ๋ฅผ ์ํ ๊ฒ์ ์คํ
|
| 813 |
+
search_queries = []
|
| 814 |
+
|
| 815 |
+
# ๋๋คํ๊ฒ ๋ค์ํ ๊ฒ์์ด ์์ฑ
|
| 816 |
+
base_items = random.sample(diverse_combinations, 8)
|
| 817 |
+
for item in base_items:
|
| 818 |
+
search_queries.append(f"{item} ์ผํํค์๋")
|
| 819 |
+
|
| 820 |
+
search_results = ""
|
| 821 |
+
for query in search_queries:
|
| 822 |
+
result = search_with_api(query, search_engine)
|
| 823 |
+
search_results += f"[๊ฒ์์ด: {query}]\n{result}\n\n"
|
| 824 |
+
|
| 825 |
+
search_info = f"{search_engine} ๋ค์ํ ์ผํํค์๋ ๊ฒ์ ๊ฒฐ๊ณผ (์๋: {random_seed}):\n{search_results}"
|
| 826 |
+
|
| 827 |
+
else: # ๊ฒ์ ์์ด AI๋ง ์ฌ์ฉ
|
| 828 |
+
logger.info("๊ฒ์ ์์ด AI ์ง์๋ง ์ฌ์ฉ")
|
| 829 |
+
search_info = f"AI ๋ด์ฅ ์ง์์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ํ ์ผํํค์๋ ์์ฑ (์๋: {random_seed})"
|
| 830 |
+
|
| 831 |
+
# ๋ค์์ฑ์ ๊ฐํํ ํ๋กฌํํธ - ๋งค๋ฒ ๋ค๋ฅธ ์กฐํฉ ์์ฒญ
|
| 832 |
+
diverse_sample = random.sample(diverse_combinations, 20)
|
| 833 |
+
|
| 834 |
+
prompt = f"""
|
| 835 |
+
๐ฏ ๋ค์์ฑ ๊ฐํ ์ผํํค์๋ ๋ฐ๊ตด ์์คํ
v5.0
|
| 836 |
+
โก ์ค์: ์ ๋ ์ค๋ณต๋์ง ์๋ ๋ค์ํ ํค์๋๋ง ์์ฑํ์ธ์!
|
| 837 |
+
๐ฌ ์ญํ ์ ์
|
| 838 |
+
๋น์ ์ ๋งค๋ฒ ์์ ํ ๋ค๋ฅธ ์กฐํฉ์ ์ผํํค์๋๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
| 839 |
+
๐ฏ ๋ชฉํ
|
| 840 |
+
์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง๋ ์ค์ ์ผํํค์๋ 50๊ฐ๋ฅผ ๋ฐ๊ตดํ๋, ์ ๋ ์ค๋ณต๋์ง ์๊ณ ๋งค๋ฒ ๋ค๋ฅธ ์กฐํฉ์ผ๋ก ๊ตฌ์ฑํ์ญ์์ค.
|
| 841 |
+
๐ ์
๋ ฅ๋ ์กฐ๊ฑด:
|
| 842 |
+
์นดํ
๊ณ ๋ฆฌ: {category}
|
| 843 |
+
์ถ๊ฐ ์์ฒญ์ฌํญ: {additional_request}
|
| 844 |
+
์ถ์ํ์ด๋ฐ: {launch_timing}
|
| 845 |
+
๊ณ์ ์ฑ: {seasonality}
|
| 846 |
+
๋งค์ถ๋ชฉํ: {sales_target}
|
| 847 |
+
ํ๋งค์ฑ๋: {sales_channel}
|
| 848 |
+
๊ฒฝ์๊ฐ๋: {competition_level}
|
| 849 |
+
๊ฒ์์์ง: {search_engine}
|
| 850 |
+
๐ ์ผํํค์๋ ๋ถ์ ์ ๋ณด:
|
| 851 |
+
{search_info}
|
| 852 |
+
๐ฒ ๋ค์์ฑ ๋ณด์ฅ ์ฐธ๊ณ ์กฐํฉ ์์ (์ด๊ฒ๊ณผ ๋ค๋ฅด๊ฒ ์์ฑํ์ธ์):
|
| 853 |
+
{', '.join(diverse_sample[:10])}
|
| 854 |
+
โ ๏ธ ํค์๋๋ณ ๋๋ค ์ ์ฉ ๊ท์น:
|
| 855 |
+
๊ฐ ํค์๋๋ง๋ค ๋ค์๊ณผ ๊ฐ์ด ์ ์ฉํ์ธ์:
|
| 856 |
+
{"- ์นดํ
๊ณ ๋ฆฌ: ๋งค ํค์๋๋ง๋ค " + str(random_settings['categories']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['category_random'] else f"- ์นดํ
๊ณ ๋ฆฌ: {fixed_values['category']} ๊ณ ์ "}
|
| 857 |
+
{"- ์ถ์ํ์ด๋ฐ: ๋งค ํค์๋๋ง๋ค " + str(random_settings['launch_timings']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['launch_timing_random'] else f"- ์ถ์ํ์ด๋ฐ: {fixed_values['launch_timing']} ๊ณ ์ "}
|
| 858 |
+
{"- ๊ณ์ ์ฑ: ๋งค ํค์๋๋ง๋ค " + str(random_settings['seasonalities']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['seasonality_random'] else f"- ๊ณ์ ์ฑ: {fixed_values['seasonality']} ๊ณ ์ "}
|
| 859 |
+
{"- ๋งค์ถ๋ชฉํ: ๋งค ํค์๋๋ง๋ค " + str(random_settings['sales_targets']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['sales_target_random'] else f"- ๋งค์ถ๋ชฉํ: {fixed_values['sales_target']} ๊ณ ์ "}
|
| 860 |
+
{"- ํ๋งค์ฑ๋: ๋งค ํค์๋๋ง๋ค " + str(random_settings['sales_channels']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['sales_channel_random'] else f"- ํ๋งค์ฑ๋: {fixed_values['sales_channel']} ๊ณ ์ "}
|
| 861 |
+
{"- ๊ฒฝ์๊ฐ๋: ๋งค ํค์๋๋ง๋ค " + str(random_settings['competition_levels']) + " ์ค์์ ๋๋ค ์ ํ" if random_settings['competition_level_random'] else f"- ๊ฒฝ์๊ฐ๋: {fixed_values['competition_level']} ๊ณ ์ "}
|
| 862 |
+
โ๏ธ ๋ค์์ฑ ๊ฐํ ์ํฌํ๋ก์ฐ
|
| 863 |
+
1๋จ๊ณ: ์์ ํ ์๋ก์ด ์กฐํฉ ์์ฑ
|
| 864 |
+
- ์ด์ ๊ฒฐ๊ณผ์ ์ ๋ ์ค๋ณต๋์ง ์๋ ํค์๋ ์กฐํฉ
|
| 865 |
+
- ์์ฌ({', '.join(MATERIAL_KEYWORDS[:5])}) + ์ํ๋ช
์กฐํฉ
|
| 866 |
+
- ํํ({', '.join(SHAPE_KEYWORDS[:5])}) + ์ํ๋ช
์กฐํฉ
|
| 867 |
+
- ๊ธฐ๋ฅ({', '.join(FUNCTION_KEYWORDS[:5])}) + ์ํ๋ช
์กฐํฉ
|
| 868 |
+
2๋จ๊ณ: ์ค๋ณต ๋ฐฉ์ง ํํฐ๋ง
|
| 869 |
+
- ๋์ผํ ํค์๋ ์กฐํฉ ์์ ๋ฐฐ์
|
| 870 |
+
- ์ ์ฌํ ์๋ฏธ์ ํค์๋ ์กฐํฉ ๋ฐฐ์
|
| 871 |
+
- ๋งค๋ฒ ์๋ก์ด ๊ฐ๋๋ก ์ ๊ทผ
|
| 872 |
+
3๋จ๊ณ: 50๊ฐ ๋ค์ํ ํค์๋ ์ ๋ณ
|
| 873 |
+
- ๋ธ๋๋๋ช
์ ๋ ๊ธ์ง
|
| 874 |
+
- ๋ณต์กํ ๊ธฐ์ ์ฉ์ด ๊ธ์ง
|
| 875 |
+
- ์ต๋ 2๊ฐ ๋จ์ด ์กฐํฉ๋ง ํ์ฉ
|
| 876 |
+
โ ๏ธ ๋ค์์ฑ ๊ฐํ ํค์๋ ๊ตฌ์ฑ ๊ท์น (๋งค์ฐ ์ค์):
|
| 877 |
+
๐ซ ์ ๋ ๊ธ์ง ์ฌํญ:
|
| 878 |
+
- ๋์ผํ๊ฑฐ๋ ์ ์ฌํ ํค์๋ ๋ฐ๋ณต
|
| 879 |
+
- ๋ธ๋๋๋ช
(์ผ์ฑ, LG, ๋์ดํค ๋ฑ)
|
| 880 |
+
- ๋ณต์กํ ๊ธฐ์ ์ฉ์ด
|
| 881 |
+
- 3๊ฐ ์ด์ ๋ณตํฉ์ด
|
| 882 |
+
โ
๋ฐ๋์ ๋ค์ํ๊ฒ ํฌํจํด์ผ ํ ํํ:
|
| 883 |
+
1. ์์ฌ๋ณ ํค์๋ (์: ๋๋๋ฌด ๋๋ง, ๊ตฌ๋ฆฌ ์ปต)
|
| 884 |
+
2. ํํ๋ณ ํค์๋ (์: ์ํ ์ ์, ์ฌ๋ฆผ ์ผ์ด์ค)
|
| 885 |
+
3. ๊ธฐ๋ฅ๋ณ ํค์๋ (์: ๋ฐฉ์ ํ์ฐ์น, ํญ๊ท ์๊ฑด)
|
| 886 |
+
4. ์นดํ
๊ณ ๋ฆฌ๋ณ ํค์๋ (์: ์๋ฉํจ, ์กฐ๋ฆฌ๋๊ตฌ)
|
| 887 |
+
๐ฏ ๋ค์์ฑ ๋ณด์ฅ ์ ๋ต:
|
| 888 |
+
- ์ ๋ ๊ฐ์ ์์ฌ๋ฅผ 2๋ฒ ์ด์ ์ฌ์ฉํ์ง ๋ง์ธ์
|
| 889 |
+
- ์ ๋ ๊ฐ์ ํํ๋ฅผ 2๋ฒ ์ด์ ์ฌ์ฉํ์ง ๋ง์ธ์
|
| 890 |
+
- ์ ๋ ๊ฐ์ ๊ธฐ๋ฅ์ 2๋ฒ ์ด์ ์ฌ์ฉํ์ง ๋ง์ธ์
|
| 891 |
+
- ๋งค ํค์๋๋ง๋ค ์์ ํ ๋ค๋ฅธ ์กฐํฉ์ผ๋ก ์์ฑํ์ธ์
|
| 892 |
+
์ฌ๋ฐ๋ฅธ ๋ค์ํ ํค์๋ ์์:
|
| 893 |
+
โ
๋๋๋ฌด ๋๋ง (์์ฌ+์ํ)
|
| 894 |
+
โ
์ํ ์ ์ (ํํ+์ํ)
|
| 895 |
+
โ
๋ฐฉ์ ํ์ฐ์น (๊ธฐ๋ฅ+์ํ)
|
| 896 |
+
โ
์ธ๋ผ๋ฏน ๋จธ๊ทธ์ปต (์์ฌ+์ํ)
|
| 897 |
+
โ
์ ์ด์ ์ ๋ฐ (ํํ+์ํ)
|
| 898 |
+
โ
ํญ๊ท ์๊ฑด (๊ธฐ๋ฅ+์ํ)
|
| 899 |
+
์๋ชป๋ ๋ฐ๋ณต ํค์๋ ์์:
|
| 900 |
+
โ ๋๋๋ฌด ๋๋ง, ๋๋๋ฌด ์ ๊ฐ๋ฝ (์์ฌ ๋ฐ๋ณต)
|
| 901 |
+
โ ์ํ ์ ์, ์ํ ์๋ฐ (ํํ ๋ฐ๋ณต)
|
| 902 |
+
โ ๋ฐฉ์ ํ์ฐ์น, ๋ฐฉ์ ์ผ์ด์ค (๊ธฐ๋ฅ ๋ฐ๋ณต)
|
| 903 |
+
๐ ์ถ๋ ฅ ํ์:
|
| 904 |
+
์ค์ง ์์ ํ ๋ค๋ฅธ ์ผํํค์๋๋ง ํ ์ค์ฉ 50๊ฐ ์ถ๋ ฅ
|
| 905 |
+
- ๋ฒํธ ๊ธ์ง
|
| 906 |
+
- ์ค๋ช
๊ธ์ง
|
| 907 |
+
- ๊ธฐํธ๋ ํน์๋ฌธ์ ๊ธ์ง
|
| 908 |
+
- ๊ดํธ ์ ์ค๋ช
๊ธ์ง
|
| 909 |
+
- ์์ ํค์๋๋ง ์ถ๋ ฅ
|
| 910 |
+
- ์ ๋ ์ค๋ณต ๊ธ์ง
|
| 911 |
+
์์ ์ถ๋ ฅ ํํ (๋งค๋ฒ ์์ ํ ๋ค๋ฅด๊ฒ):
|
| 912 |
+
์ ๋ฆฌ ํ๋ถ
|
| 913 |
+
์ ์ด์ ์์
|
| 914 |
+
ํญ๊ท ๋๋ง
|
| 915 |
+
์๋ฃจ๋ฏธ๋ ํ
๋ธ๋ฌ
|
| 916 |
+
์ฌ๋ฆผ ํ์ผํจ
|
| 917 |
+
โก ์ง๊ธ ๋ฐ๋ก ์ ๋ ์ค๋ณต๋์ง ์๋ ์์ ํ ์๋ก์ด ์ผํํค์๋ 50๊ฐ๋ฅผ ๊ฐ๊ฐ ๋ค๋ฅธ ๋๋ค ์กฐ๊ฑด์ ์ ์ฉํ์ฌ ์ถ๋ ฅํ์ธ์.
|
| 918 |
+
๋งค๋ฒ ์คํํ ๋๋ง๋ค ์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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ํค์๋ ์ฒ๋ฆฌ ๊ด๋ จ ๊ธฐ๋ฅ - ์๋ค ์กฐํฉ ์ค ๋์ ๊ฒ์๋๋ง ์ ํ, ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์ ๊ฑฐ
|
| 3 |
+
- ํค์๋ ์ถ์ถ ๋ฐ ์กฐํฉ
|
| 4 |
+
- ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import re
|
| 9 |
+
from collections import defaultdict, Counter
|
| 10 |
+
import text_utils
|
| 11 |
+
import keyword_search
|
| 12 |
+
import product_search
|
| 13 |
+
import logging
|
| 14 |
+
|
| 15 |
+
# ๋ก๊น
์ค์
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
logger.setLevel(logging.INFO)
|
| 18 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 19 |
+
handler = logging.StreamHandler()
|
| 20 |
+
handler.setFormatter(formatter)
|
| 21 |
+
logger.addHandler(handler)
|
| 22 |
+
|
| 23 |
+
def process_search_results(search_results, current_keyword="", exclude_zero_volume=True):
|
| 24 |
+
"""
|
| 25 |
+
๊ฒ์ ๊ฒฐ๊ณผ์์ ํค์๋์ ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์ถ์ถ ๋ฐ ์ฒ๋ฆฌ - ์๋ค ์กฐํฉ ์ค ๋์ ๊ฒ์๋๋ง ์ ํ
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
search_results (dict): ๊ฒ์ ๊ฒฐ๊ณผ ์ ๋ณด
|
| 29 |
+
current_keyword (str): ํ์ฌ ๊ฒ์ ์ค์ธ ํค์๋
|
| 30 |
+
exclude_zero_volume (bool): ๊ฒ์๋์ด 0์ธ ํค์๋ ์ ์ธ ์ฌ๋ถ
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
dict: ์ฒ๋ฆฌ๋ ๊ฒฐ๊ณผ
|
| 34 |
+
"""
|
| 35 |
+
logger.info("\n===== ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์์ =====")
|
| 36 |
+
logger.info(f"ํ์ฌ ํค์๋: '{current_keyword}'")
|
| 37 |
+
logger.info(f"๊ฒ์๋ 0 ํค์๋ ์ ์ธ: {exclude_zero_volume}")
|
| 38 |
+
|
| 39 |
+
if not search_results or not search_results.get("product_list"):
|
| 40 |
+
logger.warning("๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.")
|
| 41 |
+
return {
|
| 42 |
+
"products_df": None,
|
| 43 |
+
"keywords_df": None,
|
| 44 |
+
"categories": ["์ ์ฒด ๋ณด๊ธฐ"],
|
| 45 |
+
"message": "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
product_list = search_results["product_list"]
|
| 49 |
+
combo_candidates = search_results["combo_candidates"]
|
| 50 |
+
category_counter = search_results["category_counter"]
|
| 51 |
+
keyword_indices = search_results["keyword_indices"]
|
| 52 |
+
keyword_pairs = search_results.get("keyword_pairs", {}) # ์๋ค ์กฐํฉ ์ ๋ณด
|
| 53 |
+
|
| 54 |
+
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ - ์ํ ์: {len(product_list)}๊ฐ")
|
| 55 |
+
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ - ์กฐํฉ ํ๋ณด ์: {len(combo_candidates)}๊ฐ")
|
| 56 |
+
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ - ์นดํ
๊ณ ๋ฆฌ ์: {len(category_counter)}๊ฐ")
|
| 57 |
+
|
| 58 |
+
# ์ํ ์ ๋ณด ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 59 |
+
df_products = pd.DataFrame(product_list)
|
| 60 |
+
|
| 61 |
+
# API ํค์๋๋ฅผ UI ํค์๋๋ก ๋ณํํ๋ ๋งคํ ์์ฑ
|
| 62 |
+
api_to_ui_keywords = {}
|
| 63 |
+
|
| 64 |
+
for api_keyword in combo_candidates.keys():
|
| 65 |
+
# API ํค์๋์์ UI ํค์๋๋ก ๋ณํ
|
| 66 |
+
if current_keyword and current_keyword in api_keyword:
|
| 67 |
+
# ๋ฉ์ธ ํค์๋ ์์ฒด์ธ ๊ฒฝ์ฐ
|
| 68 |
+
if api_keyword == current_keyword:
|
| 69 |
+
api_to_ui_keywords[api_keyword] = current_keyword
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์ด๋ฏธ ํฌํจ๋ ๊ฒฝ์ฐ (์: ๊ฐ์ค์ง์ด, ๊ท์ค์ง์ด)
|
| 73 |
+
# ๊ณต๋ฐฑ ์๋ ํํ๋ก ๋ณํ
|
| 74 |
+
ui_keyword = api_keyword
|
| 75 |
+
# ๊ณต๋ฐฑ์ด ์๋ ํํ๋ผ๋ฉด ์ ์ ํ ์์น์ ๊ณต๋ฐฑ ์ถ๊ฐ
|
| 76 |
+
if " " not in api_keyword:
|
| 77 |
+
# ๋ฉ์ธ ํค์๋ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ
|
| 78 |
+
if api_keyword.startswith(current_keyword):
|
| 79 |
+
# ์ค์ง์ด๊ฐ => ์ค์ง์ด ๊ฐ
|
| 80 |
+
prefix = current_keyword
|
| 81 |
+
suffix = api_keyword[len(current_keyword):]
|
| 82 |
+
if suffix:
|
| 83 |
+
ui_keyword = f"{prefix} {suffix}"
|
| 84 |
+
elif api_keyword.endswith(current_keyword):
|
| 85 |
+
# ๊ฐ์ค์ง์ด => ๊ฐ ์ค์ง์ด
|
| 86 |
+
prefix = api_keyword[:-len(current_keyword)]
|
| 87 |
+
suffix = current_keyword
|
| 88 |
+
if prefix:
|
| 89 |
+
ui_keyword = f"{prefix} {suffix}"
|
| 90 |
+
else:
|
| 91 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์ค๊ฐ์ ์๋ ๊ฒฝ์ฐ
|
| 92 |
+
idx = api_keyword.find(current_keyword)
|
| 93 |
+
if idx > 0:
|
| 94 |
+
prefix = api_keyword[:idx]
|
| 95 |
+
middle = current_keyword
|
| 96 |
+
suffix = api_keyword[idx+len(current_keyword):]
|
| 97 |
+
ui_keyword = f"{prefix} {middle}"
|
| 98 |
+
if suffix:
|
| 99 |
+
ui_keyword += f" {suffix}"
|
| 100 |
+
|
| 101 |
+
api_to_ui_keywords[api_keyword] = ui_keyword
|
| 102 |
+
else:
|
| 103 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์๋ ๊ฒฝ์ฐ - ๊ทธ๋๋ก ์ฌ์ฉ
|
| 104 |
+
api_to_ui_keywords[api_keyword] = api_keyword
|
| 105 |
+
|
| 106 |
+
# === ์์ ๋ ๋ถ๋ถ: ๊ฒ์๋ ์กฐํ ํ ์๋ค ์กฐํฉ ์ค ๋์ ๊ฒ๋ง ์ ํ ===
|
| 107 |
+
logger.info(f"\n๊ฒ์๋ ์กฐํ ๋์ ํค์๋ ์: {len(combo_candidates)}๊ฐ")
|
| 108 |
+
search_volumes = keyword_search.fetch_all_search_volumes(list(combo_candidates.keys()))
|
| 109 |
+
logger.info(f"๊ฒ์๋ ์กฐํ ์๋ฃ: {len(search_volumes)}๊ฐ ๊ฒฐ๊ณผ")
|
| 110 |
+
|
| 111 |
+
# ์๋ค ์กฐํฉ ์ค ๋์ ๊ฒ์๋๋ง ์ ํ
|
| 112 |
+
if keyword_pairs and current_keyword:
|
| 113 |
+
logger.info("\n=== ์๋ค ์กฐํฉ ์ค ๋์ ๊ฒ์๋ ์ ํ ===")
|
| 114 |
+
filtered_candidates = {}
|
| 115 |
+
|
| 116 |
+
# ๋ฉ์ธ ํค์๋๋ ํญ์ ํฌํจ
|
| 117 |
+
main_api = current_keyword.replace(" ", "")
|
| 118 |
+
if main_api in combo_candidates:
|
| 119 |
+
filtered_candidates[main_api] = combo_candidates[main_api]
|
| 120 |
+
logger.info(f"๋ฉ์ธ ํค์๋ ์ ์ง: '{current_keyword}'")
|
| 121 |
+
|
| 122 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ํฌํจ๋ ๋ณตํฉ์ด๋ ์ ์ง
|
| 123 |
+
for api_kw, categories in combo_candidates.items():
|
| 124 |
+
ui_kw = api_to_ui_keywords[api_kw]
|
| 125 |
+
if current_keyword in ui_kw and api_kw != main_api and api_kw not in [pair_info["front"].replace(" ", "") for pair_info in keyword_pairs.values()] and api_kw not in [pair_info["back"].replace(" ", "") for pair_info in keyword_pairs.values()]:
|
| 126 |
+
filtered_candidates[api_kw] = categories
|
| 127 |
+
logger.info(f"๋ฉ์ธ ํค์๋ ํฌํจ ๋ณตํฉ์ด ์ ์ง: '{ui_kw}'")
|
| 128 |
+
|
| 129 |
+
# ์๋ค ์กฐํฉ ๋น๊ต
|
| 130 |
+
for base_word, pair_info in keyword_pairs.items():
|
| 131 |
+
front_kw = pair_info["front"] # "ํค์๋ ๋ฉ์ธํค์๋"
|
| 132 |
+
back_kw = pair_info["back"] # "๋ฉ์ธํค์๋ ํค์๋"
|
| 133 |
+
|
| 134 |
+
front_api = front_kw.replace(" ", "")
|
| 135 |
+
back_api = back_kw.replace(" ", "")
|
| 136 |
+
|
| 137 |
+
front_vol = search_volumes.get(front_api, {}).get("์ด๊ฒ์๋", 0)
|
| 138 |
+
back_vol = search_volumes.get(back_api, {}).get("์ด๊ฒ์๋", 0)
|
| 139 |
+
|
| 140 |
+
# ๋์ ๊ฒ์๋ ์ ํ
|
| 141 |
+
if front_vol > back_vol:
|
| 142 |
+
selected_api = front_api
|
| 143 |
+
selected_kw = front_kw
|
| 144 |
+
selected_vol = front_vol
|
| 145 |
+
removed_kw = back_kw
|
| 146 |
+
removed_vol = back_vol
|
| 147 |
+
elif back_vol > front_vol:
|
| 148 |
+
selected_api = back_api
|
| 149 |
+
selected_kw = back_kw
|
| 150 |
+
selected_vol = back_vol
|
| 151 |
+
removed_kw = front_kw
|
| 152 |
+
removed_vol = front_vol
|
| 153 |
+
elif front_vol == back_vol and front_vol > 0:
|
| 154 |
+
# ๊ฐ์ ๊ฒ์๋์ด๋ฉด ๋ ์์ฐ์ค๋ฌ์ด ์์ ์ ํ (๋ฉ์ธํค์๋๊ฐ ๋ค์ ์ค๋ ๊ฒ)
|
| 155 |
+
selected_api = back_api
|
| 156 |
+
selected_kw = back_kw
|
| 157 |
+
selected_vol = back_vol
|
| 158 |
+
removed_kw = front_kw
|
| 159 |
+
removed_vol = front_vol
|
| 160 |
+
else:
|
| 161 |
+
# ๋ ๋ค 0์ด๋ฉด ์ ์ธ
|
| 162 |
+
logger.info(f" '{base_word}' ์กฐํฉ: ๋ ๋ค ๊ฒ์๋ 0์ผ๋ก ์ ์ธ")
|
| 163 |
+
continue
|
| 164 |
+
|
| 165 |
+
# ์ ํ๋ ํค์๋๋ง ์ถ๊ฐ
|
| 166 |
+
if selected_vol > 0 or not exclude_zero_volume:
|
| 167 |
+
filtered_candidates[selected_api] = combo_candidates[selected_api]
|
| 168 |
+
logger.info(f" '{base_word}' ์กฐํฉ ์ ํ: '{selected_kw}' ({selected_vol:,}) > '{removed_kw}' ({removed_vol:,})")
|
| 169 |
+
else:
|
| 170 |
+
logger.info(f" '{base_word}' ์กฐํฉ: ๊ฒ์๋ 0์ผ๋ก ์ ์ธ")
|
| 171 |
+
|
| 172 |
+
# ํํฐ๋ง๋ ์กฐํฉ์ผ๋ก ๊ต์ฒด
|
| 173 |
+
combo_candidates = filtered_candidates
|
| 174 |
+
logger.info(f"์๋ค ์กฐํฉ ํํฐ๋ง ์๋ฃ: {len(combo_candidates)}๊ฐ ํค์๋ ์ ํ")
|
| 175 |
+
|
| 176 |
+
# ๊ฒ์๋ 0 ํค์๋ ํต๊ณ
|
| 177 |
+
zero_volume_count = sum(1 for vol in search_volumes.values() if vol.get("์ด๊ฒ์๋", 0) == 0)
|
| 178 |
+
logger.info(f"๊ฒ์๋ 0์ธ ํค์๋ ์: {zero_volume_count}๊ฐ ({zero_volume_count/max(1, len(search_volumes))*100:.1f}%)")
|
| 179 |
+
|
| 180 |
+
# ์ค๋ณต ํค์๋ ์ ๊ฑฐ๋ฅผ ์ํ ์ ๊ทํ๋ ํค์๋ ์งํฉ
|
| 181 |
+
normalized_keywords = {}
|
| 182 |
+
|
| 183 |
+
for api_keyword in combo_candidates.keys():
|
| 184 |
+
ui_keyword = api_to_ui_keywords[api_keyword]
|
| 185 |
+
|
| 186 |
+
# ๊ฒ์๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 187 |
+
pc_count = 0
|
| 188 |
+
mobile_count = 0
|
| 189 |
+
total_count = 0
|
| 190 |
+
if api_keyword in search_volumes:
|
| 191 |
+
pc_count = search_volumes[api_keyword]["PC๊ฒ์๋"]
|
| 192 |
+
mobile_count = search_volumes[api_keyword]["๋ชจ๋ฐ์ผ๊ฒ์๋"]
|
| 193 |
+
total_count = search_volumes[api_keyword]["์ด๊ฒ์๋"]
|
| 194 |
+
|
| 195 |
+
# ๊ฒ์๋ 0์ธ ํค์๋ ์ ์ธ ์ต์
์ ์ฉ
|
| 196 |
+
if exclude_zero_volume and total_count == 0:
|
| 197 |
+
logger.debug(f" - '{ui_keyword}' (API: '{api_keyword}') - ๊ฒ์๋ 0์ผ๋ก ์ ์ธ๋จ")
|
| 198 |
+
continue
|
| 199 |
+
|
| 200 |
+
# 1. ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋จ์ด ๋ถ๋ฆฌ ํ ์ ๋ ฌํด ์ ๊ทํ ํค ์์ฑ
|
| 201 |
+
words = ui_keyword.split()
|
| 202 |
+
normalized = "".join(sorted(words))
|
| 203 |
+
|
| 204 |
+
# 2. ์ด๋ฏธ ์ ๊ทํ๋ ํค์๋๊ฐ ์์ผ๋ฉด ๊ฒ์๋์ด ๋ ๋์ ๊ฒ์ ์ ํ
|
| 205 |
+
if normalized in normalized_keywords:
|
| 206 |
+
existing_api_keyword, existing_ui_keyword, existing_total = normalized_keywords[normalized]
|
| 207 |
+
if total_count > existing_total:
|
| 208 |
+
logger.debug(f" - ์ค๋ณต ํค์๋ ๋์ฒด: '{existing_ui_keyword}' ({existing_total}) -> '{ui_keyword}' ({total_count})")
|
| 209 |
+
normalized_keywords[normalized] = (api_keyword, ui_keyword, total_count)
|
| 210 |
+
else:
|
| 211 |
+
logger.debug(f" - ์ค๋ณต ํค์๋ ์ ์ธ: '{ui_keyword}' ({total_count}) < '{existing_ui_keyword}' ({existing_total})")
|
| 212 |
+
else:
|
| 213 |
+
normalized_keywords[normalized] = (api_keyword, ui_keyword, total_count)
|
| 214 |
+
logger.debug(f" - ํค์๋ ์ถ๊ฐ: '{ui_keyword}' (๊ฒ์๋: {total_count})")
|
| 215 |
+
|
| 216 |
+
logger.info(f"\n์ค๋ณต ์ ๊ฑฐ ํ ํค์๋ ์: {len(normalized_keywords)}๊ฐ")
|
| 217 |
+
|
| 218 |
+
# ์ค๋ณต์ด ์ ๊ฑฐ๋ ํค์๋๋ง ์ฒ๋ฆฌ
|
| 219 |
+
final_combos = []
|
| 220 |
+
for normalized, (api_keyword, ui_keyword, total_count) in normalized_keywords.items():
|
| 221 |
+
|
| 222 |
+
# ํค์๋ ๊ฐ๋
์ฑ ๊ฐ์ - fix_keyword_order ํจ์ ์ ์ฉ
|
| 223 |
+
readable = fix_keyword_order(ui_keyword, current_keyword)
|
| 224 |
+
|
| 225 |
+
# ๊ฒ์๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 226 |
+
pc_count = 0
|
| 227 |
+
mobile_count = 0
|
| 228 |
+
if api_keyword in search_volumes:
|
| 229 |
+
pc_count = search_volumes[api_keyword]["PC๊ฒ์๋"]
|
| 230 |
+
mobile_count = search_volumes[api_keyword]["๋ชจ๋ฐ์ผ๊ฒ์๋"]
|
| 231 |
+
total_count = search_volumes[api_keyword]["์ด๊ฒ์๋"]
|
| 232 |
+
|
| 233 |
+
# ๊ฒ์๋ ๊ตฌ๊ฐ ๊ณ์ฐ
|
| 234 |
+
search_volume_range = text_utils.get_search_volume_range(total_count)
|
| 235 |
+
|
| 236 |
+
# ๋ฑ์ฅ ์์ ๋ฐ ํ์ ๊ณ์ฐ
|
| 237 |
+
base_word = readable.replace(current_keyword, "").strip() if current_keyword else readable
|
| 238 |
+
ranks = []
|
| 239 |
+
if base_word in keyword_indices:
|
| 240 |
+
ranks = [idx + 1 for idx in keyword_indices[base_word]]
|
| 241 |
+
elif api_keyword in keyword_indices: # ๋ฉ์ธ ํค์๋๊ฐ ํฌํจ๋ ๋จ์ด์ธ ๊ฒฝ์ฐ
|
| 242 |
+
ranks = [idx + 1 for idx in keyword_indices.get(api_keyword, [])]
|
| 243 |
+
|
| 244 |
+
ranks_str = ", ".join(map(str, ranks)) if ranks else "-"
|
| 245 |
+
usage_count = len(ranks)
|
| 246 |
+
|
| 247 |
+
# === ์์ ๋ ๋ถ๋ถ: "์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ(์์100์)" ํญ๋ชฉ ์ ๊ฑฐ ===
|
| 248 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด๋ ๋ด๋ถ์ ์ผ๋ก๋ง ์ฌ์ฉํ๊ณ ํ
์ด๋ธ์๋ ํ์ํ์ง ์์
|
| 249 |
+
|
| 250 |
+
final_combos.append({
|
| 251 |
+
"์กฐํฉ ํค์๋": readable.strip(),
|
| 252 |
+
"PC๊ฒ์๋": pc_count,
|
| 253 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": mobile_count,
|
| 254 |
+
"์ด๊ฒ์๋": total_count,
|
| 255 |
+
"๊ฒ์๋๊ตฌ๊ฐ": search_volume_range,
|
| 256 |
+
"ํค์๋ ์ฌ์ฉ์์์": ranks_str,
|
| 257 |
+
"ํค์๋ ์ฌ์ฉํ์": usage_count
|
| 258 |
+
# "์ํ ๋ฑ๋ก ์นดํ
๊ณ ๋ฆฌ(์์100์)" ํญ๋ชฉ ์ ๊ฑฐ๋จ
|
| 259 |
+
})
|
| 260 |
+
|
| 261 |
+
# ํค์๋ ์ ๋ณด ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 262 |
+
df_keywords = pd.DataFrame(final_combos)
|
| 263 |
+
|
| 264 |
+
# ๊ฒ์๋ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
|
| 265 |
+
if not df_keywords.empty:
|
| 266 |
+
df_keywords = df_keywords.sort_values(by="์ด๊ฒ์๋", ascending=False)
|
| 267 |
+
# ์๋ฒ์ ์ํด ์ธ๋ฑ์ค ๋ฆฌ์
(์์ฐจ์ ์๋ฒ ๋ณด์ฅ)
|
| 268 |
+
df_keywords = df_keywords.reset_index(drop=True)
|
| 269 |
+
|
| 270 |
+
# ๋ฐ์ดํฐํ๋ ์ ์์ฑ ํ ๋ก๊น
|
| 271 |
+
logger.info(f"\n์์ฑ๋ ํค์๋ ๋ฐ์ดํฐํ๋ ์ ํ ์: {len(df_keywords)}")
|
| 272 |
+
if not df_keywords.empty:
|
| 273 |
+
logger.debug(f"๋ฐ์ดํฐํ๋ ์ ์ด: {df_keywords.columns.tolist()}")
|
| 274 |
+
logger.info(f"์ด {len(df_keywords)}๊ฐ ํค์๋ ์์ฑ ์๋ฃ")
|
| 275 |
+
|
| 276 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ๊ฐ๊ณต
|
| 277 |
+
category_with_counts = [f"{cat} ({category_counter[cat]})" for cat in sorted(category_counter.keys())]
|
| 278 |
+
category_with_counts.insert(0, "์ ์ฒด ๋ณด๊ธฐ")
|
| 279 |
+
|
| 280 |
+
logger.info(f"์นดํ
๊ณ ๋ฆฌ ์: {len(category_counter)}๊ฐ")
|
| 281 |
+
logger.info("===== ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์๋ฃ =====\n")
|
| 282 |
+
|
| 283 |
+
return {
|
| 284 |
+
"products_df": df_products,
|
| 285 |
+
"keywords_df": df_keywords,
|
| 286 |
+
"categories": category_with_counts,
|
| 287 |
+
"message": "โ
๊ฒ์์ด ์๋ฃ๋์์ต๋๋ค. ์๋์์ ํค์๋๋ฅผ ํ์ธํ์ธ์."
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
def filter_and_sort_table(df, selected_cat, keyword_sort, total_volume_sort, usage_count_sort, selected_volume_range, exclude_zero_volume=False):
|
| 291 |
+
"""ํ
์ด๋ธ ํํฐ๋ง ๋ฐ ์ ๋ ฌ ํจ์ (๊ฒ์๋ 0 ์ ์ธ ๊ธฐ๋ฅ ์ถ๊ฐ)"""
|
| 292 |
+
if df is None or df.empty:
|
| 293 |
+
return ""
|
| 294 |
+
|
| 295 |
+
# ํํฐ๋ง ์ ์ฉ
|
| 296 |
+
filtered = df.copy()
|
| 297 |
+
|
| 298 |
+
# ์นดํ
๊ณ ๋ฆฌ ํํฐ ์ ์ฉ (์นดํ
๊ณ ๋ฆฌ ์ด์ด ์ ๊ฑฐ๋์์ผ๋ฏ๋ก ์ฃผ์ ์ฒ๋ฆฌ)
|
| 299 |
+
# if selected_cat and selected_cat != "์ ์ฒด ๋ณด๊ธฐ":
|
| 300 |
+
# cat_name = selected_cat.rsplit(" (", 1)[0]
|
| 301 |
+
# filtered = filtered[filtered["๊ด๋ จ ์นดํ
๊ณ ๋ฆฌ"].str.contains(cat_name)]
|
| 302 |
+
|
| 303 |
+
# ๊ฒ์๋ ๊ตฌ๊ฐ ํํฐ ์ ์ฉ
|
| 304 |
+
if selected_volume_range and selected_volume_range != "์ ์ฒด":
|
| 305 |
+
filtered = filtered[filtered["๊ฒ์๋๊ตฌ๊ฐ"] == selected_volume_range]
|
| 306 |
+
|
| 307 |
+
# ๊ฒ์๋ 0 ์ ์ธ ํํฐ ์ ์ฉ
|
| 308 |
+
if exclude_zero_volume:
|
| 309 |
+
filtered = filtered[filtered["์ด๊ฒ์๋"] > 0]
|
| 310 |
+
logger.info(f"๊ฒ์๋ 0 ์ ์ธ ํํฐ ์ ์ฉ - ๋จ์ ํค์๋ ์: {len(filtered)}")
|
| 311 |
+
|
| 312 |
+
# ์ ๋ ฌ ์ ์ฉ
|
| 313 |
+
if keyword_sort != "์ ๋ ฌ ์์":
|
| 314 |
+
is_ascending = keyword_sort == "์ค๋ฆ์ฐจ์"
|
| 315 |
+
filtered = filtered.sort_values(by="์กฐํฉ ํค์๋", ascending=is_ascending)
|
| 316 |
+
|
| 317 |
+
if total_volume_sort != "์ ๋ ฌ ์์":
|
| 318 |
+
is_ascending = total_volume_sort == "์ค๋ฆ์ฐจ์"
|
| 319 |
+
filtered = filtered.sort_values(by="์ด๊ฒ์๋", ascending=is_ascending)
|
| 320 |
+
|
| 321 |
+
# ํค์๋ ์ฌ์ฉํ์ ์ ๏ฟฝ๏ฟฝ๏ฟฝ ์ ์ฉ
|
| 322 |
+
if usage_count_sort != "์ ๋ ฌ ์์":
|
| 323 |
+
is_ascending = usage_count_sort == "์ค๋ฆ์ฐจ์"
|
| 324 |
+
filtered = filtered.sort_values(by="ํค์๋ ์ฌ์ฉํ์", ascending=is_ascending)
|
| 325 |
+
|
| 326 |
+
# ๋ฐ์ดํฐํ๋ ์ ๋ด์ฉ ๋ก๊น
|
| 327 |
+
logger.info(f"ํํฐ ์ ์ฉ ํ - ํํฐ๋ง๋ DataFrame ํ ์: {len(filtered)}")
|
| 328 |
+
|
| 329 |
+
# ์๋ฒ์ 1๋ถํฐ ์์ฐจ์ ์ผ๋ก ์ ์งํ๊ธฐ ์ํด ํ ์ธ๋ฑ์ค ์ฌ์ค์
|
| 330 |
+
filtered = filtered.reset_index(drop=True)
|
| 331 |
+
|
| 332 |
+
from export_utils import create_table_without_checkboxes
|
| 333 |
+
|
| 334 |
+
# ์๋ฒ์ ํฌํจํ HTML ํ
์ด๋ธ ์์ฑ
|
| 335 |
+
html = create_table_without_checkboxes(filtered)
|
| 336 |
+
|
| 337 |
+
return html
|
| 338 |
+
|
| 339 |
+
def fix_keyword_order(keyword, main_keyword):
|
| 340 |
+
"""
|
| 341 |
+
ํค์๋ ์์๋ฅผ ์์ ํ๋ ํจ์ - ํ๊ธ์ด ์์ ์ค๊ณ ์์ด/์ซ์๊ฐ ๋ค์ ์ค๋๋ก ํจ
|
| 342 |
+
|
| 343 |
+
Args:
|
| 344 |
+
keyword (str): ์์ ํ ํค์๋
|
| 345 |
+
main_keyword (str): ๋ฉ์ธ ํค์๋
|
| 346 |
+
|
| 347 |
+
Returns:
|
| 348 |
+
str: ์์๊ฐ ์์ ๋ ํค์๋
|
| 349 |
+
"""
|
| 350 |
+
# ๊ณต๋ฐฑ ์์ด ์ซ์+์์ด์ ํ๊ธ์ด ๋ถ์ด์๋ ํจํด ์ฒ๋ฆฌ
|
| 351 |
+
# ์: "300g์ค์ง์ด" โ "์ค์ง์ด 300g"
|
| 352 |
+
pattern_combined = re.compile(r'^([0-9]+[a-zA-Z]*)([๊ฐ-ํฃ]+.*)$')
|
| 353 |
+
match = pattern_combined.match(keyword)
|
| 354 |
+
if match:
|
| 355 |
+
number_part = match.group(1) # ์ซ์+์์ด ๋ถ๋ถ
|
| 356 |
+
korean_part = match.group(2) # ํ๊ธ ๋ถ๋ถ
|
| 357 |
+
fixed_keyword = f"{korean_part} {number_part}"
|
| 358 |
+
logger.debug(f"๋ถ์ด์๋ ํจํด ์์ : '{keyword}' -> '{fixed_keyword}'")
|
| 359 |
+
return fixed_keyword
|
| 360 |
+
|
| 361 |
+
# ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 362 |
+
if ' ' in keyword:
|
| 363 |
+
parts = keyword.split()
|
| 364 |
+
|
| 365 |
+
# ํ๊ธ ํฌํจ ์ฌ๋ถ์ ์์ด/์ซ์ ํฌํจ ์ฌ๋ถ๋ฅผ ๊ฐ ๋ถ๋ถ๋ณ๋ก ํ์ธ
|
| 366 |
+
korean_parts = []
|
| 367 |
+
non_korean_parts = []
|
| 368 |
+
|
| 369 |
+
for part in parts:
|
| 370 |
+
if re.search(r'[๊ฐ-ํฃ]', part):
|
| 371 |
+
korean_parts.append(part) # ํ๊ธ์ด ํฌํจ๋ ๋ถ๋ถ
|
| 372 |
+
else:
|
| 373 |
+
non_korean_parts.append(part) # ํ๊ธ์ด ์๋ ๋ถ๋ถ (์์ด, ์ซ์, ๊ธฐํธ ๋ฑ)
|
| 374 |
+
|
| 375 |
+
# ํ๊ธ ๋ถ๋ถ์ด ํ๋๋ ์๊ฑฐ๋ ๋นํ๊ธ ๋ถ๋ถ์ด ํ๋๋ ์์ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
|
| 376 |
+
if not korean_parts or not non_korean_parts:
|
| 377 |
+
return keyword
|
| 378 |
+
|
| 379 |
+
# ํ๊ธ ๋ถ๋ถ์ ์์ผ๋ก, ๋นํ๊ธ ๋ถ๋ถ์ ๋ค๋ก ๋ฐฐ์น
|
| 380 |
+
fixed_keyword = " ".join(korean_parts + non_korean_parts)
|
| 381 |
+
|
| 382 |
+
# ์๋ ํค์๋์ ๋ค๋ฅธ ๊ฒฝ์ฐ์๋ง ๋ก๊ทธ ์ถ๋ ฅ
|
| 383 |
+
if fixed_keyword != keyword:
|
| 384 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ํค์๋ ๊ฒ์๋ ์กฐํ ๊ด๋ จ ๊ธฐ๋ฅ
|
| 3 |
+
- ๋ค์ด๋ฒ API๋ฅผ ํตํ ํค์๋ ๊ฒ์๋ ์กฐํ
|
| 4 |
+
- ๊ฒ์๋ ๋ฐฐ์น ์ฒ๋ฆฌ
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import requests
|
| 8 |
+
import time
|
| 9 |
+
import random
|
| 10 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 11 |
+
import api_utils
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
# ๋ก๊น
์ค์
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
logger.setLevel(logging.INFO)
|
| 17 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 18 |
+
handler = logging.StreamHandler()
|
| 19 |
+
handler.setFormatter(formatter)
|
| 20 |
+
logger.addHandler(handler)
|
| 21 |
+
|
| 22 |
+
def exponential_backoff_sleep(retry_count, base_delay=0.3, max_delay=5.0):
|
| 23 |
+
"""์ง์ ๋ฐฑ์คํ ๋ฐฉ์์ ๋๊ธฐ ์๊ฐ ๊ณ์ฐ"""
|
| 24 |
+
delay = min(base_delay * (2 ** retry_count), max_delay)
|
| 25 |
+
# ์ฝ๊ฐ์ ๋๋ค์ฑ ์ถ๊ฐ (์งํฐ)
|
| 26 |
+
jitter = random.uniform(0, 0.5) * delay
|
| 27 |
+
time.sleep(delay + jitter)
|
| 28 |
+
|
| 29 |
+
def fetch_search_volume_batch(keywords_batch):
|
| 30 |
+
"""ํค์๋ ๋ฐฐ์น์ ๋ํ ๋ค์ด๋ฒ ๊ฒ์๋ ์กฐํ"""
|
| 31 |
+
|
| 32 |
+
# 1. ์คํ์ด์ค๋ฐ ์ ๊ฑฐ ๊ฐ์ - ๋ฐฐ์น ํค์๋๋ค ์ ์ฒ๋ฆฌ
|
| 33 |
+
cleaned_keywords_batch = []
|
| 34 |
+
for kw in keywords_batch:
|
| 35 |
+
cleaned_kw = kw.strip().replace(" ", "") if kw else ""
|
| 36 |
+
cleaned_keywords_batch.append(cleaned_kw)
|
| 37 |
+
|
| 38 |
+
keywords_batch = cleaned_keywords_batch
|
| 39 |
+
|
| 40 |
+
result = {}
|
| 41 |
+
max_retries = 3
|
| 42 |
+
retry_count = 0
|
| 43 |
+
|
| 44 |
+
while retry_count < max_retries:
|
| 45 |
+
try:
|
| 46 |
+
# ์์ฐจ์ ์ผ๋ก API ์ค์ ๊ฐ์ ธ์ค๊ธฐ (๋ฐฐ์น๋ง๋ค ํ ๋ฒ๋ง ํธ์ถ)
|
| 47 |
+
api_config = api_utils.get_next_api_config()
|
| 48 |
+
API_KEY = api_config["API_KEY"]
|
| 49 |
+
SECRET_KEY = api_config["SECRET_KEY"]
|
| 50 |
+
CUSTOMER_ID_STR = api_config["CUSTOMER_ID"]
|
| 51 |
+
|
| 52 |
+
logger.debug(f"=== ํ๊ฒฝ ๋ณ์ ์ฒดํฌ (์๋ #{retry_count+1}) ===")
|
| 53 |
+
logger.info(f"๋ฐฐ์น ํฌ๊ธฐ: {len(keywords_batch)}๊ฐ ํค์๋")
|
| 54 |
+
|
| 55 |
+
# API ์ค์ ์ ํจ์ฑ ๊ฒ์ฌ
|
| 56 |
+
is_valid, message = api_utils.validate_api_config(api_config)
|
| 57 |
+
if not is_valid:
|
| 58 |
+
logger.error(f"โ {message}")
|
| 59 |
+
retry_count += 1
|
| 60 |
+
exponential_backoff_sleep(retry_count)
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
# CUSTOMER_ID๋ฅผ ์ ์๋ก ๋ณํ
|
| 64 |
+
try:
|
| 65 |
+
CUSTOMER_ID = int(CUSTOMER_ID_STR)
|
| 66 |
+
except ValueError:
|
| 67 |
+
logger.error(f"โ CUSTOMER_ID ๋ณํ ์ค๋ฅ: '{CUSTOMER_ID_STR}'๋ ์ ํจํ ์ซ์๊ฐ ์๋๋๋ค.")
|
| 68 |
+
retry_count += 1
|
| 69 |
+
exponential_backoff_sleep(retry_count)
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
BASE_URL = "https://api.naver.com"
|
| 73 |
+
uri = "/keywordstool"
|
| 74 |
+
method = "GET"
|
| 75 |
+
headers = api_utils.get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
|
| 76 |
+
|
| 77 |
+
# ํค์๋ ๋ฐฐ์น๋ฅผ ํ ๋ฒ์ API๋ก ์ ์ก
|
| 78 |
+
params = {
|
| 79 |
+
"hintKeywords": keywords_batch,
|
| 80 |
+
"showDetail": "1"
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
logger.debug(f"์์ฒญ ํ๋ผ๋ฏธํฐ: {len(keywords_batch)}๊ฐ ํค์๋")
|
| 84 |
+
|
| 85 |
+
# API ํธ์ถ
|
| 86 |
+
response = requests.get(BASE_URL + uri, params=params, headers=headers, timeout=10)
|
| 87 |
+
|
| 88 |
+
logger.debug(f"์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
| 89 |
+
|
| 90 |
+
if response.status_code != 200:
|
| 91 |
+
logger.error(f"โ API ์ค๋ฅ ์๋ต (์๋ #{retry_count+1}):")
|
| 92 |
+
logger.error(f" ๋ณธ๋ฌธ: {response.text}")
|
| 93 |
+
retry_count += 1
|
| 94 |
+
exponential_backoff_sleep(retry_count)
|
| 95 |
+
continue
|
| 96 |
+
|
| 97 |
+
# ์๋ต ๋ฐ์ดํฐ ํ์ฑ
|
| 98 |
+
result_data = response.json()
|
| 99 |
+
|
| 100 |
+
logger.debug(f"์๋ต ๋ฐ์ดํฐ ๊ตฌ์กฐ:")
|
| 101 |
+
logger.debug(f" ํ์
: {type(result_data)}")
|
| 102 |
+
logger.debug(f" ํค๋ค: {result_data.keys() if isinstance(result_data, dict) else 'N/A'}")
|
| 103 |
+
|
| 104 |
+
if isinstance(result_data, dict) and "keywordList" in result_data:
|
| 105 |
+
logger.debug(f" keywordList ๊ธธ์ด: {len(result_data['keywordList'])}")
|
| 106 |
+
|
| 107 |
+
# ๋ฐฐ์น ๋ด ๊ฐ ํค์๋์ ๋งค์นญ
|
| 108 |
+
for keyword in keywords_batch:
|
| 109 |
+
found = False
|
| 110 |
+
for item in result_data["keywordList"]:
|
| 111 |
+
rel_keyword = item.get("relKeyword", "")
|
| 112 |
+
if rel_keyword == keyword:
|
| 113 |
+
pc_count = item.get("monthlyPcQcCnt", 0)
|
| 114 |
+
mobile_count = item.get("monthlyMobileQcCnt", 0)
|
| 115 |
+
|
| 116 |
+
# ์ซ์ ๋ณํ
|
| 117 |
+
try:
|
| 118 |
+
if isinstance(pc_count, str):
|
| 119 |
+
pc_count_converted = int(pc_count.replace(",", ""))
|
| 120 |
+
else:
|
| 121 |
+
pc_count_converted = int(pc_count)
|
| 122 |
+
except:
|
| 123 |
+
pc_count_converted = 0
|
| 124 |
+
|
| 125 |
+
try:
|
| 126 |
+
if isinstance(mobile_count, str):
|
| 127 |
+
mobile_count_converted = int(mobile_count.replace(",", ""))
|
| 128 |
+
else:
|
| 129 |
+
mobile_count_converted = int(mobile_count)
|
| 130 |
+
except:
|
| 131 |
+
mobile_count_converted = 0
|
| 132 |
+
|
| 133 |
+
total_count = pc_count_converted + mobile_count_converted
|
| 134 |
+
|
| 135 |
+
result[keyword] = {
|
| 136 |
+
"PC๊ฒ์๋": pc_count_converted,
|
| 137 |
+
"๋ชจ๋ฐ์ผ๊ฒ์๋": mobile_count_converted,
|
| 138 |
+
"์ด๊ฒ์๋": total_count
|
| 139 |
+
}
|
| 140 |
+
logger.debug(f"โ
'{keyword}': PC={pc_count_converted}, Mobile={mobile_count_converted}, Total={total_count}")
|
| 141 |
+
found = True
|
| 142 |
+
break
|
| 143 |
+
|
| 144 |
+
if not found:
|
| 145 |
+
logger.warning(f"โ '{keyword}': ๋งค์นญ๋๋ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ์ ์์")
|
| 146 |
+
|
| 147 |
+
# ์ฑ๊ณต์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผ๋ฏ๋ก ๋ฃจํ ์ข
๋ฃ
|
| 148 |
+
break
|
| 149 |
+
else:
|
| 150 |
+
logger.error(f"โ keywordList๊ฐ ์์ (์๋ #{retry_count+1})")
|
| 151 |
+
logger.error(f"์ ์ฒด ์๋ต: {result_data}")
|
| 152 |
+
retry_count += 1
|
| 153 |
+
exponential_backoff_sleep(retry_count)
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
logger.error(f"โ ๋ฐฐ์น ์ฒ๋ฆฌ ์ค ์ค๋ฅ (์๋ #{retry_count+1}): {str(e)}")
|
| 157 |
+
import traceback
|
| 158 |
+
logger.error(traceback.format_exc())
|
| 159 |
+
retry_count += 1
|
| 160 |
+
exponential_backoff_sleep(retry_count)
|
| 161 |
+
|
| 162 |
+
logger.info(f"\n=== ๋ฐฐ์น ์ฒ๋ฆฌ ์๋ฃ ===")
|
| 163 |
+
logger.info(f"์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌ๋ ํค์๋ ์: {len(result)}")
|
| 164 |
+
|
| 165 |
+
return result
|
| 166 |
+
|
| 167 |
+
def fetch_all_search_volumes(keywords, batch_size=5):
|
| 168 |
+
"""ํค์๋ ๋ฆฌ์คํธ์ ๋ํ ๋ค์ด๋ฒ ๊ฒ์๋ ๋ณ๋ ฌ ์กฐํ"""
|
| 169 |
+
results = {}
|
| 170 |
+
batches = []
|
| 171 |
+
|
| 172 |
+
# ํค์๋๋ฅผ 5๊ฐ์ฉ ๋ฌถ์ด์ ๋ฐฐ์น ์์ฑ
|
| 173 |
+
for i in range(0, len(keywords), batch_size):
|
| 174 |
+
batch = keywords[i:i + batch_size]
|
| 175 |
+
batches.append(batch)
|
| 176 |
+
|
| 177 |
+
logger.info(f"์ด {len(batches)}๊ฐ ๋ฐฐ์น๋ก {len(keywords)}๊ฐ ํค์๋ ์ฒ๋ฆฌ ์คโฆ")
|
| 178 |
+
logger.info(f"๋ฐฐ์น ํฌ๊ธฐ: {batch_size}, ๋ณ๋ ฌ ์์ปค: 3๊ฐ, API ๊ณ์ : {len(api_utils.NAVER_API_CONFIGS)}๊ฐ ์์ฐจ ์ฌ์ฉ")
|
| 179 |
+
|
| 180 |
+
with ThreadPoolExecutor(max_workers=3) as executor: # ์์ปค ์ ์ ํ
|
| 181 |
+
futures = {executor.submit(fetch_search_volume_batch, batch): batch for batch in batches}
|
| 182 |
+
for future in as_completed(futures):
|
| 183 |
+
batch = futures[future]
|
| 184 |
+
try:
|
| 185 |
+
batch_results = future.result()
|
| 186 |
+
results.update(batch_results)
|
| 187 |
+
logger.info(f"๋ฐฐ์น ์ฒ๋ฆฌ ์๋ฃ: {len(batch)}๊ฐ ํค์๋ (์ฑ๊ณต: {len(batch_results)}๊ฐ)")
|
| 188 |
+
except Exception as e:
|
| 189 |
+
logger.error(f"๋ฐฐ์น ์ฒ๋ฆฌ ์ค๋ฅ: {e}")
|
| 190 |
+
# API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง๋ฅผ ์ํ ์ง์ ๋ฐฑ์คํ ์ฌ์ฉ
|
| 191 |
+
exponential_backoff_sleep(0) # ์ด๊ธฐ ์ง์ฐ ์ ์ฉ
|
| 192 |
+
|
| 193 |
+
logger.info(f"๊ฒ์๋ ์กฐํ ์๋ฃ: {len(results)}๊ฐ ํค์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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
์ํ ๊ฒ์ ๊ด๋ จ ๊ธฐ๋ฅ - ๋ฉ์ธํค์๋ ์๋ค ์กฐํฉ๋ง ์์ฑํ๋๋ก ์์
|
| 3 |
+
- ๋ค์ด๋ฒ ์ผํ API๋ฅผ ํตํ ์ํ ๊ฒ์
|
| 4 |
+
- ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import requests
|
| 8 |
+
import time
|
| 9 |
+
import re
|
| 10 |
+
import random
|
| 11 |
+
from collections import defaultdict, Counter
|
| 12 |
+
import api_utils
|
| 13 |
+
import text_utils
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
# ๋ก๊น
์ค์
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
logger.setLevel(logging.INFO)
|
| 19 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 20 |
+
handler = logging.StreamHandler()
|
| 21 |
+
handler.setFormatter(formatter)
|
| 22 |
+
logger.addHandler(handler)
|
| 23 |
+
|
| 24 |
+
# ๋ชจ๋ ๋ ๋ฒจ์์ Gemini ๋ชจ๋ธ ์ด๊ธฐํ (์ฑ๊ธํค ํจํด)
|
| 25 |
+
gemini_model = None
|
| 26 |
+
try:
|
| 27 |
+
gemini_model = text_utils.get_gemini_model()
|
| 28 |
+
logger.info("Gemini ๋ชจ๋ธ ์ด๊ธฐํ ์ฑ๊ณต")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Gemini ๋ชจ๋ธ ์ด๊ธฐํ ์คํจ: {e}")
|
| 31 |
+
|
| 32 |
+
def exponential_backoff_sleep(retry_count, base_delay=0.3, max_delay=5.0):
|
| 33 |
+
"""์ง์ ๋ฐฑ์คํ ๋ฐฉ์์ ๋๊ธฐ ์๊ฐ ๊ณ์ฐ"""
|
| 34 |
+
delay = min(base_delay * (2 ** retry_count), max_delay)
|
| 35 |
+
# ์ฝ๊ฐ์ ๋๋ค์ฑ ์ถ๊ฐ (์งํฐ)
|
| 36 |
+
jitter = random.uniform(0, 0.5) * delay
|
| 37 |
+
time.sleep(delay + jitter)
|
| 38 |
+
|
| 39 |
+
def extract_keywords_with_dedup(titles):
|
| 40 |
+
"""
|
| 41 |
+
์ํ๋ช
์์ ํค์๋๋ฅผ ์ถ์ถํ๊ณ ์ฆ์ ์ค๋ณต์ ์ ๊ฑฐํ๋ ํจ์
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
titles (list): ์ํ๋ช
๋ชฉ๋ก
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
list: ์ค๋ณต์ด ์ ๊ฑฐ๋ ํค์๋ ๋ชฉ๋ก
|
| 48 |
+
"""
|
| 49 |
+
all_words = []
|
| 50 |
+
|
| 51 |
+
for title in titles:
|
| 52 |
+
# ์ํ๋ช
์์ ํค์๋ ์ถ์ถ (๊ณต๋ฐฑ๊ณผ ์ผํ ๊ธฐ์ค)
|
| 53 |
+
words = re.split(r'[,\s]+', title)
|
| 54 |
+
all_words.extend(words)
|
| 55 |
+
|
| 56 |
+
# ๋น ๋ฌธ์์ด ์ ๊ฑฐ ๋ฐ ์ ๋ฆฌ
|
| 57 |
+
all_words = [word.strip() for word in all_words if word.strip() and len(word.strip()) >= 2]
|
| 58 |
+
|
| 59 |
+
# ์ค๋ณต ์ ๊ฑฐ - ์ ๊ทํ ๊ธฐ๋ฐ (์คํ์ด์ค ์ฐจ์ด ๋ฌด์)
|
| 60 |
+
normalized_dict = {}
|
| 61 |
+
|
| 62 |
+
for word in all_words:
|
| 63 |
+
# ์ ๊ทํ: ์๋ฌธ์ ๋ณํ, ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 64 |
+
normalized = word.lower().replace(" ", "")
|
| 65 |
+
|
| 66 |
+
# ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ, ๊ฐ๋
์ฑ์ด ์ข์ ๋ฒ์ (๊ณต๋ฐฑ ์๋) ์ ํ
|
| 67 |
+
if normalized in normalized_dict:
|
| 68 |
+
existing = normalized_dict[normalized]
|
| 69 |
+
# ๊ณต๋ฐฑ์ด ์๋ ๋ฒ์ ์ ํธ
|
| 70 |
+
if " " in word and " " not in existing:
|
| 71 |
+
normalized_dict[normalized] = word
|
| 72 |
+
else:
|
| 73 |
+
normalized_dict[normalized] = word
|
| 74 |
+
|
| 75 |
+
# ์ ๊ทํ๋ ๋์
๋๋ฆฌ์์ ์๋ณธ ํํ ์ถ์ถ
|
| 76 |
+
return list(normalized_dict.values())
|
| 77 |
+
|
| 78 |
+
def fetch_naver_shopping_data(keyword, korean_only=True, apply_main_keyword=True, exclude_zero_volume=True):
|
| 79 |
+
"""
|
| 80 |
+
๋ค์ด๋ฒ ์ผํ API๋ฅผ ํตํด ์ํ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ - ๋ฉ์ธํค์๋ ์๋ค ์กฐํฉ๋ง ์์ฑ
|
| 81 |
+
|
| 82 |
+
Args:
|
| 83 |
+
keyword (str): ๊ฒ์ ํค์๋
|
| 84 |
+
korean_only (bool): ํ๊ธ๋ง ์ถ์ถ ์ฌ๋ถ
|
| 85 |
+
apply_main_keyword (bool): ๋ฉ์ธํค์๋ ์ ์ฉ ์ฌ๋ถ
|
| 86 |
+
exclude_zero_volume (bool): ๊ฒ์๋์ด 0์ธ ํค์๋ ์ ์ธ ์ฌ๋ถ
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
dict: ๊ฒ์ ๊ฒฐ๊ณผ ๋ฐ์ดํฐ
|
| 90 |
+
"""
|
| 91 |
+
global gemini_model
|
| 92 |
+
|
| 93 |
+
# 1. ์คํ์ด์ค๋ฐ ์ ๊ฑฐ ๊ฐ์ - ์
๋ ฅ ํค์๋ ์ ์ฒ๋ฆฌ
|
| 94 |
+
cleaned_keyword = keyword.strip().replace(" ", "") if keyword else ""
|
| 95 |
+
|
| 96 |
+
# ๋ก๊ทธ ์ถ๊ฐ - ํจ์ ์์
|
| 97 |
+
logger.info(f"===== ํค์๋ ๊ฒ์ ์์ =====")
|
| 98 |
+
logger.info(f"๊ฒ์ ํค์๋: '{keyword}'")
|
| 99 |
+
logger.info(f"์ต์
: ํ๊ธ๋ง ์ถ์ถ={korean_only}, ๋ฉ์ธํค์๋ ์ ์ฉ={apply_main_keyword}")
|
| 100 |
+
logger.info(f"Gemini ๋ชจ๋ธ ์ํ: {'์ฌ์ฉ ๊ฐ๋ฅ' if gemini_model else '์ฌ์ฉ ๋ถ๊ฐ'}")
|
| 101 |
+
|
| 102 |
+
# ๊ณต๋ฐฑ ์ ๊ฑฐํ ํค์๋๋ก API ํธ์ถ
|
| 103 |
+
api_keyword = cleaned_keyword
|
| 104 |
+
logger.info(f"API ํธ์ถ ํค์๋: '{api_keyword}'")
|
| 105 |
+
|
| 106 |
+
# ๋ค์ด๋ฒ ์ผํ API ํธ์ถ
|
| 107 |
+
all_products = []
|
| 108 |
+
total_count = 0
|
| 109 |
+
|
| 110 |
+
if apply_main_keyword:
|
| 111 |
+
# ๋ฉ์ธ ํค์๋ ์ ์ฉ ์ ํ ๋ฒ์ 100๊ฐ ์ํ ๊ฐ์ ธ์ค๊ธฐ
|
| 112 |
+
logger.info("๋ฉ์ธ ํค์๋ ์ ์ฉ - ํ ๋ฒ์ 100๊ฐ ์ํ ๊ฐ์ ธ์ค๊ธฐ")
|
| 113 |
+
result = fetch_products_by_keyword(api_keyword, page=1, display=100)
|
| 114 |
+
if result["status"] == "success" and result["products"]:
|
| 115 |
+
all_products.extend(result["products"])
|
| 116 |
+
total_count = result.get("total", 0)
|
| 117 |
+
logger.info(f"ํ ๋ฒ์ {len(result['products'])}๊ฐ ์ํ ๊ฒ์๋จ")
|
| 118 |
+
else:
|
| 119 |
+
# ๋ฉ์ธ ํค์๋ ๋ฏธ์ ์ฉ ์ ํ์ด์ง๋น 10๊ฐ์ฉ ์ต๋ 10ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ
|
| 120 |
+
logger.info("๋ฉ์ธ ํค์๋ ๋ฏธ์ ์ฉ - ํ์ด์ง๋น 10๊ฐ์ฉ ๊ฐ์ ธ์ค๊ธฐ")
|
| 121 |
+
for page in range(1, 11): # 1~10 ํ์ด์ง
|
| 122 |
+
result = fetch_products_by_keyword(api_keyword, page=page, display=100)
|
| 123 |
+
if result["status"] == "success" and result["products"]:
|
| 124 |
+
all_products.extend(result["products"])
|
| 125 |
+
total_count = result.get("total", 0)
|
| 126 |
+
logger.info(f"ํ์ด์ง {page}: {len(result['products'])}๊ฐ ์ํ ๊ฒ์๋จ")
|
| 127 |
+
else:
|
| 128 |
+
logger.info(f"ํ์ด์ง {page}: ์ํ ๊ฒ์ ์คํจ ๋๋ ๊ฒฐ๊ณผ ์์")
|
| 129 |
+
|
| 130 |
+
# API ๋ ์ดํธ ๋ฆฌ๋ฐ ๋ฐฉ์ง๋ฅผ ์ํ ์งง์ ๋๊ธฐ ์๊ฐ
|
| 131 |
+
exponential_backoff_sleep(0) # ์ด๊ธฐ ์ง์ฐ
|
| 132 |
+
|
| 133 |
+
# ์ด๋ฏธ ์ถฉ๋ถํ ์ํ์ ๊ฐ์ ธ์๊ฑฐ๋ ๋ ์ด์ ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด ์ค๋จ
|
| 134 |
+
if len(all_products) >= 100 or (result["status"] == "success" and len(result["products"]) < 10):
|
| 135 |
+
break
|
| 136 |
+
|
| 137 |
+
if not all_products:
|
| 138 |
+
logger.warning("๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.")
|
| 139 |
+
return {
|
| 140 |
+
"product_list": [],
|
| 141 |
+
"combo_candidates": {},
|
| 142 |
+
"category_counter": {},
|
| 143 |
+
"keyword_indices": {},
|
| 144 |
+
"keyword_pairs": {}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ: ์ด {len(all_products)}๊ฐ์ ์ํ์ ์ฐพ์์ต๋๋ค. (์ ์ฒด ๊ฒ์ ๊ฒฐ๊ณผ: {total_count}๊ฐ)")
|
| 148 |
+
|
| 149 |
+
# ์ํ ๋ฐ์ดํฐ์์ ํค์๋ ์ถ์ถ
|
| 150 |
+
product_list = []
|
| 151 |
+
combo_candidates = {}
|
| 152 |
+
category_counter = {}
|
| 153 |
+
keyword_indices = {}
|
| 154 |
+
keyword_pairs = {} # ์๋ค ์กฐํฉ ์ ๋ณด ์ ์ฅ
|
| 155 |
+
|
| 156 |
+
all_extracted_keywords = set() # ๋ชจ๋ ์ถ์ถ๋ ํค์๋ ์ถ์
|
| 157 |
+
|
| 158 |
+
# ๊ฐ ์ํ ์ฒ๋ฆฌ
|
| 159 |
+
for idx, product in enumerate(all_products):
|
| 160 |
+
# ์ํ ์ ๋ณด ์ถ์ถ
|
| 161 |
+
title = product.get("์ํ๋ช
", "")
|
| 162 |
+
category = product.get("์นดํ
๊ณ ๋ฆฌ", "")
|
| 163 |
+
|
| 164 |
+
logger.debug(f"\n์ํ #{idx+1}: '{title}' (์นดํ
๊ณ ๋ฆฌ: {category})")
|
| 165 |
+
|
| 166 |
+
# ์ํ๋ช
์์ ํค์๋ ์ถ์ถ - 1๊ธ์ ์ด์ ๋จ์ด๋ ํฌํจํ๋๋ก ๋ณ๊ฒฝ
|
| 167 |
+
keywords = text_utils.clean_and_split(title, only_korean=korean_only)
|
| 168 |
+
logger.debug(f" - ์ถ์ถ๋ ํค์๋ ({len(keywords)}๊ฐ): {keywords}")
|
| 169 |
+
|
| 170 |
+
# ๋ชจ๋ ์ถ์ถ๋ ํค์๋ ์ถ์
|
| 171 |
+
all_extracted_keywords.update(keywords)
|
| 172 |
+
|
| 173 |
+
# ํค์๋ ์์น ๊ธฐ๋ก
|
| 174 |
+
for kw in keywords:
|
| 175 |
+
if kw not in keyword_indices:
|
| 176 |
+
keyword_indices[kw] = []
|
| 177 |
+
if idx not in keyword_indices[kw]:
|
| 178 |
+
keyword_indices[kw].append(idx)
|
| 179 |
+
|
| 180 |
+
# ์นดํ
๊ณ ๋ฆฌ ์นด์ดํฐ ์
๋ฐ์ดํธ
|
| 181 |
+
if category not in category_counter:
|
| 182 |
+
category_counter[category] = 0
|
| 183 |
+
category_counter[category] += 1
|
| 184 |
+
|
| 185 |
+
# ์ํ ์ ๋ณด ์ ์ฅ
|
| 186 |
+
product_list.append(product)
|
| 187 |
+
|
| 188 |
+
logger.info(f"\n์ด ์ถ์ถ๋ ๊ณ ์ ํค์๋: {len(all_extracted_keywords)}๊ฐ")
|
| 189 |
+
logger.debug(f"์ถ์ถ๋ ๊ณ ์ ํค์๋ ๋ชฉ๋ก: {sorted(list(all_extracted_keywords))}")
|
| 190 |
+
|
| 191 |
+
# === ์์ ๋ ๋ถ๋ถ: ๋ฉ์ธํค์๋ ์๋ค ์กฐํฉ๋ง ์์ฑ ===
|
| 192 |
+
if apply_main_keyword:
|
| 193 |
+
logger.info(f"\n๋ฉ์ธ ํค์๋ '{keyword}'๋ก ์๋ค ์กฐํฉ๋ง ์์ฑ:")
|
| 194 |
+
combo_count = 0
|
| 195 |
+
|
| 196 |
+
# ๋ฉ์ธ ํค์๋ ์์ฒด๋ ์ถ๊ฐ
|
| 197 |
+
combo_candidates[keyword] = set([category for category in category_counter.keys()])
|
| 198 |
+
logger.info(f" - ๋ฉ์ธ ํค์๋ ์ถ๊ฐ: '{keyword}'")
|
| 199 |
+
|
| 200 |
+
# ๋ฉ์ธ ํค์๋์ ์๋ค ์กฐํฉ ์์ฑ
|
| 201 |
+
main_keyword = keyword.strip()
|
| 202 |
+
|
| 203 |
+
for kw in all_extracted_keywords:
|
| 204 |
+
if kw == main_keyword:
|
| 205 |
+
continue
|
| 206 |
+
|
| 207 |
+
# ๋ฉ์ธ ํค์๋๊ฐ ์ด๋ฏธ ํฌํจ๋ ๊ฒฝ์ฐ๋ ๊ทธ๋๋ก ์ถ๊ฐ
|
| 208 |
+
if main_keyword in kw:
|
| 209 |
+
kw_api = kw.replace(" ", "")
|
| 210 |
+
if kw_api not in combo_candidates:
|
| 211 |
+
combo_candidates[kw_api] = set()
|
| 212 |
+
for cat in category_counter.keys():
|
| 213 |
+
combo_candidates[kw_api].add(cat)
|
| 214 |
+
logger.info(f" - ๋ฉ์ธ ํค์๋ ํฌํจ ๋จ์ด ์ถ๊ฐ: '{kw}' (API: '{kw_api}')")
|
| 215 |
+
combo_count += 1
|
| 216 |
+
continue
|
| 217 |
+
|
| 218 |
+
# ์๋ค ์กฐํฉ ์์ฑ
|
| 219 |
+
front_combo = f"{kw} {main_keyword}" # ํค์๋ + ๋ฉ์ธํค์๋
|
| 220 |
+
back_combo = f"{main_keyword} {kw}" # ๋ฉ์ธํค์๋ + ํค์๋
|
| 221 |
+
|
| 222 |
+
# ์๋ค ์กฐํฉ ์ ๋ณด ์ ์ฅ
|
| 223 |
+
keyword_pairs[kw] = {
|
| 224 |
+
"front": front_combo,
|
| 225 |
+
"back": back_combo
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
# API ํธ์ถ์ ์ํ ๊ณต๋ฐฑ ์๋ ๋ฒ์
|
| 229 |
+
front_api = front_combo.replace(" ", "")
|
| 230 |
+
back_api = back_combo.replace(" ", "")
|
| 231 |
+
|
| 232 |
+
logger.debug(f" - ์กฐํฉ ์์ฑ: '{front_combo}' (API: '{front_api}') / '{back_combo}' (API: '{back_api}')")
|
| 233 |
+
|
| 234 |
+
# ์ฒซ ๋ฒ์งธ ์กฐํฉ ์ฒ๋ฆฌ
|
| 235 |
+
if front_api not in combo_candidates:
|
| 236 |
+
combo_candidates[front_api] = set()
|
| 237 |
+
for cat in category_counter.keys():
|
| 238 |
+
combo_candidates[front_api].add(cat)
|
| 239 |
+
combo_count += 1
|
| 240 |
+
|
| 241 |
+
# ๋ ๋ฒ์งธ ์กฐํฉ ์ฒ๋ฆฌ
|
| 242 |
+
if back_api not in combo_candidates:
|
| 243 |
+
combo_candidates[back_api] = set()
|
| 244 |
+
for cat in category_counter.keys():
|
| 245 |
+
combo_candidates[back_api].add(cat)
|
| 246 |
+
combo_count += 1
|
| 247 |
+
|
| 248 |
+
logger.info(f"์กฐํฉ ์์ฑ ์๋ฃ: ์ด {combo_count}๊ฐ ์กฐํฉ ์์ฑ")
|
| 249 |
+
else:
|
| 250 |
+
logger.info("\n๋ฉ์ธ ํค์๋ ์ ์ฉ ์ํจ - ๊ฐ๋ณ ํค์๋๋ง ์ฌ์ฉ")
|
| 251 |
+
for kw in all_extracted_keywords:
|
| 252 |
+
kw_api = kw.replace(" ", "")
|
| 253 |
+
if kw_api not in combo_candidates:
|
| 254 |
+
combo_candidates[kw_api] = set()
|
| 255 |
+
for cat in category_counter.keys():
|
| 256 |
+
combo_candidates[kw_api].add(cat)
|
| 257 |
+
|
| 258 |
+
logger.info(f"\n์ต์ข
์กฐํฉ ํค์๋ ์: {len(combo_candidates)}๊ฐ")
|
| 259 |
+
logger.debug(f"์กฐํฉ ํค์๋ ์ํ(์ต๋ 10๊ฐ): {list(combo_candidates.keys())[:10]}")
|
| 260 |
+
logger.info("===== ํค์๋ ๊ฒ์ ์๋ฃ =====\n")
|
| 261 |
+
|
| 262 |
+
return {
|
| 263 |
+
"product_list": product_list,
|
| 264 |
+
"combo_candidates": combo_candidates,
|
| 265 |
+
"category_counter": category_counter,
|
| 266 |
+
"keyword_indices": keyword_indices,
|
| 267 |
+
"keyword_pairs": keyword_pairs # ์๋ค ์กฐํฉ ์ ๋ณด ์ถ๊ฐ
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
def fetch_products_by_keyword(keyword, page=1, display=10, max_retries=3):
|
| 271 |
+
"""
|
| 272 |
+
๋ค์ด๋ฒ ์ผํ API๋ฅผ ํตํด ํค์๋๋ก ์ํ ๊ฒ์
|
| 273 |
+
|
| 274 |
+
Args:
|
| 275 |
+
keyword (str): ๊ฒ์ ํค์๋
|
| 276 |
+
page (int): ํ์ด์ง ๋ฒํธ
|
| 277 |
+
display (int): ํ ํ์ด์ง์ ํ์ํ ์ํ ์ (์ต๋ 100)
|
| 278 |
+
max_retries (int): ์ต๋ ์ฌ์๋ ํ์
|
| 279 |
+
|
| 280 |
+
Returns:
|
| 281 |
+
dict: ๊ฒ์ ๊ฒฐ๊ณผ
|
| 282 |
+
"""
|
| 283 |
+
# ์ํ ์๊ฐ 100์ ์ด๊ณผํ์ง ์๋๋ก ์ ํ
|
| 284 |
+
if display > 100:
|
| 285 |
+
display = 100
|
| 286 |
+
logger.info(f"์ํ ํ์ ์ 100์ผ๋ก ์ ํ๋จ")
|
| 287 |
+
|
| 288 |
+
retry_count = 0
|
| 289 |
+
while retry_count < max_retries:
|
| 290 |
+
try:
|
| 291 |
+
# API ์ค์ ๊ฐ์ ธ์ค๊ธฐ
|
| 292 |
+
api_config = api_utils.get_next_shopping_api_config()
|
| 293 |
+
|
| 294 |
+
# ์์ฒญ ํค๋
|
| 295 |
+
headers = {
|
| 296 |
+
'X-Naver-Client-Id': api_config["CLIENT_ID"],
|
| 297 |
+
'X-Naver-Client-Secret': api_config["CLIENT_SECRET"]
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
# ์์ฒญ URL (์คํ์ด์ค ์์ด ํธ์ถ)
|
| 301 |
+
url = f"https://openapi.naver.com/v1/search/shop.json?query={keyword}&display={display}&start={(page-1)*display+1}"
|
| 302 |
+
|
| 303 |
+
logger.debug(f"API ์์ฒญ: page={page}, display={display}, ์์ ์์น={(page-1)*display+1}")
|
| 304 |
+
response = requests.get(url, headers=headers, timeout=10) # ํ์์์ ์ค์
|
| 305 |
+
|
| 306 |
+
if response.status_code == 200:
|
| 307 |
+
result = response.json()
|
| 308 |
+
|
| 309 |
+
# ์ํ ์ ๋ณด ์ถ์ถ
|
| 310 |
+
products = []
|
| 311 |
+
|
| 312 |
+
for item in result.get("items", []):
|
| 313 |
+
title = item.get("title", "").replace("<b>", "").replace("</b>", "")
|
| 314 |
+
link = item.get("link", "")
|
| 315 |
+
image = item.get("image", "")
|
| 316 |
+
lprice = item.get("lprice", "0")
|
| 317 |
+
mall_name = item.get("mallName", "")
|
| 318 |
+
|
| 319 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์ถ์ถ
|
| 320 |
+
category1 = item.get("category1", "")
|
| 321 |
+
category2 = item.get("category2", "")
|
| 322 |
+
category3 = item.get("category3", "")
|
| 323 |
+
category4 = item.get("category4", "")
|
| 324 |
+
|
| 325 |
+
# ์ ์ฒด ์นดํ
๊ณ ๋ฆฌ ๊ฒฝ๋ก ์์ฑ
|
| 326 |
+
category_path = " > ".join([c for c in [category1, category2, category3, category4] if c])
|
| 327 |
+
|
| 328 |
+
products.append({
|
| 329 |
+
"์ํ๋ช
": title,
|
| 330 |
+
"๊ฐ๊ฒฉ": lprice,
|
| 331 |
+
"์ผํ๋ชฐ": mall_name,
|
| 332 |
+
"์นดํ
๊ณ ๋ฆฌ": category_path if category_path else category1
|
| 333 |
+
})
|
| 334 |
+
|
| 335 |
+
logger.info(f"API ์๋ต: {len(products)}๊ฐ ์ํ ๊ฒ์๋จ (์ ์ฒด ๊ฒฐ๊ณผ์: {result.get('total', 0)})")
|
| 336 |
+
return {
|
| 337 |
+
"status": "success",
|
| 338 |
+
"products": products,
|
| 339 |
+
"total": result.get("total", 0)
|
| 340 |
+
}
|
| 341 |
+
else:
|
| 342 |
+
error_msg = f"API ์ค๋ฅ: {response.status_code} - {response.text}"
|
| 343 |
+
logger.warning(error_msg)
|
| 344 |
+
retry_count += 1
|
| 345 |
+
exponential_backoff_sleep(retry_count)
|
| 346 |
+
|
| 347 |
+
except Exception as e:
|
| 348 |
+
error_msg = f"์ํ ๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์: {e}"
|
| 349 |
+
logger.error(error_msg)
|
| 350 |
+
retry_count += 1
|
| 351 |
+
exponential_backoff_sleep(retry_count)
|
| 352 |
+
|
| 353 |
+
# ์ต๋ ์ฌ์๋ ํ์๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ
|
| 354 |
+
logger.error(f"์ต๋ ์ฌ์๋ ํ์({max_retries})๋ฅผ ์ด๊ณผํ์ฌ ๋น ๊ฒฐ๊ณผ ๋ฐํ")
|
| 355 |
+
return {
|
| 356 |
+
"status": "error",
|
| 357 |
+
"message": f"์ต๋ ์ฌ์๋ ํ์({max_retries})๋ฅผ ์ด๊ณผํ์ต๋๋ค.",
|
| 358 |
+
"products": []
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
def fetch_naver_shopping_data_for_analysis(keyword, count=10):
|
| 362 |
+
"""
|
| 363 |
+
๋ค์ด๋ฒ ์ผํ API๋ฅผ ํตํด ํค์๋๋ก ์ํ ๊ฒ์ (๋ถ์์ฉ, ์คํ์ด์ค ์์ด ํธ์ถ)
|
| 364 |
+
|
| 365 |
+
Args:
|
| 366 |
+
keyword (str): ๊ฒ์ ํค์๋
|
| 367 |
+
count (int): ๊ฒ์ํ ์ํ ์
|
| 368 |
+
|
| 369 |
+
Returns:
|
| 370 |
+
list: ๊ฒ์๋ ์ํ ์ ๏ฟฝ๏ฟฝ ๋ชฉ๋ก
|
| 371 |
+
"""
|
| 372 |
+
# ์คํ์ด์ค ์ ๊ฑฐ
|
| 373 |
+
api_keyword = keyword.replace(" ", "")
|
| 374 |
+
|
| 375 |
+
# ์ต๋ ์ฌ์๋ ํ์
|
| 376 |
+
max_retries = 3
|
| 377 |
+
retry_count = 0
|
| 378 |
+
|
| 379 |
+
while retry_count < max_retries:
|
| 380 |
+
try:
|
| 381 |
+
# API ์ค์ ๊ฐ์ ธ์ค๊ธฐ
|
| 382 |
+
api_config = api_utils.get_next_shopping_api_config()
|
| 383 |
+
|
| 384 |
+
# ์์ฒญ ํค๋
|
| 385 |
+
headers = {
|
| 386 |
+
'X-Naver-Client-Id': api_config["CLIENT_ID"],
|
| 387 |
+
'X-Naver-Client-Secret': api_config["CLIENT_SECRET"]
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
# ์์ฒญ URL
|
| 391 |
+
url = f"https://openapi.naver.com/v1/search/shop.json?query={api_keyword}&display={count}"
|
| 392 |
+
|
| 393 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 394 |
+
|
| 395 |
+
if response.status_code == 200:
|
| 396 |
+
result = response.json()
|
| 397 |
+
|
| 398 |
+
# ์ํ ์ ๋ณด ์ถ์ถ
|
| 399 |
+
products = []
|
| 400 |
+
|
| 401 |
+
for item in result.get("items", []):
|
| 402 |
+
# ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์ถ์ถ ๋ฐ ์ ์ฒด ๊ฒฝ๋ก ์์ฑ
|
| 403 |
+
category1 = item.get("category1", "")
|
| 404 |
+
category2 = item.get("category2", "")
|
| 405 |
+
category3 = item.get("category3", "")
|
| 406 |
+
category4 = item.get("category4", "")
|
| 407 |
+
|
| 408 |
+
# ์ ์ฒด ์นดํ
๊ณ ๋ฆฌ ๊ฒฝ๋ก ์์ฑ (fetch_products_by_keyword์ ์ผ๊ด์ฑ ์ ์ง)
|
| 409 |
+
category_path = " > ".join([c for c in [category1, category2, category3, category4] if c])
|
| 410 |
+
|
| 411 |
+
products.append({
|
| 412 |
+
"title": item.get("title", "").replace("<b>", "").replace("</b>", ""),
|
| 413 |
+
"price": item.get("lprice", "0"),
|
| 414 |
+
"category": category_path if category_path else category1,
|
| 415 |
+
"์นดํ
๊ณ ๋ฆฌ": category_path if category_path else category1 # ํ๊ธ ํค๋ก๋ ์ ์ฅ (ํธํ์ฑ)
|
| 416 |
+
})
|
| 417 |
+
|
| 418 |
+
return products
|
| 419 |
+
else:
|
| 420 |
+
logger.warning(f"API ์ค๋ฅ (์๋ {retry_count+1}/{max_retries}): {response.status_code} - {response.text}")
|
| 421 |
+
retry_count += 1
|
| 422 |
+
exponential_backoff_sleep(retry_count)
|
| 423 |
+
|
| 424 |
+
except Exception as e:
|
| 425 |
+
logger.error(f"์ํ ๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์ (์๋ {retry_count+1}/{max_retries}): {e}")
|
| 426 |
+
retry_count += 1
|
| 427 |
+
exponential_backoff_sleep(retry_count)
|
| 428 |
+
|
| 429 |
+
logger.error(f"์ต๋ ์ฌ์๋ ํ์({max_retries})๋ฅผ ์ด๊ณผํ์ฌ ๋น ๊ฒฐ๊ณผ ๋ฐํ")
|
| 430 |
+
return []
|
requirements.txt
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
pandas
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
requests
|
| 4 |
+
google-generativeai
|
| 5 |
+
xlsxwriter
|
| 6 |
+
beautifulsoup4
|
| 7 |
+
fpdf
|
| 8 |
+
markdown
|
| 9 |
+
plotly
|
style.css
CHANGED
|
@@ -97,11 +97,6 @@ body {
|
|
| 97 |
transition: background-color 0.3s ease, color 0.3s ease;
|
| 98 |
}
|
| 99 |
|
| 100 |
-
/* ํธํฐ ์จ๊น ์ค์ */
|
| 101 |
-
footer {
|
| 102 |
-
visibility: hidden;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
.gradio-container,
|
| 106 |
.gradio-container *,
|
| 107 |
.gr-app,
|
|
@@ -644,4 +639,4 @@ pre,
|
|
| 644 |
.grid-container {
|
| 645 |
grid-template-columns: 1fr;
|
| 646 |
}
|
| 647 |
-
}
|
|
|
|
| 97 |
transition: background-color 0.3s ease, color 0.3s ease;
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
.gradio-container,
|
| 101 |
.gradio-container *,
|
| 102 |
.gr-app,
|
|
|
|
| 639 |
.grid-container {
|
| 640 |
grid-template-columns: 1fr;
|
| 641 |
}
|
| 642 |
+
}
|
text_utils.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ํตํฉ ๊ด๋ฆฌ ์ ์ฉ
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import re
|
| 9 |
+
import google.generativeai as genai
|
| 10 |
+
import os
|
| 11 |
+
import logging
|
| 12 |
+
import api_utils # API ํค ํตํฉ ๊ด๋ฆฌ๋ฅผ ์ํ ์ํฌํธ
|
| 13 |
+
|
| 14 |
+
# ๋ก๊น
์ค์
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
logger.setLevel(logging.INFO)
|
| 17 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 18 |
+
handler = logging.StreamHandler()
|
| 19 |
+
handler.setFormatter(formatter)
|
| 20 |
+
logger.addHandler(handler)
|
| 21 |
+
|
| 22 |
+
# ===== Gemini ๋ชจ๋ธ ๊ด๋ฆฌ ํจ์๋ค =====
|
| 23 |
+
def get_gemini_model():
|
| 24 |
+
"""api_utils์์ Gemini ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ (ํตํฉ ๊ด๋ฆฌ)"""
|
| 25 |
+
try:
|
| 26 |
+
model = api_utils.get_gemini_model()
|
| 27 |
+
if model:
|
| 28 |
+
logger.info("Gemini ๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต (api_utils ํตํฉ ๊ด๋ฆฌ)")
|
| 29 |
+
return model
|
| 30 |
+
else:
|
| 31 |
+
logger.warning("์ฌ์ฉ ๊ฐ๋ฅํ Gemini API ํค๊ฐ ์์ต๋๋ค.")
|
| 32 |
+
return None
|
| 33 |
+
except Exception as e:
|
| 34 |
+
logger.error(f"Gemini ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# ํ
์คํธ ๋ถ๋ฆฌ ๋ฐ ์ ์ ํจ์
|
| 38 |
+
def clean_and_split(text, only_korean=False):
|
| 39 |
+
"""ํ
์คํธ๋ฅผ ๋ถ๋ฆฌํ๊ณ ์ ์ ํ๋ ํจ์"""
|
| 40 |
+
text = re.sub(r"[()\[\]-]", " ", text)
|
| 41 |
+
text = text.replace("/", " ")
|
| 42 |
+
|
| 43 |
+
if only_korean:
|
| 44 |
+
# ํ๊ธ๋ง ์ถ์ถ ์ต์
์ด ์ผ์ง ๊ฒฝ์ฐ
|
| 45 |
+
# ๊ณต๋ฐฑ์ด๋ ์ผํ๋ก ๊ตฌ๋ถํ ๋ค ํ๊ธ๋ง ์ถ์ถ
|
| 46 |
+
words = re.split(r"[ ,]", text)
|
| 47 |
+
cleaned = []
|
| 48 |
+
for word in words:
|
| 49 |
+
word = word.strip()
|
| 50 |
+
# ํ๊ธ๋ง ๋จ๊ธฐ๊ณ ๋ค๋ฅธ ๋ฌธ์๋ ์ ๊ฑฐ
|
| 51 |
+
word = re.sub(r"[^๊ฐ-ํฃ]", "", word)
|
| 52 |
+
if word and len(word) >= 1: # ๋น ๋ฌธ์์ด์ด ์๋๊ณ 1๊ธ์ ์ด์์ธ ๊ฒฝ์ฐ๋ง ์ถ๊ฐ
|
| 53 |
+
cleaned.append(word)
|
| 54 |
+
else:
|
| 55 |
+
# ํ๊ธ๋ง ์ถ์ถ ์ต์
์ด ๊บผ์ง ๊ฒฝ์ฐ - ๋จ์ด ํต์งธ๋ก ์ฒ๋ฆฌ
|
| 56 |
+
# ๊ณต๋ฐฑ๊ณผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ๋จ์ด ์ ์ฒด๋ฅผ ์ ์ง
|
| 57 |
+
words = re.split(r"[,\s]+", text)
|
| 58 |
+
cleaned = []
|
| 59 |
+
for word in words:
|
| 60 |
+
word = word.strip()
|
| 61 |
+
if word and len(word) >= 1: # ๋น ๋ฌธ์์ด์ด ์๋๊ณ 1๊ธ์ ์ด์์ธ ๊ฒฝ์ฐ๋ง ์ถ๊ฐ
|
| 62 |
+
cleaned.append(word)
|
| 63 |
+
|
| 64 |
+
return cleaned
|
| 65 |
+
|
| 66 |
+
def filter_keywords_with_gemini(pairs, gemini_model=None):
|
| 67 |
+
"""Gemini AI๋ฅผ ์ฌ์ฉํ์ฌ ํค์๋ ์กฐํฉ ํํฐ๋ง (๊ฐ์ ๋ฒ์ ) - API ํค ํตํฉ ๊ด๋ฆฌ"""
|
| 68 |
+
if gemini_model is None:
|
| 69 |
+
# api_utils์์ Gemini ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ
|
| 70 |
+
gemini_model = get_gemini_model()
|
| 71 |
+
|
| 72 |
+
if gemini_model is None:
|
| 73 |
+
logger.error("Gemini ๋ชจ๋ธ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ๋ชจ๋ ํค์๋๋ฅผ ์ ์งํฉ๋๋ค.")
|
| 74 |
+
# ์์ ํ๊ฒ ์ฒ๋ฆฌ: ๋ชจ๋ ํค์๋๋ฅผ ์ ์ง
|
| 75 |
+
all_keywords = set()
|
| 76 |
+
for pair in pairs:
|
| 77 |
+
for keyword in pair:
|
| 78 |
+
all_keywords.add(keyword)
|
| 79 |
+
return list(all_keywords)
|
| 80 |
+
|
| 81 |
+
# ๋ชจ๋ ํค์๋๋ฅผ ๋ชฉ๋ก์ผ๋ก ์ถ์ถ (์ ๊ฑฐ๋ ํค์๋ ํ์ธ์ฉ)
|
| 82 |
+
all_keywords = set()
|
| 83 |
+
for pair in pairs:
|
| 84 |
+
for keyword in pair:
|
| 85 |
+
all_keywords.add(keyword)
|
| 86 |
+
|
| 87 |
+
# ๋๋ฌด ๋ง์ ์์ด ์์ผ๋ฉด ์ ํ
|
| 88 |
+
max_pairs = 50 # ์ต๋ 50๊ฐ ์๋ง ์ฒ๋ฆฌ
|
| 89 |
+
pairs_to_process = list(pairs)[:max_pairs] if len(pairs) > max_pairs else pairs
|
| 90 |
+
|
| 91 |
+
logger.info(f"ํํฐ๋งํ ํค์๋ ์: ์ด {len(pairs)}๊ฐ ์ค {len(pairs_to_process)}๊ฐ ์ฒ๋ฆฌ")
|
| 92 |
+
|
| 93 |
+
# ๋ณด์์ ์ธ ํ๋กฌํํธ ์ฌ์ฉ - ํค์๋ ์ ๊ฑฐ ์ต์ํ
|
| 94 |
+
prompt = (
|
| 95 |
+
"๋ค์์ ์๋น์๊ฐ ๊ฒ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ ํค์๋ ์ ๋ชฉ๋ก์
๋๋ค.\n"
|
| 96 |
+
"๊ฐ ์์ ๊ฐ์ ๋จ์ด ์กฐํฉ์ด์ง๋ง ์์๋ง ๋ค๋ฅธ ๊ฒฝ์ฐ์
๋๋ค (์: ์์ง์ค์ง์ด vs ์ค์ง์ด์์ง).\n\n"
|
| 97 |
+
"์๋์ ๊ธฐ์ค์ ๋ฐ๋ผ ๊ฐ ์์์ ๋ ์์ฐ์ค๋ฌ์ด ํค์๋๋ฅผ ์ ํํด์ฃผ์ธ์:\n"
|
| 98 |
+
"1. ์๋น์๊ฐ ์ผ์์ ์ผ๋ก ์ฌ์ฉํ๋ ์์ฐ์ค๋ฌ์ด ํํ์ ์ฐ์ ์ ํํ์ธ์.\n"
|
| 99 |
+
"2. ๋ ํค์๋๊ฐ ๋ชจ๋ ์์ฐ์ค๋ฝ๊ฑฐ๋ ์๋ฏธ๊ฐ ์ฝ๊ฐ ๋ค๋ฅด๋ค๋ฉด, ๋ฐ๋์ ๋ ๋ค ์ ์งํ์ธ์.\n"
|
| 100 |
+
"3. ํ์คํ ๋น์์ฐ์ค๋ฝ๊ฑฐ๋ ์ด์ํ ๊ฒฝ์ฐ์๋ง ์ ๊ฑฐํ์ธ์.\n"
|
| 101 |
+
"4. ๋ถํ์คํ ๊ฒฝ์ฐ์๋ ๋ฐ๋์ ํค์๋๋ฅผ ์ ์งํ์ธ์.\n"
|
| 102 |
+
"5. ์ซ์๋ ์์ด๊ฐ ํฌํจ๋ ํค์๋๋ ํ๊ธ ๋ฉ์ธ ํค์๋๊ฐ ์์ชฝ์ ์ค๋ ํํ๋ฅผ ์ ํํ์ธ์. (์: '10kg ์ค์ง์ด' ๋ณด๋ค '์ค์ง์ด 10kg' ์ ํ)\n"
|
| 103 |
+
"6. ๊ฒ์๋์ด 0์ธ ํค์๋๋ผ๋ ์ผ์์ ์ธ ํํ์ด๋ผ๋ฉด ๊ฐ๋ฅํ ์ ์งํ์ธ์. ๋ช
๋ฐฑํ๊ฒ ๋น์ ์์ ์ธ ํํ๋ง ์ ๊ฑฐํ์ธ์.\n\n"
|
| 104 |
+
"์ฃผ์: ๊ธฐ๋ณธ์ ์ผ๋ก ๋๋ถ๋ถ์ ํค์๋๋ฅผ ์ ์งํ๊ณ , ๋งค์ฐ ๋ช
ํํ๊ฒ ๋น์์ฐ์ค๋ฌ์ด ๊ฒ๋ง ์ ๊ฑฐํ์ธ์.\n\n"
|
| 105 |
+
"๊ฒฐ๊ณผ๋ ๋ค์ ํ์์ผ๋ก ์ ๊ณตํด์ฃผ์ธ์:\n"
|
| 106 |
+
"- ์ ํ๋ ํค์๋ (์ด์ : ์์ฐ์ค๋ฌ์ด ํํ์ด๊ธฐ ๋๋ฌธ)\n"
|
| 107 |
+
"- ์ ํ๋ ํค์๋1, ์ ํ๋ ํค์๋2 (์ด์ : ๋ ๋ค ์์ฐ์ค๋ฝ๊ณ ์๋ฏธ๊ฐ ์กฐ๊ธ ๋ค๋ฆ)\n\n"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# ํค์๏ฟฝ๏ฟฝ ์ ๋ชฉ๋ก
|
| 111 |
+
formatted = "\n".join([f"- {a}, {b}" for a, b in pairs_to_process])
|
| 112 |
+
full_prompt = prompt + formatted
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
# ํ์์์ ์ถ๊ฐ
|
| 116 |
+
logger.info(f"Gemini API ํธ์ถ ์์ - {len(pairs_to_process)}๊ฐ ํค์๋ ์ ์ฒ๋ฆฌ ์ค...")
|
| 117 |
+
|
| 118 |
+
# ์๋ต ๋ฐ๊ธฐ (ํ์์์ ๊ธฐ๋ฅ์ด ์์ผ๋ฉด ์ถ๊ฐ)
|
| 119 |
+
response = gemini_model.generate_content(full_prompt)
|
| 120 |
+
|
| 121 |
+
logger.info("Gemini API ์๋ต ์ฑ๊ณต")
|
| 122 |
+
lines = response.text.strip().split("\n")
|
| 123 |
+
|
| 124 |
+
# ์ ํ๋ ํค์๋ ์ถ์ถ (์ผํ๋ก ๊ตฌ๋ถ๋ ๊ฒฝ์ฐ ๋ชจ๋ ํฌํจ)
|
| 125 |
+
final_keywords = []
|
| 126 |
+
for line in lines:
|
| 127 |
+
if line.startswith("-"):
|
| 128 |
+
# ์ด์ ๋ถ๋ถ ์ ๊ฑฐ
|
| 129 |
+
keywords_part = line.strip("- ").split("(์ด์ :")[0].strip()
|
| 130 |
+
# ์ผํ๋ก ๊ตฌ๋ถ๋ ํค์๋ ๋ชจ๋ ์ถ๊ฐ
|
| 131 |
+
for kw in keywords_part.split(","):
|
| 132 |
+
kw = kw.strip()
|
| 133 |
+
if kw:
|
| 134 |
+
final_keywords.append(kw)
|
| 135 |
+
|
| 136 |
+
# ์ฒ๋ฆฌ๋์ง ์์ ์์ ์ฒซ ๋ฒ์งธ ํค์๋๋ ์ถ๊ฐ (LLM์ด ์ฒ๋ฆฌํ์ง ์์ ํค์๋)
|
| 137 |
+
if len(pairs) > max_pairs:
|
| 138 |
+
logger.info(f"์ถ๊ฐ ํค์๋ ์ฒ๋ฆฌ: ๋จ์ {len(pairs) - max_pairs}๊ฐ ์์ ์ฒซ ๋ฒ์งธ ํค์๋ ์ถ๊ฐ")
|
| 139 |
+
for pair in list(pairs)[max_pairs:]:
|
| 140 |
+
# ๊ฐ ์์ ์ฒซ ๋ฒ์งธ ํค์๋๋ง ์ฌ์ฉ
|
| 141 |
+
final_keywords.append(pair[0])
|
| 142 |
+
|
| 143 |
+
# ์ ํ๋ ํค์๋๊ฐ ์์ผ๋ฉด ๊ธฐ์กด ํค์๋ ๋ชจ๋ ๋ฐํ
|
| 144 |
+
if not final_keywords:
|
| 145 |
+
logger.warning("๊ฒฝ๊ณ : ์ ํ๋ ํค์๋๊ฐ ์์ด ๋ชจ๋ ํค์๋๋ฅผ ์ ์งํฉ๋๋ค.")
|
| 146 |
+
final_keywords = list(all_keywords)
|
| 147 |
+
|
| 148 |
+
# ์์ ๊ฐ์ ์์
|
| 149 |
+
corrected_keywords = []
|
| 150 |
+
|
| 151 |
+
# ๋จ์์ ์ซ์ ๊ด๋ จ ์ ๊ท์ ํจํด
|
| 152 |
+
unit_pattern = re.compile(r'(?i)(kg|g|mm|cm|ml|l|๋ฆฌํฐ|๊ฐ|ํฉ|๋ฐ์ค|์ธํธ|2l|l2)')
|
| 153 |
+
number_pattern = re.compile(r'\d+')
|
| 154 |
+
|
| 155 |
+
for kw in final_keywords:
|
| 156 |
+
# ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ
|
| 157 |
+
if ' ' in kw:
|
| 158 |
+
parts = kw.split()
|
| 159 |
+
first_part = parts[0]
|
| 160 |
+
|
| 161 |
+
# ์ฒซ ๋ถ๋ถ์ด ๋จ์๋ ์ซ์๋ฅผ ํฌํจํ๋์ง ํ์ธ
|
| 162 |
+
if (unit_pattern.search(first_part) or number_pattern.search(first_part)) and len(parts) > 1:
|
| 163 |
+
# ์์ ๋ฐ๊พธ๊ธฐ: ๋จ์/์ซ์ ๋ถ๋ถ์ ๋ค๋ก ์ด๋
|
| 164 |
+
corrected_kw = " ".join(parts[1:] + [first_part])
|
| 165 |
+
logger.info(f"ํค์๋ ์์ ๊ฐ์ ์์ : '{kw}' -> '{corrected_kw}'")
|
| 166 |
+
corrected_keywords.append(corrected_kw)
|
| 167 |
+
else:
|
| 168 |
+
corrected_keywords.append(kw)
|
| 169 |
+
else:
|
| 170 |
+
corrected_keywords.append(kw)
|
| 171 |
+
|
| 172 |
+
# ํน๋ณ ์ฒ๋ฆฌ: "L ์ค์ง์ด", "2L ์ค์ง์ด" ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ๋ช
์์ ์ผ๋ก ํ์ธํ๊ณ ์์
|
| 173 |
+
specific_fixes = []
|
| 174 |
+
for kw in corrected_keywords:
|
| 175 |
+
# ํน์ ํจํด ์ฒดํฌ
|
| 176 |
+
l_pattern = re.compile(r'^([0-9]*L) (.+)$', re.IGNORECASE)
|
| 177 |
+
match = l_pattern.match(kw)
|
| 178 |
+
|
| 179 |
+
if match:
|
| 180 |
+
# L ๋จ์๋ฅผ ๋ค๋ก ์ด๋
|
| 181 |
+
l_part = match.group(1)
|
| 182 |
+
main_part = match.group(2)
|
| 183 |
+
fixed_kw = f"{main_part} {l_part}"
|
| 184 |
+
logger.info(f"ํน์ ํจํด ์์ : '{kw}' -> '{fixed_kw}'")
|
| 185 |
+
specific_fixes.append(fixed_kw)
|
| 186 |
+
else:
|
| 187 |
+
specific_fixes.append(kw)
|
| 188 |
+
|
| 189 |
+
# ์ ๊ฑฐ๋ ํค์๋ ๋ชฉ๋ก ํ์ธ
|
| 190 |
+
selected_set = set(specific_fixes)
|
| 191 |
+
removed_keywords = all_keywords - selected_set
|
| 192 |
+
|
| 193 |
+
# ์ ๊ฑฐ๋ ํค์๋ ์ถ๋ ฅ
|
| 194 |
+
logger.info("\n=== LLM์ ์ํด ์ ๊ฑฐ๋ ํค์๋ ๋ชฉ๋ก ===")
|
| 195 |
+
for kw in removed_keywords:
|
| 196 |
+
logger.info(f" - {kw}")
|
| 197 |
+
logger.info(f"์ด {len(all_keywords)}๊ฐ ์ค {len(removed_keywords)}๊ฐ ์ ๊ฑฐ๋จ ({len(selected_set)}๊ฐ ์ ์ง)\n")
|
| 198 |
+
|
| 199 |
+
return specific_fixes
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
logger.error(f"Gemini ์ค๋ฅ: {e}")
|
| 203 |
+
logger.error("์ค๋ฅ ๋ฐ์์ผ๋ก ์ธํด ๋ชจ๋ ํค์๋๋ฅผ ์ ์งํฉ๋๋ค.")
|
| 204 |
+
logger.error(f"์ค๋ฅ ์ ํ: {type(e).__name__}")
|
| 205 |
+
import traceback
|
| 206 |
+
traceback.print_exc()
|
| 207 |
+
|
| 208 |
+
# ์์ ํ๊ฒ ์ฒ๋ฆฌ: ๋ชจ๋ ํค์๋๋ฅผ ์ ์ง
|
| 209 |
+
logger.info(f"์์ ๋ชจ๋: {len(all_keywords)}๊ฐ ํค์๋ ๋ชจ๋ ์ ์ง")
|
| 210 |
+
return list(all_keywords)
|
| 211 |
+
|
| 212 |
+
def get_search_volume_range(total_volume):
|
| 213 |
+
"""์ด ๊ฒ์๋์ ๊ธฐ๋ฐ์ผ๋ก ๊ฒ์๋ ๊ตฌ๊ฐ์ ๋ฐํ"""
|
| 214 |
+
if total_volume == 0:
|
| 215 |
+
return "100๋ฏธ๋ง"
|
| 216 |
+
elif total_volume <= 100:
|
| 217 |
+
return "100๋ฏธ๋ง"
|
| 218 |
+
elif total_volume <= 1000:
|
| 219 |
+
return "1000๋ฏธ๋ง"
|
| 220 |
+
elif total_volume <= 2000:
|
| 221 |
+
return "2000๋ฏธ๋ง"
|
| 222 |
+
elif total_volume <= 5000:
|
| 223 |
+
return "5000๋ฏธ๋ง"
|
| 224 |
+
elif total_volume <= 10000:
|
| 225 |
+
return "10000๋ฏธ๋ง"
|
| 226 |
+
else:
|
| 227 |
+
return "10000์ด์"
|
trend_analysis.py
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ํธ๋ ๋ ๋ถ์ ๋ชจ๋ - ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API๋ฅผ ํตํ ๊ฒ์ ํธ๋ ๋ ๋ถ์
|
| 3 |
+
- ์ฑ์ฅ๋ฅ ์ 3๋
๊ธฐ์ค์ผ๋ก ๋ณ๊ฒฝ
|
| 4 |
+
- ๋๋น 100% ์ ์ฉ
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import requests
|
| 8 |
+
import json
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import plotly.graph_objects as go
|
| 11 |
+
import plotly.express as px
|
| 12 |
+
from datetime import datetime, timedelta
|
| 13 |
+
import api_utils
|
| 14 |
+
import keyword_search
|
| 15 |
+
import logging
|
| 16 |
+
|
| 17 |
+
# ๋ก๊น
์ค์
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
logger.setLevel(logging.INFO)
|
| 20 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 21 |
+
handler = logging.StreamHandler()
|
| 22 |
+
handler.setFormatter(formatter)
|
| 23 |
+
logger.addHandler(handler)
|
| 24 |
+
|
| 25 |
+
def get_trend_data(keywords, period="1year"):
|
| 26 |
+
"""
|
| 27 |
+
๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API๋ฅผ ํตํด ๊ฒ์ ํธ๋ ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ (์์ ๋ ๋ฒ์ )
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
keywords (list): ๋ถ์ํ ํค์๋ ๋ชฉ๋ก (์ต๋ 5๊ฐ)
|
| 31 |
+
period (str): ๋ถ์ ๊ธฐ๊ฐ ("1year" ๋๋ "3year")
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
dict: ํธ๋ ๋ ๋ฐ์ดํฐ ๋ฐ ๊ทธ๋ํ HTML
|
| 35 |
+
"""
|
| 36 |
+
logger.info(f"ํธ๋ ๋ ๋ถ์ ์์: {len(keywords)}๊ฐ ํค์๋, ๊ธฐ๊ฐ: {period}")
|
| 37 |
+
|
| 38 |
+
# ๋ ์ง ๊ณ์ฐ (์ด์ ๊ธฐ์ค์ผ๋ก ์ ๋จ์ ๊ณ์ฐ)
|
| 39 |
+
yesterday = datetime.now() - timedelta(days=1)
|
| 40 |
+
end_year = yesterday.year
|
| 41 |
+
end_month = yesterday.month
|
| 42 |
+
|
| 43 |
+
if period == "1year":
|
| 44 |
+
# 1๋
์ ๊ฐ์ ๋ฌ
|
| 45 |
+
start_year = end_year - 1
|
| 46 |
+
start_month = end_month
|
| 47 |
+
else: # 3year
|
| 48 |
+
# 3๋
์ ๊ฐ์ ๋ฌ
|
| 49 |
+
start_year = end_year - 3
|
| 50 |
+
start_month = end_month
|
| 51 |
+
|
| 52 |
+
# ์ ์ฒซ์งธ ๋ ๋ก ๋ ์ง ์ค์
|
| 53 |
+
start_date_str = f"{start_year:04d}-{start_month:02d}-01"
|
| 54 |
+
end_date_str = f"{end_year:04d}-{end_month:02d}-01"
|
| 55 |
+
|
| 56 |
+
logger.info(f"๋ถ์ ๊ธฐ๊ฐ: {start_date_str} ~ {end_date_str} (์๊ฐ ๋ฐ์ดํฐ)")
|
| 57 |
+
|
| 58 |
+
# ํค์๋๋ ์ต๋ 5๊ฐ๊น์ง๋ง ์ฒ๋ฆฌ
|
| 59 |
+
keywords = keywords[:5]
|
| 60 |
+
|
| 61 |
+
# API ์ค์ ๊ฐ์ ธ์ค๊ธฐ
|
| 62 |
+
api_config = api_utils.get_next_datalab_api_config()
|
| 63 |
+
if not api_config:
|
| 64 |
+
logger.error("๋ฐ์ดํฐ๋ฉ API ์ค์ ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.")
|
| 65 |
+
return {
|
| 66 |
+
"status": "error",
|
| 67 |
+
"message": "๋ฐ์ดํฐ๋ฉ API ์ค์ ์์"
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
# ํค์๋ ๊ทธ๋ฃน ์์ฑ
|
| 71 |
+
keyword_groups = []
|
| 72 |
+
for keyword in keywords:
|
| 73 |
+
keyword_groups.append({
|
| 74 |
+
'groupName': keyword,
|
| 75 |
+
'keywords': [keyword]
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
# API ์์ฒญ ๋ฐ์ดํฐ (device ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ)
|
| 79 |
+
body_dict = {
|
| 80 |
+
'startDate': start_date_str,
|
| 81 |
+
'endDate': end_date_str,
|
| 82 |
+
'timeUnit': 'month',
|
| 83 |
+
'keywordGroups': keyword_groups
|
| 84 |
+
# device ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ โ ์ ์ฒด ํ๊ฒฝ(PC+๋ชจ๋ฐ์ผ) ์กฐํ
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
body = json.dumps(body_dict)
|
| 88 |
+
|
| 89 |
+
# API ํธ์ถ
|
| 90 |
+
url = "https://openapi.naver.com/v1/datalab/search"
|
| 91 |
+
headers = {
|
| 92 |
+
'X-Naver-Client-Id': api_config["CLIENT_ID"],
|
| 93 |
+
'X-Naver-Client-Secret': api_config["CLIENT_SECRET"],
|
| 94 |
+
'Content-Type': 'application/json'
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
response = requests.post(url, data=body, headers=headers, timeout=10)
|
| 99 |
+
|
| 100 |
+
if response.status_code == 200:
|
| 101 |
+
response_json = response.json()
|
| 102 |
+
|
| 103 |
+
# ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 104 |
+
trend_data = []
|
| 105 |
+
for result in response_json['results']:
|
| 106 |
+
for data_point in result['data']:
|
| 107 |
+
trend_data.append({
|
| 108 |
+
'keyword': result['title'],
|
| 109 |
+
'period': data_point['period'],
|
| 110 |
+
'ratio': data_point['ratio']
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
df_trend = pd.DataFrame(trend_data)
|
| 114 |
+
|
| 115 |
+
# ํ์ฌ ์๋ณ ๊ฒ์๋ ์กฐํ (๊ฒ์๊ด๊ณ API)
|
| 116 |
+
search_volumes = keyword_search.fetch_all_search_volumes(keywords)
|
| 117 |
+
|
| 118 |
+
# ์ ๋ ๊ฒ์๋์ผ๋ก ๋ณํ
|
| 119 |
+
df_trend_with_volume = convert_to_absolute_volume(df_trend, search_volumes)
|
| 120 |
+
|
| 121 |
+
# ๊ทธ๋ํ ์์ฑ (๋๋น 100% ์ ์ฉ)
|
| 122 |
+
graph_html = create_trend_graph(df_trend_with_volume, period)
|
| 123 |
+
|
| 124 |
+
logger.info(f"ํธ๋ ๋ ๋ถ์ ์๋ฃ: {len(trend_data)}๊ฐ ๋ฐ์ดํฐ ํฌ์ธํธ")
|
| 125 |
+
|
| 126 |
+
return {
|
| 127 |
+
"status": "success",
|
| 128 |
+
"trend_data": df_trend_with_volume,
|
| 129 |
+
"graph_html": graph_html,
|
| 130 |
+
"period": period,
|
| 131 |
+
"keywords": keywords
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
else:
|
| 135 |
+
logger.error(f"๋ฐ์ดํฐ๋ฉ API ์ค๋ฅ: {response.status_code} - {response.text}")
|
| 136 |
+
return {
|
| 137 |
+
"status": "error",
|
| 138 |
+
"message": f"API ์ค๋ฅ: {response.status_code}"
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logger.error(f"ํธ๋ ๋ ๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 143 |
+
return {
|
| 144 |
+
"status": "error",
|
| 145 |
+
"message": f"๋ถ์ ์ค ์ค๋ฅ: {str(e)}"
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
def convert_to_absolute_volume(df_trend, search_volumes):
|
| 149 |
+
"""
|
| 150 |
+
์๋ ๊ฒ๏ฟฝ๏ฟฝ๏ฟฝ๋์ ์ ๋ ๊ฒ์๋์ผ๋ก ๋ณํ
|
| 151 |
+
|
| 152 |
+
Args:
|
| 153 |
+
df_trend (DataFrame): ํธ๋ ๋ ๋ฐ์ดํฐ (์๋๊ฐ)
|
| 154 |
+
search_volumes (dict): ํ์ฌ ์๋ณ ๊ฒ์๋ ๋ฐ์ดํฐ
|
| 155 |
+
|
| 156 |
+
Returns:
|
| 157 |
+
DataFrame: ์ ๋ ๊ฒ์๋์ด ์ถ๊ฐ๋ ํธ๋ ๋ ๋ฐ์ดํฐ
|
| 158 |
+
"""
|
| 159 |
+
df_result = df_trend.copy()
|
| 160 |
+
df_result['absolute_volume'] = 0
|
| 161 |
+
|
| 162 |
+
# ๊ฐ ํค์๋๋ณ๋ก ์ฒ๋ฆฌ
|
| 163 |
+
for keyword in df_trend['keyword'].unique():
|
| 164 |
+
keyword_data = df_trend[df_trend['keyword'] == keyword]
|
| 165 |
+
|
| 166 |
+
# ํ์ฌ ์์ ๋น์จ (๋ง์ง๋ง ๋ฐ์ดํฐ)
|
| 167 |
+
current_ratio = keyword_data['ratio'].iloc[-1]
|
| 168 |
+
|
| 169 |
+
# ํ์ฌ ์์ ๊ฒ์๋ (PC + ๋ชจ๋ฐ์ผ)
|
| 170 |
+
volume_data = search_volumes.get(keyword.replace(" ", ""), {"์ด๊ฒ์๋": 0})
|
| 171 |
+
current_volume = volume_data.get("์ด๊ฒ์๋", 0)
|
| 172 |
+
|
| 173 |
+
if current_ratio > 0 and current_volume > 0:
|
| 174 |
+
# 1%๋น ๊ฒ์๋ ๊ณ์ฐ
|
| 175 |
+
volume_per_percent = current_volume / current_ratio
|
| 176 |
+
|
| 177 |
+
# ๊ฐ ๊ธฐ๊ฐ์ ์ ๋ ๊ฒ์๋ ๊ณ์ฐ
|
| 178 |
+
mask = df_result['keyword'] == keyword
|
| 179 |
+
df_result.loc[mask, 'absolute_volume'] = (
|
| 180 |
+
df_result.loc[mask, 'ratio'] * volume_per_percent
|
| 181 |
+
).astype(int)
|
| 182 |
+
|
| 183 |
+
logger.info(f"'{keyword}': ํ์ฌ ๋น์จ {current_ratio}%, ๊ฒ์๋ {current_volume:,}, 1%๋น {volume_per_percent:.0f}")
|
| 184 |
+
else:
|
| 185 |
+
logger.warning(f"'{keyword}': ๊ฒ์๋ ๋ณํ ๋ถ๊ฐ (๋น์จ: {current_ratio}, ๊ฒ์๋: {current_volume})")
|
| 186 |
+
|
| 187 |
+
return df_result
|
| 188 |
+
|
| 189 |
+
def create_trend_graph(df_trend, period):
|
| 190 |
+
"""
|
| 191 |
+
ํธ๋ ๋ ๊ทธ๋ํ ์์ฑ (๋๋น 100% ์ ์ฉ)
|
| 192 |
+
|
| 193 |
+
Args:
|
| 194 |
+
df_trend (DataFrame): ํธ๋ ๋ ๋ฐ์ดํฐ
|
| 195 |
+
period (str): ๋ถ์ ๊ธฐ๊ฐ
|
| 196 |
+
|
| 197 |
+
Returns:
|
| 198 |
+
str: HTML ํํ์ ๊ทธ๋ํ
|
| 199 |
+
"""
|
| 200 |
+
# Plotly ๊ทธ๋ํ ์์ฑ
|
| 201 |
+
fig = go.Figure()
|
| 202 |
+
|
| 203 |
+
# ํค์๋๋ณ๋ก ๋ผ์ธ ์ถ๊ฐ
|
| 204 |
+
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
|
| 205 |
+
|
| 206 |
+
for i, keyword in enumerate(df_trend['keyword'].unique()):
|
| 207 |
+
keyword_data = df_trend[df_trend['keyword'] == keyword]
|
| 208 |
+
|
| 209 |
+
fig.add_trace(go.Scatter(
|
| 210 |
+
x=keyword_data['period'],
|
| 211 |
+
y=keyword_data['absolute_volume'],
|
| 212 |
+
mode='lines+markers',
|
| 213 |
+
name=keyword,
|
| 214 |
+
line=dict(color=colors[i % len(colors)], width=3),
|
| 215 |
+
marker=dict(size=6),
|
| 216 |
+
hovertemplate='<b>%{fullData.name}</b><br>' +
|
| 217 |
+
'๊ธฐ๊ฐ: %{x}<br>' +
|
| 218 |
+
'๊ฒ์๋: %{y:,}<br>' +
|
| 219 |
+
'<extra></extra>'
|
| 220 |
+
))
|
| 221 |
+
|
| 222 |
+
# ๋ ์ด์์ ์ค์ (๋๋น 100% ์ ์ฉ)
|
| 223 |
+
period_text = "์ต๊ทผ 1๋
" if period == "1year" else "์ต๊ทผ 3๋
"
|
| 224 |
+
|
| 225 |
+
fig.update_layout(
|
| 226 |
+
title=f'ํค์๋๋ณ ์๋ณ ๊ฒ์๋ ํธ๋ ๋ ({period_text})',
|
| 227 |
+
xaxis_title='๊ธฐ๊ฐ',
|
| 228 |
+
yaxis_title='์๋ณ ๊ฒ์๋',
|
| 229 |
+
hovermode='x unified',
|
| 230 |
+
template='plotly_white',
|
| 231 |
+
height=500,
|
| 232 |
+
showlegend=True,
|
| 233 |
+
legend=dict(
|
| 234 |
+
orientation="h",
|
| 235 |
+
yanchor="bottom",
|
| 236 |
+
y=1.02,
|
| 237 |
+
xanchor="right",
|
| 238 |
+
x=1
|
| 239 |
+
),
|
| 240 |
+
width=None, # ๋๋น ์๋ ์กฐ์
|
| 241 |
+
autosize=True # ์๋ ํฌ๊ธฐ ์กฐ์
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# y์ถ ํฌ๋งท ์ค์ (์ฒ ๋จ์ ๊ตฌ๋ถ)
|
| 245 |
+
fig.update_yaxis(tickformat=',')
|
| 246 |
+
|
| 247 |
+
# HTML๋ก ๋ณํ (๋๋น 100% ์ ์ฉ)
|
| 248 |
+
graph_html = fig.to_html(
|
| 249 |
+
include_plotlyjs='cdn',
|
| 250 |
+
div_id="trend-graph",
|
| 251 |
+
config={'responsive': True} # ๋ฐ์ํ ๊ทธ๋ํ
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
return graph_html
|
| 255 |
+
|
| 256 |
+
def calculate_3year_growth_rate(volumes):
|
| 257 |
+
"""
|
| 258 |
+
3๋
๊ธฐ์ค ์ฑ์ฅ๋ฅ ๊ณ์ฐ (์ ์ฒด ๊ธฐ๊ฐ ๊ธฐ์ค)
|
| 259 |
+
|
| 260 |
+
Args:
|
| 261 |
+
volumes (list): ์๋ณ ๊ฒ์๋ ๋ฐ์ดํฐ
|
| 262 |
+
|
| 263 |
+
Returns:
|
| 264 |
+
float: 3๋
๊ธฐ์ค ์ฑ์ฅ๋ฅ
|
| 265 |
+
"""
|
| 266 |
+
if len(volumes) < 6: # ์ต์ 6๊ฐ์ ๋ฐ์ดํฐ ํ์
|
| 267 |
+
return 0
|
| 268 |
+
|
| 269 |
+
# ์ ์ฒด ๊ธฐ๊ฐ์ 3๋ฑ๋ถํ์ฌ ์ฑ์ฅ๋ฅ ๊ณ์ฐ
|
| 270 |
+
total_months = len(volumes)
|
| 271 |
+
period_size = max(1, total_months // 3) # ์ต์ 1๊ฐ์
|
| 272 |
+
|
| 273 |
+
# ์ด๊ธฐ ๊ธฐ๊ฐ ํ๊ท (์ฒซ 1/3)
|
| 274 |
+
early_period = volumes[:period_size]
|
| 275 |
+
early_avg = sum(early_period) / len(early_period)
|
| 276 |
+
|
| 277 |
+
# ์ต๊ทผ ๊ธฐ๊ฐ ํ๊ท (๋ง์ง๋ง 1/3)
|
| 278 |
+
recent_period = volumes[-period_size:]
|
| 279 |
+
recent_avg = sum(recent_period) / len(recent_period)
|
| 280 |
+
|
| 281 |
+
if early_avg > 0:
|
| 282 |
+
return round(((recent_avg - early_avg) / early_avg) * 100, 1)
|
| 283 |
+
return 0
|
| 284 |
+
|
| 285 |
+
def analyze_trend_insights(df_trend):
|
| 286 |
+
"""
|
| 287 |
+
ํธ๋ ๋ ๋ฐ์ดํฐ์์ ์ธ์ฌ์ดํธ ์ถ์ถ (3๋
๊ธฐ์ค ์ฑ์ฅ๋ฅ ๋ก ๋ณ๊ฒฝ)
|
| 288 |
+
|
| 289 |
+
Args:
|
| 290 |
+
df_trend (DataFrame): ํธ๋ ๋ ๋ฐ์ดํฐ
|
| 291 |
+
|
| 292 |
+
Returns:
|
| 293 |
+
dict: ํธ๋ ๋ ์ธ์ฌ์ดํธ
|
| 294 |
+
"""
|
| 295 |
+
insights = {}
|
| 296 |
+
|
| 297 |
+
for keyword in df_trend['keyword'].unique():
|
| 298 |
+
keyword_data = df_trend[df_trend['keyword'] == keyword].sort_values('period')
|
| 299 |
+
|
| 300 |
+
# ์ต๊ณ ์ ๊ณผ ์ต์ ์
|
| 301 |
+
max_volume = keyword_data['absolute_volume'].max()
|
| 302 |
+
min_volume = keyword_data['absolute_volume'].min()
|
| 303 |
+
max_period = keyword_data[keyword_data['absolute_volume'] == max_volume]['period'].iloc[0]
|
| 304 |
+
min_period = keyword_data[keyword_data['absolute_volume'] == min_volume]['period'].iloc[0]
|
| 305 |
+
|
| 306 |
+
# ์ ์ฒด ๊ธฐ๊ฐ ํ๊ท
|
| 307 |
+
total_avg = keyword_data['absolute_volume'].mean()
|
| 308 |
+
|
| 309 |
+
# 3๋
๊ธฐ์ค ์ฑ์ฅ๋ฅ ๊ณ์ฐ (์ ์ฒด ๊ธฐ๊ฐ ๊ธฐ์ค)
|
| 310 |
+
volumes = keyword_data['absolute_volume'].tolist()
|
| 311 |
+
growth_rate = calculate_3year_growth_rate(volumes)
|
| 312 |
+
|
| 313 |
+
insights[keyword] = {
|
| 314 |
+
'max_volume': int(max_volume),
|
| 315 |
+
'max_period': max_period,
|
| 316 |
+
'min_volume': int(min_volume),
|
| 317 |
+
'min_period': min_period,
|
| 318 |
+
'total_avg': int(total_avg),
|
| 319 |
+
'growth_rate': growth_rate,
|
| 320 |
+
'total_months': len(volumes)
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
return insights
|
trend_analysis_v2.py
ADDED
|
@@ -0,0 +1,1128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ํธ๋ ๋ ๋ถ์ ๋ชจ๋ v2.16 - ์ ๊ตํ ์ด์ค API ํธ์ถ ๋ฐ ์ญ์ฐ ๋ก์ง ๊ตฌํ
|
| 3 |
+
- ์ด์ค ํธ๋ ๋ API ํธ์ถ: ์ผ๋ณ + ์๋ณ ๋ฐ์ดํฐ
|
| 4 |
+
- ์ผ๋ณ ๋ฐ์ดํฐ๋ก ์ ์ ์ ํํ ๊ฒ์๋ ์ญ์ฐ
|
| 5 |
+
- ์ ์ ๊ธฐ์ค์ผ๋ก 3๋
๋ชจ๋ ์ ๊ฒ์๋ ์ญ์ฐ
|
| 6 |
+
- ์๋
๋์ ๊ธฐ๋ฐ ๋ฏธ๋ 3๊ฐ์ ์์
|
| 7 |
+
- ์ต์ข
๋จ๊ณ์์ 10% ๊ฐ์ ์กฐ์ ์ ์ฉ
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import urllib.request
|
| 11 |
+
import json
|
| 12 |
+
import time
|
| 13 |
+
import logging
|
| 14 |
+
from datetime import datetime, timedelta
|
| 15 |
+
import calendar
|
| 16 |
+
import api_utils
|
| 17 |
+
|
| 18 |
+
# ๋ก๊น
์ค์
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# ===== ํต์ฌ ๊ฐ์ ํจ์๋ค =====
|
| 22 |
+
|
| 23 |
+
def get_complete_month():
|
| 24 |
+
"""์์ฑ๋ ๋ง์ง๋ง ์ ๊ณ์ฐ - ๋จ์ํ๋ ๋ก์ง"""
|
| 25 |
+
current_date = datetime.now()
|
| 26 |
+
current_day = current_date.day
|
| 27 |
+
current_year = current_date.year
|
| 28 |
+
current_month = current_date.month
|
| 29 |
+
|
| 30 |
+
# 3์ผ ์ดํ์๋ง ์ ์์ ์์ฑ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผ
|
| 31 |
+
if current_day >= 3:
|
| 32 |
+
completed_year = current_year
|
| 33 |
+
completed_month = current_month - 1
|
| 34 |
+
else:
|
| 35 |
+
completed_year = current_year
|
| 36 |
+
completed_month = current_month - 2
|
| 37 |
+
|
| 38 |
+
# ์์ด 0 ์ดํ๊ฐ ๋๋ฉด ์ฐ๋ ์กฐ์
|
| 39 |
+
while completed_month <= 0:
|
| 40 |
+
completed_month += 12
|
| 41 |
+
completed_year -= 1
|
| 42 |
+
|
| 43 |
+
return completed_year, completed_month
|
| 44 |
+
|
| 45 |
+
def get_daily_trend_data(keywords, max_retries=3):
|
| 46 |
+
"""1์ฐจ ํธ์ถ: ์ผ๋ณ ํธ๋ ๋ ๋ฐ์ดํฐ (์ ์ ์ ํ ๊ณ์ฐ์ฉ)"""
|
| 47 |
+
for retry_attempt in range(max_retries):
|
| 48 |
+
try:
|
| 49 |
+
# ๋ฐ์ดํฐ๋ฉ API ์ค์
|
| 50 |
+
datalab_config = api_utils.get_next_datalab_api_config()
|
| 51 |
+
if not datalab_config:
|
| 52 |
+
logger.warning("๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
client_id = datalab_config["CLIENT_ID"]
|
| 56 |
+
client_secret = datalab_config["CLIENT_SECRET"]
|
| 57 |
+
|
| 58 |
+
# ์์ฑ๋ ๋ง์ง๋ง ์ ๊ณ์ฐ
|
| 59 |
+
completed_year, completed_month = get_complete_month()
|
| 60 |
+
|
| 61 |
+
# ์ผ๋ณ ๋ฐ์ดํฐ ๊ธฐ๊ฐ: ์ ์ 1์ผ ~ ์ด์
|
| 62 |
+
current_date = datetime.now()
|
| 63 |
+
yesterday = current_date - timedelta(days=1)
|
| 64 |
+
|
| 65 |
+
start_date = f"{completed_year:04d}-{completed_month:02d}-01"
|
| 66 |
+
end_date = yesterday.strftime("%Y-%m-%d")
|
| 67 |
+
|
| 68 |
+
logger.info(f"๐ 1์ฐจ ํธ์ถ (์ผ๋ณ): {start_date} ~ {end_date}")
|
| 69 |
+
|
| 70 |
+
# ํค์๋ ๊ทธ๋ฃน ์์ฑ
|
| 71 |
+
keywordGroups = []
|
| 72 |
+
for kw in keywords[:5]:
|
| 73 |
+
keywordGroups.append({
|
| 74 |
+
'groupName': kw,
|
| 75 |
+
'keywords': [kw]
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
# API ์์ฒญ
|
| 79 |
+
body_dict = {
|
| 80 |
+
'startDate': start_date,
|
| 81 |
+
'endDate': end_date,
|
| 82 |
+
'timeUnit': 'date', # ์ผ๋ณ!
|
| 83 |
+
'keywordGroups': keywordGroups
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
url = "https://openapi.naver.com/v1/datalab/search"
|
| 87 |
+
body = json.dumps(body_dict, ensure_ascii=False)
|
| 88 |
+
|
| 89 |
+
request = urllib.request.Request(url)
|
| 90 |
+
request.add_header("X-Naver-Client-Id", client_id)
|
| 91 |
+
request.add_header("X-Naver-Client-Secret", client_secret)
|
| 92 |
+
request.add_header("Content-Type", "application/json")
|
| 93 |
+
|
| 94 |
+
response = urllib.request.urlopen(request, data=body.encode("utf-8"), timeout=15)
|
| 95 |
+
rescode = response.getcode()
|
| 96 |
+
|
| 97 |
+
if rescode == 200:
|
| 98 |
+
response_body = response.read()
|
| 99 |
+
response_json = json.loads(response_body)
|
| 100 |
+
logger.info(f"์ผ๋ณ ํธ๋ ๋ ๋ฐ์ดํฐ ์กฐํ ์ฑ๊ณต")
|
| 101 |
+
return response_json
|
| 102 |
+
else:
|
| 103 |
+
logger.error(f"์ผ๋ณ API ์ค๋ฅ: ์ํ์ฝ๋ {rescode}")
|
| 104 |
+
if retry_attempt < max_retries - 1:
|
| 105 |
+
time.sleep(2 * (retry_attempt + 1))
|
| 106 |
+
continue
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logger.error(f"์ผ๋ณ ํธ๋ ๋ ์กฐํ ์ค๋ฅ (์๋ {retry_attempt + 1}): {e}")
|
| 111 |
+
if retry_attempt < max_retries - 1:
|
| 112 |
+
time.sleep(2 * (retry_attempt + 1))
|
| 113 |
+
continue
|
| 114 |
+
return None
|
| 115 |
+
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
def get_monthly_trend_data(keywords, max_retries=3):
|
| 119 |
+
"""2์ฐจ ํธ์ถ: ์๋ณ ํธ๋ ๋ ๋ฐ์ดํฐ (3๋
์ ์ฒด + ์์์ฉ)"""
|
| 120 |
+
for retry_attempt in range(max_retries):
|
| 121 |
+
try:
|
| 122 |
+
# ๋ฐ์ดํฐ๋ฉ API ์ค์
|
| 123 |
+
datalab_config = api_utils.get_next_datalab_api_config()
|
| 124 |
+
if not datalab_config:
|
| 125 |
+
logger.warning("๋ฐ์ดํฐ๋ฉ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 126 |
+
return None
|
| 127 |
+
|
| 128 |
+
client_id = datalab_config["CLIENT_ID"]
|
| 129 |
+
client_secret = datalab_config["CLIENT_SECRET"]
|
| 130 |
+
|
| 131 |
+
# ์์ฑ๋ ๋ง์ง๋ง ์ ๊ณ์ฐ
|
| 132 |
+
completed_year, completed_month = get_complete_month()
|
| 133 |
+
|
| 134 |
+
# ์๋ณ ๋ฐ์ดํฐ ๊ธฐ๊ฐ: 3๋
์ 1์ ~ ์์ฑ๋ ๋ง์ง๋ง ์
|
| 135 |
+
start_year = completed_year - 3
|
| 136 |
+
start_date = f"{start_year:04d}-01-01"
|
| 137 |
+
end_date = f"{completed_year:04d}-{completed_month:02d}-01"
|
| 138 |
+
|
| 139 |
+
logger.info(f"๐ 2์ฐจ ํธ์ถ (์๋ณ): {start_date} ~ {end_date}")
|
| 140 |
+
|
| 141 |
+
# ํค์๋ ๊ทธ๋ฃน ์์ฑ
|
| 142 |
+
keywordGroups = []
|
| 143 |
+
for kw in keywords[:5]:
|
| 144 |
+
keywordGroups.append({
|
| 145 |
+
'groupName': kw,
|
| 146 |
+
'keywords': [kw]
|
| 147 |
+
})
|
| 148 |
+
|
| 149 |
+
# API ์์ฒญ
|
| 150 |
+
body_dict = {
|
| 151 |
+
'startDate': start_date,
|
| 152 |
+
'endDate': end_date,
|
| 153 |
+
'timeUnit': 'month', # ์๋ณ!
|
| 154 |
+
'keywordGroups': keywordGroups
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
url = "https://openapi.naver.com/v1/datalab/search"
|
| 158 |
+
body = json.dumps(body_dict, ensure_ascii=False)
|
| 159 |
+
|
| 160 |
+
request = urllib.request.Request(url)
|
| 161 |
+
request.add_header("X-Naver-Client-Id", client_id)
|
| 162 |
+
request.add_header("X-Naver-Client-Secret", client_secret)
|
| 163 |
+
request.add_header("Content-Type", "application/json")
|
| 164 |
+
|
| 165 |
+
response = urllib.request.urlopen(request, data=body.encode("utf-8"), timeout=15)
|
| 166 |
+
rescode = response.getcode()
|
| 167 |
+
|
| 168 |
+
if rescode == 200:
|
| 169 |
+
response_body = response.read()
|
| 170 |
+
response_json = json.loads(response_body)
|
| 171 |
+
logger.info(f"์๋ณ ํธ๋ ๋ ๋ฐ์ดํฐ ์กฐํ ์ฑ๊ณต")
|
| 172 |
+
return response_json
|
| 173 |
+
else:
|
| 174 |
+
logger.error(f"์๋ณ API ์ค๋ฅ: ์ํ์ฝ๋ {rescode}")
|
| 175 |
+
if retry_attempt < max_retries - 1:
|
| 176 |
+
time.sleep(2 * (retry_attempt + 1))
|
| 177 |
+
continue
|
| 178 |
+
return None
|
| 179 |
+
|
| 180 |
+
except Exception as e:
|
| 181 |
+
logger.error(f"์๋ณ ํธ๋ ๋ ์กฐํ ์ค๋ฅ (์๋ {retry_attempt + 1}): {e}")
|
| 182 |
+
if retry_attempt < max_retries - 1:
|
| 183 |
+
time.sleep(2 * (retry_attempt + 1))
|
| 184 |
+
continue
|
| 185 |
+
return None
|
| 186 |
+
|
| 187 |
+
return None
|
| 188 |
+
|
| 189 |
+
def calculate_previous_month_from_daily(current_volume, daily_data):
|
| 190 |
+
"""์ผ๋ณ ํธ๋ ๋๋ก ์ ์ ์ ํํ ๊ฒ์๋ ์ญ์ฐ"""
|
| 191 |
+
if not daily_data or "results" not in daily_data:
|
| 192 |
+
logger.warning("์ผ๋ณ ๋ฐ์ดํฐ๊ฐ ์์ด ์ ์ ๊ณ์ฐ์ ๊ฑด๋๋๋๋ค.")
|
| 193 |
+
return current_volume
|
| 194 |
+
|
| 195 |
+
try:
|
| 196 |
+
completed_year, completed_month = get_complete_month()
|
| 197 |
+
|
| 198 |
+
# ์ ์ ์ผ์ ๊ณ์ฐ
|
| 199 |
+
prev_month_days = calendar.monthrange(completed_year, completed_month)[1]
|
| 200 |
+
|
| 201 |
+
for result in daily_data["results"]:
|
| 202 |
+
keyword = result["title"]
|
| 203 |
+
|
| 204 |
+
if not result["data"]:
|
| 205 |
+
continue
|
| 206 |
+
|
| 207 |
+
# ์ต๊ทผ 30์ผ๊ณผ ์ ์ ๋ฐ์ดํฐ ๋ถ๋ฆฌ
|
| 208 |
+
recent_30_ratios = [] # ์ต๊ทผ 30์ผ (ํ์ฌ ๊ฒ์๋ ๊ธฐ์ค)
|
| 209 |
+
prev_month_ratios = [] # ์ ์ ๋ฐ์ดํฐ
|
| 210 |
+
|
| 211 |
+
for data_point in result["data"]:
|
| 212 |
+
try:
|
| 213 |
+
date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
|
| 214 |
+
ratio = data_point["ratio"]
|
| 215 |
+
|
| 216 |
+
# ์ ์ ๋ฐ์ดํฐ
|
| 217 |
+
if date_obj.year == completed_year and date_obj.month == completed_month:
|
| 218 |
+
prev_month_ratios.append(ratio)
|
| 219 |
+
|
| 220 |
+
# ์ต๊ทผ 30์ผ (ํ์ฌ ๊ฒ์๋ ๊ธฐ์ค ๊ตฌ๊ฐ)
|
| 221 |
+
current_date = datetime.now()
|
| 222 |
+
if (current_date - date_obj).days <= 30:
|
| 223 |
+
recent_30_ratios.append(ratio)
|
| 224 |
+
|
| 225 |
+
except:
|
| 226 |
+
continue
|
| 227 |
+
|
| 228 |
+
if not recent_30_ratios or not prev_month_ratios:
|
| 229 |
+
logger.warning(f"'{keyword}' ๋น๊ต ๋ฐ์ดํฐ๊ฐ ๋ถ์กฑํฉ๋๋ค.")
|
| 230 |
+
continue
|
| 231 |
+
|
| 232 |
+
# ํ๊ท ๋น์จ ๊ณ์ฐ
|
| 233 |
+
recent_30_avg = sum(recent_30_ratios) / len(recent_30_ratios)
|
| 234 |
+
prev_month_avg = sum(prev_month_ratios) / len(prev_month_ratios)
|
| 235 |
+
|
| 236 |
+
if recent_30_avg == 0:
|
| 237 |
+
continue
|
| 238 |
+
|
| 239 |
+
# ์ ์ ๊ฒ์๋ ์ญ์ฐ
|
| 240 |
+
# ํ์ฌ ๊ฒ์๋ = ์ต๊ทผ 30์ผ ํ๊ท ๊ธฐ์ค
|
| 241 |
+
# ์ ์ ๊ฒ์๋ = (์ ์ ํ๊ท / ์ต๊ทผ 30์ผ ํ๊ท ) ร ํ์ฌ ๊ฒ์๋ ร (์ ์ ์ผ์ / 30์ผ)
|
| 242 |
+
prev_month_volume = int(
|
| 243 |
+
(prev_month_avg / recent_30_avg) * current_volume * (prev_month_days / 30)
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
logger.info(f"'{keyword}' ์ ์ {completed_year}.{completed_month:02d} ์ญ์ฐ ๊ฒ์๋: {prev_month_volume:,}ํ")
|
| 247 |
+
logger.info(f" - ์ต๊ทผ 30์ผ ํ๊ท ๋น์จ: {recent_30_avg:.1f}%")
|
| 248 |
+
logger.info(f" - ์ ์ ํ๊ท ๋น์จ: {prev_month_avg:.1f}%")
|
| 249 |
+
logger.info(f" - ์ ์ ์ผ์ ๋ณด์ : {prev_month_days}์ผ")
|
| 250 |
+
|
| 251 |
+
return prev_month_volume
|
| 252 |
+
|
| 253 |
+
except Exception as e:
|
| 254 |
+
logger.error(f"์ ์ ์ญ์ฐ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 255 |
+
return current_volume
|
| 256 |
+
|
| 257 |
+
return current_volume
|
| 258 |
+
|
| 259 |
+
def calculate_all_months_from_previous(prev_month_volume, monthly_data, completed_year, completed_month):
|
| 260 |
+
"""์ ์์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ์ ๊ฒ์๋ ์ญ์ฐ"""
|
| 261 |
+
if not monthly_data or "results" not in monthly_data:
|
| 262 |
+
logger.warning("์๋ณ ๋ฐ์ดํฐ๊ฐ ์์ด ์ญ์ฐ ๊ณ์ฐ์ ๊ฑด๋๋๋๋ค.")
|
| 263 |
+
return [], []
|
| 264 |
+
|
| 265 |
+
monthly_volumes = []
|
| 266 |
+
dates = []
|
| 267 |
+
|
| 268 |
+
try:
|
| 269 |
+
for result in monthly_data["results"]:
|
| 270 |
+
keyword = result["title"]
|
| 271 |
+
|
| 272 |
+
if not result["data"]:
|
| 273 |
+
continue
|
| 274 |
+
|
| 275 |
+
# ์ ์(๊ธฐ์ค์) ๋น์จ ์ฐพ๊ธฐ
|
| 276 |
+
base_ratio = None
|
| 277 |
+
for data_point in result["data"]:
|
| 278 |
+
try:
|
| 279 |
+
date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
|
| 280 |
+
if date_obj.year == completed_year and date_obj.month == completed_month:
|
| 281 |
+
base_ratio = data_point["ratio"]
|
| 282 |
+
break
|
| 283 |
+
except:
|
| 284 |
+
continue
|
| 285 |
+
|
| 286 |
+
if base_ratio is None or base_ratio == 0:
|
| 287 |
+
logger.warning(f"'{keyword}' ๊ธฐ์ค์ ๋น์จ์ ์ฐพ์ ์ ์์ต๋๋ค.")
|
| 288 |
+
continue
|
| 289 |
+
|
| 290 |
+
logger.info(f"'{keyword}' ๊ธฐ์ค์ {completed_year}.{completed_month:02d} ๋น์จ: {base_ratio}% (๊ฒ์๋: {prev_month_volume:,}ํ)")
|
| 291 |
+
|
| 292 |
+
# ๋ชจ๋ ์ ๊ฒ์๋ ์ญ์ฐ
|
| 293 |
+
for data_point in result["data"]:
|
| 294 |
+
try:
|
| 295 |
+
date_obj = datetime.strptime(data_point["period"], "%Y-%m-%d")
|
| 296 |
+
ratio = data_point["ratio"]
|
| 297 |
+
|
| 298 |
+
# ํด๋น ์์ ์ผ์ ๊ฐ์ ธ์ค๊ธฐ
|
| 299 |
+
month_days = calendar.monthrange(date_obj.year, date_obj.month)[1]
|
| 300 |
+
base_month_days = calendar.monthrange(completed_year, completed_month)[1]
|
| 301 |
+
|
| 302 |
+
# ์ญ์ฐ ๊ณ์ฐ: (ํด๋น์ ๋น์จ / ๊ธฐ์ค์ ๋น์จ) ร ๊ธฐ์ค์ ๊ฒ์๋ ร (ํด๋น์ ์ผ์ / ๊ธฐ์ค์ ์ผ์)
|
| 303 |
+
calculated_volume = int(
|
| 304 |
+
(ratio / base_ratio) * prev_month_volume * (month_days / base_month_days)
|
| 305 |
+
)
|
| 306 |
+
calculated_volume = max(calculated_volume, 0) # ์์ ๋ฐฉ์ง
|
| 307 |
+
|
| 308 |
+
monthly_volumes.append(calculated_volume)
|
| 309 |
+
dates.append(data_point["period"])
|
| 310 |
+
|
| 311 |
+
except:
|
| 312 |
+
continue
|
| 313 |
+
|
| 314 |
+
logger.info(f"'{keyword}' ์ ์ฒด ์๋ณ ๊ฒ์๋ ์ญ์ฐ ์๋ฃ: {len(monthly_volumes)}๊ฐ์")
|
| 315 |
+
break # ์ฒซ ๋ฒ์งธ ํค์๋๋ง ์ฒ๋ฆฌ
|
| 316 |
+
|
| 317 |
+
except Exception as e:
|
| 318 |
+
logger.error(f"์๋ณ ์ญ์ฐ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 319 |
+
return [], []
|
| 320 |
+
|
| 321 |
+
return monthly_volumes, dates
|
| 322 |
+
|
| 323 |
+
def generate_future_from_growth_rate(monthly_volumes, dates, completed_year, completed_month):
|
| 324 |
+
"""์ฆ๊ฐ์จ ๊ธฐ๋ฐ ๋ฏธ๋ 3๊ฐ์ ์์ ์์ฑ"""
|
| 325 |
+
if len(monthly_volumes) < 12:
|
| 326 |
+
logger.warning("๋ฏธ๋ ์์ธก์ ์ํ ์ถฉ๋ถํ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
|
| 327 |
+
return [], []
|
| 328 |
+
|
| 329 |
+
try:
|
| 330 |
+
# ์๋
๋์๋ค๊ณผ ์ฌํด ์์ฑ๋ ์๋ค ๋น๊ตํ์ฌ ์ฆ๊ฐ์จ ๊ณ์ฐ
|
| 331 |
+
this_year_volumes = []
|
| 332 |
+
last_year_volumes = []
|
| 333 |
+
|
| 334 |
+
for i, date_str in enumerate(dates):
|
| 335 |
+
try:
|
| 336 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 337 |
+
|
| 338 |
+
# ์ฌํด ์์ฑ๋ ๋ฐ์ดํฐ (1์ ~ ์์ฑ๋ ์)
|
| 339 |
+
if date_obj.year == completed_year and date_obj.month <= completed_month:
|
| 340 |
+
this_year_volumes.append(monthly_volumes[i])
|
| 341 |
+
|
| 342 |
+
# ์๋
๋์ผ ๊ธฐ๊ฐ ๋ฐ์ดํฐ
|
| 343 |
+
if date_obj.year == completed_year - 1 and date_obj.month <= completed_month:
|
| 344 |
+
last_year_volumes.append(monthly_volumes[i])
|
| 345 |
+
|
| 346 |
+
except:
|
| 347 |
+
continue
|
| 348 |
+
|
| 349 |
+
# ์ฆ๊ฐ์จ ๊ณ์ฐ
|
| 350 |
+
if len(this_year_volumes) >= 3 and len(last_year_volumes) >= 3:
|
| 351 |
+
this_year_avg = sum(this_year_volumes) / len(this_year_volumes)
|
| 352 |
+
last_year_avg = sum(last_year_volumes) / len(last_year_volumes)
|
| 353 |
+
|
| 354 |
+
if last_year_avg > 0:
|
| 355 |
+
growth_rate = (this_year_avg - last_year_avg) / last_year_avg
|
| 356 |
+
# ์ฆ๊ฐ์จ ๋ฒ์ ์ ํ (-50% ~ +100%)
|
| 357 |
+
growth_rate = max(-0.5, min(growth_rate, 1.0))
|
| 358 |
+
else:
|
| 359 |
+
growth_rate = 0
|
| 360 |
+
else:
|
| 361 |
+
growth_rate = 0
|
| 362 |
+
|
| 363 |
+
logger.info(f"๊ณ์ฐ๋ ์ฆ๊ฐ์จ: {growth_rate*100:+.1f}%")
|
| 364 |
+
|
| 365 |
+
# ๋ฏธ๋ 3๊ฐ์ ์์
|
| 366 |
+
predicted_volumes = []
|
| 367 |
+
predicted_dates = []
|
| 368 |
+
|
| 369 |
+
for month_offset in range(1, 4): # 1, 2, 3๊ฐ์ ํ
|
| 370 |
+
pred_year = completed_year
|
| 371 |
+
pred_month = completed_month + month_offset
|
| 372 |
+
|
| 373 |
+
while pred_month > 12:
|
| 374 |
+
pred_month -= 12
|
| 375 |
+
pred_year += 1
|
| 376 |
+
|
| 377 |
+
# ์๋
๋์ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
|
| 378 |
+
last_year_pred_year = pred_year - 1
|
| 379 |
+
last_year_pred_month = pred_month
|
| 380 |
+
last_year_volume = None
|
| 381 |
+
|
| 382 |
+
for i, date_str in enumerate(dates):
|
| 383 |
+
try:
|
| 384 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 385 |
+
if date_obj.year == last_year_pred_year and date_obj.month == last_year_pred_month:
|
| 386 |
+
last_year_volume = monthly_volumes[i]
|
| 387 |
+
break
|
| 388 |
+
except:
|
| 389 |
+
continue
|
| 390 |
+
|
| 391 |
+
# ์์ ๊ฒ์๋ = ์๋
๋์ ร (1 + ์ฆ๊ฐ์จ)
|
| 392 |
+
if last_year_volume is not None:
|
| 393 |
+
predicted_volume = int(last_year_volume * (1 + growth_rate))
|
| 394 |
+
predicted_volume = max(predicted_volume, 0) # ์์ ๋ฐฉ์ง
|
| 395 |
+
|
| 396 |
+
predicted_volumes.append(predicted_volume)
|
| 397 |
+
predicted_dates.append(f"{pred_year:04d}-{pred_month:02d}-01")
|
| 398 |
+
|
| 399 |
+
logger.info(f"์์ {pred_year}.{pred_month:02d}: ์๋
๋์ {last_year_volume:,}ํ โ ์์ {predicted_volume:,}ํ")
|
| 400 |
+
|
| 401 |
+
return predicted_volumes, predicted_dates
|
| 402 |
+
|
| 403 |
+
except Exception as e:
|
| 404 |
+
logger.error(f"๋ฏธ๋ ์์ธก ์์ฑ ์ค๋ฅ: {e}")
|
| 405 |
+
return [], []
|
| 406 |
+
|
| 407 |
+
def apply_final_10_percent_reduction(monthly_data):
|
| 408 |
+
"""์ต์ข
๋จ๊ณ: ๋ชจ๋ ๊ฒฐ๊ณผ์ 10% ๊ฐ์ ์ ์ฉ"""
|
| 409 |
+
adjusted_data = {}
|
| 410 |
+
|
| 411 |
+
try:
|
| 412 |
+
for keyword, data in monthly_data.items():
|
| 413 |
+
adjusted_volumes = []
|
| 414 |
+
|
| 415 |
+
for volume in data["monthly_volumes"]:
|
| 416 |
+
if volume >= 10:
|
| 417 |
+
adjusted_volume = int(volume * 0.9) # 10% ๊ฐ์
|
| 418 |
+
else:
|
| 419 |
+
adjusted_volume = volume # 10 ๋ฏธ๋ง์ ๊ทธ๋๋ก
|
| 420 |
+
|
| 421 |
+
adjusted_volumes.append(adjusted_volume)
|
| 422 |
+
|
| 423 |
+
# ๋ฐ์ดํฐ ๋ณต์ฌ ๋ฐ ๊ฒ์๋ ์กฐ์
|
| 424 |
+
adjusted_data[keyword] = data.copy()
|
| 425 |
+
adjusted_data[keyword]["monthly_volumes"] = adjusted_volumes
|
| 426 |
+
|
| 427 |
+
# ํ์ฌ ๊ฒ์๋๋ ์กฐ์
|
| 428 |
+
if data["current_volume"] >= 10:
|
| 429 |
+
adjusted_data[keyword]["current_volume"] = int(data["current_volume"] * 0.9)
|
| 430 |
+
|
| 431 |
+
logger.info("์ต์ข
10% ๊ฐ์ ์กฐ์ ์๋ฃ")
|
| 432 |
+
|
| 433 |
+
except Exception as e:
|
| 434 |
+
logger.error(f"10% ๊ฐ์ ์กฐ์ ์ค๋ฅ: {e}")
|
| 435 |
+
return monthly_data
|
| 436 |
+
|
| 437 |
+
return adjusted_data
|
| 438 |
+
|
| 439 |
+
# ===== ๋ฉ์ธ ํจ์๋ค (๊ธฐ์กด ํธํ์ฑ ์ ์ง) =====
|
| 440 |
+
|
| 441 |
+
def get_naver_trend_data_v5(keywords, period="1year", max_retries=3):
|
| 442 |
+
"""๊ฐ์ ๋ ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ API ํธ์ถ - ์ด์ค ํธ์ถ ๊ตฌํ"""
|
| 443 |
+
|
| 444 |
+
if period == "1year":
|
| 445 |
+
# ์ด์ค API ํธ์ถ
|
| 446 |
+
daily_data = get_daily_trend_data(keywords, max_retries)
|
| 447 |
+
monthly_data = get_monthly_trend_data(keywords, max_retries)
|
| 448 |
+
|
| 449 |
+
# ๊ฒฐ๊ณผ ๋ฐํ (๊ธฐ์กด ์ฝ๋์ ํธํ์ฑ ์ ์ง๋ฅผ ์ํด ์๋ณ ๋ฐ์ดํฐ ์ฐ์ ๋ฐํ)
|
| 450 |
+
return {
|
| 451 |
+
'daily_data': daily_data,
|
| 452 |
+
'monthly_data': monthly_data,
|
| 453 |
+
'results': monthly_data['results'] if monthly_data else []
|
| 454 |
+
}
|
| 455 |
+
else: # 3year
|
| 456 |
+
# 3๋
๋ฐ์ดํฐ๋ ์๋ณ๋ง ํธ์ถ (๊ธฐ์กด ๋ฐฉ์ ์ ์ง)
|
| 457 |
+
monthly_data = get_monthly_trend_data(keywords, max_retries)
|
| 458 |
+
return monthly_data
|
| 459 |
+
|
| 460 |
+
def calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period="1year"):
|
| 461 |
+
"""๊ฐ์ ๋ ์๋ณ ๊ฒ์๋ ๊ณ์ฐ - ์ ๊ตํ ์ญ์ฐ ๋ก์ง ์ ์ฉ"""
|
| 462 |
+
monthly_data = {}
|
| 463 |
+
|
| 464 |
+
# ํธ๋ ๋ ๋ฐ์ดํฐ ํ์ธ
|
| 465 |
+
if isinstance(trend_data, dict) and 'daily_data' in trend_data and 'monthly_data' in trend_data:
|
| 466 |
+
# ์๋ก์ด ์ด์ค ๋ฐ์ดํฐ ๊ตฌ์กฐ
|
| 467 |
+
daily_data = trend_data['daily_data']
|
| 468 |
+
monthly_data_api = trend_data['monthly_data']
|
| 469 |
+
else:
|
| 470 |
+
# ๊ธฐ์กด ๋จ์ผ ๋ฐ์ดํฐ ๊ตฌ์กฐ (3year ๋ฑ)
|
| 471 |
+
daily_data = None
|
| 472 |
+
monthly_data_api = trend_data
|
| 473 |
+
|
| 474 |
+
if not monthly_data_api or "results" not in monthly_data_api:
|
| 475 |
+
logger.warning("์๋ณ ํธ๋ ๋ ๋ฐ์ดํฐ๊ฐ ์์ด ๊ณ์ฐ์ ๊ฑด๋๋๋๋ค.")
|
| 476 |
+
return monthly_data
|
| 477 |
+
|
| 478 |
+
logger.info(f"๊ฐ์ ๋ ์๋ณ ๊ฒ์๋ ๊ณ์ฐ ์์: {len(monthly_data_api['results'])}๊ฐ ํค์๋")
|
| 479 |
+
|
| 480 |
+
for result in monthly_data_api["results"]:
|
| 481 |
+
keyword = result["title"]
|
| 482 |
+
api_keyword = keyword.replace(" ", "")
|
| 483 |
+
|
| 484 |
+
# ํ์ฌ ๊ฒ์๋
|
| 485 |
+
volume_data = current_volumes.get(api_keyword, {"์ด๊ฒ์๋": 0})
|
| 486 |
+
current_volume = volume_data["์ด๊ฒ์๋"]
|
| 487 |
+
|
| 488 |
+
if current_volume == 0:
|
| 489 |
+
logger.warning(f"'{keyword}' ํ์ฌ ๊ฒ์๋์ด 0์ด๋ฏ๋ก ๊ณ์ฐ์ ๊ฑด๋๋๋๋ค.")
|
| 490 |
+
continue
|
| 491 |
+
|
| 492 |
+
logger.info(f"'{keyword}' ์ฒ๋ฆฌ ์์ - ํ์ฌ ๊ฒ์๋: {current_volume:,}ํ")
|
| 493 |
+
|
| 494 |
+
if period == "1year" and daily_data:
|
| 495 |
+
# ๐ฅ ์๋ก์ด ์ ๊ตํ ๋ก์ง ์ ์ฉ
|
| 496 |
+
completed_year, completed_month = get_complete_month()
|
| 497 |
+
|
| 498 |
+
# 1๋จ๊ณ: ์ผ๋ณ ๋ฐ์ดํฐ๋ก ์ ์ ์ ํํ ๊ฒ์๋ ๊ณ์ฐ
|
| 499 |
+
prev_month_volume = calculate_previous_month_from_daily(current_volume, daily_data)
|
| 500 |
+
|
| 501 |
+
# 2๋จ๊ณ: ์ ์์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ์ ๊ฒ์๋ ์ญ์ฐ
|
| 502 |
+
monthly_volumes, dates = calculate_all_months_from_previous(
|
| 503 |
+
prev_month_volume, monthly_data_api, completed_year, completed_month
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
if not monthly_volumes:
|
| 507 |
+
logger.warning(f"'{keyword}' ์๋ณ ๊ฒ์๋ ๊ณ์ฐ ์คํจ")
|
| 508 |
+
continue
|
| 509 |
+
|
| 510 |
+
# 3๋จ๊ณ: ๋ฏธ๋ 3๊ฐ์ ์์ ์์ฑ
|
| 511 |
+
predicted_volumes, predicted_dates = generate_future_from_growth_rate(
|
| 512 |
+
monthly_volumes, dates, completed_year, completed_month
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
+
# ์ค์ + ์์ ๋ฐ์ดํฐ ๊ฒฐํฉ - ์ต๊ทผ 12๊ฐ์ + ํฅํ 3๊ฐ์ = ์ด 15๊ฐ์
|
| 516 |
+
# ์ต๊ทผ 12๊ฐ์๋ง ์ค์ ๋ฐ์ดํฐ๋ก ์ ํ
|
| 517 |
+
recent_12_months = monthly_volumes[-12:] if len(monthly_volumes) >= 12 else monthly_volumes
|
| 518 |
+
recent_12_dates = dates[-12:] if len(dates) >= 12 else dates
|
| 519 |
+
|
| 520 |
+
all_volumes = recent_12_months + predicted_volumes
|
| 521 |
+
all_dates = recent_12_dates + predicted_dates
|
| 522 |
+
|
| 523 |
+
# ์ฆ๊ฐ์จ ๊ณ์ฐ (์์ 3๊ฐ์)
|
| 524 |
+
growth_rate = calculate_future_3month_growth_rate(all_volumes, all_dates)
|
| 525 |
+
|
| 526 |
+
monthly_data[keyword] = {
|
| 527 |
+
"monthly_volumes": all_volumes, # 10% ๊ฐ์ ์ ์ฉ ์ - ์ด 15๊ฐ์ (12๊ฐ์ ์ค์ + 3๊ฐ์ ์์)
|
| 528 |
+
"dates": all_dates,
|
| 529 |
+
"current_volume": current_volume, # 10% ๊ฐ์ ์ ์ฉ ์
|
| 530 |
+
"growth_rate": growth_rate,
|
| 531 |
+
"volume_per_percent": prev_month_volume / 100 if prev_month_volume > 0 else 0,
|
| 532 |
+
"current_ratio": 100,
|
| 533 |
+
"actual_count": len(recent_12_months), # ์ค์ 12๊ฐ์
|
| 534 |
+
"predicted_count": len(predicted_volumes) # ์์ 3๊ฐ์
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
else:
|
| 538 |
+
# ๊ธฐ์กด ๋ฐฉ์ (3year ๋ฑ)
|
| 539 |
+
if not result["data"]:
|
| 540 |
+
continue
|
| 541 |
+
|
| 542 |
+
current_ratio = result["data"][-1]["ratio"]
|
| 543 |
+
if current_ratio == 0:
|
| 544 |
+
continue
|
| 545 |
+
|
| 546 |
+
volume_per_percent = current_volume / current_ratio
|
| 547 |
+
|
| 548 |
+
monthly_volumes = []
|
| 549 |
+
dates = []
|
| 550 |
+
|
| 551 |
+
for data_point in result["data"]:
|
| 552 |
+
ratio = data_point["ratio"]
|
| 553 |
+
period_date = data_point["period"]
|
| 554 |
+
estimated_volume = int(volume_per_percent * ratio)
|
| 555 |
+
|
| 556 |
+
monthly_volumes.append(estimated_volume)
|
| 557 |
+
dates.append(period_date)
|
| 558 |
+
|
| 559 |
+
growth_rate = calculate_3year_growth_rate_improved(monthly_volumes)
|
| 560 |
+
|
| 561 |
+
monthly_data[keyword] = {
|
| 562 |
+
"monthly_volumes": monthly_volumes, # 10% ๊ฐ์ ์ ์ฉ ์
|
| 563 |
+
"dates": dates,
|
| 564 |
+
"current_volume": current_volume, # 10% ๊ฐ์ ์ ์ฉ ์
|
| 565 |
+
"growth_rate": growth_rate,
|
| 566 |
+
"volume_per_percent": volume_per_percent,
|
| 567 |
+
"current_ratio": current_ratio,
|
| 568 |
+
"actual_count": len(monthly_volumes),
|
| 569 |
+
"predicted_count": 0
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
logger.info(f"'{keyword}' ๊ณ์ฐ ์๋ฃ - ๊ฒ์๋ ๋ฐ์ดํฐ {len(monthly_data[keyword]['monthly_volumes'])}๊ฐ")
|
| 573 |
+
|
| 574 |
+
# ๐ฅ 4๋จ๊ณ: ์ต์ข
10% ๊ฐ์ ์ ์ฉ
|
| 575 |
+
final_data = apply_final_10_percent_reduction(monthly_data)
|
| 576 |
+
|
| 577 |
+
logger.info("๊ฐ์ ๋ ์๋ณ ๊ฒ์๋ ๊ณ์ฐ ์๋ฃ (10% ๊ฐ์ ์ ์ฉ๋จ)")
|
| 578 |
+
return final_data
|
| 579 |
+
|
| 580 |
+
# ===== ์ฆ๊ฐ์จ ๊ณ์ฐ ํจ์๋ค (๊ธฐ์กด ์ ์ง) =====
|
| 581 |
+
|
| 582 |
+
def calculate_future_3month_growth_rate(volumes, dates):
|
| 583 |
+
"""์์ 3๊ฐ์ ์ฆ๊ฐ์จ ๊ณ์ฐ"""
|
| 584 |
+
if len(volumes) < 4:
|
| 585 |
+
return 0
|
| 586 |
+
|
| 587 |
+
try:
|
| 588 |
+
completed_year, completed_month = get_complete_month()
|
| 589 |
+
|
| 590 |
+
# ๊ธฐ์ค์ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
|
| 591 |
+
base_month_volume = None
|
| 592 |
+
for i, date_str in enumerate(dates):
|
| 593 |
+
try:
|
| 594 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 595 |
+
if date_obj.year == completed_year and date_obj.month == completed_month:
|
| 596 |
+
base_month_volume = volumes[i]
|
| 597 |
+
break
|
| 598 |
+
except:
|
| 599 |
+
continue
|
| 600 |
+
|
| 601 |
+
if base_month_volume is None:
|
| 602 |
+
return 0
|
| 603 |
+
|
| 604 |
+
# ํฅํ 3๊ฐ์ ์์ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
|
| 605 |
+
future_volumes = []
|
| 606 |
+
for month_offset in range(1, 4):
|
| 607 |
+
pred_year = completed_year
|
| 608 |
+
pred_month = completed_month + month_offset
|
| 609 |
+
|
| 610 |
+
while pred_month > 12:
|
| 611 |
+
pred_month -= 12
|
| 612 |
+
pred_year += 1
|
| 613 |
+
|
| 614 |
+
for i, date_str in enumerate(dates):
|
| 615 |
+
try:
|
| 616 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 617 |
+
if date_obj.year == pred_year and date_obj.month == pred_month:
|
| 618 |
+
future_volumes.append(volumes[i])
|
| 619 |
+
break
|
| 620 |
+
except:
|
| 621 |
+
continue
|
| 622 |
+
|
| 623 |
+
if len(future_volumes) < 3:
|
| 624 |
+
return 0
|
| 625 |
+
|
| 626 |
+
# ์ฆ๊ฐ์จ ๊ณ์ฐ
|
| 627 |
+
future_average = sum(future_volumes) / len(future_volumes)
|
| 628 |
+
|
| 629 |
+
if base_month_volume > 0:
|
| 630 |
+
growth_rate = ((future_average - base_month_volume) / base_month_volume) * 100
|
| 631 |
+
return min(max(growth_rate, -50), 100)
|
| 632 |
+
|
| 633 |
+
return 0
|
| 634 |
+
|
| 635 |
+
except Exception as e:
|
| 636 |
+
logger.error(f"์์ 3๊ฐ์ ์ฆ๊ฐ์จ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 637 |
+
return 0
|
| 638 |
+
|
| 639 |
+
def calculate_3year_growth_rate_improved(volumes):
|
| 640 |
+
"""3๋
์ฆ๊ฐ์จ ๊ณ์ฐ"""
|
| 641 |
+
if len(volumes) < 24:
|
| 642 |
+
return 0
|
| 643 |
+
|
| 644 |
+
try:
|
| 645 |
+
first_year = volumes[:12]
|
| 646 |
+
last_year = volumes[-12:]
|
| 647 |
+
|
| 648 |
+
first_year_avg = sum(first_year) / len(first_year)
|
| 649 |
+
last_year_avg = sum(last_year) / len(last_year)
|
| 650 |
+
|
| 651 |
+
if first_year_avg == 0:
|
| 652 |
+
return 0
|
| 653 |
+
|
| 654 |
+
growth_rate = ((last_year_avg - first_year_avg) / first_year_avg) * 100
|
| 655 |
+
return min(max(growth_rate, -50), 200)
|
| 656 |
+
|
| 657 |
+
except Exception as e:
|
| 658 |
+
logger.error(f"3๋
์ฆ๊ฐ์จ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 659 |
+
return 0
|
| 660 |
+
|
| 661 |
+
def calculate_correct_growth_rate(volumes, dates):
|
| 662 |
+
"""์๋
๋๋น ์ฆ๊ฐ์จ ๊ณ์ฐ"""
|
| 663 |
+
if len(volumes) < 13:
|
| 664 |
+
return 0
|
| 665 |
+
|
| 666 |
+
try:
|
| 667 |
+
completed_year, completed_month = get_complete_month()
|
| 668 |
+
|
| 669 |
+
this_year_volume = None
|
| 670 |
+
last_year_volume = None
|
| 671 |
+
|
| 672 |
+
for i, date_str in enumerate(dates):
|
| 673 |
+
try:
|
| 674 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 675 |
+
|
| 676 |
+
if date_obj.year == completed_year and date_obj.month == completed_month:
|
| 677 |
+
this_year_volume = volumes[i]
|
| 678 |
+
|
| 679 |
+
if date_obj.year == completed_year - 1 and date_obj.month == completed_month:
|
| 680 |
+
last_year_volume = volumes[i]
|
| 681 |
+
|
| 682 |
+
except:
|
| 683 |
+
continue
|
| 684 |
+
|
| 685 |
+
if this_year_volume is not None and last_year_volume is not None and last_year_volume > 0:
|
| 686 |
+
growth_rate = ((this_year_volume - last_year_volume) / last_year_volume) * 100
|
| 687 |
+
return min(max(growth_rate, -50), 100)
|
| 688 |
+
|
| 689 |
+
return 0
|
| 690 |
+
|
| 691 |
+
except Exception as e:
|
| 692 |
+
logger.error(f"์๋
๋๋น ์ฆ๊ฐ์จ ๊ณ์ฐ ์ค๋ฅ: {e}")
|
| 693 |
+
return 0
|
| 694 |
+
|
| 695 |
+
def generate_future_predictions_correct(volumes, dates, growth_rate):
|
| 696 |
+
"""๋ฏธ๋ ์์ธก ์์ฑ (ํธํ์ฑ ์ ์ง)"""
|
| 697 |
+
return generate_future_from_growth_rate(volumes, dates, *get_complete_month())
|
| 698 |
+
|
| 699 |
+
# ===== ์ฐจํธ ์์ฑ ํจ์๋ค =====
|
| 700 |
+
|
| 701 |
+
def create_enhanced_current_chart(volume_data, keyword):
|
| 702 |
+
"""ํฅ์๋ ํ์ฌ ๊ฒ์๋ ์ ๋ณด ์ฐจํธ - PC vs ๋ชจ๋ฐ์ผ ๋น์จ ํฌํจ"""
|
| 703 |
+
total_vol = volume_data['์ด๊ฒ์๋']
|
| 704 |
+
pc_vol = volume_data['PC๊ฒ์๋']
|
| 705 |
+
mobile_vol = volume_data['๋ชจ๋ฐ์ผ๊ฒ์๋']
|
| 706 |
+
|
| 707 |
+
# ๊ฒ์๋ ์์ค ํ๊ฐ
|
| 708 |
+
if total_vol >= 100000:
|
| 709 |
+
level_text = "๋์ ๐ฅ"
|
| 710 |
+
level_color = "#dc3545"
|
| 711 |
+
elif total_vol >= 10000:
|
| 712 |
+
level_text = "์ค๊ฐ ๐"
|
| 713 |
+
level_color = "#ffc107"
|
| 714 |
+
elif total_vol > 0:
|
| 715 |
+
level_text = "๋ฎ์ ๐"
|
| 716 |
+
level_color = "#6c757d"
|
| 717 |
+
else:
|
| 718 |
+
level_text = "๋ฐ์ดํฐ ์์ โ ๏ธ"
|
| 719 |
+
level_color = "#6c757d"
|
| 720 |
+
|
| 721 |
+
# PC vs ๋ชจ๋ฐ์ผ ๋น์จ
|
| 722 |
+
if total_vol > 0:
|
| 723 |
+
pc_ratio = (pc_vol / total_vol) * 100
|
| 724 |
+
mobile_ratio = (mobile_vol / total_vol) * 100
|
| 725 |
+
else:
|
| 726 |
+
pc_ratio = mobile_ratio = 0
|
| 727 |
+
|
| 728 |
+
return f"""
|
| 729 |
+
<div style="width: 100%; padding: 30px; font-family: 'Pretendard', sans-serif; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
|
| 730 |
+
<!-- ๊ฒ์๋ ์์ค ํ์ -->
|
| 731 |
+
<div style="text-align: center; margin-bottom: 25px; padding: 20px; background: #f8f9fa; border-radius: 12px;">
|
| 732 |
+
<h4 style="margin: 0 0 15px 0; color: #495057; font-size: 20px;">๐ ๊ฒ์๋ ์์ค</h4>
|
| 733 |
+
<span style="display: inline-block; padding: 12px 24px; background: {level_color}; color: white; border-radius: 25px; font-weight: bold; font-size: 18px;">
|
| 734 |
+
{level_text}
|
| 735 |
+
</span>
|
| 736 |
+
</div>
|
| 737 |
+
|
| 738 |
+
<!-- ๊ฒ์๋ ์์ธ ์ ๋ณด -->
|
| 739 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 25px;">
|
| 740 |
+
<div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
|
| 741 |
+
<div style="color: #007bff; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{pc_vol:,}</div>
|
| 742 |
+
<div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">PC ๊ฒ์๋</div>
|
| 743 |
+
<div style="color: #007bff; font-size: 14px;">({pc_ratio:.1f}%)</div>
|
| 744 |
+
</div>
|
| 745 |
+
<div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
|
| 746 |
+
<div style="color: #28a745; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{mobile_vol:,}</div>
|
| 747 |
+
<div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">๋ชจ๋ฐ์ผ ๊ฒ์๋</div>
|
| 748 |
+
<div style="color: #28a745; font-size: 14px;">({mobile_ratio:.1f}%)</div>
|
| 749 |
+
</div>
|
| 750 |
+
<div style="background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; border: 1px solid #e9ecef;">
|
| 751 |
+
<div style="color: #dc3545; font-size: 32px; font-weight: bold; margin-bottom: 8px;">{total_vol:,}</div>
|
| 752 |
+
<div style="color: #6c757d; font-size: 16px; margin-bottom: 8px; font-weight: 600;">์ด ๊ฒ์๋</div>
|
| 753 |
+
<div style="color: #dc3545; font-size: 14px;">(100%)</div>
|
| 754 |
+
</div>
|
| 755 |
+
</div>
|
| 756 |
+
|
| 757 |
+
<!-- ๋น์จ ๋ฐ ์ฐจํธ -->
|
| 758 |
+
<div style="background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
|
| 759 |
+
<h5 style="margin: 0 0 20px 0; color: #495057; text-align: center; font-size: 18px;">PC vs ๋ชจ๋ฐ์ผ ๋น์จ</h5>
|
| 760 |
+
<div style="display: flex; height: 25px; border-radius: 15px; overflow: hidden; background: #e9ecef;">
|
| 761 |
+
<div style="background: #007bff; width: {pc_ratio}%; display: flex; align-items: center; justify-content: center; font-size: 14px; color: white; font-weight: bold;">
|
| 762 |
+
{f'PC {pc_ratio:.1f}%' if pc_ratio > 15 else ''}
|
| 763 |
+
</div>
|
| 764 |
+
<div style="background: #28a745; width: {mobile_ratio}%; display: flex; align-items: center; justify-content: center; font-size: 14px; color: white; font-weight: bold;">
|
| 765 |
+
{f'๋ชจ๋ฐ์ผ {mobile_ratio:.1f}%' if mobile_ratio > 15 else ''}
|
| 766 |
+
</div>
|
| 767 |
+
</div>
|
| 768 |
+
</div>
|
| 769 |
+
|
| 770 |
+
<div style="margin-top: 20px; padding: 15px; background: #fff3cd; border-radius: 8px; text-align: center;">
|
| 771 |
+
<p style="margin: 0; font-size: 14px; color: #856404;">
|
| 772 |
+
๐ <strong>ํธ๋ ๋ ๋ถ์ ์์คํ
</strong>: ๋ค์ด๋ฒ ๋ฐ์ดํฐ๋ฉ ๊ธฐ๋ฐ ์ ํํ ๊ฒ์๋ ๋ถ์
|
| 773 |
+
</p>
|
| 774 |
+
</div>
|
| 775 |
+
</div>
|
| 776 |
+
"""
|
| 777 |
+
|
| 778 |
+
def create_visual_trend_chart(monthly_data_1year, monthly_data_3year):
|
| 779 |
+
"""์๊ฐ์ ํธ๋ ๋ ์ฐจํธ ์์ฑ"""
|
| 780 |
+
try:
|
| 781 |
+
chart_html = f"""
|
| 782 |
+
<div style="width: 100%; margin: 20px auto; font-family: 'Pretendard', sans-serif;">
|
| 783 |
+
"""
|
| 784 |
+
|
| 785 |
+
periods = [
|
| 786 |
+
{"data": monthly_data_1year, "title": "์ต๊ทผ 1๋
+ ํฅํ 3๊ฐ์ ์์ (์ ๊ตํ ์ญ์ฐ)", "period": "1year"},
|
| 787 |
+
{"data": monthly_data_3year, "title": "์ต๊ทผ 3๋
(10% ๋ณด์ ์ ์ฉ)", "period": "3year"}
|
| 788 |
+
]
|
| 789 |
+
|
| 790 |
+
colors = ['#FB7F0D', '#4ECDC4', '#45B7D1', '#96CEB4', '#FF6B6B']
|
| 791 |
+
|
| 792 |
+
for period_info in periods:
|
| 793 |
+
monthly_data = period_info["data"]
|
| 794 |
+
period_title = period_info["title"]
|
| 795 |
+
period_code = period_info["period"]
|
| 796 |
+
|
| 797 |
+
if not monthly_data:
|
| 798 |
+
chart_html += f"""
|
| 799 |
+
<div style="width: 100%; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin-bottom: 30px; border: 1px solid #e9ecef;">
|
| 800 |
+
<h4 style="text-align: center; color: #666; margin: 20px 0;">{period_title} - ํธ๋ ๋ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.</h4>
|
| 801 |
+
</div>
|
| 802 |
+
"""
|
| 803 |
+
continue
|
| 804 |
+
|
| 805 |
+
chart_html += f"""
|
| 806 |
+
<div style="width: 100%; background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin-bottom: 30px; border: 1px solid #e9ecef;">
|
| 807 |
+
<h4 style="text-align: center; color: #333; margin-bottom: 25px; font-size: 18px; border-bottom: 2px solid #FB7F0D; padding-bottom: 10px;">
|
| 808 |
+
๐ {period_title}
|
| 809 |
+
</h4>
|
| 810 |
+
"""
|
| 811 |
+
|
| 812 |
+
# ๊ฐ ํค์๋๋ณ๋ก ์ฐจํธ ์์ฑ
|
| 813 |
+
for i, (keyword, data) in enumerate(monthly_data.items()):
|
| 814 |
+
volumes = data["monthly_volumes"]
|
| 815 |
+
dates = data["dates"]
|
| 816 |
+
growth_rate = data["growth_rate"]
|
| 817 |
+
actual_count = data.get("actual_count", len(volumes))
|
| 818 |
+
|
| 819 |
+
if not volumes:
|
| 820 |
+
continue
|
| 821 |
+
|
| 822 |
+
# ์ฐจํธ ์์
|
| 823 |
+
color = colors[i % len(colors)]
|
| 824 |
+
predicted_color = f"{color}80" # 50% ํฌ๋ช
๋
|
| 825 |
+
|
| 826 |
+
# Y์ถ์ 0๋ถํฐ ์ต๋๊ฐ๊น์ง๋ก ์ค์
|
| 827 |
+
max_volume = max(volumes) if volumes else 1
|
| 828 |
+
|
| 829 |
+
chart_html += f"""
|
| 830 |
+
<div style="width: 100%; margin-bottom: 30px; border: 1px solid #e9ecef; border-radius: 8px; overflow: hidden;">
|
| 831 |
+
<div style="padding: 20px; background: white;">
|
| 832 |
+
<h5 style="margin: 0 0 20px 0; color: #333; font-size: 16px;">
|
| 833 |
+
{keyword} ({get_growth_rate_label(period_code)}: {growth_rate:+.1f}%)
|
| 834 |
+
</h5>
|
| 835 |
+
|
| 836 |
+
<!-- ์ฐจํธ ์์ญ -->
|
| 837 |
+
<div style="position: relative; height: 350px; margin: 30px 0 60px 80px; border-left: 2px solid #333; border-bottom: 2px solid #333; padding: 10px;">
|
| 838 |
+
|
| 839 |
+
<!-- Y์ถ ๋ผ๋ฒจ -->
|
| 840 |
+
<div style="position: absolute; left: -70px; top: -10px; width: 60px; text-align: right; font-size: 11px; color: #333; font-weight: bold;">
|
| 841 |
+
{max_volume:,}
|
| 842 |
+
</div>
|
| 843 |
+
<div style="position: absolute; left: -70px; top: 50%; transform: translateY(-50%); width: 60px; text-align: right; font-size: 10px; color: #666;">
|
| 844 |
+
{max_volume // 2:,}
|
| 845 |
+
</div>
|
| 846 |
+
<div style="position: absolute; left: -70px; bottom: -5px; width: 60px; text-align: right; font-size: 10px; color: #666;">
|
| 847 |
+
0
|
| 848 |
+
</div>
|
| 849 |
+
|
| 850 |
+
<!-- X์ถ ๊ทธ๋ฆฌ๋ ๋ผ์ธ -->
|
| 851 |
+
<div style="position: absolute; top: 0; left: 0; right: 0; height: 1px; background: #eee;"></div>
|
| 852 |
+
<div style="position: absolute; top: 50%; left: 0; right: 0; height: 1px; background: #eee;"></div>
|
| 853 |
+
<div style="position: absolute; bottom: 0; left: 0; right: 0; height: 1px; background: #333;"></div>
|
| 854 |
+
|
| 855 |
+
<!-- ์ฐจํธ ๋ฐ ์ปจํ
์ด๋ -->
|
| 856 |
+
<div style="display: flex; align-items: end; height: 100%; gap: 1px; padding: 5px 0;">
|
| 857 |
+
"""
|
| 858 |
+
|
| 859 |
+
# ๋ฐ์ดํฐ์ ๋ ์ง๋ฅผ ์๊ฐ์์ผ๋ก ์ ๋ ฌ
|
| 860 |
+
chart_data = list(zip(dates, volumes, range(len(volumes))))
|
| 861 |
+
chart_data.sort(key=lambda x: x[0]) # ๋ ์ง์ ์ ๋ ฌ
|
| 862 |
+
|
| 863 |
+
# ๋ง๋ ์ฐจํธ ์์ฑ
|
| 864 |
+
for date, volume, original_index in chart_data:
|
| 865 |
+
# ๋ง๋ ๋์ด ๊ณ์ฐ
|
| 866 |
+
height_percent = (volume / max_volume) * 100 if max_volume > 0 else 0
|
| 867 |
+
|
| 868 |
+
# ์ค์ ๋ฐ์ดํฐ์ ์์ ๋ฐ์ดํฐ ๊ตฌ๋ถ
|
| 869 |
+
is_predicted = original_index >= actual_count
|
| 870 |
+
bar_color = predicted_color if is_predicted else color
|
| 871 |
+
|
| 872 |
+
# ๋ ์ง ํฌ๋งท
|
| 873 |
+
try:
|
| 874 |
+
date_obj = datetime.strptime(date, "%Y-%m-%d")
|
| 875 |
+
year_short = str(date_obj.year)[-2:]
|
| 876 |
+
month_num = date_obj.month
|
| 877 |
+
|
| 878 |
+
if is_predicted:
|
| 879 |
+
date_formatted = f"{year_short}.{month_num:02d}"
|
| 880 |
+
full_date = date_obj.strftime("%Y๋
%m์") + " (์์)"
|
| 881 |
+
bar_style = f"border: 2px dashed #333; background: repeating-linear-gradient(90deg, {bar_color}, {bar_color} 5px, transparent 5px, transparent 10px);"
|
| 882 |
+
else:
|
| 883 |
+
date_formatted = f"{year_short}.{month_num:02d}"
|
| 884 |
+
full_date = date_obj.strftime("%Y๋
%m์")
|
| 885 |
+
bar_style = f"background: linear-gradient(to top, {bar_color}, {bar_color}dd);"
|
| 886 |
+
except:
|
| 887 |
+
date_formatted = date[-5:].replace('-', '.')
|
| 888 |
+
full_date = date
|
| 889 |
+
bar_style = f"background: linear-gradient(to top, {bar_color}, {bar_color}dd);"
|
| 890 |
+
|
| 891 |
+
# ๊ณ ์ ID ์์ฑ
|
| 892 |
+
chart_id = f"bar_{period_code}_{i}_{original_index}"
|
| 893 |
+
|
| 894 |
+
chart_html += f"""
|
| 895 |
+
<div style="flex: 1; display: flex; flex-direction: column; align-items: center; position: relative; height: 100%;">
|
| 896 |
+
<!-- ๋ง๋ -->
|
| 897 |
+
<div id="{chart_id}" style="
|
| 898 |
+
{bar_style}
|
| 899 |
+
width: 100%;
|
| 900 |
+
height: {height_percent}%;
|
| 901 |
+
border-radius: 3px 3px 0 0;
|
| 902 |
+
position: relative;
|
| 903 |
+
cursor: pointer;
|
| 904 |
+
transition: all 0.3s ease;
|
| 905 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 906 |
+
min-height: 3px;
|
| 907 |
+
margin-top: auto;
|
| 908 |
+
"
|
| 909 |
+
onmouseover="
|
| 910 |
+
this.style.transform='scaleX(1.1)';
|
| 911 |
+
this.style.zIndex='10';
|
| 912 |
+
this.style.boxShadow='0 4px 8px rgba(0,0,0,0.3)';
|
| 913 |
+
document.getElementById('tooltip_{chart_id}').style.display='block';
|
| 914 |
+
"
|
| 915 |
+
onmouseout="
|
| 916 |
+
this.style.transform='scaleX(1)';
|
| 917 |
+
this.style.zIndex='1';
|
| 918 |
+
this.style.boxShadow='0 2px 4px rgba(0,0,0,0.1)';
|
| 919 |
+
document.getElementById('tooltip_{chart_id}').style.display='none';
|
| 920 |
+
">
|
| 921 |
+
<!-- ํดํ -->
|
| 922 |
+
<div id="tooltip_{chart_id}" style="
|
| 923 |
+
display: none;
|
| 924 |
+
position: absolute;
|
| 925 |
+
bottom: calc(100% + 10px);
|
| 926 |
+
left: 50%;
|
| 927 |
+
transform: translateX(-50%);
|
| 928 |
+
background: rgba(0,0,0,0.9);
|
| 929 |
+
color: white;
|
| 930 |
+
padding: 8px 12px;
|
| 931 |
+
border-radius: 6px;
|
| 932 |
+
font-size: 11px;
|
| 933 |
+
white-space: nowrap;
|
| 934 |
+
z-index: 1000;
|
| 935 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
|
| 936 |
+
pointer-events: none;
|
| 937 |
+
">
|
| 938 |
+
<div style="text-align: center;">
|
| 939 |
+
<div style="font-weight: bold; color: white; margin-bottom: 2px;">{full_date}</div>
|
| 940 |
+
<div style="color: #ffd700;">๊ฒ์๋: {volume:,}ํ</div>
|
| 941 |
+
{'<div style="color: #ff6b6b; margin-top: 2px; font-size: 10px;">์์ ๋ฐ์ดํฐ</div>' if is_predicted else '<div style="color: #90EE90; margin-top: 2px; font-size: 10px;">์ค์ ๋ฐ์ดํฐ</div>'}
|
| 942 |
+
</div>
|
| 943 |
+
<!-- ํ์ดํ -->
|
| 944 |
+
<div style="
|
| 945 |
+
position: absolute;
|
| 946 |
+
top: 100%;
|
| 947 |
+
left: 50%;
|
| 948 |
+
transform: translateX(-50%);
|
| 949 |
+
width: 0;
|
| 950 |
+
height: 0;
|
| 951 |
+
border-left: 5px solid transparent;
|
| 952 |
+
border-right: 5px solid transparent;
|
| 953 |
+
border-top: 5px solid rgba(0,0,0,0.9);
|
| 954 |
+
"></div>
|
| 955 |
+
</div>
|
| 956 |
+
</div>
|
| 957 |
+
</div>
|
| 958 |
+
"""
|
| 959 |
+
|
| 960 |
+
chart_html += f"""
|
| 961 |
+
</div>
|
| 962 |
+
|
| 963 |
+
<!-- ์ ๋ผ๋ฒจ -->
|
| 964 |
+
<div style="display: flex; gap: 1px; margin-top: 10px; padding: 0 5px;">
|
| 965 |
+
"""
|
| 966 |
+
|
| 967 |
+
# ์ ๋ผ๋ฒจ ์์ฑ
|
| 968 |
+
for date, volume, original_index in chart_data:
|
| 969 |
+
is_predicted = original_index >= actual_count
|
| 970 |
+
|
| 971 |
+
try:
|
| 972 |
+
date_obj = datetime.strptime(date, "%Y-%m-%d")
|
| 973 |
+
year_short = str(date_obj.year)[-2:]
|
| 974 |
+
month_num = date_obj.month
|
| 975 |
+
date_formatted = f"{year_short}.{month_num:02d}"
|
| 976 |
+
except:
|
| 977 |
+
date_formatted = date[-5:].replace('-', '.')
|
| 978 |
+
|
| 979 |
+
chart_html += f"""
|
| 980 |
+
<div style="
|
| 981 |
+
flex: 1;
|
| 982 |
+
text-align: center;
|
| 983 |
+
font-size: 9px;
|
| 984 |
+
color: {'#e74c3c' if is_predicted else '#666'};
|
| 985 |
+
font-weight: {'bold' if is_predicted else 'normal'};
|
| 986 |
+
transform: rotate(-45deg);
|
| 987 |
+
transform-origin: center;
|
| 988 |
+
line-height: 1;
|
| 989 |
+
margin-top: 8px;
|
| 990 |
+
">
|
| 991 |
+
{date_formatted}
|
| 992 |
+
</div>
|
| 993 |
+
"""
|
| 994 |
+
|
| 995 |
+
# ํต๊ณ ์ ๋ณด
|
| 996 |
+
if period_code == "1year":
|
| 997 |
+
actual_volumes = volumes[:actual_count] # ์ค์ ๋ฐ์ดํฐ๋ง
|
| 998 |
+
else:
|
| 999 |
+
actual_volumes = volumes # 3๋
์ ์ฒด ๋ฐ์ดํฐ
|
| 1000 |
+
|
| 1001 |
+
avg_volume = sum(actual_volumes) // len(actual_volumes) if actual_volumes else 0
|
| 1002 |
+
max_volume_val = max(actual_volumes) if actual_volumes else 0
|
| 1003 |
+
min_volume_val = min(actual_volumes) if actual_volumes else 0
|
| 1004 |
+
|
| 1005 |
+
chart_html += f"""
|
| 1006 |
+
</div>
|
| 1007 |
+
</div>
|
| 1008 |
+
|
| 1009 |
+
<!-- ๋ฒ๋ก -->
|
| 1010 |
+
<div style="display: flex; justify-content: center; gap: 20px; margin: 15px 0; font-size: 12px;">
|
| 1011 |
+
<div style="display: flex; align-items: center; gap: 5px;">
|
| 1012 |
+
<div style="width: 15px; height: 15px; background: {color}; border-radius: 2px;"></div>
|
| 1013 |
+
<span style="color: #333;">์ค์ ๋ฐ์ดํฐ</span>
|
| 1014 |
+
</div>
|
| 1015 |
+
<div style="display: flex; align-items: center; gap: 5px;">
|
| 1016 |
+
<div style="width: 15px; height: 15px; background: repeating-linear-gradient(90deg, {predicted_color}, {predicted_color} 3px, transparent 3px, transparent 6px); border: 1px dashed #333; border-radius: 2px;"></div>
|
| 1017 |
+
<span style="color: #e74c3c;">์์ ๋ฐ์ดํฐ</span>
|
| 1018 |
+
</div>
|
| 1019 |
+
</div>
|
| 1020 |
+
|
| 1021 |
+
<!-- ํต๊ณ ์ ๋ณด -->
|
| 1022 |
+
<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 15px; margin-top: 20px;">
|
| 1023 |
+
<div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
|
| 1024 |
+
<div style="font-size: 16px; font-weight: bold; color: #3498db;">{min_volume_val:,}</div>
|
| 1025 |
+
<div style="font-size: 11px; color: #666;">์ต์ ๊ฒ์๋</div>
|
| 1026 |
+
</div>
|
| 1027 |
+
<div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
|
| 1028 |
+
<div style="font-size: 16px; font-weight: bold; color: #2ecc71;">{avg_volume:,}</div>
|
| 1029 |
+
<div style="font-size: 11px; color: #666;">ํ๊ท ๊ฒ์๋</div>
|
| 1030 |
+
</div>
|
| 1031 |
+
<div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
|
| 1032 |
+
<div style="font-size: 16px; font-weight: bold; color: #e74c3c;">{max_volume_val:,}</div>
|
| 1033 |
+
<div style="font-size: 11px; color: #666;">์ต๊ณ ๊ฒ์๋</div>
|
| 1034 |
+
</div>
|
| 1035 |
+
<div style="text-align: center; padding: 12px; background: white; border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border: 1px solid #e9ecef;">
|
| 1036 |
+
<div style="font-size: 16px; font-weight: bold; color: #27ae60;">{growth_rate:+.1f}%</div>
|
| 1037 |
+
<div style="font-size: 11px; color: #666;">{get_growth_rate_label(period_code)}</div>
|
| 1038 |
+
</div>
|
| 1039 |
+
</div>
|
| 1040 |
+
"""
|
| 1041 |
+
|
| 1042 |
+
# ๊ฐ์ ์ค๋ช
|
| 1043 |
+
if period_code == "1year":
|
| 1044 |
+
chart_html += f"""
|
| 1045 |
+
<div style="margin-top: 15px; padding: 12px; background: #e8f5e8; border-radius: 8px; text-align: center;">
|
| 1046 |
+
<p style="margin: 0; font-size: 13px; color: #155724;">
|
| 1047 |
+
๐ <strong>์ต๊ทผ 1๋
+ ํฅํ 3๊ฐ์ ์์</strong>: ์ค์ ๋ง๋(์ค์ ), ๋น๊ธ ๋ง๋(์์)
|
| 1048 |
+
</p>
|
| 1049 |
+
</div>
|
| 1050 |
+
"""
|
| 1051 |
+
else:
|
| 1052 |
+
chart_html += f"""
|
| 1053 |
+
<div style="margin-top: 15px; padding: 12px; background: #e3f2fd; border-radius: 8px; text-align: center;">
|
| 1054 |
+
<p style="margin: 0; font-size: 13px; color: #1565c0;">
|
| 1055 |
+
๐ <strong>์ต๊ทผ 3๋
ํธ๋ ๋</strong>: ์ ์ฒด ๊ธฐ๊ฐ ๊ฒ์๋ ๋ฐ์ดํฐ
|
| 1056 |
+
</p>
|
| 1057 |
+
</div>
|
| 1058 |
+
"""
|
| 1059 |
+
|
| 1060 |
+
chart_html += """
|
| 1061 |
+
</div>
|
| 1062 |
+
</div>
|
| 1063 |
+
"""
|
| 1064 |
+
|
| 1065 |
+
chart_html += "</div>"
|
| 1066 |
+
|
| 1067 |
+
chart_html += "</div>"
|
| 1068 |
+
|
| 1069 |
+
logger.info(f"๊ฐ์ ๋ ์ ๊ตํ ํธ๋ ๋ ์ฐจํธ ์์ฑ ์๋ฃ")
|
| 1070 |
+
return chart_html
|
| 1071 |
+
|
| 1072 |
+
except Exception as e:
|
| 1073 |
+
logger.error(f"์ฐจํธ ์์ฑ ์ค๋ฅ: {e}")
|
| 1074 |
+
return f"""
|
| 1075 |
+
<div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
|
| 1076 |
+
<h4>์ฐจํธ ์์ฑ ์ค๋ฅ</h4>
|
| 1077 |
+
<p>์ค๋ฅ: {str(e)}</p>
|
| 1078 |
+
</div>
|
| 1079 |
+
"""
|
| 1080 |
+
|
| 1081 |
+
def create_trend_chart_v7(monthly_data_1year, monthly_data_3year):
|
| 1082 |
+
"""๊ฐ์ ๋ ํธ๋ ๋ ์ฐจํธ ์์ฑ"""
|
| 1083 |
+
try:
|
| 1084 |
+
chart_html = create_visual_trend_chart(monthly_data_1year, monthly_data_3year)
|
| 1085 |
+
return chart_html
|
| 1086 |
+
|
| 1087 |
+
except Exception as e:
|
| 1088 |
+
logger.error(f"์ฐจํธ ์์ฑ ์ค๋ฅ: {e}")
|
| 1089 |
+
return f"""
|
| 1090 |
+
<div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
|
| 1091 |
+
<h4>์ฐจํธ ์์ฑ ์ค๋ฅ</h4>
|
| 1092 |
+
<p>์ค๋ฅ: {str(e)}</p>
|
| 1093 |
+
</div>
|
| 1094 |
+
"""
|
| 1095 |
+
|
| 1096 |
+
def get_growth_rate_label(period_code):
|
| 1097 |
+
"""๊ธฐ๊ฐ์ ๋ฐ๋ฅธ ์ฑ์ฅ๋ฅ ๋ผ๋ฒจ ๋ฐํ"""
|
| 1098 |
+
if period_code == "1year":
|
| 1099 |
+
return "์์ 3๊ฐ์ ์ฆ๊ฐ์จ"
|
| 1100 |
+
else: # 3year
|
| 1101 |
+
return "์๋
๋๋น ์ฆ๊ฐ์จ"
|
| 1102 |
+
|
| 1103 |
+
def create_error_chart(error_msg):
|
| 1104 |
+
"""์๋ฌ ๋ฐ์์ ๋์ฒด ์ฐจํธ"""
|
| 1105 |
+
return f"""
|
| 1106 |
+
<div style="padding: 20px; background: #f8d7da; border-radius: 8px; color: #721c24;">
|
| 1107 |
+
<h4>์ฐจํธ ์์ฑ ์ค๋ฅ</h4>
|
| 1108 |
+
<p>์ค๋ฅ: {error_msg}</p>
|
| 1109 |
+
</div>
|
| 1110 |
+
"""
|
| 1111 |
+
|
| 1112 |
+
# ===== ํธํ์ฑ ํจ์๋ค (๊ธฐ์กด ์ฝ๋์์ ํธํ์ฑ ์ ์ง) =====
|
| 1113 |
+
|
| 1114 |
+
def get_naver_trend_data_v4(keywords, period="1year", max_retries=3):
|
| 1115 |
+
"""๊ธฐ์กด ํจ์ ํธํ์ฑ ์ ์ง"""
|
| 1116 |
+
return get_naver_trend_data_v5(keywords, period, max_retries)
|
| 1117 |
+
|
| 1118 |
+
def calculate_monthly_volumes_v6(keywords, current_volumes, trend_data, period="1year"):
|
| 1119 |
+
"""๊ธฐ์กด ํจ์ ํธํ์ฑ ์ ์ง"""
|
| 1120 |
+
return calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period)
|
| 1121 |
+
|
| 1122 |
+
def calculate_monthly_volumes_v5(keywords, current_volumes, trend_data, period="1year"):
|
| 1123 |
+
"""๊ธฐ์กด ํจ์ ํธํ์ฑ ์ ์ง"""
|
| 1124 |
+
return calculate_monthly_volumes_v7(keywords, current_volumes, trend_data, period)
|
| 1125 |
+
|
| 1126 |
+
def create_trend_chart_v6(monthly_data_1year, monthly_data_3year):
|
| 1127 |
+
"""๊ธฐ์กด ํจ์ ํธํ์ฑ ์ ์ง"""
|
| 1128 |
+
return create_trend_chart_v7(monthly_data_1year, monthly_data_3year)
|